@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,21 +1,40 @@
1
1
  <script lang="ts">
2
2
  import * as echarts from 'echarts/core';
3
3
  import { PieChart as EChartsPieChart } from 'echarts/charts';
4
- import { TooltipComponent, LegendComponent, TitleComponent } from 'echarts/components';
5
- import { CanvasRenderer } from 'echarts/renderers';
4
+ import { TooltipComponent, LegendComponent } from 'echarts/components';
6
5
  import type { EChartsOption } from 'echarts';
7
6
 
8
- import type { DonutChartProps, SliceEventParams } from '@classic-homes/charts-core';
9
- import { generatePieChartDescription, getChartColors } from '@classic-homes/charts-core';
7
+ import type { DonutChartProps } from '@classic-homes/charts-core';
8
+ import {
9
+ buildDataLabel,
10
+ buildLegend,
11
+ buildTooltip,
12
+ escapeHtml,
13
+ formatTooltipValue,
14
+ generatePieChartDescription,
15
+ getChartColors,
16
+ itemLabelLookup,
17
+ anchorCenterGraphic,
18
+ legendCenter,
19
+ mergeChartOption,
20
+ pointLabel,
21
+ resolveValueFormatter,
22
+ sliceTable,
23
+ } from '@classic-homes/charts-core';
10
24
 
11
25
  import { cn } from '../../utils.js';
26
+ import { chartEvents, toSliceParams } from '../../events.js';
12
27
  import { useChart } from '../../composables/useChart.svelte.js';
28
+ import {
29
+ useChartLegend,
30
+ type CustomLegendProps,
31
+ } from '../../composables/useChartLegend.svelte.js';
13
32
  import ChartContainer from '../base/ChartContainer.svelte';
14
33
 
15
34
  // Register required ECharts modules
16
- echarts.use([EChartsPieChart, TooltipComponent, LegendComponent, TitleComponent, CanvasRenderer]);
35
+ echarts.use([EChartsPieChart, TooltipComponent, LegendComponent]);
17
36
 
18
- interface Props extends Omit<DonutChartProps, 'class'> {
37
+ interface Props extends Omit<DonutChartProps, 'class'>, CustomLegendProps {
19
38
  class?: string;
20
39
  }
21
40
 
@@ -24,8 +43,11 @@
24
43
  description,
25
44
  data,
26
45
  height = 400,
46
+ minHeight,
47
+ maxHeight,
27
48
  loading,
28
49
  error,
50
+ onRetry,
29
51
  emptyMessage,
30
52
  theme = 'auto',
31
53
  animation = true,
@@ -36,149 +58,216 @@
36
58
  innerRadius = '50%',
37
59
  centerLabel,
38
60
  centerValue,
61
+ valueFormat,
62
+ locale,
63
+ labelFormatter,
64
+ tooltipFormatter,
65
+ tooltipValueFormatter,
66
+ legend: legendConfig,
67
+ renderLegend,
68
+ onLegendChange,
69
+ option: optionOverride,
70
+ onReady,
71
+ events,
72
+ showDataTable,
73
+ exportable,
74
+ patterns,
39
75
  onClick,
40
76
  class: className,
41
77
  }: Props = $props();
42
78
 
43
79
  const isEmpty = $derived(!data?.length);
44
80
 
81
+ // Slice labels show shares; `valueFormat` writes the values in tooltips and the data table
82
+ const valueFormatter = $derived(
83
+ tooltipValueFormatter ?? resolveValueFormatter(valueFormat, locale)
84
+ );
85
+
45
86
  // Per-element colours that follow the resolved chart theme
46
87
  const chartColors = $derived.by(() => getChartColors(chart.theme));
47
88
 
48
89
  const option: EChartsOption = $derived.by(() => {
49
90
  if (isEmpty) return {};
50
91
 
92
+ const getLabel = itemLabelLookup(data);
93
+
94
+ const pieCenter = legendCenter(legendConfig, showLegend && !renderLegend, ['50%', '45%']);
51
95
  const graphicElements: unknown[] = [];
52
96
 
53
97
  if (centerLabel || centerValue) {
54
- graphicElements.push({
55
- type: 'group',
56
- left: 'center',
57
- top: '40%',
58
- children: [
59
- ...(centerValue !== undefined
60
- ? [
61
- {
62
- type: 'text',
63
- style: {
64
- text: String(centerValue),
65
- fontSize: 28,
66
- fontWeight: 'bold',
67
- fill: chartColors.foreground,
68
- align: 'center',
69
- },
70
- top: 0,
71
- left: 'center',
72
- },
73
- ]
74
- : []),
75
- ...(centerLabel
76
- ? [
77
- {
78
- type: 'text',
79
- style: {
80
- text: centerLabel,
81
- fontSize: 14,
82
- fill: chartColors.mutedForeground,
83
- align: 'center',
84
- },
85
- top: centerValue !== undefined ? 35 : 0,
86
- left: 'center',
87
- },
88
- ]
89
- : []),
90
- ],
91
- });
98
+ graphicElements.push(
99
+ anchorCenterGraphic(
100
+ {
101
+ type: 'group',
102
+ left: 'center',
103
+ top: '40%',
104
+ children: [
105
+ ...(centerValue !== undefined
106
+ ? [
107
+ {
108
+ type: 'text',
109
+ style: {
110
+ text: String(centerValue),
111
+ fontSize: 28,
112
+ fontWeight: 'bold',
113
+ fill: chartColors.foreground,
114
+ align: 'center',
115
+ },
116
+ top: 0,
117
+ left: 'center',
118
+ },
119
+ ]
120
+ : []),
121
+ ...(centerLabel
122
+ ? [
123
+ {
124
+ type: 'text',
125
+ style: {
126
+ text: centerLabel,
127
+ fontSize: 14,
128
+ fill: chartColors.mutedForeground,
129
+ align: 'center',
130
+ },
131
+ top: centerValue !== undefined ? 35 : 0,
132
+ left: 'center',
133
+ },
134
+ ]
135
+ : []),
136
+ ],
137
+ },
138
+ pieCenter,
139
+ ['50%', '45%']
140
+ )
141
+ );
92
142
  }
93
143
 
94
- return {
95
- tooltip: showTooltip
96
- ? {
144
+ return mergeChartOption(
145
+ {
146
+ tooltip: buildTooltip(
147
+ {
97
148
  trigger: 'item',
98
- formatter: '{a} <br/>{b}: {c} ({d}%)',
99
- }
100
- : undefined,
101
- legend: showLegend
102
- ? {
103
- orient: 'horizontal',
104
- bottom: 0,
105
- data: data.map((d) => d.name),
149
+ // A string template ignores valueFormatter, so format the value here
150
+ formatter:
151
+ valueFormatter && !tooltipFormatter
152
+ ? (params: unknown) => {
153
+ const p = params as {
154
+ seriesName?: string;
155
+ name?: string;
156
+ value?: unknown;
157
+ percent?: number;
158
+ };
159
+ return `${escapeHtml(p.seriesName)} <br/>${escapeHtml(p.name)}: ${escapeHtml(formatTooltipValue(p.value, valueFormatter))} (${p.percent}%)`;
160
+ }
161
+ : '{a} <br/>{b}: {c} ({d}%)',
162
+ },
163
+ {
164
+ show: showTooltip,
165
+ formatter: tooltipFormatter,
166
+ valueFormatter,
167
+ getLabel,
106
168
  }
107
- : undefined,
108
- graphic:
109
- graphicElements.length > 0 ? (graphicElements as EChartsOption['graphic']) : undefined,
110
- series: [
111
- {
112
- name: title,
113
- type: 'pie',
114
- radius: [innerRadius, '70%'],
115
- center: ['50%', '45%'],
116
- avoidLabelOverlap: true,
117
- data: data.map((item) => ({
118
- name: item.name,
119
- value: item.value,
120
- itemStyle: item.color ? { color: item.color } : undefined,
121
- })),
122
- label: showLabels
123
- ? {
169
+ ),
170
+ legend: buildLegend(
171
+ data.map((d) => d.name),
172
+ legendConfig,
173
+ { show: showLegend, custom: !!renderLegend, defaults: { orient: 'horizontal' } }
174
+ ),
175
+ graphic:
176
+ graphicElements.length > 0 ? (graphicElements as EChartsOption['graphic']) : undefined,
177
+ series: [
178
+ {
179
+ name: title,
180
+ type: 'pie',
181
+ radius: [innerRadius, '70%'],
182
+ center: pieCenter,
183
+ avoidLabelOverlap: true,
184
+ data: data.map((item) => ({
185
+ name: item.name,
186
+ value: item.value,
187
+ itemStyle: item.color ? { color: item.color } : undefined,
188
+ label: pointLabel(item.label, labelFormatter, { position: labelPosition }),
189
+ })),
190
+ label: buildDataLabel(
191
+ {
124
192
  show: true,
125
193
  position: labelPosition,
126
194
  formatter: labelPosition === 'inside' ? '{d}%' : '{b}: {d}%',
127
- }
128
- : {
129
- show: false,
130
195
  },
131
- labelLine: {
132
- show: showLabels && labelPosition === 'outside',
133
- },
134
- emphasis: {
135
- itemStyle: {
136
- shadowBlur: 10,
137
- shadowOffsetX: 0,
138
- shadowColor: 'rgba(0, 0, 0, 0.5)',
196
+ { show: false },
197
+ { show: showLabels, formatter: labelFormatter, getLabel }
198
+ ),
199
+ labelLine: {
200
+ show: showLabels && labelPosition === 'outside',
201
+ },
202
+ emphasis: {
203
+ itemStyle: {
204
+ shadowBlur: 10,
205
+ shadowOffsetX: 0,
206
+ shadowColor: 'rgba(0, 0, 0, 0.5)',
207
+ },
139
208
  },
140
209
  },
141
- },
142
- ],
143
- };
210
+ ],
211
+ } as EChartsOption,
212
+ optionOverride
213
+ );
144
214
  });
145
215
 
146
216
  const accessibilityDescription = $derived(
147
217
  description || (!isEmpty ? generatePieChartDescription(title, data, true) : undefined)
148
218
  );
149
219
 
220
+ const dataTable = $derived(isEmpty ? undefined : sliceTable(data));
221
+
222
+ // Legend selection state, shared with a custom legend
223
+ const legend = useChartLegend({
224
+ names: () => data?.map((d) => d.name) ?? [],
225
+ colors: () => data?.map((d) => d.color) ?? [],
226
+ palette: () => getChartColors(chart.theme).series,
227
+ config: () => legendConfig,
228
+ title: () => title,
229
+ onLegendChange: () => onLegendChange,
230
+ });
231
+
150
232
  // Manage ECharts instance lifecycle (init, theme, resize, option updates)
151
233
  const chart = useChart({
152
234
  option: () => option,
153
235
  theme: () => theme,
154
236
  animation: () => animation,
155
237
  isEmpty: () => isEmpty,
156
- onReady: (instance) => {
157
- if (onClick) {
158
- instance.on('click', (params) => {
159
- onClick({
160
- type: params.type || 'click',
161
- componentType: params.componentType || 'series',
162
- name: params.name || '',
163
- value: params.value as number,
164
- percent: params.percent,
165
- color: params.color as string,
166
- } as SliceEventParams);
167
- });
168
- }
169
- },
238
+ patterns: () => patterns,
239
+ events: () =>
240
+ chartEvents(events, {
241
+ click: onClick && ((params) => onClick(toSliceParams(params, 'click'))),
242
+ }),
243
+ onReady: (instance) => legend.attach(instance),
244
+ onCreated: () => onReady,
170
245
  });
171
246
  </script>
172
247
 
248
+ {#snippet customLegend()}
249
+ {@render renderLegend?.(legend.renderProps)}
250
+ {/snippet}
251
+
173
252
  <ChartContainer
174
253
  {title}
175
254
  description={accessibilityDescription}
176
255
  {height}
256
+ {minHeight}
257
+ {maxHeight}
177
258
  {loading}
178
259
  {error}
260
+ {onRetry}
179
261
  empty={isEmpty}
180
262
  {emptyMessage}
181
263
  class={cn(className)}
264
+ legend={renderLegend ? customLegend : undefined}
265
+ legendPosition={legendConfig?.position}
266
+ {dataTable}
267
+ {showDataTable}
268
+ format={resolveValueFormatter(valueFormat, locale)}
269
+ {exportable}
270
+ exportImage={chart.exportImage}
182
271
  >
183
272
  <div bind:this={chart.container} class="h-full w-full"></div>
184
273
  </ChartContainer>
@@ -1,5 +1,6 @@
1
1
  import type { DonutChartProps } from '@classic-homes/charts-core';
2
- interface Props extends Omit<DonutChartProps, 'class'> {
2
+ import { type CustomLegendProps } from '../../composables/useChartLegend.svelte.js';
3
+ interface Props extends Omit<DonutChartProps, 'class'>, CustomLegendProps {
3
4
  class?: string;
4
5
  }
5
6
  declare const DonutChart: import("svelte").Component<Props, {}, "">;