billboard.js 3.6.3 → 3.7.0-next.2

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 (91) hide show
  1. package/dist/billboard.css +6 -1
  2. package/dist/billboard.js +1704 -843
  3. package/dist/billboard.min.css +2 -2
  4. package/dist/billboard.min.js +2 -2
  5. package/dist/billboard.pkgd.js +3819 -2263
  6. package/dist/billboard.pkgd.min.js +6 -3
  7. package/dist/plugin/billboardjs-plugin-bubblecompare.js +2 -2
  8. package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
  9. package/dist/plugin/billboardjs-plugin-sparkline.js +62 -23
  10. package/dist/plugin/billboardjs-plugin-sparkline.min.js +2 -2
  11. package/dist/plugin/billboardjs-plugin-stanford.js +62 -23
  12. package/dist/plugin/billboardjs-plugin-stanford.min.js +2 -2
  13. package/dist/plugin/billboardjs-plugin-tableview.js +48 -14
  14. package/dist/plugin/billboardjs-plugin-tableview.min.js +2 -2
  15. package/dist/plugin/billboardjs-plugin-textoverlap.js +48 -14
  16. package/dist/plugin/billboardjs-plugin-textoverlap.min.js +2 -2
  17. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.js +1087 -1031
  18. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.js +1171 -1112
  19. package/dist/plugin/pkgd/billboardjs-plugin-stanford.js +1222 -1169
  20. package/dist/plugin/pkgd/billboardjs-plugin-tableview.js +1159 -1105
  21. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.js +1365 -1319
  22. package/dist/theme/datalab.css +6 -1
  23. package/dist/theme/datalab.min.css +2 -2
  24. package/dist/theme/graph.css +6 -1
  25. package/dist/theme/graph.min.css +2 -2
  26. package/dist/theme/insight.css +6 -1
  27. package/dist/theme/insight.min.css +2 -2
  28. package/dist-esm/billboard.js +942 -277
  29. package/dist-esm/plugin/billboardjs-plugin-bubblecompare.js +3 -5
  30. package/dist-esm/plugin/billboardjs-plugin-sparkline.js +10 -9
  31. package/dist-esm/plugin/billboardjs-plugin-stanford.js +10 -9
  32. package/dist-esm/plugin/billboardjs-plugin-tableview.js +4 -8
  33. package/dist-esm/plugin/billboardjs-plugin-textoverlap.js +3 -5
  34. package/package.json +44 -38
  35. package/src/Chart/api/chart.ts +13 -4
  36. package/src/Chart/api/export.ts +159 -14
  37. package/src/Chart/api/legend.ts +1 -0
  38. package/src/Chart/api/load.ts +1 -0
  39. package/src/Chart/api/tooltip.ts +11 -2
  40. package/src/ChartInternal/Axis/Axis.ts +1 -1
  41. package/src/ChartInternal/ChartInternal.ts +35 -25
  42. package/src/ChartInternal/data/IData.ts +9 -0
  43. package/src/ChartInternal/data/convert.helper.ts +35 -21
  44. package/src/ChartInternal/data/data.ts +26 -5
  45. package/src/ChartInternal/data/load.ts +3 -0
  46. package/src/ChartInternal/interactions/interaction.ts +42 -33
  47. package/src/ChartInternal/internals/class.ts +3 -4
  48. package/src/ChartInternal/internals/clip.ts +13 -10
  49. package/src/ChartInternal/internals/domain.ts +11 -4
  50. package/src/ChartInternal/internals/format.ts +1 -1
  51. package/src/ChartInternal/internals/grid.ts +1 -1
  52. package/src/ChartInternal/internals/legend.ts +3 -2
  53. package/src/ChartInternal/internals/redraw.ts +12 -5
  54. package/src/ChartInternal/internals/scale.ts +7 -2
  55. package/src/ChartInternal/internals/size.ts +41 -26
  56. package/src/ChartInternal/internals/text.ts +60 -32
  57. package/src/ChartInternal/internals/tooltip.ts +12 -8
  58. package/src/ChartInternal/internals/transform.ts +2 -2
  59. package/src/ChartInternal/internals/type.ts +6 -1
  60. package/src/ChartInternal/shape/arc.ts +145 -68
  61. package/src/ChartInternal/shape/area.ts +3 -0
  62. package/src/ChartInternal/shape/bar.ts +4 -1
  63. package/src/ChartInternal/shape/line.ts +12 -15
  64. package/src/ChartInternal/shape/radar.ts +2 -2
  65. package/src/ChartInternal/shape/shape.ts +10 -5
  66. package/src/ChartInternal/shape/treemap.ts +293 -0
  67. package/src/config/Options/common/tooltip.ts +11 -8
  68. package/src/config/Options/data/data.ts +6 -3
  69. package/src/config/Options/shape/arc.ts +47 -0
  70. package/src/config/Options/shape/gauge.ts +2 -2
  71. package/src/config/Options/shape/pie.ts +1 -1
  72. package/src/config/Options/shape/polar.ts +1 -1
  73. package/src/config/Options/shape/radar.ts +1 -1
  74. package/src/config/Options/shape/spline.ts +1 -1
  75. package/src/config/Options/shape/treemap.ts +53 -0
  76. package/src/config/Store/State.ts +1 -0
  77. package/src/config/classes.ts +7 -0
  78. package/src/config/const.ts +4 -2
  79. package/src/config/resolver/shape.ts +22 -5
  80. package/src/index.esm.ts +2 -1
  81. package/src/module/util.ts +1 -1
  82. package/src/scss/billboard.scss +6 -0
  83. package/src/scss/theme/datalab.scss +6 -0
  84. package/src/scss/theme/graph.scss +6 -0
  85. package/src/scss/theme/insight.scss +6 -0
  86. package/types/bb.d.ts +1 -0
  87. package/types/chart.d.ts +7 -0
  88. package/types/index.d.ts +1 -0
  89. package/types/options.d.ts +42 -624
  90. package/types/options.shape.d.ts +664 -0
  91. package/types/types.d.ts +4 -3
@@ -3,18 +3,31 @@
3
3
  * billboard.js project is licensed under the MIT license
4
4
  */
5
5
  import {namespaces as d3Namespaces} from "d3-selection";
6
- import {document} from "../../module/browser";
6
+ import {document, window} from "../../module/browser";
7
7
  import {isFunction, toArray, getCssRules, mergeObj} from "../../module/util";
8
8
 
9
- type Size = {
9
+ type TExportOption = TSize & {
10
+ preserveAspectRatio: boolean;
11
+ preserveFontStyle: boolean;
12
+ mimeType: string;
13
+ };
14
+
15
+ type TSize = {
16
+ x?: number;
17
+ y?: number;
10
18
  width: number;
11
19
  height: number;
12
20
  };
13
21
 
14
- type ExportOption = Size & {
15
- mimeType: string;
16
- preserveAspectRatio: boolean;
17
- }
22
+ type TTextGlyph = {
23
+ [key: string]: TSize & {
24
+ fill: string;
25
+ fontFamily: string;
26
+ fontSize: string;
27
+ textAnchor: string;
28
+ transform: string;
29
+ }
30
+ };
18
31
 
19
32
  /**
20
33
  * Encode to base64
@@ -23,7 +36,7 @@ type ExportOption = Size & {
23
36
  * @private
24
37
  * @see https://developer.mozilla.org/ko/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
25
38
  */
26
- const b64EncodeUnicode = (str: string): string => btoa(
39
+ const b64EncodeUnicode = (str: string): string => window.btoa?.(
27
40
  encodeURIComponent(str)
28
41
  .replace(/%([0-9A-F]{2})/g, (match, p: number | string): string => String.fromCharCode(Number(`0x${p}`)))
29
42
  );
@@ -36,16 +49,27 @@ const b64EncodeUnicode = (str: string): string => btoa(
36
49
  * @returns {string}
37
50
  * @private
38
51
  */
39
- function nodeToSvgDataUrl(node, option: ExportOption, orgSize: Size) {
52
+ function nodeToSvgDataUrl(node, option: TExportOption, orgSize: TSize) {
40
53
  const {width, height} = option || orgSize;
41
54
  const serializer = new XMLSerializer();
42
55
  const clone = node.cloneNode(true);
43
56
  const cssText = getCssRules(toArray(document.styleSheets))
44
- .filter((r: any) => r.cssText)
45
- .map((r: any) => r.cssText);
57
+ .filter((r: CSSStyleRule) => r.cssText)
58
+ .map((r: CSSStyleRule) => r.cssText);
46
59
 
47
60
  clone.setAttribute("xmlns", d3Namespaces.xhtml);
48
61
 
62
+ // remove padding & margin
63
+ clone.style.margin = "0";
64
+ clone.style.padding = "0";
65
+
66
+ // remove text nodes
67
+ if (option.preserveFontStyle) {
68
+ clone.querySelectorAll("text").forEach(t => {
69
+ t.innerHTML = "";
70
+ });
71
+ }
72
+
49
73
  const nodeXml = serializer.serializeToString(clone);
50
74
 
51
75
  // escape css for XML
@@ -69,12 +93,117 @@ function nodeToSvgDataUrl(node, option: ExportOption, orgSize: Size) {
69
93
  return `data:image/svg+xml;base64,${b64EncodeUnicode(dataStr)}`;
70
94
  }
71
95
 
96
+ /**
97
+ * Get coordinate of the element
98
+ * @param {SVGElement} elem Target element
99
+ * @param {object} svgOffset SVG offset
100
+ * @returns {object}
101
+ * @private
102
+ */
103
+ function getCoords(elem, svgOffset): TSize {
104
+ const {top, left} = svgOffset;
105
+ const {x, y} = elem.getBBox();
106
+ const {a, b, c, d, e, f} = elem.getScreenCTM();
107
+ const {width, height} = elem.getBoundingClientRect();
108
+
109
+ return {
110
+ x: (a * x) + (c * y) + e - left,
111
+ y: (b * x) + (d * y) + f - top + (height - Math.round(height / 4)),
112
+ width,
113
+ height
114
+ };
115
+ }
116
+
117
+ /**
118
+ * Get text glyph
119
+ * @param {SVGTextElement} svg Target svg node
120
+ * @returns {Array}
121
+ * @private
122
+ */
123
+ function getGlyph(svg: SVGElement): TTextGlyph[] {
124
+ const {left, top} = svg.getBoundingClientRect();
125
+ const filterFn = t => t.textContent || t.childElementCount;
126
+ const glyph: TTextGlyph[] = [];
127
+
128
+ toArray(svg.querySelectorAll("text"))
129
+ .filter(filterFn)
130
+ .forEach((t: SVGTextElement) => { // eslint-disable-line
131
+ const getStyleFn = (ts: SVGTextElement): TTextGlyph => {
132
+ const {fill, fontFamily, fontSize, textAnchor, transform} = window.getComputedStyle(ts);
133
+ const {x, y, width, height} = getCoords(ts, {left, top});
134
+
135
+ return {
136
+ [ts.textContent as string]: {
137
+ x, y, width, height, fill, fontFamily, fontSize, textAnchor, transform
138
+ }
139
+ };
140
+ };
141
+
142
+ if (t.childElementCount > 1) {
143
+ const text: TTextGlyph[] = [];
144
+
145
+ toArray(t.querySelectorAll("tspan"))
146
+ .filter(filterFn)
147
+ .forEach((ts: SVGTSpanElement) => {
148
+ glyph.push(getStyleFn(ts));
149
+ });
150
+
151
+ return text;
152
+ } else {
153
+ glyph.push(getStyleFn(t));
154
+ }
155
+ });
156
+
157
+ return glyph;
158
+ }
159
+
160
+ /**
161
+ * Render text glyph
162
+ * - NOTE: Called when the 'preserveFontStyle' option is true
163
+ * @param {CanvasRenderingContext2D} ctx Canvas context
164
+ * @param {Array} glyph Text glyph array
165
+ * @private
166
+ */
167
+ function renderText(ctx, glyph): void {
168
+ glyph.forEach(g => {
169
+ Object.keys(g).forEach(key => {
170
+ const {x, y, width, height, fill, fontFamily, fontSize, transform} = g[key];
171
+
172
+ ctx.save();
173
+
174
+ ctx.font = `${fontSize} ${fontFamily}`;
175
+ ctx.fillStyle = fill;
176
+
177
+ if (transform === "none") {
178
+ ctx.fillText(key, x, y);
179
+ } else {
180
+ const args = transform
181
+ .replace(/(matrix|\(|\))/g, "")
182
+ .split(",");
183
+
184
+ if (args.splice(4).every(v => +v === 0)) {
185
+ args.push(x + width - (width / 4));
186
+ args.push(y - height + (height / 3));
187
+ } else {
188
+ args.push(x);
189
+ args.push(y);
190
+ }
191
+
192
+ ctx.transform(...args);
193
+ ctx.fillText(key, 0, 0);
194
+ }
195
+
196
+ ctx.restore();
197
+ });
198
+ });
199
+ }
200
+
72
201
  export default {
73
202
  /**
74
203
  * Export chart as an image.
75
204
  * - **NOTE:**
76
205
  * - IE11 and below not work properly due to the lack of the feature(<a href="https://msdn.microsoft.com/en-us/library/hh834675(v=vs.85).aspx">foreignObject</a>) support
77
- * - The basic CSS file(ex. billboard.css) should be at same domain as API call context to get correct styled export image.
206
+ * - Every style applied to the chart & the basic CSS file(ex. billboard.css) should be at same domain as API call context to get correct styled export image.
78
207
  * @function export
79
208
  * @instance
80
209
  * @memberof Chart
@@ -83,6 +212,12 @@ export default {
83
212
  * @param {number} [option.width={currentWidth}] width
84
213
  * @param {number} [option.height={currentHeigth}] height
85
214
  * @param {boolean} [option.preserveAspectRatio=true] Preserve aspect ratio on given size
215
+ * @param {boolean} [option.preserveFontStyle=false] Preserve font style(font-family).<br>
216
+ * **NOTE:**
217
+ * - This option is useful when outlink web font style's `font-family` are applied to chart's text element.
218
+ * - Text element's position(especially "transformed") can't be preserved correctly according the page's layout condition.
219
+ * - If need to preserve accurate text position, embed the web font data within to the page and set `preserveFontStyle=false`.
220
+ * - Checkout the embed example: <a href="https://stackblitz.com/edit/zfbya9-8nf9nn?file=index.html">https://stackblitz.com/edit/zfbya9-8nf9nn?file=index.html</a>
86
221
  * @param {Function} [callback] The callback to be invoked when export is ready.
87
222
  * @returns {string} dataURI
88
223
  * @example
@@ -106,23 +241,26 @@ export default {
106
241
  * width: 800,
107
242
  * height: 600,
108
243
  * preserveAspectRatio: false,
244
+ * preserveFontStyle: false,
109
245
  * mimeType: "image/png"
110
246
  * },
111
247
  * dataUrl => { ... }
112
248
  * );
113
249
  */
114
- export(option?: ExportOption, callback?: (dataUrl: string) => void): string {
250
+ export(option?: TExportOption, callback?: (dataUrl: string) => void): string {
115
251
  const $$ = this.internal;
116
- const {state, $el: {chart}} = $$;
252
+ const {state, $el: {chart, svg}} = $$;
117
253
  const {width, height} = state.current;
118
254
  const opt = mergeObj({
119
255
  width,
120
256
  height,
121
257
  preserveAspectRatio: true,
258
+ preserveFontStyle: false,
122
259
  mimeType: "image/png"
123
- }, option) as ExportOption;
260
+ }, option) as TExportOption;
124
261
 
125
262
  const svgDataUrl = nodeToSvgDataUrl(chart.node(), opt, {width, height});
263
+ const glyph = opt.preserveFontStyle ? getGlyph(svg.node()) : [];
126
264
 
127
265
  if (callback && isFunction(callback)) {
128
266
  const img = new Image();
@@ -136,6 +274,13 @@ export default {
136
274
  canvas.height = opt.height || height;
137
275
  ctx.drawImage(img, 0, 0);
138
276
 
277
+ if (glyph.length) {
278
+ renderText(ctx, glyph);
279
+
280
+ // release glyph array
281
+ glyph.length = 0;
282
+ }
283
+
139
284
  callback.bind(this)(canvas.toDataURL(opt.mimeType));
140
285
  };
141
286
 
@@ -9,6 +9,7 @@
9
9
  const legend = {
10
10
  /**
11
11
  * Show legend for each target.
12
+ * - **NOTE:** Legend APIs aren't supported for `treemap` type.
12
13
  * @function legend․show
13
14
  * @instance
14
15
  * @memberof Chart
@@ -174,6 +174,7 @@ export default {
174
174
  window.requestIdleCallback(() => $$.loadFromArgs(args));
175
175
  });
176
176
  } else {
177
+ $$.api.tooltip.hide();
177
178
  $$.loadFromArgs(args);
178
179
  }
179
180
  },
@@ -41,6 +41,13 @@ const tooltip = {
41
41
  * x: new Date("2018-01-02 00:00")
42
42
  * });
43
43
  *
44
+ * // treemap type can be shown by using "id" only.
45
+ * chart.tooltip.show({
46
+ * data: {
47
+ * id: "data1" // data id
48
+ * }
49
+ * });
50
+ *
44
51
  * // when data.xs is used
45
52
  * chart.tooltip.show({
46
53
  * data: {
@@ -61,7 +68,7 @@ const tooltip = {
61
68
  */
62
69
  show: function(args): void {
63
70
  const $$ = this.internal;
64
- const {config, state: {inputType}} = $$;
71
+ const {$el, config, state: {eventReceiver, hasTreemap, inputType}} = $$;
65
72
  let index;
66
73
  let mouse;
67
74
 
@@ -75,7 +82,9 @@ const tooltip = {
75
82
  const {data} = args;
76
83
  const y = $$.getYScaleById(data.id)(data.value);
77
84
 
78
- if ($$.isMultipleX()) {
85
+ if (hasTreemap && data.id) {
86
+ eventReceiver.rect = $el.main.select(`${$$.selectorTarget(data.id, undefined, "rect")}`);
87
+ } else if ($$.isMultipleX()) {
79
88
  // if multiple xs, target point will be determined by mouse
80
89
  mouse = [$$.scale.x(data.x), y];
81
90
  } else {
@@ -580,7 +580,7 @@ class Axis {
580
580
  const currentTickMax = current.maxTickWidths[id];
581
581
  let maxWidth = 0;
582
582
 
583
- if (withoutRecompute || !config[`axis_${id}_show`] || $$.filterTargetsToShow().length === 0) {
583
+ if (withoutRecompute || !config[`axis_${id}_show`] || (currentTickMax.size > 0 && $$.filterTargetsToShow().length === 0)) {
584
584
  return currentTickMax.size;
585
585
  }
586
586
 
@@ -180,8 +180,9 @@ export default class ChartInternal {
180
180
 
181
181
  checkModuleImport($$);
182
182
 
183
- state.hasAxis = !$$.hasArcType();
184
183
  state.hasRadar = !state.hasAxis && $$.hasType("radar");
184
+ state.hasTreemap = !state.hasAxis && $$.hasType("treemap");
185
+ state.hasAxis = !$$.hasArcType() && !state.hasTreemap;
185
186
 
186
187
  // datetime to be used for uniqueness
187
188
  state.datetimeId = `bb-${+new Date() * (getRandom() as number)}`;
@@ -284,9 +285,9 @@ export default class ChartInternal {
284
285
  $$.point = $$.generatePoint();
285
286
  }
286
287
 
287
- if (state.hasAxis) {
288
- $$.initClip();
288
+ $$.initClip();
289
289
 
290
+ if (state.hasAxis) {
290
291
  format.extraLineClasses = $$.generateExtraLineClass();
291
292
  format.dataTime = config.data_xLocaltime ? d3TimeParse : d3UtcParse;
292
293
  format.axisTime = config.axis_x_localtime ? d3TimeFormat : d3UtcFormat;
@@ -331,7 +332,7 @@ export default class ChartInternal {
331
332
  initWithData(data): void {
332
333
  const $$ = <any> this;
333
334
  const {config, scale, state, $el, org} = $$;
334
- const {hasAxis} = state;
335
+ const {hasAxis, hasTreemap} = state;
335
336
  const hasInteraction = config.interaction_enabled;
336
337
  const hasPolar = $$.hasType("polar");
337
338
 
@@ -371,25 +372,27 @@ export default class ChartInternal {
371
372
  $$.updateScales(true);
372
373
 
373
374
  // retrieve scale after the 'updateScales()' is called
374
- const {x, y, y2, subX, subY, subY2} = scale;
375
+ if (hasAxis) {
376
+ const {x, y, y2, subX, subY, subY2} = scale;
375
377
 
376
- // Set domains for each scale
377
- if (x) {
378
- x.domain(sortValue($$.getXDomain($$.data.targets)));
379
- subX.domain(x.domain());
378
+ // Set domains for each scale
379
+ if (x) {
380
+ x.domain(sortValue($$.getXDomain($$.data.targets)));
381
+ subX.domain(x.domain());
380
382
 
381
- // Save original x domain for zoom update
382
- org.xDomain = x.domain();
383
- }
383
+ // Save original x domain for zoom update
384
+ org.xDomain = x.domain();
385
+ }
384
386
 
385
- if (y) {
386
- y.domain($$.getYDomain($$.data.targets, "y"));
387
- subY.domain(y.domain());
388
- }
387
+ if (y) {
388
+ y.domain($$.getYDomain($$.data.targets, "y"));
389
+ subY.domain(y.domain());
390
+ }
389
391
 
390
- if (y2) {
391
- y2.domain($$.getYDomain($$.data.targets, "y2"));
392
- subY2 && subY2.domain(y2.domain());
392
+ if (y2) {
393
+ y2.domain($$.getYDomain($$.data.targets, "y2"));
394
+ subY2 && subY2.domain(y2.domain());
395
+ }
393
396
  }
394
397
 
395
398
  // -- Basic Elements --
@@ -412,7 +415,8 @@ export default class ChartInternal {
412
415
  // Define defs
413
416
  const hasColorPatterns = (isFunction(config.color_tiles) && $$.patterns);
414
417
 
415
- if (hasAxis || hasColorPatterns || config.data_labels_backgroundColors || hasPolar) {
418
+ if (hasAxis || hasColorPatterns || hasPolar || hasTreemap ||
419
+ config.data_labels_backgroundColors) {
416
420
  $el.defs = $el.svg.append("defs");
417
421
 
418
422
  if (hasAxis) {
@@ -438,7 +442,7 @@ export default class ChartInternal {
438
442
  // Define regions
439
443
  const main = $el.svg.append("g")
440
444
  .classed($COMMON.main, true)
441
- .attr("transform", $$.getTranslate("main"));
445
+ .attr("transform", hasTreemap ? null : $$.getTranslate("main"));
442
446
 
443
447
  $el.main = main;
444
448
 
@@ -446,8 +450,9 @@ export default class ChartInternal {
446
450
  config.subchart_show && $$.initSubchart();
447
451
 
448
452
  config.tooltip_show && $$.initTooltip();
453
+
449
454
  config.title_text && $$.initTitle();
450
- config.legend_show && $$.initLegend();
455
+ !hasTreemap && config.legend_show && $$.initLegend();
451
456
 
452
457
  // -- Main Region --
453
458
 
@@ -526,7 +531,7 @@ export default class ChartInternal {
526
531
  */
527
532
  initChartElements(): void {
528
533
  const $$ = <any> this;
529
- const {hasAxis, hasRadar} = $$.state;
534
+ const {hasAxis, hasRadar, hasTreemap} = $$.state;
530
535
  const types: string[] = [];
531
536
 
532
537
  if (hasAxis) {
@@ -537,6 +542,8 @@ export default class ChartInternal {
537
542
  types.push(name);
538
543
  }
539
544
  });
545
+ } else if (hasTreemap) {
546
+ types.push("Treemap");
540
547
  } else {
541
548
  const hasPolar = $$.hasType("polar");
542
549
 
@@ -574,7 +581,7 @@ export default class ChartInternal {
574
581
  candlestick,
575
582
  line: lines,
576
583
  area: areas,
577
- text: texts
584
+ text: texts,
578
585
  }} = $$;
579
586
 
580
587
  $$.api.$ = {
@@ -629,7 +636,7 @@ export default class ChartInternal {
629
636
  */
630
637
  updateTargets(targets): void {
631
638
  const $$ = <any> this;
632
- const {hasAxis, hasRadar} = $$.state;
639
+ const {hasAxis, hasRadar, hasTreemap} = $$.state;
633
640
  const helper = type => $$[`updateTargetsFor${type}`](
634
641
  targets.filter($$[`is${type}Type`].bind($$))
635
642
  );
@@ -661,6 +668,9 @@ export default class ChartInternal {
661
668
  }
662
669
 
663
670
  helper(type);
671
+ // Arc, Polar, Radar
672
+ } else if (hasTreemap) {
673
+ helper("Treemap");
664
674
  }
665
675
 
666
676
  // Point types
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Copyright (c) 2017 ~ present NAVER Corp.
3
3
  * billboard.js project is licensed under the MIT license
4
+ * @ignore
4
5
  */
5
6
  type TDataRow = {
6
7
  value: number | null;
@@ -9,6 +10,14 @@ type TDataRow = {
9
10
  name?: string;
10
11
  };
11
12
 
13
+ export interface ITreemapData {
14
+ name: string;
15
+ id?: string; // for compatibility
16
+ value?: number;
17
+ ratio?: number;
18
+ children?: ITreemapData[];
19
+ }
20
+
12
21
  export interface IDataRow extends TDataRow {
13
22
  x: number;
14
23
  }
@@ -11,7 +11,7 @@ import {
11
11
  tsvParseRows as d3TsvParseRows,
12
12
  } from "d3-dsv";
13
13
 
14
- export {columns, json, rows, url, csv, tsv};
14
+ export {columns, json, jsonTreemap, rows, url, csv, tsv};
15
15
 
16
16
  /***** Functions to be executed on Web Worker *****
17
17
  * NOTE: Don't allowed to use
@@ -85,29 +85,29 @@ function rows(rows) {
85
85
  */
86
86
  function json(json, keysParam) {
87
87
  const newRows: string[][] = [];
88
- const findValueInJson = function(object, path) {
89
- if (object[path] !== undefined) {
90
- return object[path];
91
- }
92
-
93
- const convertedPath = path.replace(/\[(\w+)\]/g, ".$1"); // convert indexes to properties (replace [] with .)
94
- const pathArray = convertedPath.replace(/^\./, "").split("."); // strip a leading dot
95
- let target = object;
96
-
97
- pathArray.some(function(k) {
98
- return !(
99
- target = target && k in target ?
100
- target[k] : undefined
101
- );
102
- });
103
-
104
- return target;
105
- };
106
-
107
88
  let targetKeys: string[];
108
89
  let data;
109
90
 
110
91
  if (Array.isArray(json)) {
92
+ const findValueInJson = function(object, path) {
93
+ if (object[path] !== undefined) {
94
+ return object[path];
95
+ }
96
+
97
+ const convertedPath = path.replace(/\[(\w+)\]/g, ".$1"); // convert indexes to properties (replace [] with .)
98
+ const pathArray = convertedPath.replace(/^\./, "").split("."); // strip a leading dot
99
+ let target = object;
100
+
101
+ pathArray.some(function(k) {
102
+ return !(
103
+ target = target && k in target ?
104
+ target[k] : undefined
105
+ );
106
+ });
107
+
108
+ return target;
109
+ };
110
+
111
111
  if (keysParam.x) {
112
112
  targetKeys = keysParam.value.concat(keysParam.x);
113
113
  } else {
@@ -136,7 +136,7 @@ function json(json, keysParam) {
136
136
  Object.keys(json).forEach(function(key) {
137
137
  const tmp = json[key].concat();
138
138
 
139
- tmp.unshift(key);
139
+ tmp.unshift?.(key);
140
140
  newRows.push(tmp);
141
141
  });
142
142
 
@@ -146,6 +146,20 @@ function json(json, keysParam) {
146
146
  return data;
147
147
  }
148
148
 
149
+ function jsonTreemap(json) {
150
+ const convertKey = v => {
151
+ if (v.children) {
152
+ v.children.forEach(convertKey);
153
+ }
154
+
155
+ v.name = v.id;
156
+ }
157
+
158
+ json.forEach(convertKey);
159
+
160
+ return json;
161
+ }
162
+
149
163
  /***** Functions can't be executed on Web Worker *****/
150
164
  /**
151
165
  * Convert URL data
@@ -313,7 +313,7 @@ export default {
313
313
  const cacheKey = KEY.dataTotalPerIndex;
314
314
  let sum = $$.cache.get(cacheKey);
315
315
 
316
- if ($$.isStackNormalized() && !sum) {
316
+ if (($$.config.data_groups.length || $$.isStackNormalized()) && !sum) {
317
317
  sum = [];
318
318
 
319
319
  $$.data.targets.forEach(row => {
@@ -603,11 +603,11 @@ export default {
603
603
 
604
604
  /**
605
605
  * Get data.order compare function
606
- * @param {boolean} isArc Is for Arc type sort or not
606
+ * @param {boolean} isReversed for Arc & Treemap type sort order needs to be reversed
607
607
  * @returns {Function} compare function
608
608
  * @private
609
609
  */
610
- getSortCompareFn(isArc = false): Function | null {
610
+ getSortCompareFn(isReversed = false): Function | null {
611
611
  const $$ = this;
612
612
  const {config} = $$;
613
613
  const order = config.data_order;
@@ -622,7 +622,7 @@ export default {
622
622
  const t1Sum = "values" in t1 ? t1.values.reduce(reducer, 0) : t1.value;
623
623
  const t2Sum = "values" in t2 ? t2.values.reduce(reducer, 0) : t2.value;
624
624
 
625
- return isArc ?
625
+ return isReversed ?
626
626
  (orderAsc ? t1Sum - t2Sum : t2Sum - t1Sum) :
627
627
  (orderAsc ? t2Sum - t1Sum : t1Sum - t2Sum);
628
628
  };
@@ -883,6 +883,25 @@ export default {
883
883
  return value;
884
884
  },
885
885
 
886
+ /**
887
+ * Set ratio for grouped data
888
+ * @param {Array} data Data array
889
+ * @private
890
+ */
891
+ setRatioForGroupedData(data: (IDataRow | IData)[]): void {
892
+ const $$ = this;
893
+ const {config} = $$;
894
+
895
+ // calculate ratio if grouped data exists
896
+ if (config.data_groups.length && data.some(d => $$.isGrouped(d.id))) {
897
+ const setter = (d: IDataRow) => $$.getRatio("index", d, true);
898
+
899
+ data.forEach(v => {
900
+ "values" in v ? v.values.forEach(setter) : setter(v);
901
+ });
902
+ }
903
+ },
904
+
886
905
  /**
887
906
  * Get ratio value
888
907
  * @param {string} type Ratio for given type
@@ -927,7 +946,7 @@ export default {
927
946
  }
928
947
  }
929
948
 
930
- d.ratio = isNumber(d.value) && total && total[d.index] > 0 ?
949
+ d.ratio = isNumber(d.value) && total ?
931
950
  d.value / total[d.index] : 0;
932
951
 
933
952
  ratio = d.ratio;
@@ -941,6 +960,8 @@ export default {
941
960
 
942
961
  // when all data are 0, return 0
943
962
  ratio = max === 0 ? 0 : Math.abs(d.value) / max;
963
+ } else if (type === "treemap") {
964
+ ratio /= $$.getTotalDataSum(true);
944
965
  }
945
966
  }
946
967
 
@@ -119,6 +119,9 @@ export default {
119
119
  $$.data.targets = $$.data.targets.filter(t => t.id !== id);
120
120
  });
121
121
 
122
+ // since treemap uses different data types, it needs to be transformed
123
+ state.hasTreemap && $$.updateTargetsForTreemap($$.data.targets);
124
+
122
125
  // Update current state chart type and elements list after redraw
123
126
  $$.updateTypesElements();
124
127
  }