@angx/ngx-chart-kit 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +311 -2
- package/fesm2022/angx-ngx-chart-kit.mjs +1039 -0
- package/fesm2022/angx-ngx-chart-kit.mjs.map +1 -0
- package/package.json +57 -3
- package/types/angx-ngx-chart-kit.d.ts +484 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Abhishek Singh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,312 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @angx/ngx-chart-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Accessible, signal-based [chart.js](https://www.npmjs.com/package/chart.js) charts for Angular.
|
|
4
|
+
|
|
5
|
+
[npm](https://www.npmjs.com/package/@angx/ngx-chart-kit) · [Docs](https://coderabhi.in/ngx-chart-kit.html) · [GitHub](https://github.com/angx-libs/ngx-chart-kit)
|
|
6
|
+
|
|
7
|
+
> **0.1.0 — unreleased and unproven.** The API below is deliberate, but nobody
|
|
8
|
+
> has built anything on it yet. Expect it to move before 1.0.
|
|
9
|
+
|
|
10
|
+
`ng2-charts` already wraps chart.js for Angular, and a second thin wrapper
|
|
11
|
+
would be pointless. This one is not thin, and it is not general: it makes four
|
|
12
|
+
specific choices that a wrapper around chart.js's options object cannot make
|
|
13
|
+
for you.
|
|
14
|
+
|
|
15
|
+
## Why this exists
|
|
16
|
+
|
|
17
|
+
**1. Every chart has a text equivalent, and it cannot go stale.**
|
|
18
|
+
|
|
19
|
+
A `<canvas>` is a bitmap. Assistive technology can see precisely nothing in it,
|
|
20
|
+
which means a chart without a text equivalent is a chart some of your readers
|
|
21
|
+
cannot read at all. This is the single most common real defect in charting
|
|
22
|
+
wrappers, and it is not fixed by an `aria-label` that says "sales chart".
|
|
23
|
+
|
|
24
|
+
So every chart here also renders a real `<table>` — caption, column headers,
|
|
25
|
+
row headers — built from the same data the canvas draws. It is visually hidden
|
|
26
|
+
by default and fully present in the accessibility tree. Because it is derived
|
|
27
|
+
rather than authored, it updates when your data does. There is no input that
|
|
28
|
+
turns it off.
|
|
29
|
+
|
|
30
|
+
**2. Signals throughout, and updates in place.**
|
|
31
|
+
|
|
32
|
+
Inputs are signals. No `ngOnChanges`, no string-matching on `SimpleChanges`,
|
|
33
|
+
`OnPush`, zoneless-friendly. Changing an input recomputes the configuration and
|
|
34
|
+
hands it to chart.js's own `update()`, so the chart instance, the canvas and
|
|
35
|
+
any hover state survive. The chart is only rebuilt when the chart.js controller
|
|
36
|
+
itself has to change.
|
|
37
|
+
|
|
38
|
+
**3. chart.js is never in your initial bundle.**
|
|
39
|
+
|
|
40
|
+
chart.js is around 200 kB. It is imported dynamically on first draw, so an
|
|
41
|
+
application that renders no chart on its landing route pays nothing, and only
|
|
42
|
+
the controllers the chart forms you actually use require are registered — never
|
|
43
|
+
`chart.js/auto`, which registers everything. The demo app in this repo builds
|
|
44
|
+
to a 153 kB initial bundle with chart.js in a 209 kB lazy chunk.
|
|
45
|
+
|
|
46
|
+
Every `document`, `window` and canvas touch is behind `isPlatformBrowser`. On
|
|
47
|
+
the server the component renders its table and nothing else, so it is safe to
|
|
48
|
+
import in a server-rendered route.
|
|
49
|
+
|
|
50
|
+
**4. Colours come from CSS, read at draw time.**
|
|
51
|
+
|
|
52
|
+
Set a few custom properties and your dark-mode switch recolours every chart on
|
|
53
|
+
the page. No palette threaded through inputs, no theme service, no inputs
|
|
54
|
+
rebound. The component watches `prefers-color-scheme` and the document
|
|
55
|
+
attributes a theme toggle usually sets, re-reads the resolved CSS and redraws.
|
|
56
|
+
|
|
57
|
+
## Requirements
|
|
58
|
+
|
|
59
|
+
| | |
|
|
60
|
+
|---|---|
|
|
61
|
+
| Angular | 21 or 22 |
|
|
62
|
+
| chart.js | ^4.4.0 |
|
|
63
|
+
|
|
64
|
+
## Install
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npm i @angx/ngx-chart-kit chart.js
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`chart.js` is a peer dependency, so install it alongside. There is nothing else
|
|
71
|
+
— no `@angular/cdk`, no wrapper library, no plugin packages.
|
|
72
|
+
|
|
73
|
+
## Usage
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { Component, signal } from '@angular/core';
|
|
77
|
+
import { NgxChartKitComponent, type ChartKitData } from '@angx/ngx-chart-kit';
|
|
78
|
+
|
|
79
|
+
@Component({
|
|
80
|
+
selector: 'app-revenue',
|
|
81
|
+
imports: [NgxChartKitComponent],
|
|
82
|
+
template: `
|
|
83
|
+
<chart-kit
|
|
84
|
+
type="bar"
|
|
85
|
+
[data]="data()"
|
|
86
|
+
caption="Revenue by quarter"
|
|
87
|
+
description="Net revenue in thousands of dollars, excluding refunds."
|
|
88
|
+
categoryHeader="Quarter"
|
|
89
|
+
/>
|
|
90
|
+
`,
|
|
91
|
+
})
|
|
92
|
+
export class RevenueComponent {
|
|
93
|
+
readonly data = signal<ChartKitData>({
|
|
94
|
+
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
|
|
95
|
+
series: [
|
|
96
|
+
{ label: 'Revenue', data: [820, 905, 960, 1040] },
|
|
97
|
+
{ label: 'Costs', data: [310, 348, 372, 401] },
|
|
98
|
+
],
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Call `data.set(...)` and the chart animates to the new values. The hidden table
|
|
104
|
+
changes with it.
|
|
105
|
+
|
|
106
|
+
## The data shape
|
|
107
|
+
|
|
108
|
+
Labels down the side, named series across the top — the same shape a table has,
|
|
109
|
+
which is what makes the accessible table a mechanical transformation rather
|
|
110
|
+
than something you maintain twice.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
interface ChartKitData {
|
|
114
|
+
labels: readonly string[];
|
|
115
|
+
series: readonly {
|
|
116
|
+
label: string;
|
|
117
|
+
data: readonly (number | null | undefined)[];
|
|
118
|
+
color?: string;
|
|
119
|
+
}[];
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`null` and `undefined` mean "no value here". The chart leaves a gap — a line
|
|
124
|
+
breaks rather than drawing straight through it — and the table says so in
|
|
125
|
+
words, because a screen reader reads an empty cell as "blank", which does not
|
|
126
|
+
distinguish missing data from a broken table.
|
|
127
|
+
|
|
128
|
+
A series longer than `labels` is not silently dropped: the extra rows get
|
|
129
|
+
generated labels (`Item 7`), so nothing is ever drawn without being described.
|
|
130
|
+
|
|
131
|
+
## Chart types
|
|
132
|
+
|
|
133
|
+
| `type` | Draws |
|
|
134
|
+
|---|---|
|
|
135
|
+
| `bar` | Vertical bars |
|
|
136
|
+
| `hbar` | Horizontal bars |
|
|
137
|
+
| `line` | Lines |
|
|
138
|
+
| `area` | Lines with the area beneath them filled |
|
|
139
|
+
| `pie` | Pie |
|
|
140
|
+
| `doughnut` | Pie with a hole |
|
|
141
|
+
| `radar` | Radar / spider |
|
|
142
|
+
|
|
143
|
+
`hbar` and `area` are separate entries rather than flags because that is how
|
|
144
|
+
you think about them. They share a chart.js controller with `bar` and `line`
|
|
145
|
+
respectively, so switching between those pairs at runtime updates in place
|
|
146
|
+
rather than rebuilding.
|
|
147
|
+
|
|
148
|
+
## Inputs
|
|
149
|
+
|
|
150
|
+
| Input | Type | Default | Description |
|
|
151
|
+
|---|---|---|---|
|
|
152
|
+
| `data` | `ChartKitData` | *required* | The data to draw, and the data the table is built from. |
|
|
153
|
+
| `caption` | `string` | *required* | The chart's accessible name and the table's `<caption>`. |
|
|
154
|
+
| `type` | `ChartKitType` | `'bar'` | Which form to draw. |
|
|
155
|
+
| `description` | `string` | `''` | Longer explanation, referenced by the canvas's `aria-describedby`. |
|
|
156
|
+
| `categoryHeader` | `string` | `'Category'` | Header of the table's first column. |
|
|
157
|
+
| `dataTable` | `'hidden' \| 'visible'` | `'hidden'` | Where the table goes. There is no third value. |
|
|
158
|
+
| `height` | `number` | `320` | Height of the drawing area in CSS pixels. Width follows the host. |
|
|
159
|
+
| `stacked` | `boolean` | `false` | Stack series. Bar and area charts. |
|
|
160
|
+
| `legend` | `boolean` | `true` | Ignored for a single-series cartesian chart. |
|
|
161
|
+
| `tooltips` | `boolean` | `true` | Tooltips on hover. |
|
|
162
|
+
| `animate` | `boolean` | `true` | Forced off when the reader prefers reduced motion. |
|
|
163
|
+
| `beginAtZero` | `boolean` | `true` | Keeps bar lengths proportional to their values. |
|
|
164
|
+
| `palette` | `readonly string[]` | — | Overrides the palette from TypeScript. Prefer the CSS properties. |
|
|
165
|
+
| `locale` | `string` | runtime's | Locale for the default number formatting. |
|
|
166
|
+
| `valueFormat` | `ChartKitValueFormat` | locale numbers | Formats a value for the table *and* the tooltip, so the two cannot disagree. |
|
|
167
|
+
| `emptyValueText` | `string` | `'No data'` | Table text for a missing value. Read aloud, so not an empty string. |
|
|
168
|
+
|
|
169
|
+
`caption` is required on purpose. A chart nobody can name is a chart some
|
|
170
|
+
readers cannot use, and an optional caption is a caption most charts ship
|
|
171
|
+
without.
|
|
172
|
+
|
|
173
|
+
## Outputs
|
|
174
|
+
|
|
175
|
+
| Output | Type | Fires when |
|
|
176
|
+
|---|---|---|
|
|
177
|
+
| `pointSelect` | `ChartKitPoint` | A reader clicks a point. Supplementary to the table, not a replacement for it. |
|
|
178
|
+
| `chartCreate` | `Chart` | The chart.js instance is created, and again after a rebuild. |
|
|
179
|
+
| `renderError` | `unknown` | The chart.js chunk failed to load, or the canvas refused a context. The table is on the page either way. |
|
|
180
|
+
|
|
181
|
+
`chartCreate` is the deliberate escape hatch. Reach for it to register a
|
|
182
|
+
plugin, read a scale or export an image. What this library will not do is
|
|
183
|
+
re-export chart.js's whole options object as its primary API — at that point it
|
|
184
|
+
adds nothing you could not get from chart.js directly.
|
|
185
|
+
|
|
186
|
+
## Theming
|
|
187
|
+
|
|
188
|
+
Set these on the chart's host, or anywhere it inherits from:
|
|
189
|
+
|
|
190
|
+
| Property | Default |
|
|
191
|
+
|---|---|
|
|
192
|
+
| `--chart-kit-palette` | The palette below, as a comma-separated list |
|
|
193
|
+
| `--chart-kit-series-N` | Overrides palette slot *N*, 1-based. Past the end, extends it |
|
|
194
|
+
| `--chart-kit-text` | The host's inherited `color` |
|
|
195
|
+
| `--chart-kit-muted` | Text at 74% opacity |
|
|
196
|
+
| `--chart-kit-grid` | Text at 14% opacity |
|
|
197
|
+
| `--chart-kit-surface` | The host's background, or derived from the text's luminance |
|
|
198
|
+
| `--chart-kit-tooltip-bg` | Derived from the text's luminance |
|
|
199
|
+
| `--chart-kit-tooltip-text` | Derived from the text's luminance |
|
|
200
|
+
| `--chart-kit-font` | The host's inherited `font-family` |
|
|
201
|
+
|
|
202
|
+
Only the palette, text colour and font have to come from anywhere — everything
|
|
203
|
+
else is derived from the text colour's luminance. Light text means a dark
|
|
204
|
+
surface, so a consumer who sets nothing at all still gets grid lines and
|
|
205
|
+
tooltips that suit whichever surface the chart landed on. That derivation is
|
|
206
|
+
the reason colours are read at draw time rather than at construction:
|
|
207
|
+
|
|
208
|
+
```css
|
|
209
|
+
:root {
|
|
210
|
+
--chart-kit-text: #15181d;
|
|
211
|
+
--chart-kit-grid: rgb(21 24 29 / 0.12);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
:root[data-theme='dark'] {
|
|
215
|
+
--chart-kit-text: #e8eaee;
|
|
216
|
+
--chart-kit-grid: rgb(232 234 238 / 0.14);
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
That is the whole of a dark-mode switch. Flipping `data-theme` on `<html>`
|
|
221
|
+
recolours every chart on the page.
|
|
222
|
+
|
|
223
|
+
If you switch themes some other way — a stylesheet swapped at runtime, a
|
|
224
|
+
property set from script on an ancestor — call `refreshTheme()` on the
|
|
225
|
+
component.
|
|
226
|
+
|
|
227
|
+
A colour passed as `series.color` or through the `palette` input cannot follow
|
|
228
|
+
a theme change. That is the trade-off for setting it from TypeScript.
|
|
229
|
+
|
|
230
|
+
### The default palette
|
|
231
|
+
|
|
232
|
+
```
|
|
233
|
+
#0072b2 #e69f00 #009e73 #cc79a7 #56b4e9 #d55e00 #785ef0 #7f7f7f
|
|
234
|
+
blue orange green pink sky vermil. violet grey
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Six hues from the [Okabe–Ito](https://jfly.uni-koeln.de/color/)
|
|
238
|
+
colour-blind-safe set, with two deliberate departures:
|
|
239
|
+
|
|
240
|
+
- **Okabe–Ito's black is dropped.** It is invisible against a dark surface, and
|
|
241
|
+
this library does not assume a white page.
|
|
242
|
+
- **Okabe–Ito's `#F0E442` yellow is dropped.** Its relative luminance is about
|
|
243
|
+
0.79, so a 2px line or a small legend swatch in it falls below 3:1 against
|
|
244
|
+
white.
|
|
245
|
+
|
|
246
|
+
That leaves six, extended with a violet from IBM's colour-blind-safe palette
|
|
247
|
+
and a neutral grey in the last slot, which reads naturally as "other" when a
|
|
248
|
+
chart needs more than seven series. Every entry sits in the mid-luminance band,
|
|
249
|
+
roughly 0.15–0.5, which is what lets one palette hold its contrast on a
|
|
250
|
+
near-white and a near-black surface instead of being swapped out per theme.
|
|
251
|
+
|
|
252
|
+
The order separates the pairs most easily confused under protanopia and
|
|
253
|
+
deuteranopia: blue and orange first, with sky blue held back to slot five so it
|
|
254
|
+
is never adjacent to the darker blue.
|
|
255
|
+
|
|
256
|
+
## Server-side rendering
|
|
257
|
+
|
|
258
|
+
Nothing needs configuring. On the server the component renders its table and no
|
|
259
|
+
canvas; in the browser it attaches the canvas and loads chart.js. A
|
|
260
|
+
server-rendered page therefore ships the chart's data as readable HTML before
|
|
261
|
+
any JavaScript arrives, which is a reasonable thing to happen even when
|
|
262
|
+
everything works.
|
|
263
|
+
|
|
264
|
+
## What is exported
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
NgxChartKitComponent // the component
|
|
268
|
+
CHART_KIT_PALETTE // the default palette
|
|
269
|
+
CHART_KIT_CSS_PROPERTIES // the custom property names
|
|
270
|
+
buildChartKitTable // the data-to-table transformation
|
|
271
|
+
chartKitLabels, chartKitRowCount, toChartValue, createDefaultValueFormat
|
|
272
|
+
readChartKitTheme, paletteColor
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
The helpers are exported because the accessible table is the library's point
|
|
276
|
+
and worth being able to build directly — for a custom renderer, a CSV export,
|
|
277
|
+
or a test of your own.
|
|
278
|
+
|
|
279
|
+
## Development
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
npm install
|
|
283
|
+
npm run build:lib # build the package into dist/angx/ngx-chart-kit
|
|
284
|
+
npm start # build the lib, then serve the demo app
|
|
285
|
+
npm test # run the library unit tests
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
The demo app shows every chart type, a live-updating chart, a light/dark toggle
|
|
289
|
+
and a switch that reveals the hidden data tables.
|
|
290
|
+
|
|
291
|
+
## Support
|
|
292
|
+
|
|
293
|
+
If you like my work and feel like buying me a coffee, please feel free to do so:
|
|
294
|
+
|
|
295
|
+
[Buy Me A Coffee](https://buymeacoffee.com/er.abhishek)
|
|
296
|
+
|
|
297
|
+
## Author
|
|
298
|
+
|
|
299
|
+
Built and maintained by **Abhishek Singh**, a backend and AI engineer.
|
|
300
|
+
|
|
301
|
+
I also build **[AITranslate](https://aitranslate.in)** — it translates text, images and PDFs
|
|
302
|
+
across 132 languages and re-renders the result in place, so the fonts, colours and layout of the
|
|
303
|
+
original survive the translation. It has a REST API with credit metering if you need it inside your
|
|
304
|
+
own product.
|
|
305
|
+
|
|
306
|
+
- Website and projects — [coderabhi.in](https://coderabhi.in)
|
|
307
|
+
- Writing on backend, distributed systems and AI — [coderabhi.in/articles](https://coderabhi.in/articles/)
|
|
308
|
+
- [GitHub](https://github.com/asingh0601) · [npm](https://www.npmjs.com/~onlyabhishek) · [X](https://x.com/only_abhishek)
|
|
309
|
+
|
|
310
|
+
## License
|
|
311
|
+
|
|
312
|
+
MIT © [Abhishek Singh](https://coderabhi.in)
|