@loidolt/theme-charts 0.7.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 (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +124 -0
  3. package/dist/components/BarChart.svelte +64 -0
  4. package/dist/components/BarChart.svelte.d.ts +14 -0
  5. package/dist/components/CandlestickChart.svelte +58 -0
  6. package/dist/components/CandlestickChart.svelte.d.ts +21 -0
  7. package/dist/components/Chart.svelte +282 -0
  8. package/dist/components/Chart.svelte.d.ts +73 -0
  9. package/dist/components/ChartDataTable.svelte +79 -0
  10. package/dist/components/ChartDataTable.svelte.d.ts +23 -0
  11. package/dist/components/FunnelChart.svelte +50 -0
  12. package/dist/components/FunnelChart.svelte.d.ts +18 -0
  13. package/dist/components/GaugeChart.svelte +54 -0
  14. package/dist/components/GaugeChart.svelte.d.ts +18 -0
  15. package/dist/components/HeatmapChart.svelte +51 -0
  16. package/dist/components/HeatmapChart.svelte.d.ts +14 -0
  17. package/dist/components/LineChart.svelte +66 -0
  18. package/dist/components/LineChart.svelte.d.ts +14 -0
  19. package/dist/components/PieChart.svelte +62 -0
  20. package/dist/components/PieChart.svelte.d.ts +18 -0
  21. package/dist/components/RadarChart.svelte +48 -0
  22. package/dist/components/RadarChart.svelte.d.ts +14 -0
  23. package/dist/components/SankeyChart.svelte +48 -0
  24. package/dist/components/SankeyChart.svelte.d.ts +18 -0
  25. package/dist/components/ScatterChart.svelte +61 -0
  26. package/dist/components/ScatterChart.svelte.d.ts +18 -0
  27. package/dist/components/TreemapChart.svelte +48 -0
  28. package/dist/components/TreemapChart.svelte.d.ts +17 -0
  29. package/dist/core/builders/category.d.ts +37 -0
  30. package/dist/core/builders/category.js +109 -0
  31. package/dist/core/builders/more.d.ts +119 -0
  32. package/dist/core/builders/more.js +530 -0
  33. package/dist/core/builders/slices.d.ts +36 -0
  34. package/dist/core/builders/slices.js +117 -0
  35. package/dist/core/describe.d.ts +27 -0
  36. package/dist/core/describe.js +69 -0
  37. package/dist/core/format.d.ts +15 -0
  38. package/dist/core/format.js +39 -0
  39. package/dist/core/index.d.ts +11 -0
  40. package/dist/core/index.js +12 -0
  41. package/dist/core/theme.d.ts +30 -0
  42. package/dist/core/theme.js +137 -0
  43. package/dist/core/types.d.ts +98 -0
  44. package/dist/core/types.js +1 -0
  45. package/dist/core/validate.d.ts +4 -0
  46. package/dist/core/validate.js +10 -0
  47. package/dist/echarts/all.d.ts +3 -0
  48. package/dist/echarts/all.js +23 -0
  49. package/dist/echarts/bar.d.ts +3 -0
  50. package/dist/echarts/bar.js +2 -0
  51. package/dist/echarts/base.d.ts +3 -0
  52. package/dist/echarts/base.js +13 -0
  53. package/dist/echarts/candlestick.d.ts +3 -0
  54. package/dist/echarts/candlestick.js +3 -0
  55. package/dist/echarts/funnel.d.ts +3 -0
  56. package/dist/echarts/funnel.js +2 -0
  57. package/dist/echarts/gauge.d.ts +3 -0
  58. package/dist/echarts/gauge.js +2 -0
  59. package/dist/echarts/heatmap.d.ts +3 -0
  60. package/dist/echarts/heatmap.js +3 -0
  61. package/dist/echarts/line.d.ts +3 -0
  62. package/dist/echarts/line.js +2 -0
  63. package/dist/echarts/pie.d.ts +3 -0
  64. package/dist/echarts/pie.js +2 -0
  65. package/dist/echarts/radar.d.ts +3 -0
  66. package/dist/echarts/radar.js +3 -0
  67. package/dist/echarts/sankey.d.ts +3 -0
  68. package/dist/echarts/sankey.js +2 -0
  69. package/dist/echarts/scatter.d.ts +3 -0
  70. package/dist/echarts/scatter.js +2 -0
  71. package/dist/echarts/treemap.d.ts +3 -0
  72. package/dist/echarts/treemap.js +2 -0
  73. package/dist/echarts.d.ts +22 -0
  74. package/dist/echarts.js +24 -0
  75. package/dist/index.d.ts +16 -0
  76. package/dist/index.js +15 -0
  77. package/package.json +67 -0
@@ -0,0 +1,18 @@
1
+ import type { ComponentProps } from 'svelte';
2
+ import { type ScatterOptions } from '../core/builders/more.js';
3
+ import type { ScatterSeries } from '../core/types.js';
4
+ import Chart from './Chart.svelte';
5
+ type ChartProps = Omit<ComponentProps<typeof Chart>, 'option' | 'table' | 'extensions'>;
6
+ interface Props extends ChartProps, ScatterOptions {
7
+ /** One or more series of `[x, y]` points. */
8
+ data: ScatterSeries[];
9
+ /** Headings of the data table columns. */
10
+ tableLabels?: {
11
+ seriesLabel?: string;
12
+ xLabel?: string;
13
+ yLabel?: string;
14
+ };
15
+ }
16
+ declare const ScatterChart: import("svelte").Component<Props, {}, "ref" | "instance">;
17
+ type ScatterChart = ReturnType<typeof ScatterChart>;
18
+ export default ScatterChart;
@@ -0,0 +1,48 @@
1
+ <script lang="ts">
2
+ import type { ComponentProps } from 'svelte';
3
+ import {
4
+ buildTreemapOption,
5
+ describeTreemap,
6
+ treemapTable,
7
+ type TreemapOptions,
8
+ } from '../core/builders/more.js';
9
+ import type { TreeNode } from '../core/types.js';
10
+ import Chart from './Chart.svelte';
11
+
12
+ type ChartProps = Omit<ComponentProps<typeof Chart>, 'option' | 'table' | 'extensions'>;
13
+
14
+ interface Props extends ChartProps, TreemapOptions {
15
+ /** A tree of named values; a branch is the sum of its children. */
16
+ data: TreeNode[];
17
+ /** Headings of the data table columns. */
18
+ tableLabels?: { pathLabel?: string; valueLabel?: string };
19
+ }
20
+
21
+ let {
22
+ data,
23
+ showTooltip,
24
+ showBreadcrumb,
25
+ valueFormat,
26
+ tableLabels = {},
27
+ description,
28
+ empty,
29
+ instance = $bindable(null),
30
+ ref = $bindable(null),
31
+ ...rest
32
+ }: Props = $props();
33
+
34
+ const options = $derived({ showTooltip, showBreadcrumb, valueFormat });
35
+ const extensions = () => import('../echarts/treemap.js').then((module) => module.default);
36
+ </script>
37
+
38
+ <Chart
39
+ bind:instance
40
+ bind:ref
41
+ option={(colors) => buildTreemapOption(data, options, colors)}
42
+ table={treemapTable(data, tableLabels)}
43
+ description={description ?? describeTreemap(data, valueFormat)}
44
+ empty={empty ?? data.length === 0}
45
+ {extensions}
46
+ tableFormat={valueFormat}
47
+ {...rest}
48
+ />
@@ -0,0 +1,17 @@
1
+ import type { ComponentProps } from 'svelte';
2
+ import { type TreemapOptions } from '../core/builders/more.js';
3
+ import type { TreeNode } from '../core/types.js';
4
+ import Chart from './Chart.svelte';
5
+ type ChartProps = Omit<ComponentProps<typeof Chart>, 'option' | 'table' | 'extensions'>;
6
+ interface Props extends ChartProps, TreemapOptions {
7
+ /** A tree of named values; a branch is the sum of its children. */
8
+ data: TreeNode[];
9
+ /** Headings of the data table columns. */
10
+ tableLabels?: {
11
+ pathLabel?: string;
12
+ valueLabel?: string;
13
+ };
14
+ }
15
+ declare const TreemapChart: import("svelte").Component<Props, {}, "ref" | "instance">;
16
+ type TreemapChart = ReturnType<typeof TreemapChart>;
17
+ export default TreemapChart;
@@ -0,0 +1,37 @@
1
+ import { type FormatOptions } from '../format.js';
2
+ import type { CategoryData, ChartColors, ChartOption, ChartTable } from '../types.js';
3
+ interface CategoryOptions {
4
+ /** Show the legend. Defaults to on when there is more than one series. */
5
+ showLegend?: boolean;
6
+ showTooltip?: boolean;
7
+ /** Horizontal gridlines behind the values. */
8
+ showGrid?: boolean;
9
+ stacked?: boolean;
10
+ /** How values read on the axis and in the tooltip. */
11
+ valueFormat?: FormatOptions;
12
+ /** Axis titles. */
13
+ categoryAxisName?: string;
14
+ valueAxisName?: string;
15
+ }
16
+ export interface LineOptions extends CategoryOptions {
17
+ /** Fill under each line — an area chart. */
18
+ area?: boolean;
19
+ smooth?: boolean;
20
+ /** Mark each value with a dot, not only the line. Defaults to on for 12 or fewer categories. */
21
+ showPoints?: boolean;
22
+ }
23
+ export interface BarOptions extends CategoryOptions {
24
+ /** `horizontal` puts categories down the side, which suits long labels. */
25
+ orientation?: 'vertical' | 'horizontal';
26
+ /** Print each value on its bar. */
27
+ showValues?: boolean;
28
+ }
29
+ /** Lines (or areas) over shared categories. */
30
+ export declare function buildLineOption(data: CategoryData, options?: LineOptions, colors?: ChartColors): ChartOption;
31
+ /** Bars over shared categories, side by side or stacked. */
32
+ export declare function buildBarOption(data: CategoryData, options?: BarOptions, colors?: ChartColors): ChartOption;
33
+ /** The data behind a line or bar chart: one row per category, one column per series. */
34
+ export declare function categoryTable(data: CategoryData, categoryLabel?: string): ChartTable;
35
+ export declare const describeLine: (data: CategoryData, format?: FormatOptions) => string;
36
+ export declare const describeBar: (data: CategoryData, format?: FormatOptions) => string;
37
+ export {};
@@ -0,0 +1,109 @@
1
+ import { chartColors } from '@loidolt/theme-tokens';
2
+ import { describeCategories } from '../describe.js';
3
+ import { formatNumber } from '../format.js';
4
+ import { seriesColor } from '../theme.js';
5
+ import { checkCategories } from '../validate.js';
6
+ function frame(data, options) {
7
+ const { showLegend = data.series.length > 1, showTooltip = true, valueFormat } = options;
8
+ const format = (value) => formatNumber(value, valueFormat);
9
+ return {
10
+ format,
11
+ legend: showLegend ? { type: 'scroll', top: 0, left: 0 } : undefined,
12
+ tooltip: showTooltip ? { trigger: 'axis', valueFormatter: format } : undefined,
13
+ grid: { left: 8, right: 16, top: showLegend ? 40 : 16, bottom: 8, containLabel: true },
14
+ };
15
+ }
16
+ /** Lines (or areas) over shared categories. */
17
+ export function buildLineOption(data, options = {}, colors = chartColors()) {
18
+ checkCategories(data, 'buildLineOption');
19
+ const { area = false, smooth = false, stacked = false, showGrid = true } = options;
20
+ const showPoints = options.showPoints ?? data.categories.length <= 12;
21
+ const { format, ...parts } = frame(data, options);
22
+ return {
23
+ ...parts,
24
+ xAxis: {
25
+ type: 'category',
26
+ data: data.categories,
27
+ boundaryGap: false,
28
+ name: options.categoryAxisName,
29
+ },
30
+ yAxis: {
31
+ type: 'value',
32
+ name: options.valueAxisName,
33
+ splitLine: { show: showGrid },
34
+ axisLabel: { formatter: format },
35
+ },
36
+ series: data.series.map((series, index) => {
37
+ const color = series.color ?? seriesColor(colors, index);
38
+ return {
39
+ type: 'line',
40
+ name: series.name,
41
+ data: series.data,
42
+ smooth,
43
+ stack: stacked ? 'total' : undefined,
44
+ showSymbol: showPoints,
45
+ symbol: 'rect',
46
+ symbolSize: 6,
47
+ itemStyle: { color },
48
+ lineStyle: { color, width: 2 },
49
+ areaStyle: area ? { color, opacity: stacked ? 0.8 : 0.16 } : undefined,
50
+ emphasis: { focus: 'series' },
51
+ };
52
+ }),
53
+ };
54
+ }
55
+ /** Bars over shared categories, side by side or stacked. */
56
+ export function buildBarOption(data, options = {}, colors = chartColors()) {
57
+ checkCategories(data, 'buildBarOption');
58
+ const { orientation = 'vertical', stacked = false, showValues = false, showGrid = true, } = options;
59
+ const { format, ...parts } = frame(data, options);
60
+ const categoryAxis = {
61
+ type: 'category',
62
+ data: data.categories,
63
+ name: options.categoryAxisName,
64
+ axisTick: { alignWithLabel: true },
65
+ };
66
+ const valueAxis = {
67
+ type: 'value',
68
+ name: options.valueAxisName,
69
+ splitLine: { show: showGrid },
70
+ axisLabel: { formatter: format },
71
+ };
72
+ const horizontal = orientation === 'horizontal';
73
+ return {
74
+ ...parts,
75
+ tooltip: parts.tooltip && { ...parts.tooltip, axisPointer: { type: 'shadow' } },
76
+ xAxis: horizontal ? valueAxis : categoryAxis,
77
+ // Horizontal bars read top-down in data order, as a list does.
78
+ yAxis: horizontal ? { ...categoryAxis, inverse: true } : valueAxis,
79
+ series: data.series.map((series, index) => ({
80
+ type: 'bar',
81
+ name: series.name,
82
+ data: series.data,
83
+ stack: stacked ? 'total' : undefined,
84
+ barMaxWidth: 48,
85
+ itemStyle: { color: series.color ?? seriesColor(colors, index) },
86
+ label: showValues
87
+ ? {
88
+ show: true,
89
+ position: stacked ? 'inside' : horizontal ? 'right' : 'top',
90
+ color: stacked ? colors.surface : colors.text,
91
+ formatter: (params) => format(params.value),
92
+ }
93
+ : undefined,
94
+ emphasis: { focus: 'series' },
95
+ })),
96
+ };
97
+ }
98
+ /** The data behind a line or bar chart: one row per category, one column per series. */
99
+ export function categoryTable(data, categoryLabel = 'Category') {
100
+ return {
101
+ columns: [categoryLabel, ...data.series.map((series) => series.name)],
102
+ rows: data.categories.map((category, index) => [
103
+ category,
104
+ ...data.series.map((series) => series.data[index] ?? null),
105
+ ]),
106
+ };
107
+ }
108
+ export const describeLine = (data, format) => describeCategories(data, { kind: 'line chart', format });
109
+ export const describeBar = (data, format) => describeCategories(data, { kind: 'bar chart', format });
@@ -0,0 +1,119 @@
1
+ import { type FormatOptions } from '../format.js';
2
+ import type { CandlestickDatum, ChartColors, ChartOption, ChartTable, HeatmapData, RadarData, SankeyData, ScatterSeries, TreeNode } from '../types.js';
3
+ export interface ScatterOptions {
4
+ /** Draw a least-squares line through each series. */
5
+ showTrendLine?: boolean;
6
+ /** Point size in pixels, or a function of the point. */
7
+ symbolSize?: number | ((point: [number, number]) => number);
8
+ showLegend?: boolean;
9
+ showTooltip?: boolean;
10
+ xAxisName?: string;
11
+ yAxisName?: string;
12
+ xFormat?: FormatOptions;
13
+ yFormat?: FormatOptions;
14
+ }
15
+ /** The least-squares line through `points`, or `null` when it is undefined (all x equal). */
16
+ export declare function linearFit(points: Array<[number, number]>): {
17
+ slope: number;
18
+ intercept: number;
19
+ } | null;
20
+ export declare function buildScatterOption(data: ScatterSeries[], options?: ScatterOptions, colors?: ChartColors): ChartOption;
21
+ export declare function scatterTable(data: ScatterSeries[], { seriesLabel, xLabel, yLabel }?: {
22
+ seriesLabel?: string | undefined;
23
+ xLabel?: string | undefined;
24
+ yLabel?: string | undefined;
25
+ }): ChartTable;
26
+ export declare function describeScatter(data: ScatterSeries[]): string;
27
+ export interface RadarOptions {
28
+ shape?: 'polygon' | 'circle';
29
+ /** Fill each series' area. */
30
+ area?: boolean;
31
+ showLegend?: boolean;
32
+ showTooltip?: boolean;
33
+ }
34
+ export declare function buildRadarOption(data: RadarData, options?: RadarOptions, colors?: ChartColors): ChartOption;
35
+ export declare function radarTable(data: RadarData, indicatorLabel?: string): ChartTable;
36
+ export declare function describeRadar(data: RadarData): string;
37
+ export interface GaugeData {
38
+ value: number;
39
+ min?: number;
40
+ max?: number;
41
+ }
42
+ export interface GaugeOptions {
43
+ /** Major ticks around the dial. */
44
+ splitNumber?: number;
45
+ /** Fill the dial up to the value. */
46
+ showProgress?: boolean;
47
+ valueFormat?: FormatOptions;
48
+ /** Caption under the value, e.g. the unit. */
49
+ label?: string;
50
+ }
51
+ export declare function buildGaugeOption(data: GaugeData, options?: GaugeOptions, colors?: ChartColors): ChartOption;
52
+ export declare function gaugeTable(data: GaugeData, { readingLabel, valueLabel, minLabel, maxLabel, }?: {
53
+ readingLabel?: string | undefined;
54
+ valueLabel?: string | undefined;
55
+ minLabel?: string | undefined;
56
+ maxLabel?: string | undefined;
57
+ }): ChartTable;
58
+ export declare function describeGauge(data: GaugeData, format?: FormatOptions): string;
59
+ export interface HeatmapOptions {
60
+ /** Print each cell's value. */
61
+ showValues?: boolean;
62
+ showTooltip?: boolean;
63
+ /** Show the colour scale. */
64
+ showScale?: boolean;
65
+ valueFormat?: FormatOptions;
66
+ /** Scale bounds. Default to the data's range. */
67
+ min?: number;
68
+ max?: number;
69
+ }
70
+ export declare function buildHeatmapOption(data: HeatmapData, options?: HeatmapOptions, colors?: ChartColors): ChartOption;
71
+ export declare function heatmapTable(data: HeatmapData, cornerLabel?: string): ChartTable;
72
+ export declare function describeHeatmap(data: HeatmapData, format?: FormatOptions): string;
73
+ export interface TreemapOptions {
74
+ showTooltip?: boolean;
75
+ /** The trail of parents above a zoomed-in node. */
76
+ showBreadcrumb?: boolean;
77
+ valueFormat?: FormatOptions;
78
+ }
79
+ /** A node's own value, or the sum of its children's. */
80
+ export declare const nodeValue: (node: TreeNode) => number;
81
+ export declare function buildTreemapOption(data: TreeNode[], options?: TreemapOptions, colors?: ChartColors): ChartOption;
82
+ export declare function treemapTable(data: TreeNode[], { pathLabel, valueLabel }?: {
83
+ pathLabel?: string | undefined;
84
+ valueLabel?: string | undefined;
85
+ }): ChartTable;
86
+ export declare function describeTreemap(data: TreeNode[], format?: FormatOptions): string;
87
+ export interface CandlestickOptions {
88
+ /** Volume bars under the price, when the data carries `volume`. */
89
+ showVolume?: boolean;
90
+ /** Drag and wheel to zoom along the dates, with a slider under the chart. */
91
+ zoom?: boolean;
92
+ showTooltip?: boolean;
93
+ /** Rising and falling colours. Default to the gain and loss tokens. */
94
+ upColor?: string;
95
+ downColor?: string;
96
+ valueFormat?: FormatOptions;
97
+ }
98
+ export declare function buildCandlestickOption(data: CandlestickDatum[], options?: CandlestickOptions, colors?: ChartColors): ChartOption;
99
+ export declare function candlestickTable(data: CandlestickDatum[], { dateLabel, openLabel, highLabel, lowLabel, closeLabel, volumeLabel, }?: {
100
+ dateLabel?: string | undefined;
101
+ openLabel?: string | undefined;
102
+ highLabel?: string | undefined;
103
+ lowLabel?: string | undefined;
104
+ closeLabel?: string | undefined;
105
+ volumeLabel?: string | undefined;
106
+ }): ChartTable;
107
+ export declare function describeCandlestick(data: CandlestickDatum[], format?: FormatOptions): string;
108
+ export interface SankeyOptions {
109
+ orientation?: 'horizontal' | 'vertical';
110
+ showTooltip?: boolean;
111
+ valueFormat?: FormatOptions;
112
+ }
113
+ export declare function buildSankeyOption(data: SankeyData, options?: SankeyOptions, colors?: ChartColors): ChartOption;
114
+ export declare function sankeyTable(data: SankeyData, { sourceLabel, targetLabel, valueLabel }?: {
115
+ sourceLabel?: string | undefined;
116
+ targetLabel?: string | undefined;
117
+ valueLabel?: string | undefined;
118
+ }): ChartTable;
119
+ export declare function describeSankey(data: SankeyData, format?: FormatOptions): string;