@keneth80/k-chart 1.9.0 → 1.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.
- package/CHANGELOG.md +22 -0
- package/README.md +204 -1
- package/docs/assets/kchart-hero.svg +110 -0
- package/docs/assets/kchart-icon.svg +40 -0
- package/docs/configuration-reference.md +395 -0
- package/docs/functional-api.md +103 -3
- package/examples/stackblitz-candlestick-basic/index.html +23 -0
- package/examples/stackblitz-candlestick-basic/package-lock.json +1435 -0
- package/examples/stackblitz-candlestick-basic/package.json +18 -0
- package/examples/stackblitz-candlestick-basic/src/main.ts +107 -0
- package/examples/stackblitz-candlestick-basic/src/style.css +16 -0
- package/examples/stackblitz-canvas-line-basic/index.html +23 -0
- package/examples/stackblitz-canvas-line-basic/package-lock.json +1435 -0
- package/examples/stackblitz-canvas-line-basic/package.json +18 -0
- package/examples/stackblitz-canvas-line-basic/src/main.ts +73 -0
- package/examples/stackblitz-canvas-line-basic/src/style.css +46 -0
- package/examples/stackblitz-column-basic/index.html +23 -0
- package/examples/stackblitz-column-basic/package-lock.json +1435 -0
- package/examples/stackblitz-column-basic/package.json +18 -0
- package/examples/stackblitz-column-basic/src/main.ts +128 -0
- package/examples/stackblitz-column-basic/src/style.css +41 -0
- package/examples/stackblitz-doughnut-basic/index.html +21 -0
- package/examples/stackblitz-doughnut-basic/package-lock.json +1435 -0
- package/examples/stackblitz-doughnut-basic/package.json +18 -0
- package/examples/stackblitz-doughnut-basic/src/main.ts +177 -0
- package/examples/stackblitz-doughnut-basic/src/style.css +43 -0
- package/examples/stackblitz-line-basic/index.html +23 -0
- package/examples/stackblitz-line-basic/package-lock.json +1435 -0
- package/examples/stackblitz-line-basic/package.json +18 -0
- package/examples/stackblitz-line-basic/src/main.ts +112 -0
- package/examples/stackblitz-line-basic/src/style.css +108 -0
- package/examples/stackblitz-multi-series-basic/index.html +1 -0
- package/examples/stackblitz-multi-series-basic/package-lock.json +1435 -0
- package/examples/stackblitz-multi-series-basic/package.json +9 -0
- package/examples/stackblitz-multi-series-basic/src/main.ts +29 -0
- package/examples/stackblitz-multi-series-basic/src/style.css +1 -0
- package/examples/stackblitz-options-basic/index.html +1 -0
- package/examples/stackblitz-options-basic/package-lock.json +1435 -0
- package/examples/stackblitz-options-basic/package.json +9 -0
- package/examples/stackblitz-options-basic/src/main.ts +32 -0
- package/examples/stackblitz-options-basic/src/style.css +1 -0
- package/examples/stackblitz-pie-basic/index.html +21 -0
- package/examples/stackblitz-pie-basic/package-lock.json +1435 -0
- package/examples/stackblitz-pie-basic/package.json +18 -0
- package/examples/stackblitz-pie-basic/src/main.ts +156 -0
- package/examples/stackblitz-pie-basic/src/style.css +43 -0
- package/examples/stackblitz-plot-basic/index.html +1 -0
- package/examples/stackblitz-plot-basic/package-lock.json +1435 -0
- package/examples/stackblitz-plot-basic/package.json +9 -0
- package/examples/stackblitz-plot-basic/src/main.ts +32 -0
- package/examples/stackblitz-plot-basic/src/style.css +1 -0
- package/examples/stackblitz-radial-basic/index.html +21 -0
- package/examples/stackblitz-radial-basic/package-lock.json +1435 -0
- package/examples/stackblitz-radial-basic/package.json +18 -0
- package/examples/stackblitz-radial-basic/src/main.ts +223 -0
- package/examples/stackblitz-radial-basic/src/style.css +87 -0
- package/examples/stackblitz-simple-api-basic/index.html +30 -0
- package/examples/stackblitz-simple-api-basic/package-lock.json +1435 -0
- package/examples/stackblitz-simple-api-basic/package.json +18 -0
- package/examples/stackblitz-simple-api-basic/src/main.ts +69 -0
- package/examples/stackblitz-simple-api-basic/src/style.css +127 -0
- package/examples/stackblitz-stacked-column-basic/index.html +23 -0
- package/examples/stackblitz-stacked-column-basic/package-lock.json +1435 -0
- package/examples/stackblitz-stacked-column-basic/package.json +18 -0
- package/examples/stackblitz-stacked-column-basic/src/main.ts +136 -0
- package/examples/stackblitz-stacked-column-basic/src/style.css +16 -0
- package/examples/stackblitz-three-wafer-basic/index.html +1 -0
- package/examples/stackblitz-three-wafer-basic/package-lock.json +1453 -0
- package/examples/stackblitz-three-wafer-basic/package.json +9 -0
- package/examples/stackblitz-three-wafer-basic/src/main.ts +29 -0
- package/examples/stackblitz-three-wafer-basic/src/style.css +1 -0
- package/examples/stackblitz-topology-basic/index.html +1 -0
- package/examples/stackblitz-topology-basic/package-lock.json +1435 -0
- package/examples/stackblitz-topology-basic/package.json +9 -0
- package/examples/stackblitz-topology-basic/src/main.ts +89 -0
- package/examples/stackblitz-topology-basic/src/style.css +1 -0
- package/examples/stackblitz-webgl-line-basic/index.html +23 -0
- package/examples/stackblitz-webgl-line-basic/package-lock.json +1435 -0
- package/examples/stackblitz-webgl-line-basic/package.json +18 -0
- package/examples/stackblitz-webgl-line-basic/src/main.ts +86 -0
- package/examples/stackblitz-webgl-line-basic/src/style.css +46 -0
- package/examples/stackblitz-worker-json-line/index.html +41 -0
- package/examples/stackblitz-worker-json-line/package-lock.json +1435 -0
- package/examples/stackblitz-worker-json-line/package.json +19 -0
- package/examples/stackblitz-worker-json-line/public/data/large-line.json +4322 -0
- package/examples/stackblitz-worker-json-line/src/kchart-render.worker.ts +4 -0
- package/examples/stackblitz-worker-json-line/src/main.ts +185 -0
- package/examples/stackblitz-worker-json-line/src/style.css +101 -0
- package/lib/core/contracts.d.ts +22 -0
- package/lib/core/contracts.d.ts.map +1 -1
- package/lib/core/create-kchart.d.ts.map +1 -1
- package/lib/core/create-kchart.js +93 -4
- package/lib/core/create-kchart.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/kchart.d.ts +1 -0
- package/lib/kchart.d.ts.map +1 -1
- package/lib/kchart.js +1 -0
- package/lib/kchart.js.map +1 -1
- package/lib/presets/index.d.ts +97 -0
- package/lib/presets/index.d.ts.map +1 -0
- package/lib/presets/index.js +481 -0
- package/lib/presets/index.js.map +1 -0
- package/lib/series/canvas-line.d.ts.map +1 -1
- package/lib/series/canvas-line.js +16 -11
- package/lib/series/canvas-line.js.map +1 -1
- package/lib/series/svg-line.d.ts.map +1 -1
- package/lib/series/svg-line.js +19 -3
- package/lib/series/svg-line.js.map +1 -1
- package/lib/series/webgl-line.d.ts.map +1 -1
- package/lib/series/webgl-line.js +10 -3
- package/lib/series/webgl-line.js.map +1 -1
- package/package.json +13 -2
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kchart-stackblitz-pie-basic",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite --host 0.0.0.0",
|
|
8
|
+
"build": "tsc && vite build",
|
|
9
|
+
"preview": "vite preview --host 0.0.0.0"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@keneth80/k-chart": "^1.9.0"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"typescript": "^6.0.3",
|
|
16
|
+
"vite": "^7.2.7"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { createCustomSeries, createKChart, type KChartSeries } from '@keneth80/k-chart';
|
|
2
|
+
import './style.css';
|
|
3
|
+
|
|
4
|
+
interface SegmentPoint {
|
|
5
|
+
label: string;
|
|
6
|
+
value: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const data: SegmentPoint[] = [
|
|
10
|
+
{ label: 'Product A', value: 34 },
|
|
11
|
+
{ label: 'Product B', value: 42 },
|
|
12
|
+
{ label: 'Product C', value: 26 },
|
|
13
|
+
{ label: 'Product D', value: 18 },
|
|
14
|
+
{ label: 'Product E', value: 31 }
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const palette = ['#5db8ff', '#56d08f', '#f3b45b', '#d876ff', '#ff6b8a'];
|
|
18
|
+
|
|
19
|
+
const createPieSeries = (innerRadiusRatio = 0): KChartSeries<SegmentPoint> => {
|
|
20
|
+
type PieSegment = {
|
|
21
|
+
point: SegmentPoint;
|
|
22
|
+
value: number;
|
|
23
|
+
startAngle: number;
|
|
24
|
+
endAngle: number;
|
|
25
|
+
color: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const pointOnCircle = (centerX: number, centerY: number, radius: number, angle: number): [number, number] => [
|
|
29
|
+
centerX + Math.cos(angle) * radius,
|
|
30
|
+
centerY + Math.sin(angle) * radius
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
const createSegments = (points: SegmentPoint[]): PieSegment[] => {
|
|
34
|
+
const total = points.reduce((sum, point) => sum + Math.max(0, point.value), 0) || 1;
|
|
35
|
+
let cursor = -Math.PI / 2;
|
|
36
|
+
return points.map((point, index) => {
|
|
37
|
+
const value = Math.max(0, point.value);
|
|
38
|
+
const angle = (value / total) * Math.PI * 2;
|
|
39
|
+
const segment = { point, value, startAngle: cursor, endAngle: cursor + angle, color: palette[index % palette.length] };
|
|
40
|
+
cursor += angle;
|
|
41
|
+
return segment;
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const describeSegment = (segment: PieSegment, centerX: number, centerY: number, outerRadius: number, innerRadius: number): string => {
|
|
46
|
+
const [outerStartX, outerStartY] = pointOnCircle(centerX, centerY, outerRadius, segment.startAngle);
|
|
47
|
+
const [outerEndX, outerEndY] = pointOnCircle(centerX, centerY, outerRadius, segment.endAngle);
|
|
48
|
+
const largeArcFlag = segment.endAngle - segment.startAngle > Math.PI ? 1 : 0;
|
|
49
|
+
if (innerRadius <= 0) {
|
|
50
|
+
return `M${centerX},${centerY} L${outerStartX},${outerStartY} A${outerRadius},${outerRadius} 0 ${largeArcFlag} 1 ${outerEndX},${outerEndY} Z`;
|
|
51
|
+
}
|
|
52
|
+
const [innerEndX, innerEndY] = pointOnCircle(centerX, centerY, innerRadius, segment.endAngle);
|
|
53
|
+
const [innerStartX, innerStartY] = pointOnCircle(centerX, centerY, innerRadius, segment.startAngle);
|
|
54
|
+
return `M${outerStartX},${outerStartY} A${outerRadius},${outerRadius} 0 ${largeArcFlag} 1 ${outerEndX},${outerEndY} L${innerEndX},${innerEndY} A${innerRadius},${innerRadius} 0 ${largeArcFlag} 0 ${innerStartX},${innerStartY} Z`;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const normalizeAngle = (angle: number): number => {
|
|
58
|
+
let normalized = angle;
|
|
59
|
+
while (normalized < -Math.PI / 2) normalized += Math.PI * 2;
|
|
60
|
+
while (normalized >= Math.PI * 1.5) normalized -= Math.PI * 2;
|
|
61
|
+
return normalized;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return createCustomSeries<SegmentPoint>({
|
|
65
|
+
selector: 'pie-segments',
|
|
66
|
+
displayName: 'Share',
|
|
67
|
+
xField: 'label',
|
|
68
|
+
yField: 'value',
|
|
69
|
+
render({ group, data: points, plotSize }) {
|
|
70
|
+
const centerX = plotSize.width / 2;
|
|
71
|
+
const centerY = plotSize.height / 2 + 8;
|
|
72
|
+
const outerRadius = Math.max(92, Math.min(plotSize.width, plotSize.height) * 0.34);
|
|
73
|
+
const innerRadius = outerRadius * innerRadiusRatio;
|
|
74
|
+
const segments = createSegments(points);
|
|
75
|
+
const total = segments.reduce((sum, segment) => sum + segment.value, 0) || 1;
|
|
76
|
+
|
|
77
|
+
group.selectAll<SVGPathElement, PieSegment>('path.pie-segment')
|
|
78
|
+
.data(segments)
|
|
79
|
+
.join('path')
|
|
80
|
+
.attr('class', 'pie-segment')
|
|
81
|
+
.attr('d', (segment) => describeSegment(segment, centerX, centerY, outerRadius, innerRadius))
|
|
82
|
+
.style('fill', (segment) => segment.color)
|
|
83
|
+
.style('fill-opacity', 0.84)
|
|
84
|
+
.style('stroke', '#101720')
|
|
85
|
+
.style('stroke-width', 2);
|
|
86
|
+
|
|
87
|
+
group.selectAll<SVGTextElement, PieSegment>('text.pie-label')
|
|
88
|
+
.data(segments)
|
|
89
|
+
.join('text')
|
|
90
|
+
.attr('class', 'pie-label')
|
|
91
|
+
.attr('x', (segment) => pointOnCircle(centerX, centerY, outerRadius + 28, (segment.startAngle + segment.endAngle) / 2)[0])
|
|
92
|
+
.attr('y', (segment) => pointOnCircle(centerX, centerY, outerRadius + 28, (segment.startAngle + segment.endAngle) / 2)[1])
|
|
93
|
+
.attr('text-anchor', 'middle')
|
|
94
|
+
.attr('dominant-baseline', 'middle')
|
|
95
|
+
.style('fill', 'rgba(226, 232, 240, 0.86)')
|
|
96
|
+
.style('font-size', '12px')
|
|
97
|
+
.style('font-weight', 700)
|
|
98
|
+
.text((segment) => `${segment.point.label} ${Math.round((segment.value / total) * 100)}%`);
|
|
99
|
+
},
|
|
100
|
+
tooltip({ data: points, plotSize, seriesGroup, mouseX, mouseY }) {
|
|
101
|
+
const centerX = plotSize.width / 2;
|
|
102
|
+
const centerY = plotSize.height / 2 + 8;
|
|
103
|
+
const outerRadius = Math.max(92, Math.min(plotSize.width, plotSize.height) * 0.34);
|
|
104
|
+
const innerRadius = outerRadius * innerRadiusRatio;
|
|
105
|
+
const distance = Math.hypot(mouseX - centerX, mouseY - centerY);
|
|
106
|
+
const angle = normalizeAngle(Math.atan2(mouseY - centerY, mouseX - centerX));
|
|
107
|
+
const total = points.reduce((sum, point) => sum + Math.max(0, point.value), 0) || 1;
|
|
108
|
+
|
|
109
|
+
seriesGroup.selectAll<SVGPathElement, PieSegment>('path.pie-segment')
|
|
110
|
+
.style('fill-opacity', 0.84)
|
|
111
|
+
.style('stroke', '#101720');
|
|
112
|
+
|
|
113
|
+
if (distance > outerRadius || distance < innerRadius) return undefined;
|
|
114
|
+
const segment = createSegments(points).find((item) => angle >= item.startAngle && angle <= item.endAngle);
|
|
115
|
+
if (!segment) return undefined;
|
|
116
|
+
|
|
117
|
+
seriesGroup.selectAll<SVGPathElement, PieSegment>('path.pie-segment')
|
|
118
|
+
.filter((item) => item.point === segment.point)
|
|
119
|
+
.style('fill-opacity', 1)
|
|
120
|
+
.style('stroke', '#f8fafc');
|
|
121
|
+
|
|
122
|
+
const midAngle = (segment.startAngle + segment.endAngle) / 2;
|
|
123
|
+
const [x, y] = pointOnCircle(centerX, centerY, outerRadius * 0.62, midAngle);
|
|
124
|
+
return {
|
|
125
|
+
data: segment.point,
|
|
126
|
+
x,
|
|
127
|
+
y,
|
|
128
|
+
distance: 0,
|
|
129
|
+
color: segment.color,
|
|
130
|
+
html: `<strong style="color:${segment.color}">${segment.point.label}</strong><br/>value: ${segment.value}<br/>share: ${Math.round((segment.value / total) * 100)}%`
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const chart = createKChart<SegmentPoint>({
|
|
137
|
+
selector: '#chart',
|
|
138
|
+
data,
|
|
139
|
+
width: 920,
|
|
140
|
+
height: 500,
|
|
141
|
+
margin: { top: 88, right: 36, bottom: 42, left: 36 },
|
|
142
|
+
title: { text: 'Market Share Pie Chart', align: 'left', fontSize: 18 },
|
|
143
|
+
grid: { visible: false },
|
|
144
|
+
legend: { visible: false },
|
|
145
|
+
tooltip: { visible: true },
|
|
146
|
+
axes: [],
|
|
147
|
+
series: [createPieSeries(0)]
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
chart.render();
|
|
151
|
+
|
|
152
|
+
window.addEventListener('resize', () => {
|
|
153
|
+
const panel = document.querySelector<HTMLElement>('.chart-panel');
|
|
154
|
+
if (!panel) return;
|
|
155
|
+
chart.resize({ width: Math.max(panel.clientWidth - 48, 320), height: 500 });
|
|
156
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
color-scheme: dark;
|
|
3
|
+
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
|
4
|
+
background: #0b1120;
|
|
5
|
+
color: #e5edf7;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
* { box-sizing: border-box; }
|
|
9
|
+
|
|
10
|
+
body {
|
|
11
|
+
margin: 0;
|
|
12
|
+
min-width: 320px;
|
|
13
|
+
min-height: 100vh;
|
|
14
|
+
background:
|
|
15
|
+
radial-gradient(circle at 20% 10%, rgba(93, 184, 255, 0.18), transparent 32rem),
|
|
16
|
+
radial-gradient(circle at 80% 0%, rgba(243, 180, 91, 0.12), transparent 28rem),
|
|
17
|
+
#0b1120;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.page-shell { width: min(1120px, calc(100vw - 32px)); margin: 0 auto; padding: 40px 0; }
|
|
21
|
+
.intro { margin-bottom: 18px; }
|
|
22
|
+
.eyebrow { margin: 0 0 8px; color: #7dd3fc; font-size: 12px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; }
|
|
23
|
+
h1 { margin: 0; font-size: clamp(28px, 5vw, 52px); line-height: 1.05; }
|
|
24
|
+
.intro p:last-child { margin: 12px 0 0; color: #b8c5d6; }
|
|
25
|
+
|
|
26
|
+
.chart-panel {
|
|
27
|
+
min-height: 560px;
|
|
28
|
+
padding: 24px;
|
|
29
|
+
border: 1px solid rgba(148, 163, 184, 0.24);
|
|
30
|
+
border-radius: 12px;
|
|
31
|
+
background: linear-gradient(145deg, rgba(15, 23, 42, 0.92), rgba(17, 24, 39, 0.82));
|
|
32
|
+
box-shadow: 0 24px 80px rgba(0, 0, 0, 0.28);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#chart { width: 100%; height: 500px; }
|
|
36
|
+
#chart .kchart-svg text { font-family: inherit; }
|
|
37
|
+
#chart .kchart-title { fill: #f8fafc; }
|
|
38
|
+
#chart .kchart-legend text { fill: #dbeafe; }
|
|
39
|
+
|
|
40
|
+
@media (max-width: 720px) {
|
|
41
|
+
.page-shell { width: min(100vw - 20px, 1120px); padding: 24px 0; }
|
|
42
|
+
.chart-panel { padding: 12px; }
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<!doctype html><html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>KChart Plot Basic</title></head><body><main class="page-shell"><section class="intro"><p class="eyebrow">StackBlitz starter</p><h1>KChart SVG Plot</h1><p>Edit <code>src/main.ts</code> to change point size, color, and tooltip data.</p></section><section class="chart-panel"><div id="chart"></div></section></main><script type="module" src="/src/main.ts"></script></body></html>
|