Compare two series on one line chart
LineChartCompare draws an earlier series, such as last week, in compare_color under the current one on a single scale, so a chart shows at a glance whether this period beats the last. ChartRangeForBoth sizes that scale to fit both. LineChart now shares the card, grid, and series drawing with it.
This commit is contained in:
1 parent
2857d59176
commit
8400489f6c
2 files changed
+82
-23
No files matched your search
+60
-22
@@ -14,6 +14,8 @@ LineChartProps :: struct {
|
||||
semantic_label: string
|
||||
line_color: Color
|
||||
grid_color: Color
|
||||
// The earlier series in LineChartCompare, such as last week.
|
||||
compare_color: Color
|
||||
}
|
||||
|
||||
#program_export
|
||||
@@ -36,19 +38,32 @@ ChartRangeFor :: (values: []float32) -> ChartRange {
|
||||
return result
|
||||
}
|
||||
|
||||
// One range that fits two series, for LineChartCompare.
|
||||
#program_export
|
||||
LineChart :: (session: Session, props: LineChartProps,
|
||||
values: []float32, chart_range: ChartRange) {
|
||||
ChartRangeForBoth :: (values: []float32, previous: []float32) -> ChartRange {
|
||||
if previous.count == 0 { return ChartRangeFor(values) }
|
||||
if values.count == 0 { return ChartRangeFor(previous) }
|
||||
first: ChartRange = ChartRangeFor(values)
|
||||
second: ChartRange = ChartRangeFor(previous)
|
||||
both: [2]float32
|
||||
both[0] = first.low
|
||||
both[1] = first.high
|
||||
if second.low < both[0] { both[0] = second.low }
|
||||
if second.high > both[1] { both[1] = second.high }
|
||||
return ChartRangeFor(both[:])
|
||||
}
|
||||
|
||||
// The card and its five horizontal grid lines; false when nothing fits.
|
||||
LineChartFrame :: (session: Session, props: LineChartProps,
|
||||
chart_range: ChartRange) -> bool {
|
||||
background: PlotProps
|
||||
background.key = props.key
|
||||
background.mode = cast(PlotMode)0
|
||||
background.bounds = props.bounds
|
||||
background.semantic_label = props.semantic_label
|
||||
Plot(session, background, values[0:0])
|
||||
if values.count == 0 { return }
|
||||
|
||||
span: float32 = chart_range.max_value - chart_range.min_value
|
||||
if span <= 0.0 { return }
|
||||
empty: [1]float32
|
||||
Plot(session, background, empty[0:0])
|
||||
if chart_range.max_value - chart_range.min_value <= 0.0 { return false }
|
||||
grid_index: s32 = 0
|
||||
while grid_index < 5 {
|
||||
fraction: float32 = cast(float32)grid_index / 4.0
|
||||
@@ -58,35 +73,58 @@ LineChart :: (session: Session, props: LineChartProps,
|
||||
props.grid_color)
|
||||
grid_index += 1
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
#program_export
|
||||
LineChart :: (session: Session, props: LineChartProps,
|
||||
values: []float32, chart_range: ChartRange) {
|
||||
if !LineChartFrame(session, props, chart_range) || values.count == 0 { return }
|
||||
LineChartSeries(session, props.bounds, values, chart_range, props.line_color)
|
||||
}
|
||||
|
||||
// Two series on one scale across the same width, such as this week over
|
||||
// last week: previous in compare_color, then values in line_color on top.
|
||||
#program_export
|
||||
LineChartCompare :: (session: Session, props: LineChartProps,
|
||||
values: []float32, previous: []float32, chart_range: ChartRange) {
|
||||
if !LineChartFrame(session, props, chart_range) { return }
|
||||
if previous.count > 0 {
|
||||
LineChartSeries(session, props.bounds, previous, chart_range, props.compare_color)
|
||||
}
|
||||
if values.count > 0 {
|
||||
LineChartSeries(session, props.bounds, values, chart_range, props.line_color)
|
||||
}
|
||||
}
|
||||
|
||||
// One series as a two-pixel line with a marker at its last value.
|
||||
LineChartSeries :: (session: Session, bounds: Rectangle, values: []float32,
|
||||
chart_range: ChartRange, color: Color) {
|
||||
span: float32 = chart_range.max_value - chart_range.min_value
|
||||
if values.count == 1 {
|
||||
y: float32 = props.bounds.y + props.bounds.height *
|
||||
y: float32 = bounds.y + bounds.height *
|
||||
(chart_range.max_value - values[0]) / span
|
||||
PaintLine(session, -1,
|
||||
Rectangle.{props.bounds.x, y, props.bounds.width, 0.0},
|
||||
props.line_color)
|
||||
Rectangle.{bounds.x, y, bounds.width, 0.0}, color)
|
||||
} else {
|
||||
index: s32 = 1
|
||||
while index < cast(s32)values.count {
|
||||
x0: float32 = props.bounds.x + props.bounds.width *
|
||||
x0: float32 = bounds.x + bounds.width *
|
||||
cast(float32)(index - 1) / cast(float32)(values.count - 1)
|
||||
x1: float32 = props.bounds.x + props.bounds.width *
|
||||
x1: float32 = bounds.x + bounds.width *
|
||||
cast(float32)index / cast(float32)(values.count - 1)
|
||||
y0: float32 = props.bounds.y + props.bounds.height *
|
||||
y0: float32 = bounds.y + bounds.height *
|
||||
(chart_range.max_value - values[index - 1]) / span
|
||||
y1: float32 = props.bounds.y + props.bounds.height *
|
||||
y1: float32 = bounds.y + bounds.height *
|
||||
(chart_range.max_value - values[index]) / span
|
||||
PaintLine(session, -1,
|
||||
Rectangle.{x0, y0, x1 - x0, y1 - y0}, props.line_color)
|
||||
PaintLine(session, -1,
|
||||
Rectangle.{x0, y0 + 1.0, x1 - x0, y1 - y0},
|
||||
props.line_color)
|
||||
PaintLine(session, -1, Rectangle.{x0, y0, x1 - x0, y1 - y0}, color)
|
||||
PaintLine(session, -1, Rectangle.{x0, y0 + 1.0, x1 - x0, y1 - y0}, color)
|
||||
index += 1
|
||||
}
|
||||
}
|
||||
last_y: float32 = props.bounds.y + props.bounds.height *
|
||||
last_y: float32 = bounds.y + bounds.height *
|
||||
(chart_range.max_value - values[values.count - 1]) / span
|
||||
PaintRoundedFill(session, -1,
|
||||
Rectangle.{props.bounds.x + props.bounds.width - 4.0,
|
||||
last_y - 4.0, 8.0, 8.0}, 4.0, 12, props.line_color)
|
||||
Rectangle.{bounds.x + bounds.width - 4.0, last_y - 4.0, 8.0, 8.0},
|
||||
4.0, 12, color)
|
||||
}
|
||||
@@ -78,6 +78,27 @@ Frame :: () -> s32 {
|
||||
}
|
||||
|
||||
#import "plot_widget_host"
|
||||
#import "line_chart"
|
||||
|
||||
// This week over last week: five grid lines, then two segments drawn twice
|
||||
// for each series.
|
||||
CompareLines :: () -> s32 {
|
||||
current: [3]float32 = .[1.0, 2.0, 3.0]
|
||||
previous: [3]float32 = .[4.0, 2.0, 2.0]
|
||||
both: ChartRange = ChartRangeForBoth(current[:], previous[:])
|
||||
if both.low != 1.0 || both.high != 4.0 { return 3 }
|
||||
chart: LineChartProps
|
||||
chart.key = cast(u64)21
|
||||
chart.bounds = Rectangle.{10.0, 10.0, 100.0, 60.0}
|
||||
chart.semantic_label = "Visits this week and last week"
|
||||
before: s32 = PlotLineCount()
|
||||
TreeStart(TestSession(), cast(u64)2, Rectangle.{0.0, 0.0, 140.0, 180.0})
|
||||
LineChartCompare(TestSession(), chart, current[:], previous[:], both)
|
||||
if !TreeFinish(TestSession()) { return 4 }
|
||||
PaintFlush(TestSession())
|
||||
if PlotLineCount() - before != 5 + 4 + 4 { return 5 }
|
||||
return 0
|
||||
}
|
||||
|
||||
#program_export
|
||||
main :: () -> s32 {
|
||||
@@ -91,5 +112,5 @@ main :: () -> s32 {
|
||||
PlotLabels() != (index + 1) * 4 { return 2 }
|
||||
index += 1
|
||||
}
|
||||
return 0
|
||||
return CompareLines()
|
||||
}
|
||||
Reference in new issue
Block a user