@classic-homes/charts-svelte 0.3.2 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -7,13 +7,16 @@
|
|
|
7
7
|
import type { BarChartProps } from '@classic-homes/charts-core';
|
|
8
8
|
import {
|
|
9
9
|
allSeriesEmpty,
|
|
10
|
+
barCornerRadius,
|
|
10
11
|
buildAxis,
|
|
11
12
|
buildDataLabel,
|
|
12
13
|
buildDataZoom,
|
|
13
14
|
buildLegend,
|
|
15
|
+
buildStackTotalSeries,
|
|
14
16
|
buildTooltip,
|
|
15
17
|
categoryTable,
|
|
16
18
|
chartFormatters,
|
|
19
|
+
formatValue,
|
|
17
20
|
generateBarChartDescription,
|
|
18
21
|
getChartColors,
|
|
19
22
|
gridInset,
|
|
@@ -22,8 +25,10 @@
|
|
|
22
25
|
resolveChartHeight,
|
|
23
26
|
resolveValueFormatter,
|
|
24
27
|
seriesLabelLookup,
|
|
28
|
+
stackTotals,
|
|
25
29
|
toPercentStack,
|
|
26
30
|
withPointLabels,
|
|
31
|
+
withEndLabelInset,
|
|
27
32
|
withReferenceMarks,
|
|
28
33
|
withZoomInset,
|
|
29
34
|
} from '@classic-homes/charts-core';
|
|
@@ -64,6 +69,7 @@
|
|
|
64
69
|
orientation = 'vertical',
|
|
65
70
|
stacked = false,
|
|
66
71
|
showValues = false,
|
|
72
|
+
showTotal = false,
|
|
67
73
|
valueFormat,
|
|
68
74
|
locale,
|
|
69
75
|
labelFormatter,
|
|
@@ -138,6 +144,7 @@
|
|
|
138
144
|
const zoomAxis = isHorizontal ? 'y' : 'x';
|
|
139
145
|
const legendBottom = builtInLegend && (legendConfig?.position ?? 'bottom') === 'bottom';
|
|
140
146
|
|
|
147
|
+
const stackIds = data.series.map((s) => s.stack ?? (stacked ? 'Total' : undefined));
|
|
141
148
|
const series = data.series.map((source, index) => ({
|
|
142
149
|
name: source.name,
|
|
143
150
|
type: 'bar',
|
|
@@ -147,8 +154,11 @@
|
|
|
147
154
|
formatters.label,
|
|
148
155
|
{ position: labelPosition }
|
|
149
156
|
),
|
|
150
|
-
stack:
|
|
151
|
-
itemStyle:
|
|
157
|
+
stack: stackIds[index],
|
|
158
|
+
itemStyle: {
|
|
159
|
+
...(source.color && { color: source.color }),
|
|
160
|
+
borderRadius: barCornerRadius(isHorizontal, stackIds, index),
|
|
161
|
+
},
|
|
152
162
|
label: buildDataLabel({ show: true, position: labelPosition }, undefined, {
|
|
153
163
|
show: showValues,
|
|
154
164
|
formatter: formatters.label,
|
|
@@ -159,6 +169,26 @@
|
|
|
159
169
|
},
|
|
160
170
|
}));
|
|
161
171
|
|
|
172
|
+
// Totals use the value format, not the percent one: they show the raw sum
|
|
173
|
+
const format = resolveValueFormatter(valueFormat, locale);
|
|
174
|
+
const totalSeries = showTotal
|
|
175
|
+
? buildStackTotalSeries(data.categories, data.series, {
|
|
176
|
+
stacked,
|
|
177
|
+
position: labelPosition,
|
|
178
|
+
format,
|
|
179
|
+
axisType: categoryAxisType,
|
|
180
|
+
})
|
|
181
|
+
: [];
|
|
182
|
+
|
|
183
|
+
// Labels past the end of horizontal bars need room beside the plot
|
|
184
|
+
const endLabels = !isHorizontal
|
|
185
|
+
? []
|
|
186
|
+
: totalSeries.length
|
|
187
|
+
? [...stackTotals(data.series, stacked).values()].flat().map((t) => formatValue(t, format))
|
|
188
|
+
: showValues
|
|
189
|
+
? plotted.flatMap((s) => s.data.map((value) => formatValue(value, formatters.format)))
|
|
190
|
+
: [];
|
|
191
|
+
|
|
162
192
|
return mergeChartOption(
|
|
163
193
|
{
|
|
164
194
|
grid: {
|
|
@@ -167,7 +197,10 @@
|
|
|
167
197
|
bottom: '3%',
|
|
168
198
|
containLabel: true,
|
|
169
199
|
...withZoomInset(
|
|
170
|
-
|
|
200
|
+
withEndLabelInset(
|
|
201
|
+
gridInset(legendConfig, builtInLegend, xAxisConfig, yAxisConfig),
|
|
202
|
+
endLabels
|
|
203
|
+
),
|
|
171
204
|
dataZoom,
|
|
172
205
|
zoomAxis
|
|
173
206
|
),
|
|
@@ -200,7 +233,7 @@
|
|
|
200
233
|
isHorizontal ? categoryAxis : valueAxis,
|
|
201
234
|
isHorizontal ? yAxisConfig : formatters.axis(yAxisConfig)
|
|
202
235
|
),
|
|
203
|
-
series: withReferenceMarks(series, referenceLines, referenceBands, {
|
|
236
|
+
series: withReferenceMarks([...series, ...totalSeries], referenceLines, referenceBands, {
|
|
204
237
|
valueAxis: isHorizontal ? 'x' : 'y',
|
|
205
238
|
inverse: isHorizontal,
|
|
206
239
|
format: formatters.format,
|