diff --git a/chart.go b/chart.go index de53cfc..69c2aba 100644 --- a/chart.go +++ b/chart.go @@ -56,6 +56,7 @@ type ChartOption struct { Height int Parent *Draw Padding chart.Box + Box chart.Box SeriesList []Series BackgroundColor drawing.Color Children []ChartOption @@ -337,6 +338,7 @@ func chartBasicRender(opt *ChartOption) (*basicRenderResult, error) { Height: opt.getHeight(), }, PaddingOption(opt.Padding), + BoxOption(opt.Box), ) if err != nil { return nil, err diff --git a/draw.go b/draw.go index 12c9d06..0dbcf83 100644 --- a/draw.go +++ b/draw.go @@ -71,6 +71,16 @@ func PaddingOption(padding chart.Box) Option { } } +func BoxOption(box chart.Box) Option { + return func(d *Draw) error { + if box.IsZero() { + return nil + } + d.Box = box + return nil + } +} + func NewDraw(opt DrawOption, opts ...Option) (*Draw, error) { if opt.Parent == nil && (opt.Width <= 0 || opt.Height <= 0) { return nil, errors.New("parent and width/height can not be nil") diff --git a/examples/demo/main.go b/examples/demo/main.go index 978bef6..1f37e25 100644 --- a/examples/demo/main.go +++ b/examples/demo/main.go @@ -539,10 +539,11 @@ func indexHandler(w http.ResponseWriter, req *http.Request) { "Walnut Brownie", }, }, - Height: 20, - Padding: chart.Box{ - Left: 250, - Top: -80, + Box: chart.Box{ + Top: 20, + Left: 400, + Right: 500, + Bottom: 120, }, SeriesList: charts.NewPieSeriesList([]float64{ 435.9, @@ -575,221 +576,6 @@ func indexHandler(w http.ResponseWriter, req *http.Request) { } -func indexHandlerBak(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - return - } - - zero := float64(0) - outputType := r.URL.Query().Get("type") - chartOption := charts.ChartOption{ - // Theme: "dark", - Type: outputType, - Title: charts.TitleOption{ - Left: charts.PositionCenter, - Style: chart.Style{ - FontColor: chart.ColorAlternateBlue, - }, - SubtextStyle: chart.Style{ - FontColor: chart.ColorRed, - }, - Text: "Stacked Line", - Subtext: "Hello World!", - }, - Padding: chart.Box{ - Left: 5, - Top: 15, - Bottom: 5, - Right: 10, - }, - YAxisList: []charts.YAxisOption{ - { - Min: &zero, - }, - { - Formatter: "{value} °C", - // Max: charts.NewFloatPoint(24), - }, - }, - Legend: charts.LegendOption{ - Data: []string{ - "Email", - "Union Ads", - // "Video Ads", - }, - Left: charts.PositionLeft, - // Orient: charts.OrientVertical, - }, - XAxis: charts.XAxisOption{ - Data: []string{ - "Mon", - "Tue", - "Wed", - "Thu", - "Fri", - "Sat", - "Sun", - }, - // SplitNumber: 4, - // BoundaryGap: charts.FalseFlag(), - }, - SeriesList: []charts.Series{ - { - // Type: charts.ChartTypeBar, - MarkPoint: charts.SeriesMarkPoint{ - Data: []charts.SeriesMarkPointData{ - { - Type: "max", - }, - { - Type: "min", - }, - }, - }, - MarkLine: charts.SeriesMarkLine{ - Data: []charts.SeriesMarkLineData{ - // { - // Type: "max", - // }, - { - Type: "average", - }, - }, - }, - Data: charts.NewSeriesDataFromValues([]float64{ - 2.0, - 4.9, - 7.0, - 23.2, - 25.6, - 76.7, - 135.6, - }), - }, - // { - // // Type: charts.ChartTypeBar, - // Data: charts.NewSeriesDataFromValues([]float64{ - // 2.6, - // 5.9, - // 9.0, - // 26.4, - // 28.7, - // 70.7, - // 175.6, - // }), - // }, - { - Data: charts.NewSeriesDataFromValues([]float64{ - 2.0, - 2.2, - 3.3, - 4.5, - 6.3, - 10.2, - 20.3, - }), - YAxisIndex: 1, - }, - // { - // Data: []charts.SeriesData{ - // { - // Value: 220, - // }, - // { - // Value: 182, - // }, - // { - // Value: 191, - // }, - // { - // Value: 234, - // }, - // { - // Value: 290, - // }, - // { - // Value: 330, - // }, - // { - // Value: 310, - // }, - // }, - // }, - // { - // Data: []charts.SeriesData{ - // { - // Value: 150, - // }, - // { - // Value: 232, - // }, - // { - // Value: 201, - // }, - // { - // Value: 154, - // }, - // { - // Value: 190, - // }, - // { - // Value: 330, - // }, - // { - // Value: 410, - // }, - // }, - // }, - }, - // Children: []charts.ChartOption{ - // { - // Padding: chart.Box{ - // Left: 350, - // Top: 0, - // }, - // Legend: charts.LegendOption{ - // Show: charts.FalseFlag(), - // }, - // Width: 150, - // Height: 150, - // SeriesList: []charts.Series{ - // charts.NewSeriesFromValues([]float64{ - // 1048, - // }, charts.ChartTypePie), - // { - // Data: charts.NewSeriesDataFromValues([]float64{ - // 735, - // }), - // Radius: "50%", - // Name: "test", - // }, - // charts.NewSeriesFromValues([]float64{ - // 580, - // }), - // charts.NewSeriesFromValues([]float64{ - // 484, - // }), - // }, - // }, - // }, - } - d, err := charts.Render(chartOption) - if err != nil { - panic(err) - } - - buf, _ := d.Bytes() - - if outputType == "png" { - w.Header().Set("Content-Type", "image/png") - w.Write(buf) - } else { - data := bytes.ReplaceAll([]byte(html), []byte("{{body}}"), buf) - w.Header().Set("Content-Type", "text/html") - w.Write(data) - } -} - func main() { http.HandleFunc("/", indexHandler) http.ListenAndServe(":3012", nil)