diff --git a/draw.go b/draw.go index eb9ec6a..ed6039b 100644 --- a/draw.go +++ b/draw.go @@ -41,9 +41,7 @@ func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style r.Fill() } - r.SetStrokeColor(style.GetStrokeColor()) - r.SetStrokeDashArray(style.GetStrokeDashArray()) - r.SetStrokeWidth(style.GetStrokeWidth()) + style.GetStrokeOptions().WriteToRenderer(r) r.MoveTo(x0, y0) for i := 1; i < vs.Len(); i++ { diff --git a/examples/stacked_bar/main.go b/examples/stacked_bar/main.go index f3893aa..ef65ca2 100644 --- a/examples/stacked_bar/main.go +++ b/examples/stacked_bar/main.go @@ -10,9 +10,6 @@ import ( func drawChart(res http.ResponseWriter, req *http.Request) { sbc := chart.StackedBarChart{ - Background: chart.Style{ - Padding: chart.Box{Top: 50, Left: 50, Right: 50, Bottom: 50}, - }, XAxis: chart.Style{ Show: true, }, @@ -21,7 +18,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, Bars: []chart.StackedBar{ { - Name: "Katrina like animals that are very fat and furry.", + Name: "This is a very long string to test word break wrapping.", Values: []chart.Value{ {Value: 5, Label: "Blue"}, {Value: 5, Label: "Green"}, diff --git a/stacked_bar_chart.go b/stacked_bar_chart.go index 94a7d9f..511be46 100644 --- a/stacked_bar_chart.go +++ b/stacked_bar_chart.go @@ -111,7 +111,7 @@ func (sbc StackedBarChart) Render(rp RendererProvider, w io.Writer) error { } r.SetDPI(sbc.GetDPI(DefaultDPI)) - canvasBox := sbc.getAdjustedCanvasBox(sbc.getDefaultCanvasBox()) + canvasBox := sbc.getAdjustedCanvasBox(r, sbc.getDefaultCanvasBox()) sbc.drawBars(r, canvasBox) sbc.drawXAxis(r, canvasBox) sbc.drawYAxis(r, canvasBox) @@ -228,18 +228,47 @@ func (sbc StackedBarChart) getDefaultCanvasBox() Box { return sbc.Box() } -func (sbc StackedBarChart) getAdjustedCanvasBox(canvasBox Box) Box { +func (sbc StackedBarChart) getAdjustedCanvasBox(r Renderer, canvasBox Box) Box { var totalWidth int for _, bar := range sbc.Bars { totalWidth += bar.GetWidth() + sbc.GetBarSpacing() } + if sbc.XAxis.Show { + xaxisHeight := DefaultVerticalTickHeight + + axisStyle := sbc.XAxis.InheritFrom(sbc.styleDefaultsAxes()) + axisStyle.WriteToRenderer(r) + + cursor := canvasBox.Left + for _, bar := range sbc.Bars { + if len(bar.Name) > 0 { + barLabelBox := Box{ + Top: canvasBox.Bottom + DefaultXAxisMargin, + Left: cursor, + Right: cursor + bar.GetWidth() + sbc.GetBarSpacing(), + Bottom: sbc.GetHeight(), + } + lines := Text.WrapFit(r, bar.Name, barLabelBox.Width(), axisStyle) + linesBox := Text.MeasureLines(r, lines, axisStyle) + + xaxisHeight = Math.MaxInt(linesBox.Height()+(2*DefaultXAxisMargin), xaxisHeight) + } + } + return Box{ + Top: canvasBox.Top, + Left: canvasBox.Left, + Right: canvasBox.Left + totalWidth, + Bottom: sbc.GetHeight() - xaxisHeight, + } + } return Box{ Top: canvasBox.Top, Left: canvasBox.Left, Right: canvasBox.Left + totalWidth, Bottom: canvasBox.Bottom, } + } // Box returns the chart bounds as a box. diff --git a/tick.go b/tick.go index 2428bec..f50f7f2 100644 --- a/tick.go +++ b/tick.go @@ -32,7 +32,7 @@ func (t Ticks) Less(i, j int) bool { } // GenerateContinuousTicksWithStep generates a set of ticks. -func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick { +func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter, includeMax bool) []Tick { var ticks []Tick min, max := ra.GetMin(), ra.GetMax() for cursor := min; cursor <= max; cursor += step { @@ -46,6 +46,12 @@ func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter) return ticks } } + if includeMax { + ticks = append(ticks, Tick{ + Value: ra.GetMax(), + Label: vf(ra.GetMax()), + }) + } return ticks } diff --git a/xaxis.go b/xaxis.go index 31013e7..80162cc 100644 --- a/xaxis.go +++ b/xaxis.go @@ -54,7 +54,7 @@ func (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter return tp.GetTicks(vf) } step := CalculateContinuousTickStep(r, ra, false, xa.Style.InheritFrom(defaults), vf) - return GenerateContinuousTicksWithStep(ra, step, vf) + return GenerateContinuousTicksWithStep(ra, step, vf, xa.TickPosition == TickPositionBetweenTicks) } // GetGridLines returns the gridlines for the axis. diff --git a/yaxis.go b/yaxis.go index de9cc3a..4200b04 100644 --- a/yaxis.go +++ b/yaxis.go @@ -48,7 +48,7 @@ func (ya YAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter return tp.GetTicks(vf) } step := CalculateContinuousTickStep(r, ra, true, ya.Style.InheritFrom(defaults), vf) - return GenerateContinuousTicksWithStep(ra, step, vf) + return GenerateContinuousTicksWithStep(ra, step, vf, true) } // GetGridLines returns the gridlines for the axis.