@classic-homes/charts-svelte 0.1.38 → 0.3.0

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.
Files changed (66) hide show
  1. package/dist/lib/components/base/ChartContainer.svelte +175 -9
  2. package/dist/lib/components/base/ChartContainer.svelte.d.ts +26 -0
  3. package/dist/lib/components/base/ChartDataTable.svelte +59 -0
  4. package/dist/lib/components/base/ChartDataTable.svelte.d.ts +15 -0
  5. package/dist/lib/components/base/ChartEmpty.svelte +1 -0
  6. package/dist/lib/components/base/ChartLegend.svelte +59 -0
  7. package/dist/lib/components/base/ChartLegend.svelte.d.ts +10 -0
  8. package/dist/lib/components/base/ChartSkeleton.svelte +7 -2
  9. package/dist/lib/components/base/ChartSkeleton.svelte.d.ts +2 -0
  10. package/dist/lib/components/base/index.d.ts +6 -0
  11. package/dist/lib/components/base/index.js +2 -0
  12. package/dist/lib/components/core/AreaChart.svelte +204 -92
  13. package/dist/lib/components/core/AreaChart.svelte.d.ts +2 -1
  14. package/dist/lib/components/core/BarChart.svelte +202 -74
  15. package/dist/lib/components/core/BarChart.svelte.d.ts +2 -1
  16. package/dist/lib/components/core/DonutChart.svelte +189 -100
  17. package/dist/lib/components/core/DonutChart.svelte.d.ts +2 -1
  18. package/dist/lib/components/core/LineChart.svelte +195 -97
  19. package/dist/lib/components/core/LineChart.svelte.d.ts +2 -1
  20. package/dist/lib/components/core/PieChart.svelte +137 -56
  21. package/dist/lib/components/core/PieChart.svelte.d.ts +2 -1
  22. package/dist/lib/components/core/ScatterChart.svelte +167 -95
  23. package/dist/lib/components/core/ScatterChart.svelte.d.ts +2 -1
  24. package/dist/lib/components/extended/CandlestickChart.svelte +106 -61
  25. package/dist/lib/components/extended/FunnelChart.svelte +139 -56
  26. package/dist/lib/components/extended/FunnelChart.svelte.d.ts +2 -1
  27. package/dist/lib/components/extended/GaugeChart.svelte +111 -39
  28. package/dist/lib/components/extended/HeatmapChart.svelte +107 -58
  29. package/dist/lib/components/extended/RadarChart.svelte +111 -37
  30. package/dist/lib/components/extended/RadarChart.svelte.d.ts +2 -1
  31. package/dist/lib/components/extended/SankeyChart.svelte +99 -40
  32. package/dist/lib/components/extended/TreemapChart.svelte +118 -45
  33. package/dist/lib/components/index.d.ts +2 -1
  34. package/dist/lib/components/index.js +3 -1
  35. package/dist/lib/components/report/BoxPlotChart.svelte +253 -0
  36. package/dist/lib/components/report/BoxPlotChart.svelte.d.ts +7 -0
  37. package/dist/lib/components/report/BulletChart.svelte +231 -0
  38. package/dist/lib/components/report/BulletChart.svelte.d.ts +7 -0
  39. package/dist/lib/components/report/ComboChart.svelte +327 -0
  40. package/dist/lib/components/report/ComboChart.svelte.d.ts +8 -0
  41. package/dist/lib/components/report/DotPlotChart.svelte +325 -0
  42. package/dist/lib/components/report/DotPlotChart.svelte.d.ts +8 -0
  43. package/dist/lib/components/report/HistogramChart.svelte +226 -0
  44. package/dist/lib/components/report/HistogramChart.svelte.d.ts +7 -0
  45. package/dist/lib/components/report/Sparkline.svelte +181 -0
  46. package/dist/lib/components/report/Sparkline.svelte.d.ts +4 -0
  47. package/dist/lib/components/report/StatCard.svelte +146 -0
  48. package/dist/lib/components/report/StatCard.svelte.d.ts +4 -0
  49. package/dist/lib/components/report/StatGroup.svelte +35 -0
  50. package/dist/lib/components/report/StatGroup.svelte.d.ts +12 -0
  51. package/dist/lib/components/report/WaterfallChart.svelte +271 -0
  52. package/dist/lib/components/report/WaterfallChart.svelte.d.ts +7 -0
  53. package/dist/lib/components/report/index.d.ts +12 -0
  54. package/dist/lib/components/report/index.js +9 -0
  55. package/dist/lib/composables/index.d.ts +2 -1
  56. package/dist/lib/composables/index.js +2 -1
  57. package/dist/lib/composables/useChart.svelte.d.ts +34 -0
  58. package/dist/lib/composables/useChart.svelte.js +121 -9
  59. package/dist/lib/composables/useChartLegend.svelte.d.ts +37 -0
  60. package/dist/lib/composables/useChartLegend.svelte.js +54 -0
  61. package/dist/lib/composables/useChartTheme.svelte.js +14 -11
  62. package/dist/lib/events.d.ts +15 -0
  63. package/dist/lib/events.js +56 -0
  64. package/dist/lib/index.d.ts +4 -3
  65. package/dist/lib/index.js +7 -3
  66. package/package.json +10 -4
@@ -1,33 +1,46 @@
1
1
  <script lang="ts">
2
2
  import * as echarts from 'echarts/core';
3
3
  import { BarChart as EChartsBarChart } from 'echarts/charts';
4
- import {
5
- GridComponent,
6
- TooltipComponent,
7
- LegendComponent,
8
- TitleComponent,
9
- } from 'echarts/components';
10
- import { CanvasRenderer } from 'echarts/renderers';
4
+ import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components';
11
5
  import type { EChartsOption } from 'echarts';
12
6
 
13
- import type { BarChartProps, DataPointEventParams } from '@classic-homes/charts-core';
14
- import { generateBarChartDescription } from '@classic-homes/charts-core';
7
+ import type { BarChartProps } from '@classic-homes/charts-core';
8
+ import {
9
+ allSeriesEmpty,
10
+ buildAxis,
11
+ buildDataLabel,
12
+ buildDataZoom,
13
+ buildLegend,
14
+ buildTooltip,
15
+ categoryTable,
16
+ chartFormatters,
17
+ generateBarChartDescription,
18
+ getChartColors,
19
+ gridInset,
20
+ mergeChartOption,
21
+ pairWithCategories,
22
+ resolveChartHeight,
23
+ resolveValueFormatter,
24
+ seriesLabelLookup,
25
+ toPercentStack,
26
+ withPointLabels,
27
+ withReferenceMarks,
28
+ withZoomInset,
29
+ } from '@classic-homes/charts-core';
15
30
 
16
31
  import { cn } from '../../utils.js';
32
+ import { chartEvents, toDataPointParams } from '../../events.js';
17
33
  import { useChart } from '../../composables/useChart.svelte.js';
34
+ import {
35
+ useChartLegend,
36
+ type CustomLegendProps,
37
+ } from '../../composables/useChartLegend.svelte.js';
18
38
  import ChartContainer from '../base/ChartContainer.svelte';
19
39
 
20
40
  // Register required ECharts modules
21
- echarts.use([
22
- EChartsBarChart,
23
- GridComponent,
24
- TooltipComponent,
25
- LegendComponent,
26
- TitleComponent,
27
- CanvasRenderer,
28
- ]);
29
-
30
- interface Props extends Omit<BarChartProps, 'class'> {
41
+ echarts.use([EChartsBarChart, GridComponent, TooltipComponent, LegendComponent]);
42
+
43
+ interface Props extends Omit<BarChartProps, 'class'>, CustomLegendProps {
31
44
  class?: string;
32
45
  }
33
46
 
@@ -36,8 +49,12 @@
36
49
  description,
37
50
  data,
38
51
  height = 400,
52
+ minHeight,
53
+ maxHeight,
54
+ rowHeight,
39
55
  loading,
40
56
  error,
57
+ onRetry,
41
58
  emptyMessage,
42
59
  theme = 'auto',
43
60
  animation = true,
@@ -47,22 +64,62 @@
47
64
  orientation = 'vertical',
48
65
  stacked = false,
49
66
  showValues = false,
67
+ valueFormat,
68
+ locale,
69
+ labelFormatter,
70
+ xAxisConfig,
71
+ yAxisConfig,
72
+ referenceLines,
73
+ referenceBands,
74
+ dataZoom,
75
+ tooltipFormatter,
76
+ tooltipValueFormatter,
77
+ legend: legendConfig,
78
+ renderLegend,
79
+ onLegendChange,
80
+ option: optionOverride,
81
+ onReady,
82
+ events,
83
+ showDataTable,
84
+ exportable,
85
+ patterns,
50
86
  onClick,
87
+ onHover,
51
88
  class: className,
52
89
  }: Props = $props();
53
90
 
54
- const isEmpty = $derived(
55
- !data?.categories?.length || !data?.series?.length || data.series.every((s) => !s.data?.length)
56
- );
91
+ const isEmpty = $derived(!data?.categories?.length || allSeriesEmpty(data?.series));
57
92
 
58
93
  const isHorizontal = $derived(orientation === 'horizontal');
94
+ const isPercent = $derived(stacked === 'percent');
95
+
96
+ // Percent stacks plot shares, so they always read as percentages
97
+ const formatters = $derived(
98
+ chartFormatters({
99
+ valueFormat: isPercent ? 'percentValue' : valueFormat,
100
+ locale,
101
+ tooltipValueFormatter,
102
+ labelFormatter,
103
+ })
104
+ );
105
+
106
+ // Per-element colours that follow the resolved chart theme
107
+ const chartColors = $derived.by(() => getChartColors(chart.theme));
108
+
109
+ const builtInLegend = $derived(showLegend && !renderLegend);
59
110
 
60
111
  const option: EChartsOption = $derived.by(() => {
61
112
  if (isEmpty) return {};
62
113
 
114
+ const plotted = isPercent ? toPercentStack(data.series) : data.series;
115
+ // A time or value x-axis reads [x, y] points (vertical bars only)
116
+ const categoryAxisType = isHorizontal ? undefined : xAxisConfig?.type;
117
+
63
118
  const categoryAxis = {
64
119
  type: 'category' as const,
65
120
  data: data.categories,
121
+ // Horizontal bars list the first category at the top
122
+ inverse: isHorizontal,
66
123
  axisTick: {
67
124
  alignWithLabel: true,
68
125
  },
@@ -70,91 +127,162 @@
70
127
 
71
128
  const valueAxis = {
72
129
  type: 'value' as const,
130
+ ...(isPercent && { max: 100 }),
73
131
  splitLine: {
74
132
  show: showGrid,
75
133
  },
76
134
  };
77
135
 
78
- return {
79
- grid: {
80
- left: '3%',
81
- right: '4%',
82
- bottom: '3%',
83
- containLabel: true,
136
+ const getLabel = seriesLabelLookup(data.series);
137
+ const labelPosition = isHorizontal ? 'right' : 'top';
138
+ const zoomAxis = isHorizontal ? 'y' : 'x';
139
+ const legendBottom = builtInLegend && (legendConfig?.position ?? 'bottom') === 'bottom';
140
+
141
+ const series = data.series.map((source, index) => ({
142
+ name: source.name,
143
+ type: 'bar',
144
+ data: withPointLabels(
145
+ pairWithCategories(data.categories, plotted[index].data, categoryAxisType),
146
+ source.labels,
147
+ formatters.label,
148
+ { position: labelPosition }
149
+ ),
150
+ stack: source.stack ?? (stacked ? 'Total' : undefined),
151
+ itemStyle: source.color ? { color: source.color } : undefined,
152
+ label: buildDataLabel({ show: true, position: labelPosition }, undefined, {
153
+ show: showValues,
154
+ formatter: formatters.label,
155
+ getLabel,
156
+ }),
157
+ emphasis: {
158
+ focus: 'series',
84
159
  },
85
- tooltip: showTooltip
86
- ? {
160
+ }));
161
+
162
+ return mergeChartOption(
163
+ {
164
+ grid: {
165
+ left: '3%',
166
+ right: '4%',
167
+ bottom: '3%',
168
+ containLabel: true,
169
+ ...withZoomInset(
170
+ gridInset(legendConfig, builtInLegend, xAxisConfig, yAxisConfig),
171
+ dataZoom,
172
+ zoomAxis
173
+ ),
174
+ },
175
+ tooltip: buildTooltip(
176
+ {
87
177
  trigger: 'axis',
88
178
  axisPointer: {
89
179
  type: 'shadow',
90
180
  },
181
+ },
182
+ {
183
+ show: showTooltip,
184
+ formatter: tooltipFormatter,
185
+ valueFormatter: formatters.tooltip,
186
+ getLabel,
91
187
  }
92
- : undefined,
93
- legend: showLegend
94
- ? {
95
- data: data.series.map((s) => s.name),
96
- bottom: 0,
97
- }
98
- : undefined,
99
- xAxis: isHorizontal ? valueAxis : categoryAxis,
100
- yAxis: isHorizontal ? categoryAxis : valueAxis,
101
- series: data.series.map((series) => ({
102
- name: series.name,
103
- type: 'bar',
104
- data: series.data,
105
- stack: stacked ? 'Total' : undefined,
106
- itemStyle: series.color ? { color: series.color } : undefined,
107
- label: showValues
108
- ? {
109
- show: true,
110
- position: isHorizontal ? 'right' : 'top',
111
- }
112
- : undefined,
113
- emphasis: {
114
- focus: 'series',
115
- },
116
- })),
117
- };
188
+ ),
189
+ legend: buildLegend(
190
+ data.series.map((s) => s.name),
191
+ legendConfig,
192
+ { show: showLegend, custom: !!renderLegend }
193
+ ),
194
+ dataZoom: buildDataZoom(dataZoom, zoomAxis, legendBottom ? 36 : 0),
195
+ xAxis: buildAxis(
196
+ isHorizontal ? valueAxis : categoryAxis,
197
+ isHorizontal ? formatters.axis(xAxisConfig) : xAxisConfig
198
+ ),
199
+ yAxis: buildAxis(
200
+ isHorizontal ? categoryAxis : valueAxis,
201
+ isHorizontal ? yAxisConfig : formatters.axis(yAxisConfig)
202
+ ),
203
+ series: withReferenceMarks(series, referenceLines, referenceBands, {
204
+ valueAxis: isHorizontal ? 'x' : 'y',
205
+ inverse: isHorizontal,
206
+ format: formatters.format,
207
+ color: chartColors.reference,
208
+ bandColor: chartColors.referenceBand,
209
+ }),
210
+ } as EChartsOption,
211
+ optionOverride
212
+ );
118
213
  });
119
214
 
120
215
  const accessibilityDescription = $derived(
121
- description || (!isEmpty ? generateBarChartDescription(title, data) : undefined)
216
+ description ||
217
+ (!isEmpty ? generateBarChartDescription(title, data, formatters.format) : undefined)
122
218
  );
123
219
 
220
+ const dataTable = $derived(isEmpty ? undefined : categoryTable(data));
221
+
222
+ // One row per category when horizontal; a bottom legend needs its own row
223
+ const resolvedHeight = $derived(
224
+ isHorizontal
225
+ ? resolveChartHeight(height, data?.categories?.length ?? 0, {
226
+ rowHeight,
227
+ minHeight,
228
+ maxHeight,
229
+ chrome: 56 + (builtInLegend ? 36 : 0) + (dataZoom ? 36 : 0),
230
+ })
231
+ : height === 'auto'
232
+ ? 400
233
+ : height
234
+ );
235
+
236
+ // Legend selection state, shared with a custom legend
237
+ const legend = useChartLegend({
238
+ names: () => data?.series?.map((s) => s.name) ?? [],
239
+ colors: () => data?.series?.map((s) => s.color) ?? [],
240
+ palette: () => getChartColors(chart.theme).series,
241
+ config: () => legendConfig,
242
+ title: () => title,
243
+ onLegendChange: () => onLegendChange,
244
+ });
245
+
124
246
  // Manage ECharts instance lifecycle (init, theme, resize, option updates)
125
247
  const chart = useChart({
126
248
  option: () => option,
127
249
  theme: () => theme,
128
250
  animation: () => animation,
129
251
  isEmpty: () => isEmpty,
130
- onReady: (instance) => {
131
- if (onClick) {
132
- instance.on('click', (params) => {
133
- onClick({
134
- type: params.type || 'click',
135
- componentType: params.componentType || 'series',
136
- seriesIndex: params.seriesIndex,
137
- seriesName: params.seriesName || '',
138
- dataIndex: params.dataIndex || 0,
139
- name: params.name || '',
140
- value: params.value as number,
141
- color: params.color as string,
142
- } as DataPointEventParams);
143
- });
144
- }
145
- },
252
+ patterns: () => patterns,
253
+ events: () =>
254
+ chartEvents(events, {
255
+ click: onClick && ((params) => onClick(toDataPointParams(params, 'click'))),
256
+ mouseover: onHover && ((params) => onHover(toDataPointParams(params, 'mouseover'))),
257
+ }),
258
+ onReady: (instance) => legend.attach(instance),
259
+ onCreated: () => onReady,
146
260
  });
147
261
  </script>
148
262
 
263
+ {#snippet customLegend()}
264
+ {@render renderLegend?.(legend.renderProps)}
265
+ {/snippet}
266
+
149
267
  <ChartContainer
150
268
  {title}
151
269
  description={accessibilityDescription}
152
- {height}
270
+ height={resolvedHeight}
271
+ {minHeight}
272
+ {maxHeight}
153
273
  {loading}
154
274
  {error}
275
+ {onRetry}
155
276
  empty={isEmpty}
156
277
  {emptyMessage}
157
278
  class={cn(className)}
279
+ legend={renderLegend ? customLegend : undefined}
280
+ legendPosition={legendConfig?.position}
281
+ {dataTable}
282
+ {showDataTable}
283
+ format={resolveValueFormatter(valueFormat, locale)}
284
+ {exportable}
285
+ exportImage={chart.exportImage}
158
286
  >
159
287
  <div bind:this={chart.container} class="h-full w-full"></div>
160
288
  </ChartContainer>
@@ -1,5 +1,6 @@
1
1
  import type { BarChartProps } from '@classic-homes/charts-core';
2
- interface Props extends Omit<BarChartProps, 'class'> {
2
+ import { type CustomLegendProps } from '../../composables/useChartLegend.svelte.js';
3
+ interface Props extends Omit<BarChartProps, 'class'>, CustomLegendProps {
3
4
  class?: string;
4
5
  }
5
6
  declare const BarChart: import("svelte").Component<Props, {}, "">;