@anvme/nanocharts 1.0.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 +134 -0
- package/dist/index.cjs +635 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +107 -0
- package/dist/index.d.ts +107 -0
- package/dist/index.js +606 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.cjs +2 -0
- package/dist/index.min.cjs.map +1 -0
- package/dist/index.min.js +2 -0
- package/dist/index.min.js.map +1 -0
- package/dist/nanocharts.global.js +604 -0
- package/dist/nanocharts.global.js.map +1 -0
- package/dist/nanocharts.min.global.js +2 -0
- package/dist/nanocharts.min.global.js.map +1 -0
- package/dist/style.css +181 -0
- package/dist/style.min.css +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(() => {
|
|
3
|
+
// src/shared.ts
|
|
4
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
5
|
+
function attachHoverListeners(container, svgEl, onUpdate, onLeave) {
|
|
6
|
+
var rafPending = false;
|
|
7
|
+
var lastCX = 0, lastCY = 0;
|
|
8
|
+
function schedule() {
|
|
9
|
+
if (!rafPending) {
|
|
10
|
+
rafPending = true;
|
|
11
|
+
requestAnimationFrame(function() {
|
|
12
|
+
rafPending = false;
|
|
13
|
+
var r = svgEl.getBoundingClientRect();
|
|
14
|
+
onUpdate(lastCX - r.left, lastCY - r.top);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
container.addEventListener("mousemove", function(e) {
|
|
19
|
+
lastCX = e.clientX;
|
|
20
|
+
lastCY = e.clientY;
|
|
21
|
+
schedule();
|
|
22
|
+
}, { passive: true });
|
|
23
|
+
container.addEventListener("mouseleave", onLeave, { passive: true });
|
|
24
|
+
container.addEventListener("touchmove", function(e) {
|
|
25
|
+
var t = e.touches[0];
|
|
26
|
+
lastCX = t.clientX;
|
|
27
|
+
lastCY = t.clientY;
|
|
28
|
+
schedule();
|
|
29
|
+
}, { passive: true });
|
|
30
|
+
container.addEventListener("touchend", onLeave, { passive: true });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/line-chart.ts
|
|
34
|
+
function NanoLineChart(containerId, config) {
|
|
35
|
+
var data = config.data;
|
|
36
|
+
var labels = config.labels;
|
|
37
|
+
var xLabel = config.xLabel || "";
|
|
38
|
+
var yLabel = config.yLabel || "";
|
|
39
|
+
var colors = config.colors || ["#215CAF", "#007894", "#00C9A7", "#DE4437", "#F69C3D"];
|
|
40
|
+
var padding = { top: 20, right: 30, bottom: 50, left: 60 };
|
|
41
|
+
var smooth = config.smooth === true;
|
|
42
|
+
var areaFill = config.areaFill === true;
|
|
43
|
+
var areaOpacity = config.areaOpacity !== void 0 ? config.areaOpacity : 0.1;
|
|
44
|
+
var showTicks = config.showTicks !== false;
|
|
45
|
+
var showGrid = config.showGrid !== false;
|
|
46
|
+
var gridDashed = config.gridDashed === true;
|
|
47
|
+
var strokeWidth = config.strokeWidth !== void 0 ? config.strokeWidth : 2.5;
|
|
48
|
+
var hoverEnabled = config.hover !== false;
|
|
49
|
+
var container = document.getElementById(containerId);
|
|
50
|
+
if (!container || !data.length || !data[0].length) return;
|
|
51
|
+
var chartWidth = config.width || container.clientWidth || 600;
|
|
52
|
+
var chartHeight = config.height || 350;
|
|
53
|
+
var plotWidth = chartWidth - padding.left - padding.right;
|
|
54
|
+
var plotHeight = chartHeight - padding.top - padding.bottom;
|
|
55
|
+
var plotBottom = padding.top + plotHeight;
|
|
56
|
+
var seriesIndex, pointIndex, minValue, maxValue;
|
|
57
|
+
minValue = maxValue = data[0][0];
|
|
58
|
+
for (seriesIndex = 0; seriesIndex < data.length; seriesIndex++)
|
|
59
|
+
for (pointIndex = 0; pointIndex < data[seriesIndex].length; pointIndex++) {
|
|
60
|
+
if (data[seriesIndex][pointIndex] < minValue) minValue = data[seriesIndex][pointIndex];
|
|
61
|
+
if (data[seriesIndex][pointIndex] > maxValue) maxValue = data[seriesIndex][pointIndex];
|
|
62
|
+
}
|
|
63
|
+
var range = maxValue - minValue || 1;
|
|
64
|
+
minValue -= range * 0.05;
|
|
65
|
+
maxValue += range * 0.05;
|
|
66
|
+
range = maxValue - minValue;
|
|
67
|
+
var pointCount = labels.length;
|
|
68
|
+
var stepX = pointCount > 1 ? plotWidth / (pointCount - 1) : 0;
|
|
69
|
+
var tickCount = 6;
|
|
70
|
+
var tickStep = range / (tickCount - 1);
|
|
71
|
+
var totalCoords = data.length * pointCount;
|
|
72
|
+
var coordsX = new Int16Array(totalCoords);
|
|
73
|
+
var coordsY = new Int16Array(totalCoords);
|
|
74
|
+
for (seriesIndex = 0; seriesIndex < data.length; seriesIndex++)
|
|
75
|
+
for (pointIndex = 0; pointIndex < pointCount; pointIndex++) {
|
|
76
|
+
var coordIndex = seriesIndex * pointCount + pointIndex;
|
|
77
|
+
coordsX[coordIndex] = padding.left + pointIndex * stepX | 0;
|
|
78
|
+
coordsY[coordIndex] = padding.top + plotHeight - (data[seriesIndex][pointIndex] - minValue) / range * plotHeight | 0;
|
|
79
|
+
}
|
|
80
|
+
var gridYPositions = new Int16Array(tickCount);
|
|
81
|
+
var gridLabels = new Array(tickCount);
|
|
82
|
+
var tickIndex;
|
|
83
|
+
for (tickIndex = 0; tickIndex < tickCount; tickIndex++) {
|
|
84
|
+
var tickValue = minValue + tickIndex * tickStep;
|
|
85
|
+
gridYPositions[tickIndex] = padding.top + plotHeight - (tickValue - minValue) / range * plotHeight | 0;
|
|
86
|
+
gridLabels[tickIndex] = tickValue.toFixed(1);
|
|
87
|
+
}
|
|
88
|
+
var svgMarkup = '<svg xmlns="http://www.w3.org/2000/svg" width="' + chartWidth + '" height="' + chartHeight + '" viewBox="0 0 ' + chartWidth + " " + chartHeight + '" class="chart">';
|
|
89
|
+
if (showGrid) {
|
|
90
|
+
var gridPath = "";
|
|
91
|
+
for (tickIndex = 0; tickIndex < tickCount; tickIndex++)
|
|
92
|
+
gridPath += "M" + padding.left + "," + gridYPositions[tickIndex] + "H" + (padding.left + plotWidth);
|
|
93
|
+
svgMarkup += '<path d="' + gridPath + '" class="chart-grid"' + (gridDashed ? ' stroke-dasharray="3 4"' : "") + "/>";
|
|
94
|
+
}
|
|
95
|
+
for (seriesIndex = 0; seriesIndex < data.length; seriesIndex++) {
|
|
96
|
+
var ci = seriesIndex * pointCount;
|
|
97
|
+
var pathData = "M" + coordsX[ci] + "," + coordsY[ci];
|
|
98
|
+
if (smooth) {
|
|
99
|
+
for (pointIndex = 0; pointIndex < pointCount - 1; pointIndex++) {
|
|
100
|
+
var prevI = pointIndex ? pointIndex - 1 : 0;
|
|
101
|
+
var next2I = pointIndex + 2 < pointCount ? pointIndex + 2 : pointCount - 1;
|
|
102
|
+
var curX = coordsX[ci + pointIndex], curY = coordsY[ci + pointIndex];
|
|
103
|
+
var nextX = coordsX[ci + pointIndex + 1], nextY = coordsY[ci + pointIndex + 1];
|
|
104
|
+
var prevX = coordsX[ci + prevI], prevY = coordsY[ci + prevI];
|
|
105
|
+
var next2X = coordsX[ci + next2I], next2Y = coordsY[ci + next2I];
|
|
106
|
+
pathData += " C" + (curX + (nextX - prevX) / 6 | 0) + "," + (curY + (nextY - prevY) / 6 | 0) + " " + (nextX - (next2X - curX) / 6 | 0) + "," + (nextY - (next2Y - curY) / 6 | 0) + " " + nextX + "," + nextY;
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
for (pointIndex = 1; pointIndex < pointCount; pointIndex++)
|
|
110
|
+
pathData += "L" + coordsX[ci + pointIndex] + "," + coordsY[ci + pointIndex];
|
|
111
|
+
}
|
|
112
|
+
if (areaFill) {
|
|
113
|
+
var firstX = coordsX[ci], lastX = coordsX[ci + pointCount - 1];
|
|
114
|
+
svgMarkup += '<path d="' + pathData + " L" + lastX + "," + plotBottom + " L" + firstX + "," + plotBottom + ' Z" fill="' + colors[seriesIndex % colors.length] + '" fill-opacity="' + areaOpacity + '" stroke="none" pointer-events="none"/>';
|
|
115
|
+
}
|
|
116
|
+
svgMarkup += '<path d="' + pathData + '" class="chart-series" stroke="' + colors[seriesIndex % colors.length] + '"' + (strokeWidth !== 2.5 ? ' stroke-width="' + strokeWidth + '"' : "") + "/>";
|
|
117
|
+
}
|
|
118
|
+
svgMarkup += "</svg>";
|
|
119
|
+
var labelMarkup = '<div class="chart-labels">';
|
|
120
|
+
if (showTicks) {
|
|
121
|
+
for (tickIndex = 0; tickIndex < tickCount; tickIndex++)
|
|
122
|
+
labelMarkup += '<span class="chart-tick-y" style="left:' + (padding.left - 4) + "px;top:" + (gridYPositions[tickIndex] - 1) + 'px">' + gridLabels[tickIndex] + "</span>";
|
|
123
|
+
for (pointIndex = 0; pointIndex < pointCount; pointIndex++)
|
|
124
|
+
labelMarkup += '<span class="chart-tick-x" style="left:' + coordsX[pointIndex] + "px;top:" + (padding.top + plotHeight + 6) + 'px">' + labels[pointIndex] + "</span>";
|
|
125
|
+
}
|
|
126
|
+
if (xLabel)
|
|
127
|
+
labelMarkup += '<span class="chart-title" style="left:' + (padding.left + (plotWidth >> 1)) + "px;top:" + (chartHeight - 10) + 'px">' + xLabel + "</span>";
|
|
128
|
+
if (yLabel)
|
|
129
|
+
labelMarkup += '<span class="chart-title" style="left:4px;top:' + (padding.top - 14) + 'px;transform:none;text-align:left">' + yLabel + "</span>";
|
|
130
|
+
labelMarkup += "</div>";
|
|
131
|
+
container.style.cssText = "position:relative;width:" + chartWidth + "px;height:" + chartHeight + "px";
|
|
132
|
+
container.innerHTML = svgMarkup + labelMarkup;
|
|
133
|
+
if (!hoverEnabled) return;
|
|
134
|
+
var svg = container.firstChild;
|
|
135
|
+
var indicator = null;
|
|
136
|
+
var tooltip = null;
|
|
137
|
+
function ensureHoverElements() {
|
|
138
|
+
if (!indicator) {
|
|
139
|
+
indicator = document.createElementNS(SVG_NS, "circle");
|
|
140
|
+
indicator.setAttribute("r", "5");
|
|
141
|
+
indicator.setAttribute("class", "chart-indicator");
|
|
142
|
+
indicator.style.display = "none";
|
|
143
|
+
svg.appendChild(indicator);
|
|
144
|
+
}
|
|
145
|
+
if (!tooltip) {
|
|
146
|
+
tooltip = document.createElement("div");
|
|
147
|
+
tooltip.className = "chart-tooltip";
|
|
148
|
+
container.appendChild(tooltip);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
attachHoverListeners(
|
|
152
|
+
container,
|
|
153
|
+
svg,
|
|
154
|
+
function(mouseX, mouseY) {
|
|
155
|
+
if (mouseX < padding.left || mouseX > padding.left + plotWidth || mouseY < padding.top || mouseY > padding.top + plotHeight) {
|
|
156
|
+
if (indicator) indicator.style.display = "none";
|
|
157
|
+
if (tooltip) tooltip.style.opacity = "0";
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
ensureHoverElements();
|
|
161
|
+
var relativeX = mouseX - padding.left;
|
|
162
|
+
if (relativeX < 0) relativeX = 0;
|
|
163
|
+
if (relativeX > plotWidth) relativeX = plotWidth;
|
|
164
|
+
var nearestIndex = relativeX / stepX + 0.5 | 0;
|
|
165
|
+
if (nearestIndex >= pointCount) nearestIndex = pointCount - 1;
|
|
166
|
+
var bestSeriesIndex = 0;
|
|
167
|
+
var bestDistance = Math.abs(coordsY[nearestIndex] - mouseY);
|
|
168
|
+
for (var searchIndex = 1; searchIndex < data.length; searchIndex++) {
|
|
169
|
+
var distance = Math.abs(coordsY[searchIndex * pointCount + nearestIndex] - mouseY);
|
|
170
|
+
if (distance < bestDistance) {
|
|
171
|
+
bestDistance = distance;
|
|
172
|
+
bestSeriesIndex = searchIndex;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
var pointX = coordsX[bestSeriesIndex * pointCount + nearestIndex];
|
|
176
|
+
var pointY = coordsY[bestSeriesIndex * pointCount + nearestIndex];
|
|
177
|
+
indicator.setAttribute("transform", "translate(" + pointX + "," + pointY + ")");
|
|
178
|
+
indicator.setAttribute("fill", colors[bestSeriesIndex % colors.length]);
|
|
179
|
+
indicator.style.display = "";
|
|
180
|
+
tooltip.innerHTML = "<b>Series " + (bestSeriesIndex + 1) + "</b><br>" + xLabel + ": " + labels[nearestIndex] + "<br>" + yLabel + ": " + data[bestSeriesIndex][nearestIndex];
|
|
181
|
+
tooltip.style.transform = "translate(" + pointX + "px," + (pointY - 40) + "px) translate(-50%,-100%)";
|
|
182
|
+
tooltip.style.opacity = "1";
|
|
183
|
+
},
|
|
184
|
+
function() {
|
|
185
|
+
if (indicator) indicator.style.display = "none";
|
|
186
|
+
if (tooltip) tooltip.style.opacity = "0";
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// src/bar-chart.ts
|
|
192
|
+
function NanoBarChart(containerId, config) {
|
|
193
|
+
var data = config.data;
|
|
194
|
+
var labels = config.labels;
|
|
195
|
+
var xLabel = config.xLabel || "";
|
|
196
|
+
var yLabel = config.yLabel || "";
|
|
197
|
+
var colors = config.colors || ["#215CAF", "#007894", "#00C9A7", "#DE4437", "#F69C3D"];
|
|
198
|
+
var stacked = config.stacked === true;
|
|
199
|
+
var horizontal = config.horizontal === true;
|
|
200
|
+
var showTicks = config.showTicks !== false;
|
|
201
|
+
var showGrid = config.showGrid !== false;
|
|
202
|
+
var gridDashed = config.gridDashed === true;
|
|
203
|
+
var hoverEnabled = config.hover !== false;
|
|
204
|
+
var barGap = config.barGap !== void 0 ? config.barGap : 0.2;
|
|
205
|
+
var padding = { top: 20, right: 30, bottom: 50, left: horizontal ? 80 : 60 };
|
|
206
|
+
var container = document.getElementById(containerId);
|
|
207
|
+
if (!container || !data.length || !data[0].length) return;
|
|
208
|
+
var chartWidth = config.width || container.clientWidth || 600;
|
|
209
|
+
var chartHeight = config.height || 350;
|
|
210
|
+
var plotWidth = chartWidth - padding.left - padding.right;
|
|
211
|
+
var plotHeight = chartHeight - padding.top - padding.bottom;
|
|
212
|
+
var plotBottom = padding.top + plotHeight;
|
|
213
|
+
var seriesCount = data.length;
|
|
214
|
+
var catCount = labels.length;
|
|
215
|
+
var si, ci, val, ti;
|
|
216
|
+
var minVal, maxVal;
|
|
217
|
+
if (stacked) {
|
|
218
|
+
minVal = 0;
|
|
219
|
+
maxVal = 0;
|
|
220
|
+
for (ci = 0; ci < catCount; ci++) {
|
|
221
|
+
var colSum = 0;
|
|
222
|
+
for (si = 0; si < seriesCount; si++) {
|
|
223
|
+
colSum += data[si][ci];
|
|
224
|
+
if (data[si][ci] < minVal) minVal = data[si][ci];
|
|
225
|
+
}
|
|
226
|
+
if (colSum > maxVal) maxVal = colSum;
|
|
227
|
+
}
|
|
228
|
+
} else {
|
|
229
|
+
minVal = maxVal = data[0][0];
|
|
230
|
+
for (si = 0; si < seriesCount; si++)
|
|
231
|
+
for (ci = 0; ci < catCount; ci++) {
|
|
232
|
+
val = data[si][ci];
|
|
233
|
+
if (val < minVal) minVal = val;
|
|
234
|
+
if (val > maxVal) maxVal = val;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (minVal > 0) minVal = 0;
|
|
238
|
+
if (maxVal < 0) maxVal = 0;
|
|
239
|
+
var range = maxVal - minVal || 1;
|
|
240
|
+
maxVal += range * 0.05;
|
|
241
|
+
range = maxVal - minVal;
|
|
242
|
+
var tickCount = 6;
|
|
243
|
+
var tickStep = range / (tickCount - 1);
|
|
244
|
+
var gridPositions = new Int16Array(tickCount);
|
|
245
|
+
var gridLabels = new Array(tickCount);
|
|
246
|
+
var totalBars = seriesCount * catCount;
|
|
247
|
+
var barX = new Int16Array(totalBars);
|
|
248
|
+
var barY = new Int16Array(totalBars);
|
|
249
|
+
var barW = new Int16Array(totalBars);
|
|
250
|
+
var barH = new Int16Array(totalBars);
|
|
251
|
+
var groupSize;
|
|
252
|
+
var zeroY = 0;
|
|
253
|
+
if (horizontal) {
|
|
254
|
+
groupSize = plotHeight / catCount;
|
|
255
|
+
var zeroX = padding.left + (0 - minVal) / range * plotWidth | 0;
|
|
256
|
+
for (ti = 0; ti < tickCount; ti++) {
|
|
257
|
+
var tickVal = minVal + ti * tickStep;
|
|
258
|
+
gridPositions[ti] = padding.left + (tickVal - minVal) / range * plotWidth | 0;
|
|
259
|
+
gridLabels[ti] = tickVal % 1 === 0 ? tickVal.toString() : tickVal.toFixed(1);
|
|
260
|
+
}
|
|
261
|
+
var hBarHeight = groupSize * (1 - barGap) / seriesCount | 0;
|
|
262
|
+
var hGroupStart = (groupSize - hBarHeight * seriesCount) / 2 | 0;
|
|
263
|
+
for (si = 0; si < seriesCount; si++)
|
|
264
|
+
for (ci = 0; ci < catCount; ci++) {
|
|
265
|
+
var idx = si * catCount + ci;
|
|
266
|
+
val = data[si][ci];
|
|
267
|
+
var valX = padding.left + (val - minVal) / range * plotWidth | 0;
|
|
268
|
+
barY[idx] = padding.top + ci * groupSize + hGroupStart + si * hBarHeight | 0;
|
|
269
|
+
barH[idx] = hBarHeight;
|
|
270
|
+
if (val >= 0) {
|
|
271
|
+
barX[idx] = zeroX;
|
|
272
|
+
barW[idx] = valX - zeroX || 1;
|
|
273
|
+
} else {
|
|
274
|
+
barX[idx] = valX;
|
|
275
|
+
barW[idx] = zeroX - valX || 1;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
groupSize = plotWidth / catCount;
|
|
280
|
+
zeroY = padding.top + plotHeight - (0 - minVal) / range * plotHeight | 0;
|
|
281
|
+
for (ti = 0; ti < tickCount; ti++) {
|
|
282
|
+
var tickVal = minVal + ti * tickStep;
|
|
283
|
+
gridPositions[ti] = padding.top + plotHeight - (tickVal - minVal) / range * plotHeight | 0;
|
|
284
|
+
gridLabels[ti] = tickVal % 1 === 0 ? tickVal.toString() : tickVal.toFixed(1);
|
|
285
|
+
}
|
|
286
|
+
if (stacked) {
|
|
287
|
+
var stackBarWidth = groupSize * (1 - barGap) | 0;
|
|
288
|
+
var stackBarOffset = (groupSize - stackBarWidth) / 2 | 0;
|
|
289
|
+
for (ci = 0; ci < catCount; ci++) {
|
|
290
|
+
var cumPos = 0;
|
|
291
|
+
for (si = 0; si < seriesCount; si++) {
|
|
292
|
+
var idx = si * catCount + ci;
|
|
293
|
+
val = data[si][ci];
|
|
294
|
+
var baseY = padding.top + plotHeight - (cumPos - minVal) / range * plotHeight | 0;
|
|
295
|
+
var topY = padding.top + plotHeight - (cumPos + val - minVal) / range * plotHeight | 0;
|
|
296
|
+
cumPos += val;
|
|
297
|
+
barX[idx] = padding.left + ci * groupSize + stackBarOffset | 0;
|
|
298
|
+
barY[idx] = Math.min(baseY, topY);
|
|
299
|
+
barW[idx] = stackBarWidth;
|
|
300
|
+
barH[idx] = Math.abs(topY - baseY) || 1;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
var singleBarWidth = groupSize * (1 - barGap) / seriesCount | 0;
|
|
305
|
+
var groupBarStart = (groupSize - singleBarWidth * seriesCount) / 2 | 0;
|
|
306
|
+
for (si = 0; si < seriesCount; si++)
|
|
307
|
+
for (ci = 0; ci < catCount; ci++) {
|
|
308
|
+
var idx = si * catCount + ci;
|
|
309
|
+
val = data[si][ci];
|
|
310
|
+
var topY = padding.top + plotHeight - (val - minVal) / range * plotHeight | 0;
|
|
311
|
+
barX[idx] = padding.left + ci * groupSize + groupBarStart + si * singleBarWidth | 0;
|
|
312
|
+
barW[idx] = singleBarWidth;
|
|
313
|
+
if (val >= 0) {
|
|
314
|
+
barY[idx] = topY;
|
|
315
|
+
barH[idx] = zeroY - topY || 1;
|
|
316
|
+
} else {
|
|
317
|
+
barY[idx] = zeroY;
|
|
318
|
+
barH[idx] = topY - zeroY || 1;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
var svgMarkup = '<svg xmlns="http://www.w3.org/2000/svg" width="' + chartWidth + '" height="' + chartHeight + '" viewBox="0 0 ' + chartWidth + " " + chartHeight + '" class="chart">';
|
|
324
|
+
if (showGrid) {
|
|
325
|
+
var gridPath = "";
|
|
326
|
+
if (horizontal) {
|
|
327
|
+
for (ti = 0; ti < tickCount; ti++)
|
|
328
|
+
gridPath += "M" + gridPositions[ti] + "," + padding.top + "V" + plotBottom;
|
|
329
|
+
} else {
|
|
330
|
+
for (ti = 0; ti < tickCount; ti++)
|
|
331
|
+
gridPath += "M" + padding.left + "," + gridPositions[ti] + "H" + (padding.left + plotWidth);
|
|
332
|
+
}
|
|
333
|
+
svgMarkup += '<path d="' + gridPath + '" class="chart-grid"' + (gridDashed ? ' stroke-dasharray="3 4"' : "") + "/>";
|
|
334
|
+
}
|
|
335
|
+
if (minVal < 0) {
|
|
336
|
+
if (horizontal) {
|
|
337
|
+
var zx = padding.left + (0 - minVal) / range * plotWidth | 0;
|
|
338
|
+
svgMarkup += '<line x1="' + zx + '" y1="' + padding.top + '" x2="' + zx + '" y2="' + plotBottom + '" stroke="#999" stroke-width="1" shape-rendering="crispEdges"/>';
|
|
339
|
+
} else {
|
|
340
|
+
svgMarkup += '<line x1="' + padding.left + '" y1="' + zeroY + '" x2="' + (padding.left + plotWidth) + '" y2="' + zeroY + '" stroke="#999" stroke-width="1" shape-rendering="crispEdges"/>';
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
for (si = 0; si < seriesCount; si++) {
|
|
344
|
+
var color = colors[si % colors.length];
|
|
345
|
+
for (ci = 0; ci < catCount; ci++) {
|
|
346
|
+
var idx = si * catCount + ci;
|
|
347
|
+
svgMarkup += '<rect x="' + barX[idx] + '" y="' + barY[idx] + '" width="' + barW[idx] + '" height="' + barH[idx] + '" fill="' + color + '" class="chart-bar"/>';
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
svgMarkup += "</svg>";
|
|
351
|
+
var labelMarkup = '<div class="chart-labels">';
|
|
352
|
+
if (showTicks) {
|
|
353
|
+
if (horizontal) {
|
|
354
|
+
for (ti = 0; ti < tickCount; ti++)
|
|
355
|
+
labelMarkup += '<span class="chart-tick-x" style="left:' + gridPositions[ti] + "px;top:" + (plotBottom + 6) + 'px">' + gridLabels[ti] + "</span>";
|
|
356
|
+
for (ci = 0; ci < catCount; ci++) {
|
|
357
|
+
var centerY = padding.top + ci * groupSize + groupSize / 2 | 0;
|
|
358
|
+
labelMarkup += '<span class="chart-tick-y" style="left:' + (padding.left - 4) + "px;top:" + (centerY - 1) + 'px">' + labels[ci] + "</span>";
|
|
359
|
+
}
|
|
360
|
+
} else {
|
|
361
|
+
for (ti = 0; ti < tickCount; ti++)
|
|
362
|
+
labelMarkup += '<span class="chart-tick-y" style="left:' + (padding.left - 4) + "px;top:" + (gridPositions[ti] - 1) + 'px">' + gridLabels[ti] + "</span>";
|
|
363
|
+
for (ci = 0; ci < catCount; ci++) {
|
|
364
|
+
var centerX = padding.left + ci * groupSize + groupSize / 2 | 0;
|
|
365
|
+
labelMarkup += '<span class="chart-tick-x" style="left:' + centerX + "px;top:" + (plotBottom + 6) + 'px">' + labels[ci] + "</span>";
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
if (xLabel)
|
|
370
|
+
labelMarkup += '<span class="chart-title" style="left:' + (padding.left + (plotWidth >> 1)) + "px;top:" + (chartHeight - 10) + 'px">' + xLabel + "</span>";
|
|
371
|
+
if (yLabel)
|
|
372
|
+
labelMarkup += '<span class="chart-title" style="left:4px;top:' + (padding.top - 14) + 'px;transform:none;text-align:left">' + yLabel + "</span>";
|
|
373
|
+
labelMarkup += "</div>";
|
|
374
|
+
container.style.cssText = "position:relative;width:" + chartWidth + "px;height:" + chartHeight + "px";
|
|
375
|
+
container.innerHTML = svgMarkup + labelMarkup;
|
|
376
|
+
if (!hoverEnabled) return;
|
|
377
|
+
var svgEl = container.firstChild;
|
|
378
|
+
var highlight = null;
|
|
379
|
+
var tooltip = null;
|
|
380
|
+
function ensureHover() {
|
|
381
|
+
if (!highlight) {
|
|
382
|
+
highlight = document.createElementNS(SVG_NS, "rect");
|
|
383
|
+
highlight.setAttribute("class", "chart-indicator");
|
|
384
|
+
highlight.setAttribute("fill", "rgba(255,255,255,0.3)");
|
|
385
|
+
highlight.setAttribute("stroke", "#333");
|
|
386
|
+
highlight.setAttribute("stroke-width", "1");
|
|
387
|
+
highlight.style.display = "none";
|
|
388
|
+
svgEl.appendChild(highlight);
|
|
389
|
+
}
|
|
390
|
+
if (!tooltip) {
|
|
391
|
+
tooltip = document.createElement("div");
|
|
392
|
+
tooltip.className = "chart-tooltip";
|
|
393
|
+
container.appendChild(tooltip);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
attachHoverListeners(
|
|
397
|
+
container,
|
|
398
|
+
svgEl,
|
|
399
|
+
function(mx, my) {
|
|
400
|
+
if (mx < padding.left || mx > padding.left + plotWidth || my < padding.top || my > plotBottom) {
|
|
401
|
+
if (highlight) highlight.style.display = "none";
|
|
402
|
+
if (tooltip) tooltip.style.opacity = "0";
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
var nearCat;
|
|
406
|
+
if (horizontal) {
|
|
407
|
+
nearCat = (my - padding.top) / groupSize | 0;
|
|
408
|
+
} else {
|
|
409
|
+
nearCat = (mx - padding.left) / groupSize | 0;
|
|
410
|
+
}
|
|
411
|
+
if (nearCat >= catCount) nearCat = catCount - 1;
|
|
412
|
+
if (nearCat < 0) nearCat = 0;
|
|
413
|
+
var bestIdx = -1;
|
|
414
|
+
var bestDist = Infinity;
|
|
415
|
+
for (si = 0; si < seriesCount; si++) {
|
|
416
|
+
var idx2 = si * catCount + nearCat;
|
|
417
|
+
var cx = barX[idx2] + (barW[idx2] >> 1);
|
|
418
|
+
var cy = barY[idx2] + (barH[idx2] >> 1);
|
|
419
|
+
var dx = mx - cx, dy = my - cy;
|
|
420
|
+
var dist = dx * dx + dy * dy;
|
|
421
|
+
if (dist < bestDist) {
|
|
422
|
+
bestDist = dist;
|
|
423
|
+
bestIdx = idx2;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (bestIdx === -1) return;
|
|
427
|
+
ensureHover();
|
|
428
|
+
var bsi = bestIdx / catCount | 0;
|
|
429
|
+
var bci = bestIdx % catCount;
|
|
430
|
+
highlight.setAttribute("x", "" + barX[bestIdx]);
|
|
431
|
+
highlight.setAttribute("y", "" + barY[bestIdx]);
|
|
432
|
+
highlight.setAttribute("width", "" + barW[bestIdx]);
|
|
433
|
+
highlight.setAttribute("height", "" + barH[bestIdx]);
|
|
434
|
+
highlight.setAttribute("stroke", colors[bsi % colors.length]);
|
|
435
|
+
highlight.style.display = "";
|
|
436
|
+
var tipX, tipY;
|
|
437
|
+
if (horizontal) {
|
|
438
|
+
tipX = barX[bestIdx] + barW[bestIdx];
|
|
439
|
+
tipY = barY[bestIdx] + (barH[bestIdx] >> 1);
|
|
440
|
+
tooltip.style.transform = "translate(" + (tipX + 8) + "px," + tipY + "px) translate(0,-50%)";
|
|
441
|
+
} else {
|
|
442
|
+
tipX = barX[bestIdx] + (barW[bestIdx] >> 1);
|
|
443
|
+
tipY = barY[bestIdx];
|
|
444
|
+
tooltip.style.transform = "translate(" + tipX + "px," + (tipY - 10) + "px) translate(-50%,-100%)";
|
|
445
|
+
}
|
|
446
|
+
tooltip.innerHTML = (seriesCount > 1 ? "<b>Series " + (bsi + 1) + "</b><br>" : "") + labels[bci] + ": " + data[bsi][bci];
|
|
447
|
+
tooltip.style.opacity = "1";
|
|
448
|
+
},
|
|
449
|
+
function() {
|
|
450
|
+
if (highlight) highlight.style.display = "none";
|
|
451
|
+
if (tooltip) tooltip.style.opacity = "0";
|
|
452
|
+
}
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// src/pie-chart.ts
|
|
457
|
+
var TWO_PI = Math.PI * 2;
|
|
458
|
+
function NanoPieChart(containerId, config) {
|
|
459
|
+
var data = config.data;
|
|
460
|
+
var labels = config.labels;
|
|
461
|
+
var colors = config.colors || ["#215CAF", "#007894", "#00C9A7", "#DE4437", "#F69C3D", "#0d3a6e", "#003d4a", "#006651"];
|
|
462
|
+
var isDonut = config.donut === true;
|
|
463
|
+
var donutWidth = config.donutWidth !== void 0 ? config.donutWidth : 60;
|
|
464
|
+
var showLabels = config.showLabels !== false;
|
|
465
|
+
var hoverEnabled = config.hover !== false;
|
|
466
|
+
var container = document.getElementById(containerId);
|
|
467
|
+
if (!container) return;
|
|
468
|
+
var chartWidth = config.width || container.clientWidth || 350;
|
|
469
|
+
var chartHeight = config.height || chartWidth;
|
|
470
|
+
var sliceCount = data.length;
|
|
471
|
+
var centerX = chartWidth / 2;
|
|
472
|
+
var centerY = chartHeight / 2;
|
|
473
|
+
var outerR = Math.min(centerX, centerY) - (showLabels ? 40 : 10);
|
|
474
|
+
var innerR = isDonut ? Math.max(outerR - donutWidth, 0) : 0;
|
|
475
|
+
var i;
|
|
476
|
+
var total = 0;
|
|
477
|
+
for (i = 0; i < sliceCount; i++) total += data[i];
|
|
478
|
+
var startAngles = new Float64Array(sliceCount);
|
|
479
|
+
var endAngles = new Float64Array(sliceCount);
|
|
480
|
+
var percentages = new Float64Array(sliceCount);
|
|
481
|
+
var angle = -Math.PI / 2;
|
|
482
|
+
for (i = 0; i < sliceCount; i++) {
|
|
483
|
+
startAngles[i] = angle;
|
|
484
|
+
var sliceAngle = total > 0 ? data[i] / total * TWO_PI : 0;
|
|
485
|
+
percentages[i] = total > 0 ? data[i] / total * 100 : 0;
|
|
486
|
+
angle += sliceAngle;
|
|
487
|
+
endAngles[i] = angle;
|
|
488
|
+
}
|
|
489
|
+
var svgMarkup = '<svg xmlns="http://www.w3.org/2000/svg" width="' + chartWidth + '" height="' + chartHeight + '" viewBox="0 0 ' + chartWidth + " " + chartHeight + '" class="chart">';
|
|
490
|
+
for (i = 0; i < sliceCount; i++) {
|
|
491
|
+
if (endAngles[i] - startAngles[i] < 9e-3) continue;
|
|
492
|
+
var color = colors[i % colors.length];
|
|
493
|
+
var cos1 = Math.cos(startAngles[i]), sin1 = Math.sin(startAngles[i]);
|
|
494
|
+
var cos2 = Math.cos(endAngles[i]), sin2 = Math.sin(endAngles[i]);
|
|
495
|
+
var largeArc = endAngles[i] - startAngles[i] > Math.PI ? 1 : 0;
|
|
496
|
+
var ox1 = centerX + outerR * cos1, oy1 = centerY + outerR * sin1;
|
|
497
|
+
var ox2 = centerX + outerR * cos2, oy2 = centerY + outerR * sin2;
|
|
498
|
+
var pathData;
|
|
499
|
+
if (isDonut) {
|
|
500
|
+
var ix1 = centerX + innerR * cos1, iy1 = centerY + innerR * sin1;
|
|
501
|
+
var ix2 = centerX + innerR * cos2, iy2 = centerY + innerR * sin2;
|
|
502
|
+
pathData = "M" + ox1.toFixed(2) + "," + oy1.toFixed(2) + "A" + outerR + "," + outerR + " 0 " + largeArc + " 1 " + ox2.toFixed(2) + "," + oy2.toFixed(2) + "L" + ix2.toFixed(2) + "," + iy2.toFixed(2) + "A" + innerR + "," + innerR + " 0 " + largeArc + " 0 " + ix1.toFixed(2) + "," + iy1.toFixed(2) + "Z";
|
|
503
|
+
} else {
|
|
504
|
+
pathData = "M" + centerX.toFixed(2) + "," + centerY.toFixed(2) + "L" + ox1.toFixed(2) + "," + oy1.toFixed(2) + "A" + outerR + "," + outerR + " 0 " + largeArc + " 1 " + ox2.toFixed(2) + "," + oy2.toFixed(2) + "Z";
|
|
505
|
+
}
|
|
506
|
+
svgMarkup += '<path d="' + pathData + '" fill="' + color + '" class="chart-slice"/>';
|
|
507
|
+
}
|
|
508
|
+
svgMarkup += "</svg>";
|
|
509
|
+
var labelMarkup = '<div class="chart-labels">';
|
|
510
|
+
if (showLabels) {
|
|
511
|
+
var labelR = outerR + 20;
|
|
512
|
+
for (i = 0; i < sliceCount; i++) {
|
|
513
|
+
if (percentages[i] < 5) continue;
|
|
514
|
+
var midAngle = (startAngles[i] + endAngles[i]) / 2;
|
|
515
|
+
var lx = centerX + labelR * Math.cos(midAngle);
|
|
516
|
+
var ly = centerY + labelR * Math.sin(midAngle);
|
|
517
|
+
labelMarkup += '<span style="left:' + lx.toFixed(1) + "px;top:" + ly.toFixed(1) + 'px;transform:translate(-50%,-50%);text-align:center">' + labels[i] + " " + percentages[i].toFixed(0) + "%</span>";
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
labelMarkup += "</div>";
|
|
521
|
+
container.style.cssText = "position:relative;width:" + chartWidth + "px;height:" + chartHeight + "px";
|
|
522
|
+
container.innerHTML = svgMarkup + labelMarkup;
|
|
523
|
+
if (!hoverEnabled) return;
|
|
524
|
+
var svgEl = container.firstChild;
|
|
525
|
+
var highlight = null;
|
|
526
|
+
var tooltip = null;
|
|
527
|
+
var lastSlice = -1;
|
|
528
|
+
function ensureHover() {
|
|
529
|
+
if (!highlight) {
|
|
530
|
+
highlight = document.createElementNS(SVG_NS, "path");
|
|
531
|
+
highlight.setAttribute("class", "chart-indicator");
|
|
532
|
+
highlight.setAttribute("fill", "none");
|
|
533
|
+
highlight.setAttribute("stroke", "#fff");
|
|
534
|
+
highlight.setAttribute("stroke-width", "3");
|
|
535
|
+
highlight.style.display = "none";
|
|
536
|
+
svgEl.appendChild(highlight);
|
|
537
|
+
}
|
|
538
|
+
if (!tooltip) {
|
|
539
|
+
tooltip = document.createElement("div");
|
|
540
|
+
tooltip.className = "chart-tooltip";
|
|
541
|
+
container.appendChild(tooltip);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
attachHoverListeners(
|
|
545
|
+
container,
|
|
546
|
+
svgEl,
|
|
547
|
+
function(mx, my) {
|
|
548
|
+
var dx = mx - centerX, dy = my - centerY;
|
|
549
|
+
var dist = Math.sqrt(dx * dx + dy * dy);
|
|
550
|
+
if (dist > outerR || dist < innerR) {
|
|
551
|
+
if (highlight) highlight.style.display = "none";
|
|
552
|
+
if (tooltip) tooltip.style.opacity = "0";
|
|
553
|
+
lastSlice = -1;
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
var hoverAngle = Math.atan2(dy, dx);
|
|
557
|
+
if (hoverAngle < -Math.PI / 2) hoverAngle += TWO_PI;
|
|
558
|
+
var hitSlice = -1;
|
|
559
|
+
for (var s = 0; s < sliceCount; s++) {
|
|
560
|
+
if (hoverAngle >= startAngles[s] && hoverAngle < endAngles[s]) {
|
|
561
|
+
hitSlice = s;
|
|
562
|
+
break;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (hitSlice === -1) return;
|
|
566
|
+
ensureHover();
|
|
567
|
+
if (hitSlice !== lastSlice) {
|
|
568
|
+
lastSlice = hitSlice;
|
|
569
|
+
var cos12 = Math.cos(startAngles[hitSlice]), sin12 = Math.sin(startAngles[hitSlice]);
|
|
570
|
+
var cos22 = Math.cos(endAngles[hitSlice]), sin22 = Math.sin(endAngles[hitSlice]);
|
|
571
|
+
var largeArc2 = endAngles[hitSlice] - startAngles[hitSlice] > Math.PI ? 1 : 0;
|
|
572
|
+
var ox12 = centerX + outerR * cos12, oy12 = centerY + outerR * sin12;
|
|
573
|
+
var ox22 = centerX + outerR * cos22, oy22 = centerY + outerR * sin22;
|
|
574
|
+
var hPath;
|
|
575
|
+
if (isDonut) {
|
|
576
|
+
var ix12 = centerX + innerR * cos12, iy12 = centerY + innerR * sin12;
|
|
577
|
+
var ix22 = centerX + innerR * cos22, iy22 = centerY + innerR * sin22;
|
|
578
|
+
hPath = "M" + ox12.toFixed(2) + "," + oy12.toFixed(2) + "A" + outerR + "," + outerR + " 0 " + largeArc2 + " 1 " + ox22.toFixed(2) + "," + oy22.toFixed(2) + "L" + ix22.toFixed(2) + "," + iy22.toFixed(2) + "A" + innerR + "," + innerR + " 0 " + largeArc2 + " 0 " + ix12.toFixed(2) + "," + iy12.toFixed(2) + "Z";
|
|
579
|
+
} else {
|
|
580
|
+
hPath = "M" + centerX.toFixed(2) + "," + centerY.toFixed(2) + "L" + ox12.toFixed(2) + "," + oy12.toFixed(2) + "A" + outerR + "," + outerR + " 0 " + largeArc2 + " 1 " + ox22.toFixed(2) + "," + oy22.toFixed(2) + "Z";
|
|
581
|
+
}
|
|
582
|
+
highlight.setAttribute("d", hPath);
|
|
583
|
+
}
|
|
584
|
+
highlight.style.display = "";
|
|
585
|
+
tooltip.innerHTML = "<b>" + labels[hitSlice] + "</b><br>" + data[hitSlice] + " (" + percentages[hitSlice].toFixed(1) + "%)";
|
|
586
|
+
tooltip.style.transform = "translate(" + mx + "px," + (my - 40) + "px) translate(-50%,-100%)";
|
|
587
|
+
tooltip.style.opacity = "1";
|
|
588
|
+
},
|
|
589
|
+
function() {
|
|
590
|
+
if (highlight) highlight.style.display = "none";
|
|
591
|
+
if (tooltip) tooltip.style.opacity = "0";
|
|
592
|
+
lastSlice = -1;
|
|
593
|
+
}
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// src/index.ts
|
|
598
|
+
if (typeof window !== "undefined") {
|
|
599
|
+
window.NanoLineChart = NanoLineChart;
|
|
600
|
+
window.NanoBarChart = NanoBarChart;
|
|
601
|
+
window.NanoPieChart = NanoPieChart;
|
|
602
|
+
}
|
|
603
|
+
})();
|
|
604
|
+
//# sourceMappingURL=nanocharts.global.js.map
|