Skip to content

Commit

Permalink
line_chart: Fix Boundary Gap Default when new StackSeries option is e…
Browse files Browse the repository at this point in the history
…nabled
  • Loading branch information
jentfoo committed Jan 27, 2025
1 parent 36b9d8a commit 55b6ab8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
2 changes: 0 additions & 2 deletions bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
seriesNames := seriesList.Names()
divideValues := xRange.AutoDivide()
stackedSeries := flagIs(true, opt.StackSeries)

// Basic margins around each bar or bar cluster
var margin, barMargin, barWidth int
var accumulatedHeights []int // prior heights for stacking to avoid recalculating the heights
if stackedSeries {
Expand Down
35 changes: 15 additions & 20 deletions line_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (
opt := l.opt
seriesPainter := result.seriesPainter

boundaryGap := !opt.FillArea // boundary gap default enabled unless fill area is set
stackedSeries := flagIs(true, opt.StackSeries)
fillArea := stackedSeries || opt.FillArea // fill area defaults to on if the series is stacked
boundaryGap := !fillArea // boundary gap default enabled unless fill area is set
if opt.XAxis.BoundaryGap != nil {
boundaryGap = *opt.XAxis.BoundaryGap
}
Expand All @@ -99,36 +101,23 @@ func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (
}
xDivideValues := autoDivide(seriesPainter.Width(), xDivideCount)
xValues := make([]int, len(xDivideValues)-1)
dataCount := seriesList.getMaxDataCount(ChartTypeLine)
// accumulatedValues is used for stacking: it holds the summed data values at each X index
var accumulatedValues []float64
if stackedSeries {
accumulatedValues = make([]float64, dataCount)
}
if boundaryGap {
for i := 0; i < len(xDivideValues)-1; i++ {
xValues[i] = (xDivideValues[i] + xDivideValues[i+1]) >> 1
}
} else {
xValues = xDivideValues
}

// render list must start with the markPointPainter, as it can influence label painters (if enabled)
markPointPainter := newMarkPointPainter(seriesPainter)
markLinePainter := newMarkLinePainter(seriesPainter)
rendererList := []renderer{
markPointPainter,
markLinePainter,
}

strokeWidth := opt.LineStrokeWidth
if strokeWidth == 0 {
strokeWidth = defaultStrokeWidth
}

dataCount := seriesList.getMaxDataCount(ChartTypeLine)

stackedSeries := flagIs(true, opt.StackSeries)
if stackedSeries {
accumulatedValues = make([]float64, dataCount)
}

showSymbol := dataCount < showSymbolDefaultThreshold // default enable when data count is reasonable
if opt.StrokeSmoothingTension > 0 {
showSymbol = false // default disable symbols on curved lines since the dots won't hit the line exactly
Expand All @@ -137,6 +126,11 @@ func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (
showSymbol = *opt.SymbolShow
}

// render list must start with the markPointPainter, as it can influence label painters (if enabled)
markPointPainter := newMarkPointPainter(seriesPainter)
markLinePainter := newMarkLinePainter(seriesPainter)
rendererList := []renderer{markPointPainter, markLinePainter}

seriesCount := len(seriesList)
seriesNames := seriesList.Names()
var priorSeriesPoints []Point
Expand Down Expand Up @@ -178,7 +172,7 @@ func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (
}
}

if stackedSeries || opt.FillArea {
if fillArea {
areaPoints := make([]Point, len(points))
copy(areaPoints, points)
bottomY := yRange.getRestHeight(yRange.min)
Expand Down Expand Up @@ -293,7 +287,8 @@ func (l *lineChart) Render() (Box, error) {
}
// boundary gap default must be set here as it's used by the x-axis as well
if opt.XAxis.BoundaryGap == nil {
boundaryGap := !opt.FillArea // boundary gap default enabled unless fill area is set
fillArea := flagIs(true, opt.StackSeries) || opt.FillArea
boundaryGap := !fillArea // boundary gap default enabled unless fill area is set
l.opt.XAxis.BoundaryGap = &boundaryGap
}

Expand Down
1 change: 1 addition & 0 deletions line_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func makeFullLineChartStackedOption() LineChartOption {
Data: []string{
"1", "2", "3", "4", "5", "6", "7", "8",
},
BoundaryGap: True(),
},
Legend: LegendOption{
Data: dataLabels,
Expand Down

0 comments on commit 55b6ab8

Please sign in to comment.