@katlux/block-charts 0.1.1-alpha.119 → 0.1.1-alpha.121

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.
@@ -53,7 +53,7 @@
53
53
  </template>
54
54
 
55
55
  <script setup>
56
- import { ref, watch, computed, onMounted, nextTick, useId, isRef } from "vue";
56
+ import { ref, watch, computed, onMounted, nextTick, useId } from "vue";
57
57
  import { useChartCanvas } from "../../composables/useChartCanvas";
58
58
  import { useChartSvg } from "../../composables/useChartSvg";
59
59
  import { useChartData } from "../../composables/useChartData";
@@ -61,6 +61,8 @@ import { useChartViewport } from "../../composables/useChartViewport";
61
61
  import { useChartAnimation, easeOut } from "../../composables/useChartAnimation";
62
62
  import { useChartExport } from "../../composables/useChartExport";
63
63
  import { cleanNumber, safeMax, safeMin } from "../../composables/chartUtils";
64
+ import { isChartProviderLoading } from "../../utils/canvasDrawHelpers";
65
+ import { drawBarChartScene } from "../../utils/barChartCanvas";
64
66
  const clipId = useId();
65
67
  const props = defineProps({
66
68
  dataProvider: { type: null, required: false },
@@ -89,13 +91,7 @@ const dataRef = computed(() => props.dataProvider);
89
91
  const { items, loading: dataLoading } = useChartData(dataRef);
90
92
  const { progress, animate } = useChartAnimation();
91
93
  const { exportPng, exportSvg: exportSvgFile } = useChartExport();
92
- const isLoading = computed(() => {
93
- const dp = props.dataProvider;
94
- if (!dp) return dataLoading.value;
95
- const rawLoading = dp.loading !== void 0 ? isRef(dp.loading) ? dp.loading.value : dp.loading?.value ?? dp.loading : false;
96
- const rawInitial = dp.initialLoad !== void 0 ? isRef(dp.initialLoad) ? dp.initialLoad.value : dp.initialLoad?.value ?? dp.initialLoad : false;
97
- return Boolean(dataLoading.value || rawLoading || rawInitial);
98
- });
94
+ const isLoading = computed(() => isChartProviderLoading(props.dataProvider, dataLoading));
99
95
  const tooltipStyle = computed(() => ({
100
96
  left: `${tooltipState.value.x}px`,
101
97
  top: `${tooltipState.value.y}px`
@@ -191,294 +187,29 @@ const hoveredBar = computed(() => {
191
187
  const draw = () => {
192
188
  if (!ctx.value) return;
193
189
  clear(props.backgroundColor);
194
- const c = ctx.value;
195
190
  const p = props.animated ? easeOut(progress.value) : 1;
196
- const bars = [];
197
- const isHorizontal = props.orientation === "horizontal";
198
- const catCount = visibleCategories.value.length;
199
- const gCount = groups.value.length || 1;
200
- if (isHorizontal) {
201
- const catHeight = plotHeight.value / (catCount || 1);
202
- let barHeight = catHeight * 0.7;
203
- if (props.mode === "clustered" && gCount > 1) {
204
- barHeight = catHeight * 0.8 / gCount;
205
- }
206
- const xZero = plotLeft + (0 - minValue.value) / valueRange.value * plotWidth.value;
207
- c.save();
208
- c.strokeStyle = "rgba(100,100,120,0.1)";
209
- c.lineWidth = 1;
210
- c.fillStyle = "rgba(150,150,170,0.8)";
211
- c.font = "11px system-ui, sans-serif";
212
- c.textAlign = "center";
213
- for (let i = 0; i <= 5; i++) {
214
- const val = minValue.value + valueRange.value / 5 * i;
215
- const x = plotLeft + (val - minValue.value) / valueRange.value * plotWidth.value;
216
- c.beginPath();
217
- c.moveTo(x, plotTop);
218
- c.lineTo(x, plotTop + plotHeight.value);
219
- c.stroke();
220
- c.fillText(val.toFixed(0), x, plotTop + plotHeight.value + 16);
221
- }
222
- if (minValue.value < 0) {
223
- c.strokeStyle = "rgba(150,150,170,0.4)";
224
- c.beginPath();
225
- c.moveTo(xZero, plotTop);
226
- c.lineTo(xZero, plotTop + plotHeight.value);
227
- c.stroke();
228
- }
229
- c.strokeStyle = "rgba(100,100,120,0.05)";
230
- for (let i = 0; i <= catCount; i++) {
231
- const y = plotTop + i * catHeight;
232
- c.beginPath();
233
- c.moveTo(plotLeft, y);
234
- c.lineTo(plotLeft + plotWidth.value, y);
235
- c.stroke();
236
- }
237
- c.strokeStyle = "rgba(150,150,170,0.5)";
238
- c.beginPath();
239
- c.moveTo(plotLeft, plotTop + plotHeight.value);
240
- c.lineTo(plotLeft + plotWidth.value, plotTop + plotHeight.value);
241
- c.stroke();
242
- c.beginPath();
243
- c.moveTo(plotLeft, plotTop);
244
- c.lineTo(plotLeft, plotTop + plotHeight.value);
245
- c.stroke();
246
- c.restore();
247
- c.save();
248
- c.beginPath();
249
- c.rect(plotLeft, plotTop, plotWidth.value, plotHeight.value);
250
- c.clip();
251
- visibleCategories.value.forEach((cat, catIdx) => {
252
- const catItems = categoryMap.value.get(cat) || [];
253
- const catY = plotTop + catIdx * catHeight;
254
- if (props.mode === "stacked") {
255
- let xOffsetPos = 0;
256
- let xOffsetNeg = 0;
257
- groups.value.forEach((group, gIdx) => {
258
- const item = catItems.find((r) => String(r[props.groupField] ?? "default") === group);
259
- if (!item) return;
260
- const rawVal = cleanNumber(item[props.valueField]);
261
- const bw = Math.abs(rawVal) * p / valueRange.value * plotWidth.value;
262
- let bx = xZero;
263
- if (rawVal >= 0) {
264
- bx = xZero + xOffsetPos * p / valueRange.value * plotWidth.value;
265
- xOffsetPos += rawVal;
266
- } else {
267
- xOffsetNeg += Math.abs(rawVal);
268
- bx = xZero - xOffsetNeg * p / valueRange.value * plotWidth.value;
269
- }
270
- const by = catY + (catHeight - barHeight) / 2;
271
- const isHovered = hoveredIndex.value === bars.length;
272
- const color = props.colors[gIdx % props.colors.length];
273
- c.fillStyle = isHovered ? color : color + "dd";
274
- c.fillRect(bx, by, bw, barHeight);
275
- c.strokeStyle = "rgba(255,255,255,0.3)";
276
- c.lineWidth = 1;
277
- c.strokeRect(bx, by, bw, barHeight);
278
- bars.push({ x: bx, y: by, w: bw, h: barHeight, item, label: `${cat} (${group}): ${item[props.valueField]}` });
279
- });
280
- } else if (props.mode === "simple") {
281
- const item = catItems[0];
282
- if (item) {
283
- const isHovered = hoveredIndex.value === bars.length;
284
- const rawVal = cleanNumber(item[props.valueField]);
285
- const bw = Math.abs(rawVal) * p / valueRange.value * plotWidth.value;
286
- const bx = rawVal >= 0 ? xZero : xZero - bw;
287
- const by = catY + (catHeight - barHeight) / 2;
288
- const color = props.colors[0] || "#6366f1";
289
- c.fillStyle = isHovered ? color : color + "dd";
290
- c.fillRect(bx, by, bw, barHeight);
291
- bars.push({ x: bx, y: by, w: bw, h: barHeight, item, label: `${cat}: ${item[props.valueField]}` });
292
- }
293
- } else {
294
- groups.value.forEach((group, gIdx) => {
295
- const item = catItems.find((r) => String(r[props.groupField] ?? "default") === group);
296
- if (!item) return;
297
- const isHovered = hoveredIndex.value === bars.length;
298
- const rawVal = cleanNumber(item[props.valueField]);
299
- const bw = Math.abs(rawVal) * p / valueRange.value * plotWidth.value;
300
- const bx = rawVal >= 0 ? xZero : xZero - bw;
301
- const gCountTotal = groups.value.length;
302
- const totalBarsH = gCountTotal * barHeight + (gCountTotal - 1) * 2;
303
- const startY = catY + (catHeight - totalBarsH) / 2;
304
- const by = startY + gIdx * (barHeight + 2);
305
- const color = props.colors[gIdx % props.colors.length];
306
- c.fillStyle = isHovered ? color : color + "dd";
307
- c.fillRect(bx, by, bw, barHeight);
308
- bars.push({ x: bx, y: by, w: bw, h: barHeight, item, label: `${cat} (${group}): ${item[props.valueField]}` });
309
- });
310
- }
311
- });
312
- c.restore();
313
- c.save();
314
- c.fillStyle = "rgba(150,150,170,0.9)";
315
- c.font = "11px system-ui, sans-serif";
316
- c.textAlign = "right";
317
- visibleCategories.value.forEach((cat, catIdx) => {
318
- const catY = plotTop + catIdx * catHeight;
319
- const label = cat.length > 10 ? cat.slice(0, 10) + "\u2026" : cat;
320
- c.fillText(label, plotLeft - 8, catY + catHeight / 2 + 4);
321
- });
322
- c.restore();
323
- } else {
324
- const catWidth = plotWidth.value / (catCount || 1);
325
- let barWidth = catWidth * 0.7;
326
- if (props.mode === "clustered" && gCount > 1) {
327
- barWidth = catWidth * 0.8 / gCount;
328
- }
329
- const yZero = plotTop + plotHeight.value - (0 - minValue.value) / valueRange.value * plotHeight.value;
330
- c.save();
331
- c.strokeStyle = "rgba(100,100,120,0.1)";
332
- c.lineWidth = 1;
333
- c.fillStyle = "rgba(150,150,170,0.8)";
334
- c.font = "11px system-ui, sans-serif";
335
- c.textAlign = "right";
336
- for (let i = 0; i <= 5; i++) {
337
- const val = minValue.value + valueRange.value / 5 * i;
338
- const y = plotTop + plotHeight.value - (val - minValue.value) / valueRange.value * plotHeight.value;
339
- c.beginPath();
340
- c.moveTo(plotLeft, y);
341
- c.lineTo(plotLeft + plotWidth.value, y);
342
- c.stroke();
343
- c.fillText(val.toFixed(0), plotLeft - 8, y + 4);
344
- }
345
- if (minValue.value < 0) {
346
- c.strokeStyle = "rgba(150,150,170,0.4)";
347
- c.beginPath();
348
- c.moveTo(plotLeft, yZero);
349
- c.lineTo(plotLeft + plotWidth.value, yZero);
350
- c.stroke();
351
- }
352
- c.strokeStyle = "rgba(100,100,120,0.05)";
353
- for (let i = 0; i <= catCount; i++) {
354
- const x = plotLeft + i * catWidth;
355
- c.beginPath();
356
- c.moveTo(x, plotTop);
357
- c.lineTo(x, plotTop + plotHeight.value);
358
- c.stroke();
359
- }
360
- c.strokeStyle = "rgba(150,150,170,0.5)";
361
- c.beginPath();
362
- c.moveTo(plotLeft, plotTop + plotHeight.value);
363
- c.lineTo(plotLeft + plotWidth.value, plotTop + plotHeight.value);
364
- c.stroke();
365
- c.beginPath();
366
- c.moveTo(plotLeft, plotTop);
367
- c.lineTo(plotLeft, plotTop + plotHeight.value);
368
- c.stroke();
369
- c.restore();
370
- c.save();
371
- c.beginPath();
372
- c.rect(plotLeft, plotTop, plotWidth.value, plotHeight.value);
373
- c.clip();
374
- visibleCategories.value.forEach((cat, catIdx) => {
375
- const catItems = categoryMap.value.get(cat) || [];
376
- const catX = plotLeft + catIdx * catWidth;
377
- if (props.mode === "stacked") {
378
- let yOffsetPos = 0;
379
- let yOffsetNeg = 0;
380
- groups.value.forEach((group, gIdx) => {
381
- const item = catItems.find((r) => String(r[props.groupField] ?? "default") === group);
382
- if (!item) return;
383
- const rawVal = cleanNumber(item[props.valueField]);
384
- const bh = Math.abs(rawVal) * p / valueRange.value * plotHeight.value;
385
- const bx = catX + (catWidth - barWidth) / 2;
386
- let by = yZero;
387
- if (rawVal >= 0) {
388
- yOffsetPos += rawVal;
389
- by = yZero - yOffsetPos * p / valueRange.value * plotHeight.value;
390
- } else {
391
- by = yZero + yOffsetNeg * p / valueRange.value * plotHeight.value;
392
- yOffsetNeg += Math.abs(rawVal);
393
- }
394
- const isHovered = hoveredIndex.value === bars.length;
395
- const color = props.colors[gIdx % props.colors.length];
396
- c.fillStyle = isHovered ? color : color + "dd";
397
- c.fillRect(bx, by, barWidth, bh);
398
- c.strokeStyle = "rgba(255,255,255,0.3)";
399
- c.lineWidth = 1;
400
- c.strokeRect(bx, by, barWidth, bh);
401
- bars.push({ x: bx, y: by, w: barWidth, h: bh, item, label: `${cat} (${group}): ${item[props.valueField]}` });
402
- });
403
- } else if (props.mode === "simple") {
404
- const item = catItems[0];
405
- if (item) {
406
- const isHovered = hoveredIndex.value === bars.length;
407
- const rawVal = cleanNumber(item[props.valueField]);
408
- const bh = Math.abs(rawVal) * p / valueRange.value * plotHeight.value;
409
- const bx = catX + (catWidth - barWidth) / 2;
410
- const by = rawVal >= 0 ? yZero - bh : yZero;
411
- const color = props.colors[0] || "#6366f1";
412
- c.fillStyle = isHovered ? color : color + "dd";
413
- c.fillRect(bx, by, barWidth, bh);
414
- bars.push({ x: bx, y: by, w: barWidth, h: bh, item, label: `${cat}: ${item[props.valueField]}` });
415
- }
416
- } else {
417
- groups.value.forEach((group, gIdx) => {
418
- const item = catItems.find((r) => String(r[props.groupField] ?? "default") === group);
419
- if (!item) return;
420
- const isHovered = hoveredIndex.value === bars.length;
421
- const rawVal = cleanNumber(item[props.valueField]);
422
- const bh = Math.abs(rawVal) * p / valueRange.value * plotHeight.value;
423
- const gCountTotal = groups.value.length;
424
- const totalBarsW = gCountTotal * barWidth + (gCountTotal - 1) * 2;
425
- const startX = catX + (catWidth - totalBarsW) / 2;
426
- const bx = startX + gIdx * (barWidth + 2);
427
- const by = rawVal >= 0 ? yZero - bh : yZero;
428
- const color = props.colors[gIdx % props.colors.length];
429
- c.fillStyle = isHovered ? color : color + "dd";
430
- c.fillRect(bx, by, barWidth, bh);
431
- bars.push({ x: bx, y: by, w: barWidth, h: bh, item, label: `${cat} (${group}): ${item[props.valueField]}` });
432
- });
433
- }
434
- });
435
- c.restore();
436
- c.save();
437
- c.fillStyle = "rgba(150,150,170,0.9)";
438
- c.font = "11px system-ui, sans-serif";
439
- c.textAlign = "center";
440
- visibleCategories.value.forEach((cat, catIdx) => {
441
- const catX = plotLeft + catIdx * catWidth;
442
- const label = cat.length > 10 ? cat.slice(0, 10) + "\u2026" : cat;
443
- c.fillText(label, catX + catWidth / 2, plotTop + plotHeight.value + 18);
444
- });
445
- c.restore();
446
- }
447
- c.save();
448
- c.fillStyle = "rgba(120,120,140,0.8)";
449
- if (props.xAxisTitle) {
450
- c.textAlign = "center";
451
- c.fillText(props.xAxisTitle, plotLeft + plotWidth.value / 2, plotTop + plotHeight.value + 38);
452
- }
453
- if (props.yAxisTitle) {
454
- c.translate(plotLeft - 40, plotTop + plotHeight.value / 2);
455
- c.rotate(-Math.PI / 2);
456
- c.textAlign = "center";
457
- c.fillText(props.yAxisTitle, 0, 0);
458
- }
459
- c.restore();
460
- hitBars.value = bars;
461
- if (props.showLegend) {
462
- drawLegend(c);
463
- }
464
- };
465
- const drawLegend = (c) => {
466
- c.save();
467
- c.font = "11px system-ui, sans-serif";
468
- c.textAlign = "left";
469
- c.textBaseline = "middle";
470
- let xOffset = plotLeft;
471
- const yPos = plotTop + plotHeight.value + 35;
472
- groups.value.forEach((group, i) => {
473
- const color = props.colors[i % props.colors.length];
474
- c.fillStyle = color;
475
- c.fillRect(xOffset, yPos - 4, 10, 8);
476
- c.fillStyle = "rgba(150,150,170,0.9)";
477
- const label = group === "default" ? "Series" : group;
478
- c.fillText(label, xOffset + 15, yPos);
479
- xOffset += c.measureText(label).width + 35;
191
+ hitBars.value = drawBarChartScene({
192
+ ctx: ctx.value,
193
+ plotLeft,
194
+ plotTop,
195
+ plotWidth: plotWidth.value,
196
+ plotHeight: plotHeight.value,
197
+ progress: p,
198
+ orientation: props.orientation,
199
+ mode: props.mode,
200
+ visibleCategories: visibleCategories.value,
201
+ groups: groups.value,
202
+ categoryMap: categoryMap.value,
203
+ groupField: props.groupField,
204
+ valueField: props.valueField,
205
+ minValue: minValue.value,
206
+ valueRange: valueRange.value,
207
+ colors: props.colors,
208
+ hoveredIndex: hoveredIndex.value,
209
+ showLegend: props.showLegend,
210
+ xAxisTitle: props.xAxisTitle,
211
+ yAxisTitle: props.yAxisTitle
480
212
  });
481
- c.restore();
482
213
  };
483
214
  watch(progress, draw);
484
215
  watch([scale, panOffset, hoveredIndex, resizeTick, width, height, () => props.mode, () => props.orientation], draw);
@@ -38,6 +38,8 @@ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {
38
38
  y: number;
39
39
  }) => any) | undefined;
40
40
  }>, {
41
+ xAxisTitle: string;
42
+ yAxisTitle: string;
41
43
  animated: boolean;
42
44
  zoomable: boolean;
43
45
  wheelNavigation: boolean;
@@ -45,13 +47,11 @@ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {
45
47
  colors: string[];
46
48
  backgroundColor: string;
47
49
  showLegend: boolean;
48
- xAxisTitle: string;
49
- yAxisTitle: string;
50
- categoryField: string;
51
50
  valueField: string;
51
+ mode: "simple" | "clustered" | "stacked";
52
52
  groupField: string;
53
+ categoryField: string;
53
54
  orientation: "vertical" | "horizontal";
54
- mode: "simple" | "clustered" | "stacked";
55
55
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
56
56
  declare const _default: typeof __VLS_export;
57
57
  export default _default;
@@ -21,14 +21,14 @@ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {
21
21
  "onClick-point"?: ((item: any, index: number) => any) | undefined;
22
22
  "onHover-point"?: ((item: any, index: number) => any) | undefined;
23
23
  }>, {
24
+ xAxisTitle: string;
25
+ yAxisTitle: string;
24
26
  colors: string[];
25
27
  backgroundColor: string;
26
28
  showLegend: boolean;
27
- xAxisTitle: string;
28
- yAxisTitle: string;
29
- valueField: string;
30
29
  rowField: string;
31
30
  columnField: string;
31
+ valueField: string;
32
32
  colorLow: string;
33
33
  colorHigh: string;
34
34
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
@@ -21,14 +21,14 @@ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {
21
21
  "onClick-point"?: ((item: any, index: number) => any) | undefined;
22
22
  "onHover-point"?: ((item: any, index: number) => any) | undefined;
23
23
  }>, {
24
+ xAxisTitle: string;
25
+ yAxisTitle: string;
24
26
  colors: string[];
25
27
  backgroundColor: string;
26
28
  showLegend: boolean;
27
- xAxisTitle: string;
28
- yAxisTitle: string;
29
- valueField: string;
30
29
  rowField: string;
31
30
  columnField: string;
31
+ valueField: string;
32
32
  colorLow: string;
33
33
  colorHigh: string;
34
34
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
@@ -36,6 +36,8 @@ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {
36
36
  y: number;
37
37
  }) => any) | undefined;
38
38
  }>, {
39
+ xAxisTitle: string;
40
+ yAxisTitle: string;
39
41
  xField: string;
40
42
  yField: string;
41
43
  seriesField: string;
@@ -46,8 +48,6 @@ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {
46
48
  colors: string[];
47
49
  backgroundColor: string;
48
50
  showLegend: boolean;
49
- xAxisTitle: string;
50
- yAxisTitle: string;
51
51
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
52
52
  declare const _default: typeof __VLS_export;
53
53
  export default _default;