Skip to content

Commit

Permalink
LineChartOptions.FillArea into bool pointer (*bool)
Browse files Browse the repository at this point in the history
This change matches other configuration where we accept bool pointers. Allowing the default (nil) to be able to be enabled or disabled depending on other configuration (specifically if the series is stacked). But also allowing a user to override this option and force a behavior.
  • Loading branch information
jentfoo committed Jan 27, 2025
1 parent 609c5f0 commit e9bfb8e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions chart_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type ChartOption struct {
SymbolShow *bool
// LineStrokeWidth is the stroke width for line charts.
LineStrokeWidth float64
// FillArea set to true to fill the area under the line in line charts
FillArea bool
// FillArea set to *true to fill the area under the line in line charts
FillArea *bool
// FillOpacity is the opacity (alpha) of the area fill in line charts.
FillOpacity uint8
// BarWidth is the width of the bars for bar charts.
Expand Down
2 changes: 1 addition & 1 deletion examples/line_chart-area/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
}),
// setup fill styling below
func(opt *charts.ChartOption) {
opt.FillArea = true // shade the area under the line
opt.FillArea = charts.True() // shade the area under the line
opt.FillOpacity = 150 // set the fill opacity a little lighter than default
opt.XAxis.BoundaryGap = charts.False() // BoundaryGap is less appealing when enabling FillArea
},
Expand Down
2 changes: 1 addition & 1 deletion examples/web-1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
SeriesList: charts.NewSeriesListLine([][]float64{
{120, 132, 101, 134, 90, 230, 210},
}),
FillArea: true,
FillArea: charts.True(),
},
// histogram
{
Expand Down
17 changes: 11 additions & 6 deletions line_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ type LineChartOption struct {
// smoother lines. Because the tension smooths out the line, the line will no longer hit the data points exactly.
// The more variable the points, and the higher the tension, the more the line will be moved from the points.
StrokeSmoothingTension float64
// TODO - make FillArea a pointer so that it can be disabled for stacking, update StackSeries docs when done
// FillArea set this to true to fill the area below the line.
FillArea bool
FillArea *bool
// FillOpacity is the opacity (alpha) of the area fill.
FillOpacity uint8
// ValueFormatter defines how float values should be rendered to strings, notably for numeric axis labels.
Expand All @@ -85,9 +84,12 @@ func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (
seriesPainter := result.seriesPainter

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 {
fillArea := stackedSeries // fill area defaults to on if the series is stacked
if opt.FillArea != nil { // default override
fillArea = *opt.FillArea
}
boundaryGap := !fillArea // boundary gap default enabled unless fill area is set
if opt.XAxis.BoundaryGap != nil { // default override
boundaryGap = *opt.XAxis.BoundaryGap
}
xDivideCount := len(opt.XAxis.Data)
Expand Down Expand Up @@ -287,7 +289,10 @@ 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 {
fillArea := flagIs(true, opt.StackSeries) || opt.FillArea
fillArea := flagIs(true, opt.StackSeries) // fill area default based on StackedSeries state
if opt.FillArea != nil { // default override
fillArea = *opt.FillArea
}
boundaryGap := !fillArea // boundary gap default enabled unless fill area is set
l.opt.XAxis.BoundaryGap = &boundaryGap
}
Expand Down
12 changes: 6 additions & 6 deletions line_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func TestLineChart(t *testing.T) {
makeOptions: func() LineChartOption {
opt := makeMinimalLineChartOption()
opt.SeriesList[0].Data[3] = GetNullValue()
opt.FillArea = true
opt.FillArea = True()
return opt
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 600 400\"><path d=\"M 0 0\nL 600 0\nL 600 400\nL 0 400\nL 0 0\" style=\"stroke:none;fill:white\"/><text x=\"10\" y=\"17\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1.44k</text><text x=\"10\" y=\"55\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1.28k</text><text x=\"10\" y=\"94\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1.12k</text><text x=\"22\" y=\"133\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">960</text><text x=\"22\" y=\"172\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">800</text><text x=\"22\" y=\"211\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">640</text><text x=\"22\" y=\"250\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">480</text><text x=\"22\" y=\"289\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">320</text><text x=\"22\" y=\"328\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">160</text><text x=\"40\" y=\"367\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">0</text><path d=\"M 59 10\nL 590 10\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 48\nL 590 48\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 87\nL 590 87\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 126\nL 590 126\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 165\nL 590 165\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 204\nL 590 204\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 243\nL 590 243\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 282\nL 590 282\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 321\nL 590 321\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 331\nL 147 328\nL 236 336\" style=\"stroke:none;fill:rgba(84,112,198,0.8)\"/><path d=\"M 413 339\nL 501 305\nL 590 309\nL 590 360\nL 59 360\nL 59 331\" style=\"stroke:none;fill:rgba(84,112,198,0.8)\"/><path d=\"M 59 331\nL 147 328\nL 236 336\" style=\"stroke-width:2;stroke:rgb(84,112,198);fill:none\"/><path d=\"M 413 339\nL 501 305\nL 590 309\" style=\"stroke-width:2;stroke:rgb(84,112,198);fill:none\"/><path d=\"M 59 161\nL 147 134\nL 236 142\nL 324 133\nL 413 47\nL 501 37\nL 590 40\nL 590 360\nL 59 360\nL 59 161\" style=\"stroke:none;fill:rgba(145,204,117,0.8)\"/><path d=\"M 59 161\nL 147 134\nL 236 142\nL 324 133\nL 413 47\nL 501 37\nL 590 40\" style=\"stroke-width:2;stroke:rgb(145,204,117);fill:none\"/></svg>",
Expand All @@ -618,7 +618,7 @@ func TestLineChart(t *testing.T) {
opt := makeMinimalLineChartOption()
opt.StrokeSmoothingTension = 0.8
opt.SeriesList[0].Data[3] = GetNullValue()
opt.FillArea = true
opt.FillArea = True()
return opt
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 600 400\"><path d=\"M 0 0\nL 600 0\nL 600 400\nL 0 400\nL 0 0\" style=\"stroke:none;fill:white\"/><text x=\"10\" y=\"17\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1.44k</text><text x=\"10\" y=\"55\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1.28k</text><text x=\"10\" y=\"94\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1.12k</text><text x=\"22\" y=\"133\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">960</text><text x=\"22\" y=\"172\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">800</text><text x=\"22\" y=\"211\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">640</text><text x=\"22\" y=\"250\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">480</text><text x=\"22\" y=\"289\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">320</text><text x=\"22\" y=\"328\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">160</text><text x=\"40\" y=\"367\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">0</text><path d=\"M 59 10\nL 590 10\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 48\nL 590 48\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 87\nL 590 87\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 126\nL 590 126\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 165\nL 590 165\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 204\nL 590 204\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 243\nL 590 243\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 282\nL 590 282\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 321\nL 590 321\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 59 331\nQ147,328 182,331\nQ147,328 236,336\nM 413 339\nQ501,305 536,306\nQ501,305 590,309\nL 590 360\nL 59 360\nL 59 331\" style=\"stroke:none;fill:rgba(84,112,198,0.8)\"/><path d=\"M 59 331\nQ147,328 182,331\nQ147,328 236,336\" style=\"stroke-width:2;stroke:rgb(84,112,198);fill:none\"/><path d=\"M 413 339\nQ501,305 536,306\nQ501,305 590,309\" style=\"stroke-width:2;stroke:rgb(84,112,198);fill:none\"/><path d=\"M 59 161\nQ147,134 182,137\nQ236,142 271,138\nQ324,133 359,98\nQ413,47 448,43\nQ501,37 536,38\nQ501,37 590,40\nL 590 360\nL 59 360\nL 59 161\" style=\"stroke:none;fill:rgba(145,204,117,0.8)\"/><path d=\"M 59 161\nQ147,134 182,137\nQ236,142 271,138\nQ324,133 359,98\nQ413,47 448,43\nQ501,37 536,38\nQ501,37 590,40\" style=\"stroke-width:2;stroke:rgb(145,204,117);fill:none\"/></svg>",
Expand All @@ -628,7 +628,7 @@ func TestLineChart(t *testing.T) {
defaultTheme: true,
makeOptions: func() LineChartOption {
opt := makeMinimalLineChartOption()
opt.FillArea = true
opt.FillArea = True()
opt.FillOpacity = 100
return opt
},
Expand All @@ -639,7 +639,7 @@ func TestLineChart(t *testing.T) {
defaultTheme: true,
makeOptions: func() LineChartOption {
opt := makeFullLineChartOption()
opt.FillArea = true
opt.FillArea = True()
opt.FillOpacity = 100
opt.XAxis.BoundaryGap = True()
return opt
Expand All @@ -651,7 +651,7 @@ func TestLineChart(t *testing.T) {
defaultTheme: true,
makeOptions: func() LineChartOption {
opt := makeMinimalLineChartOption()
opt.FillArea = true
opt.FillArea = True()
opt.StrokeSmoothingTension = 0.8
opt.XAxis.BoundaryGap = True()
return opt
Expand All @@ -663,7 +663,7 @@ func TestLineChart(t *testing.T) {
defaultTheme: true,
makeOptions: func() LineChartOption {
opt := makeMinimalLineChartOption()
opt.FillArea = true
opt.FillArea = True()
opt.StrokeSmoothingTension = 0.8
opt.XAxis.BoundaryGap = False()
return opt
Expand Down

0 comments on commit e9bfb8e

Please sign in to comment.