@c2n/chart 0.0.7
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 +81 -0
- package/dist/area-chart.js +29 -0
- package/dist/bar-chart.js +65 -0
- package/dist/chart-adapter.js +0 -0
- package/dist/chart-base-C4-1TyKq.js +666 -0
- package/dist/chart-base.js +2 -0
- package/dist/chart-data.js +286 -0
- package/dist/chart-series-BnpHO3lm.js +83 -0
- package/dist/chart-series.js +2 -0
- package/dist/chart-theme.js +151 -0
- package/dist/chart-types.js +8 -0
- package/dist/chart.js +13 -0
- package/dist/echarts-chart-base-LogCQN10.js +228 -0
- package/dist/echarts-chart-base.js +2 -0
- package/dist/line-chart.js +31 -0
- package/dist/pie-chart.js +100 -0
- package/dist/sparkline.js +79 -0
- package/dist/uplot-chart-base-BWr0zJE2.js +253 -0
- package/dist/uplot-chart-base.js +2 -0
- package/dist/uplot-paths-ChKohvIX.js +95 -0
- package/package.json +146 -0
- package/react.d.ts +34 -0
- package/react.js +3 -0
- package/types/src/area-chart.d.ts +36 -0
- package/types/src/area-chart.d.ts.map +1 -0
- package/types/src/bar-chart.d.ts +46 -0
- package/types/src/bar-chart.d.ts.map +1 -0
- package/types/src/chart-adapter.d.ts +67 -0
- package/types/src/chart-adapter.d.ts.map +1 -0
- package/types/src/chart-base.d.ts +265 -0
- package/types/src/chart-base.d.ts.map +1 -0
- package/types/src/chart-data.d.ts +43 -0
- package/types/src/chart-data.d.ts.map +1 -0
- package/types/src/chart-series.d.ts +51 -0
- package/types/src/chart-series.d.ts.map +1 -0
- package/types/src/chart-theme.d.ts +64 -0
- package/types/src/chart-theme.d.ts.map +1 -0
- package/types/src/chart-types.d.ts +162 -0
- package/types/src/chart-types.d.ts.map +1 -0
- package/types/src/chart.d.ts +27 -0
- package/types/src/chart.d.ts.map +1 -0
- package/types/src/echarts-chart-base.d.ts +28 -0
- package/types/src/echarts-chart-base.d.ts.map +1 -0
- package/types/src/engines/echarts-adapter.d.ts +17 -0
- package/types/src/engines/echarts-adapter.d.ts.map +1 -0
- package/types/src/engines/echarts-loader.d.ts +16 -0
- package/types/src/engines/echarts-loader.d.ts.map +1 -0
- package/types/src/engines/uplot-adapter.d.ts +4 -0
- package/types/src/engines/uplot-adapter.d.ts.map +1 -0
- package/types/src/engines/uplot-loader.d.ts +20 -0
- package/types/src/engines/uplot-loader.d.ts.map +1 -0
- package/types/src/engines/uplot-paths.d.ts +25 -0
- package/types/src/engines/uplot-paths.d.ts.map +1 -0
- package/types/src/line-chart.d.ts +40 -0
- package/types/src/line-chart.d.ts.map +1 -0
- package/types/src/pie-chart.d.ts +59 -0
- package/types/src/pie-chart.d.ts.map +1 -0
- package/types/src/sparkline.d.ts +56 -0
- package/types/src/sparkline.d.ts.map +1 -0
- package/types/src/uplot-chart-base.d.ts +50 -0
- package/types/src/uplot-chart-base.d.ts.map +1 -0
- package/vue.d.ts +134 -0
- package/vue.js +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Nguyen Thai Vinh
|
|
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
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @c2n/chart
|
|
2
|
+
|
|
3
|
+
Line, area, bar, sparkline and pie charts as custom elements, from one package.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @c2n/chart uplot # line, area, bar, sparkline
|
|
7
|
+
npm install @c2n/chart echarts # pie
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```html
|
|
11
|
+
<c2-line-chart x-field="month" legend="bottom" data='[{ "month": 1, "revenue": 128, "cost": 74 }]'>
|
|
12
|
+
<c2-chart-series field="revenue" label="Revenue"></c2-chart-series>
|
|
13
|
+
<c2-chart-series field="cost" label="Cost"></c2-chart-series>
|
|
14
|
+
</c2-line-chart>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import '@c2n/chart/line-chart.js' // one tag
|
|
19
|
+
import '@c2n/chart' // all of them
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Elements
|
|
23
|
+
|
|
24
|
+
| Tag | Engine | For |
|
|
25
|
+
| ----------------- | ------- | ----------------------------------------------- |
|
|
26
|
+
| `c2-line-chart` | uPlot | Time or numeric trends |
|
|
27
|
+
| `c2-area-chart` | uPlot | The same, with the region under the line filled |
|
|
28
|
+
| `c2-bar-chart` | uPlot | Categorical or time-bucketed values |
|
|
29
|
+
| `c2-sparkline` | uPlot | A chromeless trend for a table cell or KPI row |
|
|
30
|
+
| `c2-pie-chart` | ECharts | Parts of a whole, with a donut mode |
|
|
31
|
+
| `c2-chart-series` | — | A series definition; renders nothing |
|
|
32
|
+
|
|
33
|
+
## Engines
|
|
34
|
+
|
|
35
|
+
Both libraries are **optional peer dependencies** loaded through a dynamic `import()` on first paint, so
|
|
36
|
+
nothing is bundled into the package and a page that only shows a sparkline never downloads ECharts. Install
|
|
37
|
+
whichever engines your charts need.
|
|
38
|
+
|
|
39
|
+
## Data
|
|
40
|
+
|
|
41
|
+
`data` accepts row objects, a bare `number[]`, columnar `[xs, ys…]`, or an already-normalised frame.
|
|
42
|
+
Internally everything becomes one columnar frame of `Float64Array`s, which is the shape uPlot draws
|
|
43
|
+
natively and what makes appending O(1).
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
chart.data = rows // full replace
|
|
47
|
+
chart.revision++ // after mutating the array in place
|
|
48
|
+
chart.appendPoint(x, [a, b]) // one realtime tick
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`data` is compared by **identity**, never deep-diffed. A change to `data` or `revision` alone is pushed
|
|
52
|
+
straight into the engine — Lit does not render, and the canvas container is not touched — so a streaming
|
|
53
|
+
tick costs one engine redraw and nothing else. Anything else counts as presentation and rebuilds the engine
|
|
54
|
+
options. Use `max-points` to keep a bounded window.
|
|
55
|
+
|
|
56
|
+
For a lazy or streaming source, set `dataSource` instead: `getWindow()` is the pull half, and an optional
|
|
57
|
+
`subscribe()` feeds ticks straight into the append path.
|
|
58
|
+
|
|
59
|
+
## Sizing
|
|
60
|
+
|
|
61
|
+
A canvas has no intrinsic size, so a chart is sized by its host: `--c2-chart--height` (320px, or 32px on a
|
|
62
|
+
sparkline) plus whatever width the layout gives it. A `ResizeObserver` keeps the canvas in step, and the
|
|
63
|
+
plot box is `contain: strict` so a resize cannot feed back into itself.
|
|
64
|
+
|
|
65
|
+
## Theming
|
|
66
|
+
|
|
67
|
+
Every element shares the `--c2-chart__*` namespace, so theming once themes them all. The palette is
|
|
68
|
+
`--c2-chart__series-1--color` … `--c2-chart__series-8--color`, wired by `@c2n/theme` to the
|
|
69
|
+
`--c2-theme--chart-series-*` tokens, which carry light and dark values.
|
|
70
|
+
|
|
71
|
+
Because the plot is a canvas, these variables are resolved with `getComputedStyle` and handed to the
|
|
72
|
+
engine. They behave like any other CSS variable — including under a dark theme, which the element watches
|
|
73
|
+
for — but the marks cannot be reached with `::part()`. The chrome around the plot (legend, tooltip, state
|
|
74
|
+
messages) is ordinary DOM and does expose parts.
|
|
75
|
+
|
|
76
|
+
## Events
|
|
77
|
+
|
|
78
|
+
`chart-ready`, `chart-error`, `point-click`, `point-hover`, `range-change` and `series-toggle`. The
|
|
79
|
+
semantic ones do not bubble: several components fire similarly named events, so a listener belongs on the
|
|
80
|
+
element itself. The host also carries `data-chart-ready` once the first frame is drawn, which is the
|
|
81
|
+
signal to wait on in a test.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { i as __decorate } from "./chart-series-BnpHO3lm.js";
|
|
2
|
+
import { r as withAlpha } from "./uplot-paths-ChKohvIX.js";
|
|
3
|
+
import { LineChart } from "./line-chart.js";
|
|
4
|
+
import { property } from "lit/decorators.js";
|
|
5
|
+
import { customElement } from "@c2n/core/element-helper.js";
|
|
6
|
+
//#region src/area-chart.ts
|
|
7
|
+
var AreaChart = class AreaChart extends LineChart {
|
|
8
|
+
seriesStyle(series, index, context) {
|
|
9
|
+
const base = super.seriesStyle(series, index, context);
|
|
10
|
+
const color = this.colorOf(series, index, context.theme);
|
|
11
|
+
const opacity = this.fillOpacity ?? this.areaOpacity(context);
|
|
12
|
+
return {
|
|
13
|
+
...base,
|
|
14
|
+
fill: withAlpha(color, opacity)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** Reads `--c2-chart__area--opacity` off the host, falling back to uPlot-friendly 0.15. */
|
|
18
|
+
areaOpacity(_context) {
|
|
19
|
+
const raw = parseFloat(getComputedStyle(this).getPropertyValue("--c2-chart__area--opacity"));
|
|
20
|
+
return Number.isFinite(raw) ? raw : .15;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
__decorate([property({
|
|
24
|
+
type: Number,
|
|
25
|
+
attribute: "fill-opacity"
|
|
26
|
+
})], AreaChart.prototype, "fillOpacity", void 0);
|
|
27
|
+
AreaChart = __decorate([customElement("c2-area-chart")], AreaChart);
|
|
28
|
+
//#endregion
|
|
29
|
+
export { AreaChart };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { i as __decorate } from "./chart-series-BnpHO3lm.js";
|
|
2
|
+
import { t as UplotChartBase } from "./uplot-chart-base-BWr0zJE2.js";
|
|
3
|
+
import { t as barPaths } from "./uplot-paths-ChKohvIX.js";
|
|
4
|
+
import { property } from "lit/decorators.js";
|
|
5
|
+
import { customElement } from "@c2n/core/element-helper.js";
|
|
6
|
+
//#region src/bar-chart.ts
|
|
7
|
+
var BarChart = class BarChart extends UplotChartBase {
|
|
8
|
+
constructor(..._args) {
|
|
9
|
+
super(..._args);
|
|
10
|
+
this.barWidth = .6;
|
|
11
|
+
this.barGap = .1;
|
|
12
|
+
}
|
|
13
|
+
seriesStyle(series, index, context) {
|
|
14
|
+
const color = this.colorOf(series, index, context.theme);
|
|
15
|
+
return {
|
|
16
|
+
stroke: color,
|
|
17
|
+
fill: color,
|
|
18
|
+
width: 0,
|
|
19
|
+
paths: barPaths(this.barWidth, this.barGap, index, context.series.length),
|
|
20
|
+
points: { show: false }
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** A band chart reads its x as discrete slots, so the scale is padded by half a band at each end. */
|
|
24
|
+
decorateOptions(options, _context) {
|
|
25
|
+
return {
|
|
26
|
+
...options,
|
|
27
|
+
scales: {
|
|
28
|
+
...options.scales,
|
|
29
|
+
x: {
|
|
30
|
+
...options.scales?.x,
|
|
31
|
+
time: false,
|
|
32
|
+
range: (_self, min, max) => [min - .5, max + .5]
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
cursor: {
|
|
36
|
+
...options.cursor,
|
|
37
|
+
x: true,
|
|
38
|
+
y: false
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Bars sit on category labels when there are any, so the tick shows the label rather than the index.
|
|
44
|
+
*
|
|
45
|
+
* uPlot picks its own split values, and on a handful of bands those land on halves — which would round
|
|
46
|
+
* to the same label twice and then off the end of the list. A band has no label between its slots, so
|
|
47
|
+
* those ticks are blank.
|
|
48
|
+
*/
|
|
49
|
+
formatAxisX(value) {
|
|
50
|
+
const labels = this.frame?.labels;
|
|
51
|
+
if (!labels) return super.formatAxisX(value);
|
|
52
|
+
return Number.isInteger(value) ? labels[value] ?? "" : "";
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
__decorate([property({
|
|
56
|
+
type: Number,
|
|
57
|
+
attribute: "bar-width"
|
|
58
|
+
})], BarChart.prototype, "barWidth", void 0);
|
|
59
|
+
__decorate([property({
|
|
60
|
+
type: Number,
|
|
61
|
+
attribute: "bar-gap"
|
|
62
|
+
})], BarChart.prototype, "barGap", void 0);
|
|
63
|
+
BarChart = __decorate([customElement("c2-bar-chart")], BarChart);
|
|
64
|
+
//#endregion
|
|
65
|
+
export { BarChart };
|
|
File without changes
|