-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathline_chart.go
344 lines (321 loc) · 11.9 KB
/
line_chart.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package charts
import (
"errors"
"math"
"github.com/golang/freetype/truetype"
)
type lineChart struct {
p *Painter
opt *LineChartOption
}
// newLineChart returns a line chart render
func newLineChart(p *Painter, opt LineChartOption) *lineChart {
return &lineChart{
p: p,
opt: &opt,
}
}
// NewLineChartOptionWithData returns an initialized LineChartOption with the SeriesList set for the provided data slice.
func NewLineChartOptionWithData(data [][]float64) LineChartOption {
sl := NewSeriesListLine(data)
return LineChartOption{
SeriesList: sl,
Padding: defaultPadding,
Theme: GetDefaultTheme(),
Font: GetDefaultFont(),
XAxis: XAxisOption{
Labels: make([]string, sl.getMaxDataCount(ChartTypeLine)),
},
YAxis: make([]YAxisOption, sl.getYAxisCount()),
ValueFormatter: defaultValueFormatter,
}
}
type LineChartOption struct {
// Theme specifies the colors used for the line chart.
Theme ColorPalette
// Padding specifies the padding of line chart.
Padding Box
// Font is the font used to render the chart.
Font *truetype.Font
// SeriesList provides the data series.
SeriesList SeriesList
// StackSeries if set to *true the lines will be layered over each other, with the last series value representing
// the sum of all the values. Enabling this will also enable FillArea (which until v0.5 can't be disabled).
// Some options will be ignored when StackedSeries is enabled, this includes StrokeSmoothingTension.
// MarkLine is also interpreted differently, only the first Series will have the MarkLine rendered (as it's the
// base bar, other bars are influenced by prior values). Additionally only the 0 index y-axis is stacked,
// allowing a non-stacked line to also be included on y-axis 1.
StackSeries *bool
// XAxis are options for the x-axis.
XAxis XAxisOption
// YAxis are options for the y-axis (at most two).
YAxis []YAxisOption
// Title are options for rendering the title.
Title TitleOption
// Legend are options for the data legend.
Legend LegendOption
// SymbolShow set this to *false or *true (using False() or True()) to force if the symbols should be shown or hidden.
SymbolShow *bool
// LineStrokeWidth is the width of the rendered line.
LineStrokeWidth float64
// StrokeSmoothingTension should be between 0 and 1. At 0 perfectly straight lines will be used with 1 providing
// 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
// 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.
ValueFormatter ValueFormatter
// backgroundIsFilled is set to true if the background is filled.
backgroundIsFilled bool
}
const showSymbolDefaultThreshold = 100
func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
p := l.p
opt := l.opt
seriesCount := len(seriesList)
if seriesCount == 0 {
return BoxZero, errors.New("empty series list")
}
seriesPainter := result.seriesPainter
if len(opt.XAxis.Labels) == 0 {
opt.XAxis.Labels = opt.XAxis.Data
}
stackedSeries := flagIs(true, opt.StackSeries)
fillAreaY0 := stackedSeries || opt.FillArea // fill area defaults to on if the series is stacked
fillAreaY1 := opt.FillArea
boundaryGap := !fillAreaY0 // boundary gap default enabled unless fill area is set
if opt.XAxis.BoundaryGap != nil {
boundaryGap = *opt.XAxis.BoundaryGap
}
xDivideCount := len(opt.XAxis.Labels)
if boundaryGap && xDivideCount > 1 && seriesPainter.Width()/xDivideCount <= boundaryGapDefaultThreshold {
// boundary gap would be so small it's visually better to disable the line spacing adjustment.
// Although label changes can be forced to center, this behavior is unconditional for the line
boundaryGap = false
}
if !boundaryGap {
xDivideCount--
}
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
}
strokeWidth := opt.LineStrokeWidth
if strokeWidth == 0 {
strokeWidth = defaultStrokeWidth
}
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
}
if opt.SymbolShow != nil {
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}
seriesNames := seriesList.Names()
var priorSeriesPoints []Point
for index := range seriesList {
series := seriesList[index]
stackSeries := stackedSeries && series.YAxisIndex == 0
seriesColor := opt.Theme.GetSeriesColor(index)
yRange := result.axisRanges[series.YAxisIndex]
points := make([]Point, len(series.Data))
var labelPainter *seriesLabelPainter
if flagIs(true, series.Label.Show) {
labelPainter = newSeriesLabelPainter(seriesPainter, seriesNames, series.Label, opt.Theme, opt.Font)
rendererList = append(rendererList, labelPainter)
}
for i, item := range series.Data {
if item == GetNullValue() {
points[i] = Point{X: xValues[i], Y: math.MaxInt32}
} else if stackSeries {
accumulatedValues[i] += item
points[i] = Point{
X: xValues[i],
Y: yRange.getRestHeight(accumulatedValues[i]),
}
} else {
points[i] = Point{
X: xValues[i],
Y: yRange.getRestHeight(item),
}
}
if labelPainter != nil {
labelPainter.Add(labelValue{
index: index,
value: item,
x: points[i].X,
y: points[i].Y,
fontStyle: series.Label.FontStyle,
})
}
}
if (series.YAxisIndex == 0 && fillAreaY0) || fillAreaY1 {
areaPoints := make([]Point, len(points))
copy(areaPoints, points)
for i, p := range areaPoints {
if p.Y != math.MaxInt32 {
if i > 0 {
areaPoints = areaPoints[i:] // truncate null points from array
}
break
}
}
bottomY := yRange.getRestHeight(yRange.min)
if stackSeries && len(priorSeriesPoints) > 0 {
// Fill between current line (areaPoints) and priorSeriesPoints
for i := len(priorSeriesPoints) - 1; i >= 0; i-- {
areaPoints = append(areaPoints, priorSeriesPoints[i])
}
// Close the shape by re-appending the first of point
areaPoints = append(areaPoints, areaPoints[0])
} else {
// Not stacked or first stacked series: fill down to bottom and then back to the first point
areaPoints = append(areaPoints,
Point{
X: areaPoints[len(areaPoints)-1].X,
Y: bottomY,
}, Point{
X: areaPoints[0].X,
Y: bottomY,
},
areaPoints[0],
)
}
var opacity uint8 = 200
if opt.FillOpacity > 0 {
opacity = opt.FillOpacity
}
fillColor := seriesColor.WithAlpha(opacity)
// If smoothing is enabled, do a smooth fill (not currently supported for stacked series)
if !stackSeries && opt.StrokeSmoothingTension > 0 {
seriesPainter.smoothFillChartArea(areaPoints, opt.StrokeSmoothingTension, fillColor)
} else {
seriesPainter.FillArea(areaPoints, fillColor)
}
}
// Draw the line
if opt.StrokeSmoothingTension > 0 {
seriesPainter.SmoothLineStroke(points, opt.StrokeSmoothingTension, seriesColor, strokeWidth)
} else {
seriesPainter.LineStroke(points, seriesColor, strokeWidth)
}
// Draw dots if enabled
if showSymbol {
dotFillColor := ColorWhite
if opt.Theme.IsDark() {
dotFillColor = seriesColor
}
seriesPainter.Dots(points, dotFillColor, seriesColor, 1, 2)
}
// Formatter set on the MarkLine or MarkPoint is checked in the respective painter
markLineValueFormatter := getPreferredValueFormatter(series.Label.ValueFormatter, opt.ValueFormatter)
markPointValueFormatter := getPreferredValueFormatter(series.Label.ValueFormatter, opt.ValueFormatter)
var globalSeriesData []float64 // lazily initialized
if series.MarkLine.GlobalLine && stackSeries && index == seriesCount-1 {
if globalSeriesData == nil {
globalSeriesData = seriesList.makeSumSeries(ChartTypeLine, series.YAxisIndex).Data
}
markLinePainter.add(markLineRenderOption{
fillColor: defaultGlobalMarkFillColor,
fontColor: opt.Theme.GetTextColor(),
strokeColor: defaultGlobalMarkFillColor,
font: opt.Font,
markline: series.MarkLine,
seriesData: globalSeriesData,
axisRange: yRange,
valueFormatterDefault: markLineValueFormatter,
})
} else if !stackSeries || index == 0 {
// In stacked mode we only support the line painter for the first series
markLinePainter.add(markLineRenderOption{
fillColor: seriesColor,
fontColor: opt.Theme.GetTextColor(),
strokeColor: seriesColor,
font: opt.Font,
markline: series.MarkLine,
seriesData: series.Data,
axisRange: yRange,
valueFormatterDefault: markLineValueFormatter,
})
}
if series.MarkPoint.GlobalPoint && stackSeries && index == seriesCount-1 {
if globalSeriesData == nil {
globalSeriesData = seriesList.makeSumSeries(ChartTypeLine, series.YAxisIndex).Data
}
markPointPainter.add(markPointRenderOption{
fillColor: defaultGlobalMarkFillColor,
font: opt.Font,
points: points,
markpoint: series.MarkPoint,
seriesData: globalSeriesData,
valueFormatterDefault: markPointValueFormatter,
seriesLabelPainter: labelPainter,
})
} else {
markPointPainter.add(markPointRenderOption{
fillColor: seriesColor,
font: opt.Font,
points: points,
markpoint: series.MarkPoint,
seriesData: series.Data,
valueFormatterDefault: markPointValueFormatter,
seriesLabelPainter: labelPainter,
})
}
// Save these points as "priorSeriesPoints" for the next series to stack onto (if needed)
priorSeriesPoints = points
}
if err := doRender(rendererList...); err != nil {
return BoxZero, err
}
return p.box, nil
}
func (l *lineChart) Render() (Box, error) {
p := l.p
opt := l.opt
if opt.Theme == nil {
opt.Theme = getPreferredTheme(p.theme)
}
// 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
boundaryGap := !fillArea // boundary gap default enabled unless fill area is set
l.opt.XAxis.BoundaryGap = &boundaryGap
}
renderResult, err := defaultRender(p, defaultRenderOption{
theme: opt.Theme,
padding: opt.Padding,
seriesList: opt.SeriesList,
stackSeries: flagIs(true, opt.StackSeries),
xAxis: &l.opt.XAxis,
yAxis: opt.YAxis,
title: opt.Title,
legend: &l.opt.Legend,
valueFormatter: opt.ValueFormatter,
backgroundIsFilled: opt.backgroundIsFilled,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeLine)
return l.render(renderResult, seriesList)
}