@visactor/vseed 0.0.4 → 0.0.5

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/dist/index.js ADDED
@@ -0,0 +1,1134 @@
1
+ import { clone as external_remeda_clone, mergeDeep as external_remeda_mergeDeep, pick as external_remeda_pick } from "remeda";
2
+ import { z } from "zod";
3
+ const FoldMeasureName = '__MeaName__';
4
+ const FoldMeasureValue = '__MeaValue__';
5
+ const FoldMeasureId = '__MeaId__';
6
+ const FoldDimensionGroup = '__DimGroup__';
7
+ const Separator = '-';
8
+ const unfoldDimensions = (dataset, dimensions, measures, unfoldStartIndex = 0, foldGroupName = FoldDimensionGroup, dimensionsSeparator = Separator)=>{
9
+ if (unfoldStartIndex < 0 || unfoldStartIndex >= dimensions.length) throw new Error('unfoldStartIndex is out of range');
10
+ const dimensionsToBeUnfolded = dimensions.slice(unfoldStartIndex);
11
+ const unfoldInfo = {
12
+ groupName: foldGroupName,
13
+ colorItems: []
14
+ };
15
+ if (0 === dimensions.length || 0 === measures.length) return {
16
+ dataset,
17
+ unfoldInfo: {
18
+ groupName: foldGroupName,
19
+ colorItems: []
20
+ }
21
+ };
22
+ const colorItems = [];
23
+ for(let i = 0; i < dataset.length; i++){
24
+ const datum = dataset[i];
25
+ const colorItem = generateDimGroupName(dimensionsToBeUnfolded, datum, dimensionsSeparator);
26
+ datum[foldGroupName] = colorItem;
27
+ colorItems.push(colorItem);
28
+ }
29
+ unfoldInfo.colorItems = colorItems;
30
+ return {
31
+ dataset,
32
+ unfoldInfo
33
+ };
34
+ };
35
+ const generateDimGroupName = (dimensionsToBeGrouped, datum, dimensionsSeparator)=>dimensionsToBeGrouped.map((dim)=>String(datum[dim.id])).join(dimensionsSeparator);
36
+ const foldMeasures = (dataset, measures, measureId = FoldMeasureId, measureName = FoldMeasureName, measureValue = FoldMeasureValue)=>{
37
+ const foldInfo = {
38
+ measureId,
39
+ measureName,
40
+ measureValue,
41
+ foldMap: {}
42
+ };
43
+ const result = new Array(dataset.length * measures.length);
44
+ let index = 0;
45
+ for(let i = 0; i < dataset.length; i++)for(let j = 0; j < measures.length; j++){
46
+ const datum = {
47
+ ...dataset[i]
48
+ };
49
+ const measure = measures[j];
50
+ const { id, alias } = measure;
51
+ datum[measureId] = id;
52
+ datum[measureName] = alias;
53
+ datum[measureValue] = dataset[i][id];
54
+ foldInfo.foldMap[id] = alias;
55
+ result[index++] = datum;
56
+ }
57
+ return {
58
+ dataset: result,
59
+ foldInfo
60
+ };
61
+ };
62
+ const emptyReshapeResult = {
63
+ dataset: [],
64
+ foldInfo: {
65
+ foldMap: {},
66
+ measureId: '',
67
+ measureName: '',
68
+ measureValue: ''
69
+ },
70
+ unfoldInfo: {
71
+ colorItems: [],
72
+ groupName: ''
73
+ }
74
+ };
75
+ const dataReshapeFor2D1M = (dataset, dimensions, measures)=>{
76
+ if (0 === dimensions.length && 0 === measures.length) return emptyReshapeResult;
77
+ const { dataset: foldedDataset, foldInfo } = foldMeasures(dataset, measures, FoldMeasureId, FoldMeasureName, FoldMeasureValue);
78
+ if (0 === dimensions.length) {
79
+ const { dataset: finalDataset, unfoldInfo } = unfoldDimensions(foldedDataset, [
80
+ {
81
+ id: FoldMeasureId,
82
+ alias: "\u6307\u6807Id",
83
+ location: 'dimension'
84
+ },
85
+ {
86
+ id: FoldMeasureName,
87
+ alias: "\u6307\u6807\u540D\u79F0",
88
+ location: 'dimension'
89
+ }
90
+ ], [
91
+ {
92
+ id: FoldMeasureValue,
93
+ alias: "\u6307\u6807\u503C"
94
+ }
95
+ ], 1, FoldDimensionGroup);
96
+ return {
97
+ dataset: finalDataset,
98
+ foldInfo,
99
+ unfoldInfo
100
+ };
101
+ }
102
+ {
103
+ const { dataset: finalDataset, unfoldInfo } = unfoldDimensions(foldedDataset, [
104
+ ...dimensions,
105
+ {
106
+ id: FoldMeasureName,
107
+ alias: "\u6307\u6807\u540D\u79F0",
108
+ location: 'dimension'
109
+ }
110
+ ], [
111
+ {
112
+ id: FoldMeasureValue,
113
+ alias: "\u6307\u6807\u503C"
114
+ }
115
+ ], 1, FoldDimensionGroup);
116
+ return {
117
+ dataset: finalDataset,
118
+ foldInfo,
119
+ unfoldInfo
120
+ };
121
+ }
122
+ };
123
+ const dataReshapeFor1D1M_emptyReshapeResult = {
124
+ dataset: [],
125
+ foldInfo: {
126
+ foldMap: {},
127
+ measureId: '',
128
+ measureName: '',
129
+ measureValue: ''
130
+ },
131
+ unfoldInfo: {
132
+ groupName: '',
133
+ colorItems: []
134
+ }
135
+ };
136
+ const dataReshapeFor1D1M_dataReshapeFor1D1M = (dataset, dimensions, measures)=>{
137
+ if (0 === dimensions.length && 0 === measures.length) return dataReshapeFor1D1M_emptyReshapeResult;
138
+ const { dataset: foldedDataset, foldInfo } = foldMeasures(dataset, measures, FoldMeasureId, FoldMeasureName, FoldMeasureValue);
139
+ if (0 === dimensions.length) {
140
+ const { dataset: finalDataset, unfoldInfo } = unfoldDimensions(foldedDataset, [
141
+ {
142
+ id: FoldMeasureName,
143
+ alias: "\u6307\u6807\u540D\u79F0",
144
+ location: 'dimension'
145
+ }
146
+ ], [
147
+ {
148
+ id: FoldMeasureValue,
149
+ alias: "\u6307\u6807\u503C"
150
+ }
151
+ ], 0, FoldDimensionGroup);
152
+ return {
153
+ dataset: finalDataset,
154
+ foldInfo,
155
+ unfoldInfo
156
+ };
157
+ }
158
+ {
159
+ const { dataset: finalDataset, unfoldInfo } = unfoldDimensions(foldedDataset, [
160
+ ...dimensions,
161
+ {
162
+ id: FoldMeasureName,
163
+ alias: "\u6307\u6807\u540D\u79F0",
164
+ location: 'dimension'
165
+ }
166
+ ], [
167
+ {
168
+ id: FoldMeasureValue,
169
+ alias: "\u6307\u6807\u503C"
170
+ }
171
+ ], 0, FoldDimensionGroup);
172
+ return {
173
+ dataset: finalDataset,
174
+ foldInfo,
175
+ unfoldInfo
176
+ };
177
+ }
178
+ };
179
+ const reshapeTo2D1M = (advancedVSeed, context)=>{
180
+ const result = {
181
+ ...advancedVSeed
182
+ };
183
+ const { vseed } = context;
184
+ const { dataset } = vseed;
185
+ const { dimensions, measures } = advancedVSeed;
186
+ if (!measures || !dimensions || !dataset) return result;
187
+ if (0 === measures.length) throw new Error('measures can not be empty');
188
+ const { dataset: newDatasets, foldInfo, unfoldInfo } = dataReshapeFor2D1M(dataset, dimensions, measures);
189
+ return {
190
+ ...result,
191
+ dataset: newDatasets,
192
+ datasetReshapeInfo: {
193
+ foldInfo,
194
+ unfoldInfo
195
+ },
196
+ dimensions,
197
+ measures
198
+ };
199
+ };
200
+ const encodingXY = (advancedVSeed)=>{
201
+ const result = {
202
+ ...advancedVSeed
203
+ };
204
+ const { datasetReshapeInfo, dimensions } = advancedVSeed;
205
+ if (!datasetReshapeInfo || !dimensions) return result;
206
+ const { foldInfo, unfoldInfo } = datasetReshapeInfo;
207
+ const isSingleDimension = 0 === dimensions.length;
208
+ const x = [
209
+ isSingleDimension ? foldInfo.measureName : dimensions[0].id
210
+ ];
211
+ const y = [
212
+ foldInfo.measureValue
213
+ ];
214
+ const group = [
215
+ isSingleDimension ? foldInfo.measureName : unfoldInfo.groupName
216
+ ];
217
+ const color = [
218
+ foldInfo.measureName
219
+ ];
220
+ const encoding = [
221
+ {
222
+ x,
223
+ y,
224
+ group,
225
+ color
226
+ }
227
+ ];
228
+ return {
229
+ ...result,
230
+ encoding
231
+ };
232
+ };
233
+ const encodingYX = (advancedVSeed)=>{
234
+ const result = {
235
+ ...advancedVSeed
236
+ };
237
+ const { datasetReshapeInfo, dimensions } = advancedVSeed;
238
+ if (!datasetReshapeInfo || !dimensions) return result;
239
+ const { foldInfo, unfoldInfo } = datasetReshapeInfo;
240
+ const isSingleDimension = 0 === dimensions.length;
241
+ const y = [
242
+ isSingleDimension ? foldInfo.measureName : dimensions[0].id
243
+ ];
244
+ const x = [
245
+ foldInfo.measureValue
246
+ ];
247
+ const group = [
248
+ isSingleDimension ? foldInfo.measureName : unfoldInfo.groupName
249
+ ];
250
+ const color = [
251
+ foldInfo.measureName
252
+ ];
253
+ const encoding = [
254
+ {
255
+ x,
256
+ y,
257
+ group,
258
+ color
259
+ }
260
+ ];
261
+ return {
262
+ ...result,
263
+ encoding
264
+ };
265
+ };
266
+ const initAdvancedVSeed = (advancedVSeed, context)=>{
267
+ const { vseed } = context;
268
+ const { chartType = 'table' } = vseed;
269
+ return {
270
+ ...advancedVSeed,
271
+ chartType
272
+ };
273
+ };
274
+ const autoMeasures = (advancedVSeed, context)=>{
275
+ const result = {
276
+ ...advancedVSeed
277
+ };
278
+ const { vseed } = context;
279
+ const { measures, dataset } = vseed;
280
+ if (!dataset) throw new Error('dataset is required');
281
+ if (0 === dataset.length) return result;
282
+ if (measures) {
283
+ result.measures = measures;
284
+ return result;
285
+ }
286
+ const top100dataset = dataset.slice(0, 100);
287
+ const sample = top100dataset.reduce((prev, cur)=>({
288
+ ...prev,
289
+ ...cur
290
+ }), {});
291
+ result.measures = Object.keys(sample).filter((key)=>top100dataset.some((item)=>'number' == typeof item[key]) && ![
292
+ '',
293
+ null,
294
+ void 0
295
+ ].includes(key)).map((measure)=>({
296
+ id: measure,
297
+ alias: measure
298
+ }));
299
+ return result;
300
+ };
301
+ const autoDimensions = (advancedVSeed, context)=>{
302
+ const result = {
303
+ ...advancedVSeed
304
+ };
305
+ const { vseed } = context;
306
+ const { dimensions, dataset } = vseed;
307
+ const { measures = [] } = advancedVSeed;
308
+ if (!dataset) throw new Error('dataset is required');
309
+ if (0 === dataset.length) return result;
310
+ if (dimensions) {
311
+ result.dimensions = dimensions;
312
+ return result;
313
+ }
314
+ const top100dataset = dataset.slice(0, 100);
315
+ const sample = top100dataset.reduce((prev, cur)=>({
316
+ ...prev,
317
+ ...cur
318
+ }), {});
319
+ result.dimensions = Object.keys(sample).filter((key)=>top100dataset.some((item)=>'string' == typeof item[key]) && ![
320
+ '',
321
+ null,
322
+ void 0
323
+ ].includes(key) && !measures.some((measure)=>measure.id === key)).map((dim)=>({
324
+ id: dim,
325
+ alias: dim,
326
+ location: 'dimension'
327
+ }));
328
+ return result;
329
+ };
330
+ const vchartBaseConfig = (advancedVSeed, context)=>{
331
+ const { vseed } = context;
332
+ const result = {
333
+ ...advancedVSeed
334
+ };
335
+ const config = external_remeda_pick(vseed, [
336
+ 'backgroundColor',
337
+ 'color',
338
+ 'label',
339
+ 'legend',
340
+ 'tooltip'
341
+ ]);
342
+ result.baseConfig = {
343
+ vchart: {
344
+ ...config
345
+ }
346
+ };
347
+ return result;
348
+ };
349
+ const vchartTheme = (advancedVSeed, context)=>{
350
+ const { customTheme, vseed } = context;
351
+ const { theme = 'light' } = vseed;
352
+ const result = {
353
+ ...advancedVSeed
354
+ };
355
+ if (!customTheme || !customTheme[theme]) return result;
356
+ const config = result.baseConfig?.vchart;
357
+ const themeConfig = customTheme?.[theme].baseConfig?.vchart;
358
+ if (!themeConfig || !config) return result;
359
+ const mergedConfig = external_remeda_mergeDeep(themeConfig, external_remeda_clone(config));
360
+ result.baseConfig = {
361
+ vchart: mergedConfig
362
+ };
363
+ return result;
364
+ };
365
+ const lineAdvancedPipeline = [
366
+ initAdvancedVSeed,
367
+ autoMeasures,
368
+ autoDimensions,
369
+ reshapeTo2D1M,
370
+ encodingXY,
371
+ vchartBaseConfig,
372
+ vchartTheme
373
+ ];
374
+ const barAdvancedPipeline = [
375
+ initAdvancedVSeed,
376
+ autoMeasures,
377
+ autoDimensions,
378
+ reshapeTo2D1M,
379
+ encodingYX,
380
+ vchartBaseConfig,
381
+ vchartTheme
382
+ ];
383
+ const barParallelAdvancedPipeline = [
384
+ initAdvancedVSeed,
385
+ autoMeasures,
386
+ autoDimensions,
387
+ reshapeTo2D1M,
388
+ encodingYX,
389
+ vchartBaseConfig,
390
+ vchartTheme
391
+ ];
392
+ const barPercentAdvancedPipeline = [
393
+ initAdvancedVSeed,
394
+ autoMeasures,
395
+ autoDimensions,
396
+ reshapeTo2D1M,
397
+ encodingYX,
398
+ vchartBaseConfig,
399
+ vchartTheme
400
+ ];
401
+ const columnAdvancedPipeline = [
402
+ initAdvancedVSeed,
403
+ autoMeasures,
404
+ autoDimensions,
405
+ reshapeTo2D1M,
406
+ encodingXY,
407
+ vchartBaseConfig,
408
+ vchartTheme
409
+ ];
410
+ const columnParallelAdvancedPipeline = [
411
+ initAdvancedVSeed,
412
+ autoMeasures,
413
+ autoDimensions,
414
+ reshapeTo2D1M,
415
+ encodingXY,
416
+ vchartBaseConfig,
417
+ vchartTheme
418
+ ];
419
+ const columnPercentAdvancedPipeline = [
420
+ initAdvancedVSeed,
421
+ autoMeasures,
422
+ autoDimensions,
423
+ reshapeTo2D1M,
424
+ encodingXY,
425
+ vchartBaseConfig,
426
+ vchartTheme
427
+ ];
428
+ const areaAdvancedPipeline = [
429
+ initAdvancedVSeed,
430
+ autoMeasures,
431
+ autoDimensions,
432
+ reshapeTo2D1M,
433
+ encodingXY,
434
+ vchartBaseConfig,
435
+ vchartTheme
436
+ ];
437
+ const areaPercentAdvancedPipeline = [
438
+ initAdvancedVSeed,
439
+ autoMeasures,
440
+ autoDimensions,
441
+ reshapeTo2D1M,
442
+ encodingXY,
443
+ vchartBaseConfig,
444
+ vchartTheme
445
+ ];
446
+ const dataset_dataset = (spec, context)=>{
447
+ const { advancedVSeed } = context;
448
+ return {
449
+ ...spec,
450
+ data: {
451
+ values: advancedVSeed.dataset
452
+ }
453
+ };
454
+ };
455
+ const initColumn = (spec, context)=>{
456
+ const result = {
457
+ ...spec
458
+ };
459
+ const { advancedVSeed } = context;
460
+ const { encoding } = advancedVSeed;
461
+ if (!encoding[0].y || !encoding[0].x || !encoding[0].group) return result;
462
+ result.type = 'bar';
463
+ result.direction = 'vertical';
464
+ result.xField = encoding[0].x[0];
465
+ result.yField = encoding[0].y[0];
466
+ result.seriesField = encoding[0].group[0];
467
+ result.padding = 0;
468
+ return result;
469
+ };
470
+ const initBar = (spec, context)=>{
471
+ const result = {
472
+ ...spec
473
+ };
474
+ const { advancedVSeed } = context;
475
+ const { encoding } = advancedVSeed;
476
+ if (!encoding[0].y || !encoding[0].x || !encoding[0].group) return result;
477
+ result.type = 'bar';
478
+ result.direction = 'horizontal';
479
+ result.yField = encoding[0].y?.[0];
480
+ result.xField = encoding[0].x?.[0];
481
+ result.seriesField = encoding[0].group?.[0];
482
+ result.padding = 0;
483
+ return result;
484
+ };
485
+ const initBarParallel = (spec, context)=>{
486
+ const result = {
487
+ ...spec
488
+ };
489
+ const { advancedVSeed } = context;
490
+ const { encoding, datasetReshapeInfo } = advancedVSeed;
491
+ const { unfoldInfo } = datasetReshapeInfo;
492
+ if (!encoding[0].y || !encoding[0].x || !encoding[0].group) return result;
493
+ result.type = 'bar';
494
+ result.direction = 'horizontal';
495
+ result.yField = [
496
+ encoding[0].y[0],
497
+ unfoldInfo.groupName
498
+ ];
499
+ result.xField = encoding[0].x[0];
500
+ result.seriesField = encoding[0].group[0];
501
+ result.padding = 0;
502
+ return result;
503
+ };
504
+ const initArea = (spec, context)=>{
505
+ const result = {
506
+ ...spec
507
+ };
508
+ const { advancedVSeed } = context;
509
+ const { encoding } = advancedVSeed;
510
+ if (!encoding[0].y || !encoding[0].x || !encoding[0].group) return result;
511
+ result.type = 'area';
512
+ result.direction = 'vertical';
513
+ result.xField = encoding[0].x[0];
514
+ result.yField = encoding[0].y[0];
515
+ result.seriesField = encoding[0].group[0];
516
+ result.padding = 0;
517
+ return result;
518
+ };
519
+ const initLine = (spec, context)=>{
520
+ const result = {
521
+ ...spec
522
+ };
523
+ const { advancedVSeed } = context;
524
+ const { encoding } = advancedVSeed;
525
+ if (!encoding[0].y || !encoding[0].x || !encoding[0].group) return result;
526
+ result.type = 'line';
527
+ result.direction = 'vertical';
528
+ result.xField = encoding[0].x[0];
529
+ result.yField = encoding[0].y[0];
530
+ result.seriesField = encoding[0].group[0];
531
+ result.padding = 0;
532
+ return result;
533
+ };
534
+ const initColumnParallel = (spec, context)=>{
535
+ const result = {
536
+ ...spec
537
+ };
538
+ const { advancedVSeed } = context;
539
+ const { encoding, datasetReshapeInfo } = advancedVSeed;
540
+ const { unfoldInfo } = datasetReshapeInfo;
541
+ if (!encoding[0].y || !encoding[0].x || !encoding[0].group) return result;
542
+ result.type = 'bar';
543
+ result.direction = 'vertical';
544
+ result.xField = [
545
+ encoding[0].x[0],
546
+ unfoldInfo.groupName
547
+ ];
548
+ result.yField = encoding[0].y[0];
549
+ result.seriesField = encoding[0].group[0];
550
+ result.padding = 0;
551
+ return result;
552
+ };
553
+ const xBand = (spec)=>{
554
+ const result = {
555
+ ...spec
556
+ };
557
+ if (!result.axes) result.axes = [];
558
+ result.axes = [
559
+ ...result.axes,
560
+ {
561
+ visible: true,
562
+ type: 'band',
563
+ orient: 'bottom'
564
+ }
565
+ ];
566
+ return result;
567
+ };
568
+ const xLinear = (spec)=>{
569
+ const result = {
570
+ ...spec
571
+ };
572
+ if (!result.axes) result.axes = [];
573
+ result.axes = [
574
+ ...result.axes,
575
+ {
576
+ visible: true,
577
+ type: 'linear',
578
+ orient: 'bottom'
579
+ }
580
+ ];
581
+ return result;
582
+ };
583
+ const yBand = (spec)=>{
584
+ const result = {
585
+ ...spec
586
+ };
587
+ if (!result.axes) result.axes = [];
588
+ result.axes = [
589
+ ...result.axes,
590
+ {
591
+ visible: true,
592
+ type: 'band',
593
+ orient: 'left'
594
+ }
595
+ ];
596
+ return result;
597
+ };
598
+ const yLinear = (spec)=>{
599
+ const result = {
600
+ ...spec
601
+ };
602
+ if (!result.axes) result.axes = [];
603
+ result.axes = [
604
+ ...result.axes,
605
+ {
606
+ visible: true,
607
+ type: 'linear',
608
+ orient: 'left'
609
+ }
610
+ ];
611
+ return result;
612
+ };
613
+ const percent = (spec, context)=>{
614
+ const result = {
615
+ ...spec
616
+ };
617
+ result.percent = true;
618
+ return result;
619
+ };
620
+ const stack = (spec, context)=>{
621
+ const result = {
622
+ ...spec
623
+ };
624
+ result.stack = true;
625
+ return result;
626
+ };
627
+ const background_backgroundColor = (spec, context)=>{
628
+ const result = {
629
+ ...spec
630
+ };
631
+ const { advancedVSeed } = context;
632
+ const { baseConfig } = advancedVSeed;
633
+ if (!baseConfig?.vchart) return result;
634
+ const { backgroundColor } = baseConfig.vchart;
635
+ return {
636
+ ...result,
637
+ background: backgroundColor
638
+ };
639
+ };
640
+ const defaultTooltip = {
641
+ enable: true
642
+ };
643
+ const tooltip_tooltip = (spec, context)=>{
644
+ const result = {
645
+ ...spec
646
+ };
647
+ const { advancedVSeed } = context;
648
+ const baseConfig = advancedVSeed.baseConfig.vchart;
649
+ const { tooltip = defaultTooltip } = baseConfig;
650
+ const { enable } = tooltip;
651
+ result.tooltip = {
652
+ visible: enable
653
+ };
654
+ return result;
655
+ };
656
+ const label_label = (spec, context)=>{
657
+ const result = {
658
+ ...spec
659
+ };
660
+ const { advancedVSeed } = context;
661
+ const baseConfig = advancedVSeed.baseConfig.vchart;
662
+ if (!baseConfig || !baseConfig.label) return result;
663
+ const { label } = baseConfig;
664
+ const { enable } = label;
665
+ result.label = {
666
+ visible: enable
667
+ };
668
+ return result;
669
+ };
670
+ const defaultLegend = {
671
+ enable: true
672
+ };
673
+ const legend_legend = (spec, context)=>{
674
+ const result = {
675
+ ...spec
676
+ };
677
+ const { advancedVSeed } = context;
678
+ const baseConfig = advancedVSeed.baseConfig.vchart;
679
+ if (!baseConfig || !baseConfig.legend) return result;
680
+ const { legend = defaultLegend } = baseConfig;
681
+ const { enable } = legend;
682
+ result.legends = {
683
+ visible: enable
684
+ };
685
+ return result;
686
+ };
687
+ const color_color = (spec, context)=>{
688
+ const result = {
689
+ ...spec
690
+ };
691
+ const { advancedVSeed } = context;
692
+ const { datasetReshapeInfo } = advancedVSeed;
693
+ const { unfoldInfo } = datasetReshapeInfo;
694
+ const baseConfig = advancedVSeed.baseConfig.vchart;
695
+ if (!baseConfig || !baseConfig.color) return result;
696
+ const { color } = baseConfig;
697
+ const { colorScheme, colorMapping } = color;
698
+ result.color = {
699
+ type: 'ordinal',
700
+ domain: unfoldInfo.colorItems,
701
+ range: colorScheme,
702
+ specified: colorMapping
703
+ };
704
+ return result;
705
+ };
706
+ const lineSpecPipeline = [
707
+ initLine,
708
+ color_color,
709
+ background_backgroundColor,
710
+ dataset_dataset,
711
+ xBand,
712
+ yLinear,
713
+ label_label,
714
+ tooltip_tooltip,
715
+ legend_legend
716
+ ];
717
+ const columnSpecPipeline = [
718
+ initColumn,
719
+ color_color,
720
+ background_backgroundColor,
721
+ dataset_dataset,
722
+ xBand,
723
+ yLinear,
724
+ label_label,
725
+ tooltip_tooltip,
726
+ legend_legend
727
+ ];
728
+ const columnParallelSpecPipeline = [
729
+ initColumnParallel,
730
+ color_color,
731
+ background_backgroundColor,
732
+ dataset_dataset,
733
+ xBand,
734
+ yLinear,
735
+ label_label,
736
+ tooltip_tooltip,
737
+ legend_legend
738
+ ];
739
+ const columnPercentSpecPipeline = [
740
+ initColumn,
741
+ color_color,
742
+ background_backgroundColor,
743
+ percent,
744
+ dataset_dataset,
745
+ xBand,
746
+ yLinear,
747
+ label_label,
748
+ tooltip_tooltip,
749
+ legend_legend
750
+ ];
751
+ const barSpecPipeline = [
752
+ initBar,
753
+ color_color,
754
+ background_backgroundColor,
755
+ dataset_dataset,
756
+ xLinear,
757
+ yBand,
758
+ label_label,
759
+ tooltip_tooltip,
760
+ legend_legend
761
+ ];
762
+ const barParallelSpecPipeline = [
763
+ initBarParallel,
764
+ color_color,
765
+ background_backgroundColor,
766
+ dataset_dataset,
767
+ xLinear,
768
+ yBand,
769
+ label_label,
770
+ tooltip_tooltip,
771
+ legend_legend
772
+ ];
773
+ const barPercentSpecPipeline = [
774
+ initBar,
775
+ color_color,
776
+ background_backgroundColor,
777
+ percent,
778
+ dataset_dataset,
779
+ xLinear,
780
+ yBand,
781
+ label_label,
782
+ tooltip_tooltip,
783
+ legend_legend
784
+ ];
785
+ const areaSpecPipeline = [
786
+ initArea,
787
+ color_color,
788
+ background_backgroundColor,
789
+ stack,
790
+ dataset_dataset,
791
+ xBand,
792
+ yLinear,
793
+ label_label,
794
+ tooltip_tooltip,
795
+ legend_legend
796
+ ];
797
+ const areaPercentSpecPipeline = [
798
+ initArea,
799
+ color_color,
800
+ background_backgroundColor,
801
+ percent,
802
+ dataset_dataset,
803
+ xBand,
804
+ yLinear,
805
+ label_label,
806
+ tooltip_tooltip,
807
+ legend_legend
808
+ ];
809
+ const execPipeline = (pipeline, context, initialValue = {})=>{
810
+ const result = pipeline.reduce((prev, cur)=>cur(prev, context), initialValue);
811
+ return result;
812
+ };
813
+ const isVTable = (chartType)=>[
814
+ 'table',
815
+ 'pivotTable'
816
+ ].includes(chartType);
817
+ const isVChart = (chartType)=>!isVTable(chartType);
818
+ const buildAdvanced = (builder)=>{
819
+ const { chartType } = builder.vseed;
820
+ if (!chartType) throw new Error('chartType is nil in buildAdvanced');
821
+ const pipeline = builder.getAdvancedPipeline(chartType);
822
+ if (!pipeline) throw new Error(`no advanced pipeline for chartType ${chartType}`);
823
+ const context = {
824
+ vseed: builder.vseed,
825
+ customTheme: builder.getThemeMap()
826
+ };
827
+ try {
828
+ return execPipeline(pipeline, context);
829
+ } catch (e) {
830
+ console.error(e);
831
+ throw new Error("buildAdvanced error, see error info in console");
832
+ }
833
+ };
834
+ const buildSpec = (builder, advancedVSeed)=>{
835
+ const { chartType } = builder.vseed;
836
+ if (!chartType) throw new Error('chartType is nil in buildSpec');
837
+ const pipeline = builder.getSpecPipeline(chartType);
838
+ if (!pipeline) throw new Error(`no spec pipeline for chartType ${chartType}`);
839
+ const context = {
840
+ vseed: builder.vseed,
841
+ advancedVSeed
842
+ };
843
+ try {
844
+ return execPipeline(pipeline, context);
845
+ } catch (e) {
846
+ console.error(e);
847
+ throw new Error("buildSpec error, see error info in console");
848
+ }
849
+ };
850
+ const build = (builder)=>{
851
+ console.log('debug vseed', builder.vseed);
852
+ const advancedVSeed = builder.buildAdvanced();
853
+ console.log('debug advancedVSeed', advancedVSeed);
854
+ if (!advancedVSeed) throw new Error('advancedVSeed is null');
855
+ const spec = builder.buildSpec(advancedVSeed);
856
+ console.log('debug spec', spec);
857
+ return spec;
858
+ };
859
+ class Builder {
860
+ _vseed;
861
+ _advancedVSeed = null;
862
+ constructor(vseed){
863
+ this._vseed = vseed;
864
+ }
865
+ build = ()=>build(this);
866
+ buildSpec = (advanced)=>buildSpec(this, advanced);
867
+ buildAdvanced = ()=>buildAdvanced(this);
868
+ getAdvancedPipeline = (chartType)=>Builder._advancedPipelineMap[chartType];
869
+ getSpecPipeline = (chartType)=>Builder._specPipelineMap[chartType];
870
+ getTheme = (themeKey)=>Builder._themeMap[themeKey];
871
+ getThemeMap = ()=>Builder._themeMap;
872
+ get vseed() {
873
+ return this._vseed;
874
+ }
875
+ set vseed(value) {
876
+ this._vseed = value;
877
+ }
878
+ get advancedVSeed() {
879
+ return this._advancedVSeed;
880
+ }
881
+ set advancedVSeed(value) {
882
+ this._advancedVSeed = value;
883
+ }
884
+ static _advancedPipelineMap = {};
885
+ static _specPipelineMap = {};
886
+ static _themeMap = {};
887
+ static from = (vseed)=>new Builder(vseed);
888
+ }
889
+ const registerColumn = ()=>{
890
+ Builder._advancedPipelineMap.column = columnAdvancedPipeline;
891
+ Builder._specPipelineMap.column = columnSpecPipeline;
892
+ };
893
+ const registerBar = ()=>{
894
+ Builder._advancedPipelineMap.bar = barAdvancedPipeline;
895
+ Builder._specPipelineMap.bar = barSpecPipeline;
896
+ };
897
+ const registerLine = ()=>{
898
+ Builder._advancedPipelineMap.line = lineAdvancedPipeline;
899
+ Builder._specPipelineMap.line = lineSpecPipeline;
900
+ };
901
+ const registerArea = ()=>{
902
+ Builder._advancedPipelineMap.area = areaAdvancedPipeline;
903
+ Builder._specPipelineMap.area = areaSpecPipeline;
904
+ };
905
+ const registerAreaPercent = ()=>{
906
+ Builder._advancedPipelineMap.areaPercent = areaPercentAdvancedPipeline;
907
+ Builder._specPipelineMap.areaPercent = areaPercentSpecPipeline;
908
+ };
909
+ const registerBarPercent = ()=>{
910
+ Builder._advancedPipelineMap.barPercent = barPercentAdvancedPipeline;
911
+ Builder._specPipelineMap.barPercent = barPercentSpecPipeline;
912
+ };
913
+ const registerColumnPercent = ()=>{
914
+ Builder._advancedPipelineMap.columnPercent = columnPercentAdvancedPipeline;
915
+ Builder._specPipelineMap.columnPercent = columnPercentSpecPipeline;
916
+ };
917
+ const registerColumnParallel = ()=>{
918
+ Builder._advancedPipelineMap.columnParallel = columnParallelAdvancedPipeline;
919
+ Builder._specPipelineMap.columnParallel = columnParallelSpecPipeline;
920
+ };
921
+ const registerBarParallel = ()=>{
922
+ Builder._advancedPipelineMap.barParallel = barParallelAdvancedPipeline;
923
+ Builder._specPipelineMap.barParallel = barParallelSpecPipeline;
924
+ };
925
+ const darkTheme = ()=>({
926
+ baseConfig: {
927
+ vtable: {
928
+ backgroundColor: '#141414'
929
+ },
930
+ vchart: {
931
+ backgroundColor: '#141414',
932
+ color: {
933
+ colorScheme: [
934
+ '#2E62F1',
935
+ '#4DC36A',
936
+ '#FF8406',
937
+ '#FFCC00',
938
+ '#4F44CF',
939
+ '#5AC8FA',
940
+ '#003A8C',
941
+ '#B08AE2',
942
+ '#FF6341',
943
+ '#98DD62'
944
+ ]
945
+ },
946
+ label: {
947
+ enable: true
948
+ },
949
+ tooltip: {
950
+ enable: true
951
+ },
952
+ legend: {
953
+ enable: true
954
+ }
955
+ }
956
+ }
957
+ });
958
+ const lightTheme = ()=>({
959
+ baseConfig: {
960
+ vtable: {
961
+ backgroundColor: '#ffffff'
962
+ },
963
+ vchart: {
964
+ backgroundColor: '#ffffff',
965
+ color: {
966
+ colorScheme: [
967
+ '#8D72F6',
968
+ '#5766EC',
969
+ '#66A3FE',
970
+ '#51D5E6',
971
+ '#4EC0B3',
972
+ '#F9DF90',
973
+ '#F9AD71',
974
+ '#ED8888',
975
+ '#E9A0C3',
976
+ '#D77DD3'
977
+ ]
978
+ },
979
+ label: {
980
+ enable: true
981
+ },
982
+ tooltip: {
983
+ enable: true
984
+ },
985
+ legend: {
986
+ enable: true
987
+ }
988
+ }
989
+ }
990
+ });
991
+ const registerCustomTheme = (key, themeConfig)=>{
992
+ Builder._themeMap[key] = themeConfig;
993
+ };
994
+ const registerLightTheme = ()=>{
995
+ registerCustomTheme('light', lightTheme());
996
+ };
997
+ const registerDarkTheme = ()=>{
998
+ registerCustomTheme('dark', darkTheme());
999
+ };
1000
+ const all_registerAll = ()=>{
1001
+ registerLine();
1002
+ registerColumn();
1003
+ registerColumnParallel();
1004
+ registerColumnPercent();
1005
+ registerBar();
1006
+ registerBarParallel();
1007
+ registerBarPercent();
1008
+ registerArea();
1009
+ registerAreaPercent();
1010
+ registerLightTheme();
1011
+ registerDarkTheme();
1012
+ };
1013
+ const zChartType = z["enum"]([
1014
+ 'table',
1015
+ 'pivotTable',
1016
+ 'line',
1017
+ 'column',
1018
+ 'columnPercent',
1019
+ 'columnParallel',
1020
+ 'bar',
1021
+ 'barPercent',
1022
+ 'barParallel',
1023
+ 'area',
1024
+ 'areaPercent',
1025
+ 'rose',
1026
+ 'pie',
1027
+ 'donut',
1028
+ 'dualAxis'
1029
+ ]);
1030
+ const zDatum = z.record(z.string().or(z.number()), z.any());
1031
+ const zDataset = z.array(zDatum);
1032
+ const zDimension = z.object({
1033
+ id: z.string(),
1034
+ alias: z.string().optional(),
1035
+ visible: z.boolean().default(true).optional(),
1036
+ location: z["enum"]([
1037
+ 'dimension',
1038
+ 'rowDimension',
1039
+ 'columnDimension'
1040
+ ])
1041
+ });
1042
+ const zDimensions = z.array(zDimension).optional();
1043
+ const zMeasure = z.object({
1044
+ id: z.string(),
1045
+ alias: z.string().optional(),
1046
+ visible: z.boolean().default(true).optional(),
1047
+ autoFormat: z.boolean().default(true).optional(),
1048
+ format: z.object({
1049
+ type: z["enum"]([
1050
+ 'number',
1051
+ 'percent',
1052
+ 'permille'
1053
+ ]).optional().default('number'),
1054
+ ratio: z.number().optional().default(1),
1055
+ symbol: z.string().optional().default(''),
1056
+ thousandSeparator: z.boolean().optional().default(false),
1057
+ decimalPlaces: z.number().optional().default(2),
1058
+ round: z["enum"]([
1059
+ 'round',
1060
+ 'floor',
1061
+ 'ceil'
1062
+ ]).optional().default('round'),
1063
+ prefix: z.string().optional().default(''),
1064
+ suffix: z.string().optional().default('')
1065
+ }).optional()
1066
+ });
1067
+ const zMeasureGroup = z.object({
1068
+ id: z.string(),
1069
+ alias: z.string().optional(),
1070
+ visible: z.boolean().default(true).optional(),
1071
+ get children () {
1072
+ return z.array(zMeasureGroup.or(zMeasure)).optional();
1073
+ }
1074
+ });
1075
+ const zMeasures = z.array(zMeasureGroup.or(zMeasure)).optional();
1076
+ const zFoldInfo = z.object({
1077
+ foldMap: z.record(z.string(), z.string().or(z.undefined())),
1078
+ measureId: z.string(),
1079
+ measureName: z.string(),
1080
+ measureValue: z.string()
1081
+ });
1082
+ const zUnfoldInfo = z.object({
1083
+ colorItems: z.array(z.string()),
1084
+ groupName: z.string()
1085
+ });
1086
+ const zDatasetReshapeInfo = z.object({
1087
+ foldInfo: zFoldInfo,
1088
+ unfoldInfo: zUnfoldInfo
1089
+ });
1090
+ const zEncoding = z.array(z.object({
1091
+ x: z.array(z.string()).optional(),
1092
+ y: z.array(z.string()).optional(),
1093
+ color: z.array(z.string()).optional(),
1094
+ group: z.array(z.string()).optional(),
1095
+ angle: z.array(z.string()).optional(),
1096
+ radius: z.array(z.string()).optional(),
1097
+ tooltip: z.array(z.string()).optional()
1098
+ }));
1099
+ const zBackgroundColor = z.string().default('transparent').optional();
1100
+ const zLabel = z.object({
1101
+ enable: z.boolean().default(true).optional()
1102
+ });
1103
+ const zColor = z.object({
1104
+ colorScheme: z.array(z.string()).optional(),
1105
+ colorMapping: z.record(z.string(), z.string()).optional()
1106
+ });
1107
+ const zTooltip = z.object({
1108
+ enable: z.boolean().default(true).optional()
1109
+ });
1110
+ const zLegend = z.object({
1111
+ enable: z.boolean().default(true).optional()
1112
+ });
1113
+ const zVChartBaseConfig = z.object({
1114
+ backgroundColor: zBackgroundColor,
1115
+ label: zLabel.optional(),
1116
+ color: zColor.optional(),
1117
+ tooltip: zTooltip.optional(),
1118
+ legend: zLegend.optional()
1119
+ });
1120
+ const zVTableBaseConfig = z.object({
1121
+ backgroundColor: zBackgroundColor
1122
+ });
1123
+ const zBaseConfig = z.object({
1124
+ vchart: zVChartBaseConfig.optional(),
1125
+ vtable: zVTableBaseConfig.optional()
1126
+ });
1127
+ const zCustomThemeConfig = z.object({
1128
+ baseConfig: zBaseConfig.optional()
1129
+ });
1130
+ const zCustomTheme = z.record(z.string(), zCustomThemeConfig).optional();
1131
+ const zTheme = z.string();
1132
+ export { Builder, areaAdvancedPipeline, areaPercentAdvancedPipeline, areaPercentSpecPipeline, areaSpecPipeline, barAdvancedPipeline, barParallelAdvancedPipeline, barParallelSpecPipeline, barPercentAdvancedPipeline, barPercentSpecPipeline, barSpecPipeline, columnAdvancedPipeline, columnParallelAdvancedPipeline, columnParallelSpecPipeline, columnPercentAdvancedPipeline, columnPercentSpecPipeline, columnSpecPipeline, darkTheme, dataReshapeFor1D1M_dataReshapeFor1D1M as dataReshapeFor1D1M, dataReshapeFor2D1M, execPipeline, foldMeasures, isVChart, isVTable, lightTheme, lineAdvancedPipeline, lineSpecPipeline, all_registerAll as registerAll, registerArea, registerAreaPercent, registerBar, registerBarParallel, registerBarPercent, registerColumn, registerColumnParallel, registerColumnPercent, registerCustomTheme, registerDarkTheme, registerLightTheme, registerLine, unfoldDimensions, zBackgroundColor, zBaseConfig, zChartType, zColor, zCustomTheme, zCustomThemeConfig, zDataset, zDatasetReshapeInfo, zDatum, zDimension, zDimensions, zEncoding, zFoldInfo, zLabel, zLegend, zMeasure, zMeasureGroup, zMeasures, zTheme, zTooltip, zUnfoldInfo };
1133
+
1134
+ //# sourceMappingURL=index.js.map