@classic-homes/charts-svelte 0.1.38 → 0.2.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 (36) hide show
  1. package/dist/lib/components/base/ChartContainer.svelte +47 -8
  2. package/dist/lib/components/base/ChartContainer.svelte.d.ts +8 -0
  3. package/dist/lib/components/base/ChartLegend.svelte +59 -0
  4. package/dist/lib/components/base/ChartLegend.svelte.d.ts +10 -0
  5. package/dist/lib/components/base/index.d.ts +3 -0
  6. package/dist/lib/components/base/index.js +1 -0
  7. package/dist/lib/components/core/AreaChart.svelte +121 -54
  8. package/dist/lib/components/core/AreaChart.svelte.d.ts +2 -1
  9. package/dist/lib/components/core/BarChart.svelte +91 -35
  10. package/dist/lib/components/core/BarChart.svelte.d.ts +2 -1
  11. package/dist/lib/components/core/DonutChart.svelte +152 -82
  12. package/dist/lib/components/core/DonutChart.svelte.d.ts +2 -1
  13. package/dist/lib/components/core/LineChart.svelte +114 -47
  14. package/dist/lib/components/core/LineChart.svelte.d.ts +2 -1
  15. package/dist/lib/components/core/PieChart.svelte +100 -38
  16. package/dist/lib/components/core/PieChart.svelte.d.ts +2 -1
  17. package/dist/lib/components/core/ScatterChart.svelte +102 -34
  18. package/dist/lib/components/core/ScatterChart.svelte.d.ts +2 -1
  19. package/dist/lib/components/extended/CandlestickChart.svelte +44 -22
  20. package/dist/lib/components/extended/FunnelChart.svelte +102 -33
  21. package/dist/lib/components/extended/FunnelChart.svelte.d.ts +2 -1
  22. package/dist/lib/components/extended/GaugeChart.svelte +59 -38
  23. package/dist/lib/components/extended/HeatmapChart.svelte +47 -27
  24. package/dist/lib/components/extended/RadarChart.svelte +76 -22
  25. package/dist/lib/components/extended/RadarChart.svelte.d.ts +2 -1
  26. package/dist/lib/components/extended/SankeyChart.svelte +50 -21
  27. package/dist/lib/components/extended/TreemapChart.svelte +68 -27
  28. package/dist/lib/components/index.d.ts +1 -1
  29. package/dist/lib/components/index.js +1 -1
  30. package/dist/lib/composables/index.d.ts +1 -0
  31. package/dist/lib/composables/index.js +1 -0
  32. package/dist/lib/composables/useChartLegend.svelte.d.ts +37 -0
  33. package/dist/lib/composables/useChartLegend.svelte.js +54 -0
  34. package/dist/lib/index.d.ts +3 -3
  35. package/dist/lib/index.js +2 -2
  36. package/package.json +1 -1
@@ -6,6 +6,13 @@
6
6
  import type { EChartsOption } from 'echarts';
7
7
 
8
8
  import type { SankeyChartProps, SankeyEventParams } from '@classic-homes/charts-core';
9
+ import {
10
+ buildDataLabel,
11
+ buildTooltip,
12
+ mergeChartOption,
13
+ nameLabelLookup,
14
+ pointLabel,
15
+ } from '@classic-homes/charts-core';
9
16
 
10
17
  import { cn } from '../../utils.js';
11
18
  import { useChart } from '../../composables/useChart.svelte.js';
@@ -29,6 +36,10 @@
29
36
  animation = true,
30
37
  showTooltip = true,
31
38
  orient = 'horizontal',
39
+ labelFormatter,
40
+ tooltipFormatter,
41
+ tooltipValueFormatter,
42
+ option: optionOverride,
32
43
  onClick,
33
44
  class: className,
34
45
  }: Props = $props();
@@ -38,27 +49,45 @@
38
49
  const option: EChartsOption = $derived.by(() => {
39
50
  if (isEmpty) return {};
40
51
 
41
- return {
42
- tooltip: showTooltip ? { trigger: 'item', triggerOn: 'mousemove' } : undefined,
43
- series: [
44
- {
45
- type: 'sankey',
46
- orient,
47
- emphasis: { focus: 'adjacency' },
48
- data: data.nodes.map((node) => ({
49
- name: node.name,
50
- itemStyle: node.color ? { color: node.color } : undefined,
51
- })),
52
- links: data.links.map((link) => ({
53
- source: link.source,
54
- target: link.target,
55
- value: link.value,
56
- })),
57
- lineStyle: { color: 'gradient', curveness: 0.5 },
58
- label: { position: orient === 'horizontal' ? 'right' : 'bottom' },
59
- },
60
- ],
61
- };
52
+ const getLabel = nameLabelLookup(data.nodes);
53
+
54
+ return mergeChartOption(
55
+ {
56
+ tooltip: buildTooltip(
57
+ { trigger: 'item', triggerOn: 'mousemove' },
58
+ {
59
+ show: showTooltip,
60
+ formatter: tooltipFormatter,
61
+ valueFormatter: tooltipValueFormatter,
62
+ getLabel,
63
+ }
64
+ ),
65
+ series: [
66
+ {
67
+ type: 'sankey',
68
+ orient,
69
+ emphasis: { focus: 'adjacency' },
70
+ data: data.nodes.map((node) => ({
71
+ name: node.name,
72
+ itemStyle: node.color ? { color: node.color } : undefined,
73
+ label: pointLabel(node.label, labelFormatter),
74
+ })),
75
+ links: data.links.map((link) => ({
76
+ source: link.source,
77
+ target: link.target,
78
+ value: link.value,
79
+ })),
80
+ lineStyle: { color: 'gradient', curveness: 0.5 },
81
+ label: buildDataLabel(
82
+ { position: orient === 'horizontal' ? 'right' : 'bottom' },
83
+ undefined,
84
+ { show: true, formatter: labelFormatter, getLabel }
85
+ ),
86
+ },
87
+ ],
88
+ } as EChartsOption,
89
+ optionOverride
90
+ );
62
91
  });
63
92
 
64
93
  const accessibilityDescription = $derived(
@@ -5,8 +5,21 @@
5
5
  import { CanvasRenderer } from 'echarts/renderers';
6
6
  import type { EChartsOption } from 'echarts';
7
7
 
8
- import type { TreemapChartProps, TreemapEventParams } from '@classic-homes/charts-core';
9
- import { escapeHtml, getChartColors } from '@classic-homes/charts-core';
8
+ import type {
9
+ TreemapChartProps,
10
+ TreemapEventParams,
11
+ TreemapNode,
12
+ } from '@classic-homes/charts-core';
13
+ import {
14
+ buildDataLabel,
15
+ buildTooltip,
16
+ escapeHtml,
17
+ formatTooltipValue,
18
+ getChartColors,
19
+ mergeChartOption,
20
+ nameLabelLookup,
21
+ pointLabel,
22
+ } from '@classic-homes/charts-core';
10
23
 
11
24
  import { cn } from '../../utils.js';
12
25
  import { useChart } from '../../composables/useChart.svelte.js';
@@ -30,6 +43,10 @@
30
43
  theme = 'auto',
31
44
  animation = true,
32
45
  showTooltip = true,
46
+ labelFormatter,
47
+ tooltipFormatter,
48
+ tooltipValueFormatter,
49
+ option: optionOverride,
33
50
  onClick,
34
51
  class: className,
35
52
  }: Props = $props();
@@ -42,35 +59,59 @@
42
59
  const option: EChartsOption = $derived.by(() => {
43
60
  if (isEmpty) return {};
44
61
 
45
- return {
46
- tooltip: showTooltip
47
- ? {
62
+ const getLabel = nameLabelLookup(data);
63
+ // ECharts expects a label object, so swap each node's label text for one
64
+ const toNode = (n: TreemapNode): Record<string, unknown> => ({
65
+ ...n,
66
+ children: n.children?.map(toNode),
67
+ label: pointLabel(n.label, labelFormatter),
68
+ });
69
+
70
+ return mergeChartOption(
71
+ {
72
+ tooltip: buildTooltip(
73
+ {
48
74
  formatter: (params: unknown) => {
49
75
  const p = params as { name?: string; value?: number };
50
- return `${escapeHtml(p.name)}: ${escapeHtml(p.value)}`;
76
+ return `${escapeHtml(p.name)}: ${escapeHtml(formatTooltipValue(p.value, tooltipValueFormatter))}`;
51
77
  },
78
+ },
79
+ {
80
+ show: showTooltip,
81
+ formatter: tooltipFormatter,
82
+ valueFormatter: tooltipValueFormatter,
83
+ getLabel,
52
84
  }
53
- : undefined,
54
- series: [
55
- {
56
- type: 'treemap',
57
- data,
58
- roam: false,
59
- levels: (levels || [
60
- { itemStyle: { borderColor: chartColors.divider, borderWidth: 4, gapWidth: 4 } },
61
- {
62
- colorSaturation: [0.3, 0.6],
63
- itemStyle: { borderColorSaturation: 0.7, gapWidth: 2, borderWidth: 2 },
64
- },
65
- { colorSaturation: [0.3, 0.5], itemStyle: { borderColorSaturation: 0.6, gapWidth: 1 } },
66
- { colorSaturation: [0.3, 0.5] },
67
- ]) as unknown[],
68
- label: { show: true, formatter: '{b}' },
69
- upperLabel: { show: true, height: 30 },
70
- breadcrumb: { show: true },
71
- },
72
- ] as EChartsOption['series'],
73
- };
85
+ ),
86
+ series: [
87
+ {
88
+ type: 'treemap',
89
+ data: data.map(toNode),
90
+ roam: false,
91
+ levels: (levels || [
92
+ { itemStyle: { borderColor: chartColors.divider, borderWidth: 4, gapWidth: 4 } },
93
+ {
94
+ colorSaturation: [0.3, 0.6],
95
+ itemStyle: { borderColorSaturation: 0.7, gapWidth: 2, borderWidth: 2 },
96
+ },
97
+ {
98
+ colorSaturation: [0.3, 0.5],
99
+ itemStyle: { borderColorSaturation: 0.6, gapWidth: 1 },
100
+ },
101
+ { colorSaturation: [0.3, 0.5] },
102
+ ]) as unknown[],
103
+ label: buildDataLabel({ show: true, formatter: '{b}' }, undefined, {
104
+ show: true,
105
+ formatter: labelFormatter,
106
+ getLabel,
107
+ }),
108
+ upperLabel: { show: true, height: 30 },
109
+ breadcrumb: { show: true },
110
+ },
111
+ ] as EChartsOption['series'],
112
+ } as EChartsOption,
113
+ optionOverride
114
+ );
74
115
  });
75
116
 
76
117
  const accessibilityDescription = $derived(
@@ -1,3 +1,3 @@
1
- export { ChartContainer, ChartSkeleton, ChartError, ChartEmpty, type ChartContainerProps, type ChartSkeletonProps, type ChartErrorProps, type ChartEmptyProps, } from './base/index.js';
1
+ export { ChartContainer, ChartSkeleton, ChartError, ChartEmpty, ChartLegend, type ChartContainerProps, type ChartLegendProps, type ChartSkeletonProps, type ChartErrorProps, type ChartEmptyProps, } from './base/index.js';
2
2
  export { LineChart, BarChart, PieChart, AreaChart, ScatterChart, DonutChart, } from './core/index.js';
3
3
  export { RadarChart, FunnelChart, GaugeChart, HeatmapChart, TreemapChart, CandlestickChart, SankeyChart, } from './extended/index.js';
@@ -1,5 +1,5 @@
1
1
  // Base components
2
- export { ChartContainer, ChartSkeleton, ChartError, ChartEmpty, } from './base/index.js';
2
+ export { ChartContainer, ChartSkeleton, ChartError, ChartEmpty, ChartLegend, } from './base/index.js';
3
3
  // Core charts
4
4
  export { LineChart, BarChart, PieChart, AreaChart, ScatterChart, DonutChart, } from './core/index.js';
5
5
  // Extended charts
@@ -2,3 +2,4 @@ export { useChart, type UseChartOptions, type UseChartReturn } from './useChart.
2
2
  export { useChartTheme, registerClassicThemes } from './useChartTheme.svelte.js';
3
3
  export { useReducedMotion } from './useReducedMotion.svelte.js';
4
4
  export { useChartResize } from './useChartResize.svelte.js';
5
+ export { useChartLegend, type CustomLegendProps, type UseChartLegendOptions, type UseChartLegendReturn, } from './useChartLegend.svelte.js';
@@ -2,3 +2,4 @@ export { useChart } from './useChart.svelte.js';
2
2
  export { useChartTheme, registerClassicThemes } from './useChartTheme.svelte.js';
3
3
  export { useReducedMotion } from './useReducedMotion.svelte.js';
4
4
  export { useChartResize } from './useChartResize.svelte.js';
5
+ export { useChartLegend, } from './useChartLegend.svelte.js';
@@ -0,0 +1,37 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type * as echarts from 'echarts/core';
3
+ import { type ChartLegendConfig, type LegendRenderProps } from '@classic-homes/charts-core';
4
+ /** Props shared by charts that can render a custom legend */
5
+ export interface CustomLegendProps {
6
+ /**
7
+ * Render your own legend in place of the built-in one, e.g.
8
+ * `{#snippet legend(props)}<ChartLegend {...props} />{/snippet}` passed as
9
+ * `renderLegend={legend}`
10
+ */
11
+ renderLegend?: Snippet<[LegendRenderProps]>;
12
+ }
13
+ export interface UseChartLegendOptions {
14
+ /** Getter returning legend item names in chart order */
15
+ names: () => string[];
16
+ /** Getter returning per-item colour overrides, by index */
17
+ colors: () => (string | undefined)[];
18
+ /** Getter returning the theme series palette */
19
+ palette: () => string[];
20
+ /** Getter returning the built-in legend config (its position also places a custom legend) */
21
+ config?: () => ChartLegendConfig | undefined;
22
+ /** Getter returning the chart title */
23
+ title: () => string;
24
+ /** Getter returning the change callback, read at event time */
25
+ onLegendChange?: () => ((visible: Record<string, boolean>) => void) | undefined;
26
+ }
27
+ export interface UseChartLegendReturn {
28
+ /** Register legend listeners; call from the chart's `onReady` */
29
+ attach: (chart: echarts.ECharts) => void;
30
+ /** State and actions for a custom legend renderer */
31
+ readonly renderProps: LegendRenderProps;
32
+ }
33
+ /**
34
+ * Track legend selection for a chart and expose it to custom legends
35
+ * Matches behavior of React useChartLegend hook
36
+ */
37
+ export declare function useChartLegend(opts: UseChartLegendOptions): UseChartLegendReturn;
@@ -0,0 +1,54 @@
1
+ import { getLegendItems, } from '@classic-homes/charts-core';
2
+ // Every legend selection event carries the full `selected` map
3
+ const LEGEND_EVENTS = [
4
+ 'legendselectchanged',
5
+ 'legendselected',
6
+ 'legendunselected',
7
+ 'legendselectall',
8
+ 'legendinverseselect',
9
+ ];
10
+ /**
11
+ * Track legend selection for a chart and expose it to custom legends
12
+ * Matches behavior of React useChartLegend hook
13
+ */
14
+ export function useChartLegend(opts) {
15
+ let chartInstance = null;
16
+ let visible = $state({});
17
+ const dispatch = (action) => {
18
+ if (chartInstance && !chartInstance.isDisposed())
19
+ chartInstance.dispatchAction(action);
20
+ };
21
+ const items = $derived(getLegendItems(opts.names(), opts.palette(), opts.colors(), visible));
22
+ const actions = {
23
+ toggle: (name) => dispatch({ type: 'legendToggleSelect', name }),
24
+ setVisible: (name, show) => dispatch({ type: show ? 'legendSelect' : 'legendUnSelect', name }),
25
+ showOnly: (name) => {
26
+ for (const other of opts.names()) {
27
+ dispatch({ type: other === name ? 'legendSelect' : 'legendUnSelect', name: other });
28
+ }
29
+ },
30
+ showAll: () => dispatch({ type: 'legendAllSelect' }),
31
+ };
32
+ return {
33
+ attach(chart) {
34
+ chartInstance = chart;
35
+ // A new instance starts with every item shown
36
+ visible = {};
37
+ const handler = (params) => {
38
+ const selected = { ...params.selected };
39
+ visible = selected;
40
+ opts.onLegendChange?.()?.(selected);
41
+ };
42
+ for (const event of LEGEND_EVENTS)
43
+ chart.on(event, handler);
44
+ },
45
+ get renderProps() {
46
+ return {
47
+ items,
48
+ position: opts.config?.()?.position ?? 'bottom',
49
+ chartTitle: opts.title(),
50
+ ...actions,
51
+ };
52
+ },
53
+ };
54
+ }
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Svelte ECharts components for the Classic Theme design system
5
5
  */
6
- export { ChartContainer, ChartSkeleton, ChartError, ChartEmpty, type ChartContainerProps, type ChartSkeletonProps, type ChartErrorProps, type ChartEmptyProps, LineChart, BarChart, PieChart, AreaChart, ScatterChart, DonutChart, RadarChart, FunnelChart, GaugeChart, HeatmapChart, TreemapChart, CandlestickChart, SankeyChart, } from './components/index.js';
7
- export { useChart, useChartTheme, useChartResize, registerClassicThemes, useReducedMotion, type UseChartOptions, type UseChartReturn, } from './composables/index.js';
6
+ export { ChartContainer, ChartSkeleton, ChartError, ChartEmpty, ChartLegend, type ChartContainerProps, type ChartLegendProps, type ChartSkeletonProps, type ChartErrorProps, type ChartEmptyProps, LineChart, BarChart, PieChart, AreaChart, ScatterChart, DonutChart, RadarChart, FunnelChart, GaugeChart, HeatmapChart, TreemapChart, CandlestickChart, SankeyChart, } from './components/index.js';
7
+ export { useChart, useChartTheme, useChartResize, registerClassicThemes, useReducedMotion, useChartLegend, type CustomLegendProps, type UseChartLegendOptions, type UseChartLegendReturn, type UseChartOptions, type UseChartReturn, } from './composables/index.js';
8
8
  export { cn } from './utils.js';
9
- export type { BaseChartProps, LineChartProps, BarChartProps, PieChartProps, DonutChartProps, AreaChartProps, ScatterChartProps, RadarChartProps, FunnelChartProps, GaugeChartProps, HeatmapChartProps, TreemapChartProps, CandlestickChartProps, SankeyChartProps, LineChartData, LineSeriesData, BarChartData, BarSeriesData, PieSliceData, AreaChartData, AreaSeriesData, ScatterChartData, ScatterSeriesData, RadarChartData, RadarSeriesData, RadarIndicator, FunnelSliceData, HeatmapData, TreemapNode, TreemapLevel, CandlestickData, CandlestickItem, SankeyData, SankeyNode, SankeyLink, BaseEventParams, DataPointEventParams, SliceEventParams, HeatmapEventParams, TreemapEventParams, CandlestickEventParams, SankeyEventParams, } from '@classic-homes/charts-core';
9
+ export type { BaseChartProps, LegendChartProps, LabelChartProps, AxisChartProps, AxisConfig, ChartFormatter, ChartLegendConfig, ChartLegendPosition, LabelFormatterParams, TooltipFormatterParams, LegendItem, LegendRenderProps, LineChartProps, BarChartProps, PieChartProps, DonutChartProps, AreaChartProps, ScatterChartProps, RadarChartProps, FunnelChartProps, GaugeChartProps, HeatmapChartProps, TreemapChartProps, CandlestickChartProps, SankeyChartProps, LineChartData, LineSeriesData, BarChartData, BarSeriesData, PieSliceData, AreaChartData, AreaSeriesData, ScatterChartData, ScatterSeriesData, RadarChartData, RadarSeriesData, RadarIndicator, FunnelSliceData, HeatmapData, TreemapNode, TreemapLevel, CandlestickData, CandlestickItem, SankeyData, SankeyNode, SankeyLink, BaseEventParams, DataPointEventParams, SliceEventParams, HeatmapEventParams, TreemapEventParams, CandlestickEventParams, SankeyEventParams, } from '@classic-homes/charts-core';
package/dist/lib/index.js CHANGED
@@ -6,12 +6,12 @@
6
6
  // Components
7
7
  export {
8
8
  // Base
9
- ChartContainer, ChartSkeleton, ChartError, ChartEmpty,
9
+ ChartContainer, ChartSkeleton, ChartError, ChartEmpty, ChartLegend,
10
10
  // Core charts
11
11
  LineChart, BarChart, PieChart, AreaChart, ScatterChart, DonutChart,
12
12
  // Extended charts
13
13
  RadarChart, FunnelChart, GaugeChart, HeatmapChart, TreemapChart, CandlestickChart, SankeyChart, } from './components/index.js';
14
14
  // Composables
15
- export { useChart, useChartTheme, useChartResize, registerClassicThemes, useReducedMotion, } from './composables/index.js';
15
+ export { useChart, useChartTheme, useChartResize, registerClassicThemes, useReducedMotion, useChartLegend, } from './composables/index.js';
16
16
  // Utils
17
17
  export { cn } from './utils.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/charts-svelte",
3
- "version": "0.1.38",
3
+ "version": "0.2.0",
4
4
  "description": "Svelte ECharts components for Classic Theme",
5
5
  "type": "module",
6
6
  "svelte": "./dist/lib/index.js",