@visactor/vseed 0.0.12 → 0.0.13

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.
Files changed (37) hide show
  1. package/dist/builder/builder/builder.d.ts +3 -0
  2. package/dist/i18n/i18n.d.ts +17 -0
  3. package/dist/i18n/index.d.ts +1 -0
  4. package/dist/index.cjs +200 -59
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +146 -17
  8. package/dist/index.js.map +1 -1
  9. package/dist/pipeline/advanced/pipes/i18n/index.d.ts +1 -0
  10. package/dist/pipeline/advanced/pipes/i18n/locale.d.ts +2 -0
  11. package/dist/pipeline/advanced/pipes/index.d.ts +1 -0
  12. package/dist/pipeline/constant.d.ts +1 -1
  13. package/dist/pipeline/utils/format/createFormatter.d.ts +2 -1
  14. package/dist/pipeline/utils/format/createNumFormatter.d.ts +2 -1
  15. package/dist/pipeline/utils/format/index.d.ts +2 -2
  16. package/dist/types/advancedVSeed.d.ts +8 -4
  17. package/dist/types/chartType/area/area.d.ts +7 -0
  18. package/dist/types/chartType/areaPercent/areaPercent.d.ts +7 -0
  19. package/dist/types/chartType/bar/bar.d.ts +7 -0
  20. package/dist/types/chartType/barParallel/barParallel.d.ts +7 -0
  21. package/dist/types/chartType/barPercent/barPercent.d.ts +7 -0
  22. package/dist/types/chartType/column/column.d.ts +7 -0
  23. package/dist/types/chartType/columnParallel/columnParallel.d.ts +7 -0
  24. package/dist/types/chartType/columnPercent/columnPercent.d.ts +7 -0
  25. package/dist/types/chartType/donut/donut.d.ts +7 -0
  26. package/dist/types/chartType/dualAxis/dualAxis.d.ts +7 -0
  27. package/dist/types/chartType/line/line.d.ts +7 -0
  28. package/dist/types/chartType/pie/pie.d.ts +7 -0
  29. package/dist/types/chartType/pivotTable/pivotTable.d.ts +7 -0
  30. package/dist/types/chartType/rose/rose.d.ts +7 -0
  31. package/dist/types/chartType/table/table.d.ts +7 -0
  32. package/dist/types/i18n/i18n.d.ts +9 -0
  33. package/dist/types/i18n/index.d.ts +1 -0
  34. package/dist/types/index.d.ts +1 -0
  35. package/dist/types/properties/measures/measures.d.ts +8 -8
  36. package/dist/types/vseed.d.ts +13 -9
  37. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from './types';
3
3
  export * from './pipeline';
4
4
  export * from './dataReshape';
5
5
  export * from './theme';
6
+ export * from './i18n';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { clone as external_remeda_clone, groupBy, isArray, isNumber, isString, mergeDeep as external_remeda_mergeDeep, omit, pick as external_remeda_pick, unique } from "remeda";
1
+ import { clone as external_remeda_clone, groupBy, isArray, isEmpty, isNullish, isNumber, isString, mergeDeep as external_remeda_mergeDeep, omit, pick as external_remeda_pick, unique } from "remeda";
2
2
  import { z } from "zod";
3
3
  const initAdvancedVSeed = (advancedVSeed, context)=>{
4
4
  const { vseed } = context;
@@ -23,6 +23,32 @@ const isPivotChart = (vseed)=>{
23
23
  const hasMeasureGroup = measures && measures.find((measure)=>measure && measure.children);
24
24
  return hasRowOrColumnDimension || hasMeasureGroup;
25
25
  };
26
+ var i18n_namespaceObject = JSON.parse('{"指标名称":{"en-US":"MeasureName","zh-CN":"指标名称"},"指标Id":{"en-US":"MeasureId","zh-CN":"指标Id"},"指标值":{"en-US":"MeasureValue","zh-CN":"指标值"}}');
27
+ class Intl {
28
+ static instance;
29
+ translateMap = i18n_namespaceObject;
30
+ locale = 'zh-CN';
31
+ canTranslate = (value)=>!!this.translateMap[value];
32
+ i18n = (segments, ...values)=>{
33
+ const text = segments.map((segment, index)=>segment + (values[index] || '')).join('');
34
+ const translatedText = this.translateMap?.[text]?.[this.locale];
35
+ if (isNullish(translatedText)) {
36
+ console.warn(`i18n ${this.locale} no translate: ${text}`);
37
+ return text;
38
+ }
39
+ return translatedText;
40
+ };
41
+ setLocale = (locale)=>{
42
+ this.locale = locale;
43
+ };
44
+ getLocale = ()=>this.locale;
45
+ static getInstance() {
46
+ if (!Intl.instance) Intl.instance = new Intl();
47
+ return Intl.instance;
48
+ }
49
+ }
50
+ const intl = Intl.getInstance();
51
+ const i18n = intl.i18n;
26
52
  const createNumFormatter = (format)=>{
27
53
  const { type = 'number', ratio = 1, symbol = '', thousandSeparator = true, decimalPlaces = 2, round = 'round', prefix = '', suffix = '' } = format || {};
28
54
  return (value)=>{
@@ -47,7 +73,83 @@ const createNumFormatter = (format)=>{
47
73
  return `${prefix}${numStr}${typeSymbol}${symbol}${suffix}`;
48
74
  };
49
75
  };
76
+ const autoNumFormatter = (value, locale = intl.getLocale())=>{
77
+ if (null == value) return String(value);
78
+ const num = Number(value);
79
+ if (Number.isNaN(num)) return String(value);
80
+ const countDecimalPlaces = (num)=>{
81
+ if (Number.isInteger(num)) return 0;
82
+ const str = num.toString();
83
+ if (str.indexOf('e-') > -1) return parseInt(str.split('e-')[1]);
84
+ const decimalPart = str.split('.')[1];
85
+ if (!decimalPart) return 0;
86
+ const decimalPlaces = decimalPart.replace(/0+$/, '').length;
87
+ return Math.max(2, decimalPlaces);
88
+ };
89
+ const numFormat = {
90
+ type: 'number',
91
+ decimalPlaces: countDecimalPlaces(num),
92
+ round: 'round',
93
+ thousandSeparator: true
94
+ };
95
+ const rules = NUMBER_FORMAT_RULES[locale] || NUMBER_FORMAT_RULES['default'];
96
+ for (const rule of rules)if (num >= rule.threshold) {
97
+ numFormat.ratio = rule.ratio;
98
+ numFormat.symbol = rule.symbol;
99
+ break;
100
+ }
101
+ return createNumFormatter(numFormat)(value);
102
+ };
103
+ const NUMBER_FORMAT_RULES = {
104
+ 'zh-CN': [
105
+ {
106
+ threshold: 100000000,
107
+ ratio: 100000000,
108
+ symbol: "\u4EBF"
109
+ },
110
+ {
111
+ threshold: 10000,
112
+ ratio: 10000,
113
+ symbol: "\u4E07"
114
+ }
115
+ ],
116
+ 'en-US': [
117
+ {
118
+ threshold: 1000000000,
119
+ ratio: 1000000000,
120
+ symbol: 'B'
121
+ },
122
+ {
123
+ threshold: 1000000,
124
+ ratio: 1000000,
125
+ symbol: 'M'
126
+ },
127
+ {
128
+ threshold: 1000,
129
+ ratio: 1000,
130
+ symbol: 'K'
131
+ }
132
+ ],
133
+ default: [
134
+ {
135
+ threshold: 1000000000,
136
+ ratio: 1000000000,
137
+ symbol: 'B'
138
+ },
139
+ {
140
+ threshold: 1000000,
141
+ ratio: 1000000,
142
+ symbol: 'M'
143
+ },
144
+ {
145
+ threshold: 1000,
146
+ ratio: 1000,
147
+ symbol: 'K'
148
+ }
149
+ ]
150
+ };
50
151
  const createFormatter = (format)=>createNumFormatter(format);
152
+ const autoFormatter = (value, locale)=>autoNumFormatter(value, locale);
51
153
  function findMeasureById(measures, id) {
52
154
  if (!measures) return;
53
155
  const stack = [
@@ -244,18 +346,18 @@ const dataReshapeFor2D1M = (dataset, dimensions, measures, options)=>{
244
346
  const { dataset: finalDataset, unfoldInfo } = unfoldDimensions(foldedDataset, [
245
347
  {
246
348
  id: foldMeasureId,
247
- alias: "\u6307\u6807Id",
349
+ alias: i18n`指标Id`,
248
350
  location: 'dimension'
249
351
  },
250
352
  {
251
353
  id: foldMeasureName,
252
- alias: "\u6307\u6807\u540D\u79F0",
354
+ alias: i18n`指标名称`,
253
355
  location: 'dimension'
254
356
  }
255
357
  ], [
256
358
  {
257
359
  id: foldMeasureValue,
258
- alias: "\u6307\u6807\u503C"
360
+ alias: i18n`指标值`
259
361
  }
260
362
  ], 1, unfoldDimensionGroup);
261
363
  return {
@@ -269,13 +371,13 @@ const dataReshapeFor2D1M = (dataset, dimensions, measures, options)=>{
269
371
  ...dimensions,
270
372
  {
271
373
  id: foldMeasureName,
272
- alias: "\u6307\u6807\u540D\u79F0",
374
+ alias: i18n`指标名称`,
273
375
  location: 'dimension'
274
376
  }
275
377
  ], [
276
378
  {
277
379
  id: foldMeasureValue,
278
- alias: "\u6307\u6807\u503C"
380
+ alias: i18n`指标值`
279
381
  }
280
382
  ], 1, unfoldDimensionGroup);
281
383
  return {
@@ -306,13 +408,13 @@ const dataReshapeFor1D1M = (dataset, dimensions, measures, options)=>{
306
408
  const { dataset: finalDataset, unfoldInfo } = unfoldDimensions(foldedDataset, [
307
409
  {
308
410
  id: foldMeasureName,
309
- alias: "\u6307\u6807\u540D\u79F0",
411
+ alias: i18n`指标名称`,
310
412
  location: 'dimension'
311
413
  }
312
414
  ], [
313
415
  {
314
416
  id: foldMeasureValue,
315
- alias: "\u6307\u6807\u503C"
417
+ alias: i18n`指标值`
316
418
  }
317
419
  ], 0, unfoldDimensionGroup);
318
420
  return {
@@ -326,13 +428,13 @@ const dataReshapeFor1D1M = (dataset, dimensions, measures, options)=>{
326
428
  ...dimensions,
327
429
  {
328
430
  id: foldMeasureName,
329
- alias: "\u6307\u6807\u540D\u79F0",
431
+ alias: i18n`指标名称`,
330
432
  location: 'dimension'
331
433
  }
332
434
  ], [
333
435
  {
334
436
  id: foldMeasureValue,
335
- alias: "\u6307\u6807\u503C"
437
+ alias: i18n`指标值`
336
438
  }
337
439
  ], 0, unfoldDimensionGroup);
338
440
  return {
@@ -712,8 +814,17 @@ const annotation_annotation = (advancedVSeed, context)=>{
712
814
  annotation
713
815
  };
714
816
  };
817
+ const locale_locale = (advancedVSeed, context)=>{
818
+ const { vseed } = context;
819
+ const { locale } = vseed;
820
+ return {
821
+ ...advancedVSeed,
822
+ locale: locale || 'zh-CN'
823
+ };
824
+ };
715
825
  const lineAdvancedPipeline = [
716
826
  initAdvancedVSeed,
827
+ locale_locale,
717
828
  autoMeasures,
718
829
  autoDimensions,
719
830
  pivotAdapter([
@@ -730,6 +841,7 @@ const lineAdvancedPipeline = [
730
841
  ];
731
842
  const barAdvancedPipeline = [
732
843
  initAdvancedVSeed,
844
+ locale_locale,
733
845
  autoMeasures,
734
846
  autoDimensions,
735
847
  pivotAdapter([
@@ -746,6 +858,7 @@ const barAdvancedPipeline = [
746
858
  ];
747
859
  const barParallelAdvancedPipeline = [
748
860
  initAdvancedVSeed,
861
+ locale_locale,
749
862
  autoMeasures,
750
863
  autoDimensions,
751
864
  pivotAdapter([
@@ -762,6 +875,7 @@ const barParallelAdvancedPipeline = [
762
875
  ];
763
876
  const barPercentAdvancedPipeline = [
764
877
  initAdvancedVSeed,
878
+ locale_locale,
765
879
  autoMeasures,
766
880
  autoDimensions,
767
881
  pivotAdapter([
@@ -778,6 +892,7 @@ const barPercentAdvancedPipeline = [
778
892
  ];
779
893
  const columnAdvancedPipeline = [
780
894
  initAdvancedVSeed,
895
+ locale_locale,
781
896
  autoMeasures,
782
897
  autoDimensions,
783
898
  pivotAdapter([
@@ -794,6 +909,7 @@ const columnAdvancedPipeline = [
794
909
  ];
795
910
  const columnParallelAdvancedPipeline = [
796
911
  initAdvancedVSeed,
912
+ locale_locale,
797
913
  autoMeasures,
798
914
  autoDimensions,
799
915
  pivotAdapter([
@@ -810,6 +926,7 @@ const columnParallelAdvancedPipeline = [
810
926
  ];
811
927
  const columnPercentAdvancedPipeline = [
812
928
  initAdvancedVSeed,
929
+ locale_locale,
813
930
  autoMeasures,
814
931
  autoDimensions,
815
932
  pivotAdapter([
@@ -826,6 +943,7 @@ const columnPercentAdvancedPipeline = [
826
943
  ];
827
944
  const areaAdvancedPipeline = [
828
945
  initAdvancedVSeed,
946
+ locale_locale,
829
947
  autoMeasures,
830
948
  autoDimensions,
831
949
  pivotAdapter([
@@ -842,6 +960,7 @@ const areaAdvancedPipeline = [
842
960
  ];
843
961
  const areaPercentAdvancedPipeline = [
844
962
  initAdvancedVSeed,
963
+ locale_locale,
845
964
  autoMeasures,
846
965
  autoDimensions,
847
966
  pivotAdapter([
@@ -858,6 +977,7 @@ const areaPercentAdvancedPipeline = [
858
977
  ];
859
978
  const pieAdvancedPipeline = [
860
979
  initAdvancedVSeed,
980
+ locale_locale,
861
981
  autoMeasures,
862
982
  autoDimensions,
863
983
  pivotAdapter([
@@ -1161,13 +1281,14 @@ const xBand = (spec, context)=>{
1161
1281
  return result;
1162
1282
  };
1163
1283
  const ANNOTATION_Z_INDEX = 1000;
1164
- const LINEAR_AXIS_INNER_OFFSET_TOP = 5;
1284
+ const LINEAR_AXIS_INNER_OFFSET_TOP = 7;
1165
1285
  const xLinear = (spec, context)=>{
1166
1286
  const result = {
1167
1287
  ...spec
1168
1288
  };
1169
1289
  const { advancedVSeed, vseed } = context;
1170
1290
  const { chartType } = vseed;
1291
+ const { locale } = advancedVSeed;
1171
1292
  const config = advancedVSeed.config?.[chartType]?.xAxis;
1172
1293
  if (!result.axes) result.axes = [];
1173
1294
  if (!config) {
@@ -1193,6 +1314,7 @@ const xLinear = (spec, context)=>{
1193
1314
  min,
1194
1315
  label: {
1195
1316
  visible: label?.visible,
1317
+ formatMethod: (value)=>autoFormatter(value, locale),
1196
1318
  style: {
1197
1319
  fill: label?.labelColor,
1198
1320
  angle: label?.labelAngle,
@@ -1342,6 +1464,7 @@ const yLinear = (spec, context)=>{
1342
1464
  };
1343
1465
  const { advancedVSeed, vseed } = context;
1344
1466
  const { chartType } = vseed;
1467
+ const { locale } = advancedVSeed;
1345
1468
  const config = advancedVSeed.config?.[chartType]?.yAxis;
1346
1469
  if (!result.axes) result.axes = [];
1347
1470
  if (!config) {
@@ -1367,6 +1490,7 @@ const yLinear = (spec, context)=>{
1367
1490
  min,
1368
1491
  label: {
1369
1492
  visible: label?.visible,
1493
+ formatMethod: (value)=>autoFormatter(value, locale),
1370
1494
  style: {
1371
1495
  fill: label?.labelColor,
1372
1496
  angle: label?.labelAngle,
@@ -1478,10 +1602,11 @@ const tooltip_tooltip = (spec, context)=>{
1478
1602
  const measure = findMeasureById(measures, id);
1479
1603
  if (!measure) return String(value);
1480
1604
  const { format = {}, autoFormat = true } = measure;
1481
- if (format || autoFormat) {
1605
+ if (!isEmpty(format)) {
1482
1606
  const formatter = createFormatter(format);
1483
1607
  return formatter(value);
1484
1608
  }
1609
+ if (autoFormat) return autoFormatter(value);
1485
1610
  return String(value);
1486
1611
  }
1487
1612
  }
@@ -1499,10 +1624,11 @@ const tooltip_tooltip = (spec, context)=>{
1499
1624
  const measure = findMeasureById(measures, id);
1500
1625
  if (!measure) return String(value);
1501
1626
  const { format = {}, autoFormat = true } = measure;
1502
- if (format || autoFormat) {
1627
+ if (!isEmpty(format)) {
1503
1628
  const formatter = createFormatter(format);
1504
1629
  return formatter(value);
1505
1630
  }
1631
+ if (autoFormat) return autoFormatter(value);
1506
1632
  return String(value);
1507
1633
  }
1508
1634
  }
@@ -1516,7 +1642,7 @@ const label_label = (spec, context)=>{
1516
1642
  ...spec
1517
1643
  };
1518
1644
  const { advancedVSeed } = context;
1519
- const { measures, datasetReshapeInfo } = advancedVSeed;
1645
+ const { measures, datasetReshapeInfo, locale } = advancedVSeed;
1520
1646
  const baseConfig = advancedVSeed.baseConfig.vchart;
1521
1647
  if (!baseConfig || !baseConfig.label) return result;
1522
1648
  const { measureId } = datasetReshapeInfo[0].foldInfo;
@@ -1529,10 +1655,12 @@ const label_label = (spec, context)=>{
1529
1655
  const measure = findMeasureById(measures, id);
1530
1656
  if (!measure) return value;
1531
1657
  const { format = {}, autoFormat = true } = measure;
1532
- if (format || autoFormat) {
1658
+ if (!isEmpty(format)) {
1533
1659
  const formatter = createFormatter(format);
1534
1660
  return formatter(value);
1535
1661
  }
1662
+ if (autoFormat) return autoFormatter(value, locale);
1663
+ return String(value);
1536
1664
  }
1537
1665
  };
1538
1666
  return result;
@@ -3432,6 +3560,7 @@ class Builder {
3432
3560
  _spec = null;
3433
3561
  constructor(vseed){
3434
3562
  this._vseed = vseed;
3563
+ this._vseed.locale = vseed.locale || intl.getLocale();
3435
3564
  }
3436
3565
  build = ()=>build(this);
3437
3566
  buildSpec = (advanced)=>buildSpec(this, advanced);
@@ -4040,7 +4169,7 @@ const zMeasure = z.object({
4040
4169
  alias: z.string().optional(),
4041
4170
  visible: z.boolean().default(true).optional(),
4042
4171
  autoFormat: z.boolean().default(true).optional(),
4043
- format: zNumFormat.optional()
4172
+ format: zNumFormat["default"]({}).optional()
4044
4173
  });
4045
4174
  const zMeasureGroup = z.object({
4046
4175
  id: z.string(),
@@ -4675,6 +4804,6 @@ const zCustomThemeConfig = z.object({
4675
4804
  });
4676
4805
  const zCustomTheme = z.record(z.string(), zCustomThemeConfig).optional();
4677
4806
  const zTheme = z.string();
4678
- export { Builder, FoldMeasureId, FoldMeasureName, FoldMeasureValue, ORIGINAL_DATA, Separator, UnfoldDimensionGroup, areaAdvancedPipeline, areaPercentAdvancedPipeline, areaPercentSpecPipeline, areaSpecPipeline, barAdvancedPipeline, barParallelAdvancedPipeline, barParallelSpecPipeline, barPercentAdvancedPipeline, barPercentSpecPipeline, barSpecPipeline, columnAdvancedPipeline, columnParallelAdvancedPipeline, columnParallelSpecPipeline, columnPercentAdvancedPipeline, columnPercentSpecPipeline, columnSpecPipeline, createFormatter, createNumFormatter, darkTheme, dataReshapeFor1D1M, dataReshapeFor2D1M, execPipeline, findMeasureById, foldMeasures, isPivotChart, isVChart, isVTable, lightTheme, lineAdvancedPipeline, lineSpecPipeline, pieAdvancedPipeline, pieSpecPipeline, all_registerAll as registerAll, registerArea, registerAreaPercent, registerBar, registerBarParallel, registerBarPercent, registerColumn, registerColumnParallel, registerColumnPercent, registerCustomTheme, registerDarkTheme, registerLightTheme, registerLine, unfoldDimensions, zAnnotation, zAnnotationArea, zAnnotationHorizontalLine, zAnnotationPoint, zAnnotationVerticalLine, zAreaStyle, zAxis, zBackgroundColor, zBarStyle, zBaseConfig, zChartType, zColor, zConfig, zCrosshairLine, zCrosshairRect, zCustomTheme, zCustomThemeConfig, zDataset, zDatasetReshapeInfo, zDatum, zDimension, zDimensions, zEncoding, zFoldInfo, zLabel, zLegend, zLineStyle, zMarkStyle, zMeasure, zMeasureGroup, zMeasures, zNumFormat, zPointStyle, zStackCornerRadius, zTheme, zTooltip, zUnfoldInfo, zXBandAxis, zXLinearAxis, zYBandAxis, zYLinearAxis };
4807
+ export { Builder, FoldMeasureId, FoldMeasureName, FoldMeasureValue, ORIGINAL_DATA, Separator, UnfoldDimensionGroup, areaAdvancedPipeline, areaPercentAdvancedPipeline, areaPercentSpecPipeline, areaSpecPipeline, autoFormatter, autoNumFormatter, barAdvancedPipeline, barParallelAdvancedPipeline, barParallelSpecPipeline, barPercentAdvancedPipeline, barPercentSpecPipeline, barSpecPipeline, columnAdvancedPipeline, columnParallelAdvancedPipeline, columnParallelSpecPipeline, columnPercentAdvancedPipeline, columnPercentSpecPipeline, columnSpecPipeline, createFormatter, createNumFormatter, darkTheme, dataReshapeFor1D1M, dataReshapeFor2D1M, execPipeline, findMeasureById, foldMeasures, i18n, intl, isPivotChart, isVChart, isVTable, lightTheme, lineAdvancedPipeline, lineSpecPipeline, pieAdvancedPipeline, pieSpecPipeline, all_registerAll as registerAll, registerArea, registerAreaPercent, registerBar, registerBarParallel, registerBarPercent, registerColumn, registerColumnParallel, registerColumnPercent, registerCustomTheme, registerDarkTheme, registerLightTheme, registerLine, unfoldDimensions, zAnnotation, zAnnotationArea, zAnnotationHorizontalLine, zAnnotationPoint, zAnnotationVerticalLine, zAreaStyle, zAxis, zBackgroundColor, zBarStyle, zBaseConfig, zChartType, zColor, zConfig, zCrosshairLine, zCrosshairRect, zCustomTheme, zCustomThemeConfig, zDataset, zDatasetReshapeInfo, zDatum, zDimension, zDimensions, zEncoding, zFoldInfo, zLabel, zLegend, zLineStyle, zMarkStyle, zMeasure, zMeasureGroup, zMeasures, zNumFormat, zPointStyle, zStackCornerRadius, zTheme, zTooltip, zUnfoldInfo, zXBandAxis, zXLinearAxis, zYBandAxis, zYLinearAxis };
4679
4808
 
4680
4809
  //# sourceMappingURL=index.js.map