@internetstiftelsen/charts 0.9.2 → 0.10.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.
@@ -0,0 +1,175 @@
1
+ # GaugeChart API
2
+
3
+ A chart for displaying a single KPI value against a range with optional
4
+ threshold bands and target marker.
5
+
6
+ ## Constructor
7
+
8
+ ```typescript
9
+ new GaugeChart(config: GaugeChartConfig)
10
+ ```
11
+
12
+ ### Config Options
13
+
14
+ | Option | Type | Default | Description |
15
+ | ---------------- | ------------------------- | --------- | --------------------------------------------------------------------- |
16
+ | `data` | `DataItem[]` | required | Data array (first row is used) |
17
+ | `width` | `number` | - | Explicit chart width in pixels |
18
+ | `height` | `number` | - | Explicit chart height in pixels |
19
+ | `valueKey` | `string` | `'value'` | Key for the current value in the first row |
20
+ | `targetValueKey` | `string` | - | Optional key for target value in the first row |
21
+ | `gauge` | `GaugeConfig` | - | Gauge-specific configuration |
22
+ | `theme` | `DeepPartial<ChartTheme>` | - | Theme customization |
23
+ | `responsive` | `ResponsiveConfig` | - | Declarative container-query responsive overrides (theme + components) |
24
+
25
+ ### Gauge Config
26
+
27
+ ```typescript
28
+ gauge: {
29
+ value?: number, // Explicit value (overrides valueKey)
30
+ targetValue?: number, // Explicit target (overrides targetValueKey)
31
+ min?: number, // default: 0
32
+ max?: number, // default: 100
33
+ animate?: boolean | { // default: false
34
+ show?: boolean, // default: true when object is used
35
+ duration?: number, // ms, default: 700
36
+ easing?: // default: 'ease-in-out'
37
+ | 'linear'
38
+ | 'ease-in'
39
+ | 'ease-out'
40
+ | 'ease-in-out'
41
+ | 'bounce-out'
42
+ | 'elastic-out'
43
+ | `linear(...)` // CSS-like piecewise linear easing
44
+ | ((t: number) => number),
45
+ },
46
+ halfCircle?: boolean, // default: false
47
+ startAngle?: number, // default: -Math.PI * 0.75 (or -Math.PI / 2 in halfCircle mode)
48
+ endAngle?: number, // default: Math.PI * 0.75 (or Math.PI / 2 in halfCircle mode)
49
+ innerRadius?: number, // 0-1, default: 0.68
50
+ thickness?: number, // px, overrides innerRadius when provided
51
+ cornerRadius?: number, // px, default: 4
52
+ trackColor?: string, // default: #e5e7eb
53
+ progressColor?: string, // default: theme.colorPalette[0]
54
+ targetColor?: string, // default: #111827
55
+ segmentStyle?: 'solid' | 'gradient', // default: 'solid'
56
+ showValue?: boolean, // default: true
57
+ valueFormatter?: (value) => string,
58
+ valueLabelStyle?: {
59
+ fontSize?: number | string, // default: 28
60
+ fontFamily?: string, // default: theme.axis.fontFamily
61
+ fontWeight?: number | string, // default: 700
62
+ color?: string, // default: #111827
63
+ },
64
+ needle?: boolean | {
65
+ show?: boolean, // default: true
66
+ color?: string, // default: #111827
67
+ width?: number,
68
+ lengthRatio?: number,
69
+ capRadius?: number,
70
+ },
71
+ marker?: boolean | {
72
+ show?: boolean, // default: !needle.show
73
+ color?: string,
74
+ width?: number,
75
+ },
76
+ ticks?: {
77
+ count?: number, // default: 5
78
+ show?: boolean, // default: true
79
+ showLines?: boolean, // default: true
80
+ showLabels?: boolean, // default: true
81
+ size?: number, // default: 8
82
+ labelOffset?: number, // default: 12
83
+ formatter?: (value) => string,
84
+ labelStyle?: {
85
+ fontSize?: number | string, // default: 11
86
+ fontFamily?: string, // default: theme.axis.fontFamily
87
+ fontWeight?: number | string, // default: theme.axis.fontWeight
88
+ color?: string, // default: #4b5563
89
+ },
90
+ },
91
+ segments?: [
92
+ {
93
+ from: number,
94
+ to: number,
95
+ color?: string, // uses theme.colorPalette[index] when omitted
96
+ label?: string,
97
+ }
98
+ ],
99
+ }
100
+ ```
101
+
102
+ ## Example
103
+
104
+ ```javascript
105
+ import { GaugeChart } from '@internetstiftelsen/charts/gauge-chart';
106
+ import { Tooltip } from '@internetstiftelsen/charts/tooltip';
107
+ import { Legend } from '@internetstiftelsen/charts/legend';
108
+ import { Title } from '@internetstiftelsen/charts/title';
109
+
110
+ const chart = new GaugeChart({
111
+ data: [{ label: 'KPI', value: 72, target: 80 }],
112
+ valueKey: 'value',
113
+ targetValueKey: 'target',
114
+ gauge: {
115
+ min: 0,
116
+ max: 100,
117
+ animate: {
118
+ show: true,
119
+ duration: 900,
120
+ easing: 'ease-out',
121
+ },
122
+ halfCircle: true,
123
+ showValue: true,
124
+ needle: true,
125
+ ticks: { count: 5 },
126
+ segments: [
127
+ { from: 0, to: 60, color: '#10b981', label: 'Good' },
128
+ { from: 60, to: 85, color: '#f59e0b', label: 'Warning' },
129
+ { from: 85, to: 100, color: '#ef4444', label: 'Risk' },
130
+ ],
131
+ },
132
+ });
133
+
134
+ chart
135
+ .addChild(new Title({ text: 'Operational KPI' }))
136
+ .addChild(new Tooltip())
137
+ .addChild(new Legend({ position: 'bottom' }));
138
+
139
+ chart.render('#gauge-container');
140
+ ```
141
+
142
+ ## Validation and Clamping
143
+
144
+ - `gauge.min` must be less than `gauge.max`.
145
+ - Segment ranges must stay inside `[min, max]` and must not overlap.
146
+ - You can pass any number of threshold segments.
147
+ - Segment colors are optional; omitted colors use `theme.colorPalette`
148
+ (or a built-in fallback palette if the theme palette is empty).
149
+ - Out-of-range `value` and `targetValue` are clamped to `[min, max]` with a
150
+ non-fatal warning.
151
+
152
+ ## Animation Easing Example
153
+
154
+ ```typescript
155
+ const chart = new GaugeChart({
156
+ data: [{ value: 72 }],
157
+ gauge: {
158
+ min: 0,
159
+ max: 100,
160
+ animate: {
161
+ show: true,
162
+ duration: 1200,
163
+ easing: 'linear(0, 0.029 1.6%, 0.123 3.5%, 0.651 10.6%, 0.862 14.1%, 1.002 17.7%, 1.046 19.6%, 1.074 21.6%, 1.087 23.9%, 1.086 26.6%, 1.014 38.5%, 0.994 46.3%, 1)',
164
+ },
165
+ },
166
+ });
167
+ ```
168
+
169
+ ## Supported Components
170
+
171
+ GaugeChart supports the following components via `addChild()`:
172
+
173
+ - `Title` - Chart title
174
+ - `Tooltip` - Hover tooltip (value, target)
175
+ - `Legend` - Segment legend with visibility toggles
@@ -0,0 +1,311 @@
1
+ # Getting Started
2
+
3
+ This guide covers installation and basic usage with Vanilla JavaScript and React.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @internetstiftelsen/charts
9
+ ```
10
+
11
+ ## Vanilla JavaScript
12
+
13
+ ```javascript
14
+ import { XYChart } from '@internetstiftelsen/charts/xy-chart';
15
+ import { Line } from '@internetstiftelsen/charts/line';
16
+ import { Bar } from '@internetstiftelsen/charts/bar';
17
+ import { XAxis } from '@internetstiftelsen/charts/x-axis';
18
+ import { YAxis } from '@internetstiftelsen/charts/y-axis';
19
+ import { Grid } from '@internetstiftelsen/charts/grid';
20
+ import { Tooltip } from '@internetstiftelsen/charts/tooltip';
21
+ import { Legend } from '@internetstiftelsen/charts/legend';
22
+ import { Title } from '@internetstiftelsen/charts/title';
23
+
24
+ // Your data
25
+ const data = [
26
+ { date: '2010', revenue: 100, expenses: 80 },
27
+ { date: '2011', revenue: 150, expenses: 90 },
28
+ { date: '2012', revenue: 200, expenses: 110 },
29
+ { date: '2013', revenue: 250, expenses: 130 },
30
+ ];
31
+
32
+ // Create chart
33
+ const chart = new XYChart({ data });
34
+
35
+ // Add components
36
+ chart
37
+ .addChild(new Title({ text: 'Revenue vs Expenses' }))
38
+ .addChild(new Grid({ category: false, value: true }))
39
+ .addChild(new XAxis({ dataKey: 'date' }))
40
+ .addChild(new YAxis())
41
+ .addChild(
42
+ new Tooltip({
43
+ formatter: (dataKey, value) => `<strong>${dataKey}</strong>: $${value}k`,
44
+ }),
45
+ )
46
+ .addChild(new Legend({ position: 'bottom' }))
47
+ .addChild(new Line({ dataKey: 'revenue' })) // Auto-assigned color
48
+ .addChild(new Line({ dataKey: 'expenses' })); // Auto-assigned color
49
+
50
+ // Render to DOM (automatically resizes with container)
51
+ chart.render('#chart-container');
52
+
53
+ // Later: update with new data
54
+ chart.update(newData);
55
+
56
+ // Clean up when done
57
+ chart.destroy();
58
+ ```
59
+
60
+ ## Sizing
61
+
62
+ By default, charts size themselves from the render container.
63
+
64
+ ```javascript
65
+ const chart = new XYChart({ data });
66
+ chart.render('#chart-container');
67
+ ```
68
+
69
+ For fixed-size charts, set top-level `width` and `height`:
70
+
71
+ ```javascript
72
+ const chart = new XYChart({
73
+ data,
74
+ width: 800,
75
+ height: 400,
76
+ });
77
+ ```
78
+
79
+ ## Grouping Charts
80
+
81
+ ```javascript
82
+ import { ChartGroup } from '@internetstiftelsen/charts/chart-group';
83
+ import { XYChart } from '@internetstiftelsen/charts/xy-chart';
84
+ import { Line } from '@internetstiftelsen/charts/line';
85
+ import { Bar } from '@internetstiftelsen/charts/bar';
86
+ import { Legend } from '@internetstiftelsen/charts/legend';
87
+ import { Title } from '@internetstiftelsen/charts/title';
88
+
89
+ const lineChart = new XYChart({ data: lineData });
90
+ lineChart.addChild(new Line({ dataKey: 'revenue' }));
91
+
92
+ const barChart = new XYChart({ data: barData });
93
+ barChart.addChild(new Bar({ dataKey: 'revenue' }));
94
+
95
+ const group = new ChartGroup({
96
+ cols: 2,
97
+ gap: 20,
98
+ height: 420,
99
+ syncY: true,
100
+ });
101
+
102
+ group
103
+ .addChild(new Title({ text: 'Revenue vs Expenses' }))
104
+ .addChart(barChart)
105
+ .addChart(lineChart)
106
+ .addChild(new Legend());
107
+
108
+ group.render('#chart-group');
109
+ ```
110
+
111
+ Each child chart keeps its own rendering logic, scales, tooltips, and responsive
112
+ behavior. The shared group legend controls matching series keys across all child
113
+ charts, and group height can be fixed with `height` or inherited from the render
114
+ container. Use `syncY: true` when you want visible vertical `XYChart` children
115
+ to share the same Y domain.
116
+
117
+ `ChartGroup` also supports responsive layout breakpoints at both levels:
118
+
119
+ - group-level `responsive.breakpoints` can override `cols` and `gap`
120
+ - `addChart(..., options)` can override `span`, `height`, `order`, and `hidden`
121
+
122
+ These breakpoints use the same `minWidth` / `maxWidth` matching rules as chart
123
+ responsive config, and all matching breakpoints merge in declaration order.
124
+
125
+ ## React Integration
126
+
127
+ ```jsx
128
+ import { useRef, useEffect } from 'react';
129
+ import { XYChart } from '@internetstiftelsen/charts/xy-chart';
130
+ import { Line } from '@internetstiftelsen/charts/line';
131
+ import { Bar } from '@internetstiftelsen/charts/bar';
132
+ import { XAxis } from '@internetstiftelsen/charts/x-axis';
133
+ import { YAxis } from '@internetstiftelsen/charts/y-axis';
134
+ import { Grid } from '@internetstiftelsen/charts/grid';
135
+ import { Tooltip } from '@internetstiftelsen/charts/tooltip';
136
+ import { Legend } from '@internetstiftelsen/charts/legend';
137
+
138
+ function Chart({ data }) {
139
+ const containerRef = useRef(null);
140
+ const chartRef = useRef(null);
141
+
142
+ useEffect(() => {
143
+ if (containerRef.current) {
144
+ // Create chart
145
+ const chart = new XYChart({ data });
146
+
147
+ chart
148
+ .addChild(new Grid({ value: true }))
149
+ .addChild(new XAxis({ dataKey: 'column' }))
150
+ .addChild(new YAxis())
151
+ .addChild(new Tooltip())
152
+ .addChild(new Legend({ position: 'bottom' }))
153
+ .addChild(new Line({ dataKey: 'value1' }))
154
+ .addChild(new Line({ dataKey: 'value2' }));
155
+
156
+ chart.render(containerRef.current);
157
+ chartRef.current = chart;
158
+
159
+ return () => {
160
+ chart.destroy();
161
+ };
162
+ }
163
+ }, []);
164
+
165
+ // Update when data changes
166
+ useEffect(() => {
167
+ if (chartRef.current && data) {
168
+ chartRef.current.update(data);
169
+ }
170
+ }, [data]);
171
+
172
+ return <div ref={containerRef} />;
173
+ }
174
+ ```
175
+
176
+ ## Donut Chart Example
177
+
178
+ ```javascript
179
+ import { DonutChart } from '@internetstiftelsen/charts/donut-chart';
180
+ import { DonutCenterContent } from '@internetstiftelsen/charts/donut-center-content';
181
+ import { Legend } from '@internetstiftelsen/charts/legend';
182
+ import { Tooltip } from '@internetstiftelsen/charts/tooltip';
183
+
184
+ const data = [
185
+ { name: 'Desktop', value: 450 },
186
+ { name: 'Mobile', value: 320 },
187
+ { name: 'Tablet', value: 130 },
188
+ ];
189
+
190
+ const chart = new DonutChart({
191
+ data,
192
+ valueKey: 'value',
193
+ labelKey: 'name',
194
+ });
195
+
196
+ chart
197
+ .addChild(
198
+ new DonutCenterContent({
199
+ mainValue: '900',
200
+ title: 'Total',
201
+ subtitle: 'visitors',
202
+ }),
203
+ )
204
+ .addChild(new Legend({ position: 'bottom' }))
205
+ .addChild(new Tooltip());
206
+
207
+ chart.render('#donut-container');
208
+ ```
209
+
210
+ ## Pie Chart Example
211
+
212
+ ```javascript
213
+ import { PieChart } from '@internetstiftelsen/charts/pie-chart';
214
+ import { Legend } from '@internetstiftelsen/charts/legend';
215
+ import { Tooltip } from '@internetstiftelsen/charts/tooltip';
216
+
217
+ const data = [
218
+ { name: 'Desktop', value: 450 },
219
+ { name: 'Mobile', value: 320 },
220
+ { name: 'Tablet', value: 130 },
221
+ ];
222
+
223
+ const chart = new PieChart({
224
+ data,
225
+ valueKey: 'value',
226
+ labelKey: 'name',
227
+ pie: {
228
+ sort: 'none',
229
+ },
230
+ });
231
+
232
+ chart.addChild(new Legend({ position: 'bottom' })).addChild(new Tooltip());
233
+
234
+ chart.render('#pie-container');
235
+ ```
236
+
237
+ ## Gauge Chart Example
238
+
239
+ ```javascript
240
+ import { GaugeChart } from '@internetstiftelsen/charts/gauge-chart';
241
+ import { Legend } from '@internetstiftelsen/charts/legend';
242
+ import { Tooltip } from '@internetstiftelsen/charts/tooltip';
243
+
244
+ const data = [{ label: 'KPI', value: 72, target: 80 }];
245
+
246
+ const chart = new GaugeChart({
247
+ data,
248
+ valueKey: 'value',
249
+ targetValueKey: 'target',
250
+ gauge: {
251
+ min: 0,
252
+ max: 100,
253
+ halfCircle: true,
254
+ segments: [
255
+ { from: 0, to: 60, color: '#10b981', label: 'Good' },
256
+ { from: 60, to: 85, color: '#f59e0b', label: 'Warning' },
257
+ { from: 85, to: 100, color: '#ef4444', label: 'Risk' },
258
+ ],
259
+ },
260
+ });
261
+
262
+ chart.addChild(new Legend({ position: 'bottom' })).addChild(new Tooltip());
263
+
264
+ chart.render('#gauge-container');
265
+ ```
266
+
267
+ ## Word Cloud Example
268
+
269
+ ```javascript
270
+ import { WordCloudChart } from '@internetstiftelsen/charts/word-cloud-chart';
271
+ import { Title } from '@internetstiftelsen/charts/title';
272
+
273
+ const data = [
274
+ { word: 'internet', count: 96 },
275
+ { word: 'social', count: 82 },
276
+ { word: 'news', count: 75 },
277
+ { word: 'streaming', count: 68 },
278
+ ];
279
+
280
+ const chart = new WordCloudChart({
281
+ data,
282
+ wordCloud: {
283
+ minWordLength: 3,
284
+ minValue: 5,
285
+ minFontSize: 3,
286
+ maxFontSize: 20,
287
+ padding: 1,
288
+ spiral: 'archimedean',
289
+ },
290
+ });
291
+
292
+ chart.addChild(new Title({ text: 'Most mentioned words' }));
293
+
294
+ chart.render('#word-cloud-container');
295
+ ```
296
+
297
+ `minFontSize` and `maxFontSize` are percentages of the smaller plot-area
298
+ dimension and define the relative size range passed into `d3-cloud`. Word
299
+ clouds accept flat `{ word, count }` rows and use theme typography/colors
300
+ directly when laying out and rendering the cloud.
301
+
302
+ ## Next Steps
303
+
304
+ - [XYChart API](./xy-chart.md) - Line and bar charts
305
+ - [ChartGroup API](./chart-group.md) - Combined chart layouts
306
+ - [WordCloudChart API](./word-cloud-chart.md) - Word clouds and frequency filtering
307
+ - [DonutChart API](./donut-chart.md) - Donut/pie charts
308
+ - [PieChart API](./pie-chart.md) - Pie charts
309
+ - [GaugeChart API](./gauge-chart.md) - Gauge charts
310
+ - [Components](./components.md) - Axes, Grid, Tooltip, Legend, Title
311
+ - [Theming](./theming.md) - Customize colors and styles
@@ -0,0 +1,123 @@
1
+ # PieChart API
2
+
3
+ A chart for displaying proportional categorical data as pie slices.
4
+
5
+ ## Constructor
6
+
7
+ ```typescript
8
+ new PieChart(config: PieChartConfig)
9
+ ```
10
+
11
+ ### Config Options
12
+
13
+ | Option | Type | Default | Description |
14
+ | ------------ | ------------------------- | --------- | --------------------------------------------------------------------- |
15
+ | `data` | `DataItem[]` | required | Array of data objects |
16
+ | `width` | `number` | - | Explicit chart width in pixels |
17
+ | `height` | `number` | - | Explicit chart height in pixels |
18
+ | `valueKey` | `string` | `'value'` | Key for numeric values in data |
19
+ | `labelKey` | `string` | `'name'` | Key for segment labels in data |
20
+ | `pie` | `PieConfig` | - | Pie-specific configuration |
21
+ | `valueLabel` | `PieValueLabelConfig` | - | On-chart slice label/value rendering configuration |
22
+ | `theme` | `DeepPartial<ChartTheme>` | - | Theme customization |
23
+ | `responsive` | `ResponsiveConfig` | - | Declarative container-query responsive overrides (theme + components) |
24
+
25
+ ### Pie Config
26
+
27
+ ```typescript
28
+ pie: {
29
+ innerRadius?: number, // 0-1, percentage of outer radius (default: 0)
30
+ startAngle?: number, // Radians (default: 0)
31
+ endAngle?: number, // Radians (default: Math.PI * 2)
32
+ padAngle?: number, // Radians between slices (default: theme.donut.padAngle)
33
+ cornerRadius?: number, // Corner radius in pixels (default: theme.donut.cornerRadius)
34
+ sort?: 'none' | 'ascending' | 'descending' | ((a, b) => number),
35
+ }
36
+ ```
37
+
38
+ ### ValueLabel Config
39
+
40
+ ```typescript
41
+ valueLabel: {
42
+ show?: boolean, // default: false
43
+ position?: 'inside' | 'outside' | 'auto', // default: 'auto'
44
+ minInsidePercentage?: number, // default: 8
45
+ outsideOffset?: number, // default: 16
46
+ insideMargin?: number, // default: 8
47
+ minVerticalSpacing?: number, // default: 14
48
+ formatter?: (label, value, data, percentage) => string, // default: `${label}: ${value}`
49
+ }
50
+ ```
51
+
52
+ Rendered pie value-label text defaults to `{label}: {value}` and can be customized with `valueLabel.formatter`. The `percentage` argument is the computed slice share from `0` to `100`.
53
+
54
+ ## Example
55
+
56
+ ```javascript
57
+ import { PieChart } from '@internetstiftelsen/charts/pie-chart';
58
+ import { Legend } from '@internetstiftelsen/charts/legend';
59
+ import { Tooltip } from '@internetstiftelsen/charts/tooltip';
60
+ import { Title } from '@internetstiftelsen/charts/title';
61
+
62
+ const data = [
63
+ { name: 'Desktop', value: 450 },
64
+ { name: 'Mobile', value: 320 },
65
+ { name: 'Tablet', value: 130 },
66
+ ];
67
+
68
+ const chart = new PieChart({
69
+ data,
70
+ valueKey: 'value',
71
+ labelKey: 'name',
72
+ pie: {
73
+ padAngle: 0.02,
74
+ sort: 'none',
75
+ },
76
+ valueLabel: {
77
+ show: true,
78
+ position: 'auto',
79
+ formatter: (label, _value, _data, percentage) =>
80
+ `${label}: ${percentage.toFixed(1)}%`,
81
+ },
82
+ });
83
+
84
+ chart
85
+ .addChild(new Title({ text: 'Device Distribution' }))
86
+ .addChild(new Legend({ position: 'bottom' }))
87
+ .addChild(new Tooltip());
88
+
89
+ chart.render('#chart-container');
90
+ ```
91
+
92
+ ## Responsive Height and Labels
93
+
94
+ - PieChart uses container-driven `100%` height (same behavior model as width).
95
+ - Set an explicit container height for predictable visual sizing.
96
+ - Value label font size shrinks with the chart size for better fit on smaller pies.
97
+
98
+ ## Negative Values
99
+
100
+ Negative values are skipped (not rendered) and logged as warnings. If all rows
101
+ are negative, the chart throws an error because there is nothing to render.
102
+
103
+ When `valueLabel.position` is `inside`, the formatted value-label text that does not
104
+ fit inside its slice is hidden. In `auto` mode, labels that are too tight (based on
105
+ `insideMargin`) move outside instead.
106
+
107
+ ## Supported Components
108
+
109
+ PieChart supports the following components via `addChild()`:
110
+
111
+ - `Title` - Chart title
112
+ - `Legend` - Interactive legend (click to toggle slices)
113
+ - `Tooltip` - Hover/focus tooltips with slice info
114
+
115
+ ## Tooltip Formatting
116
+
117
+ The default tooltip shows label, value, and percentage. Customize with a formatter:
118
+
119
+ ```javascript
120
+ new Tooltip({
121
+ formatter: (dataKey, value, data) => `${dataKey}: ${value.toLocaleString()} visitors (${data.region})`,
122
+ });
123
+ ```