@plotdb/chart 3.11.0 → 3.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,49 @@
1
1
  # Change Logs
2
2
 
3
+ ## v3.14.0
4
+
5
+ - new `utils.labels`: `labels.place(items, {top, bottom, gap})` resolves a set of labels that each
6
+ want to sit next to their own anchor but must not overlap or leave the box. overlapping labels
7
+ are merged into a block and the block is moved to the mean of its members' wanted positions,
8
+ which minimises total displacement - "everyone stays put except the ones that were piled up".
9
+ a plain push-down pass instead accumulates the error onto the last label, pushing it far from
10
+ its own data, which is the one thing this kind of label has to get right. returns `moved` per
11
+ label so the caller only draws a leader where one is needed
12
+ - legend: new `mode` ( `list` | `direct`, default `list` ). `direct` writes each name next to its
13
+ own data instead of stacking a list: the chart supplies the anchors via `legend.anchor(fn)`,
14
+ `utils.labels` resolves the overlaps, and a leader is drawn for each label that had to move.
15
+ falls back to `list` when the chart supplies no anchor or the legend is horizontal, and
16
+ `actions` is off in `direct` ( the two shortcuts would have to sit somewhere fixed, which
17
+ contradicts the whole idea ). `@makechart/line` implements the anchors
18
+
19
+
20
+ ## v3.13.0
21
+
22
+ - empty state: when there is no data, the core writes one line in the middle of the plot area.
23
+ `common.empty` configures it ( `enabled`, `text`, `color`, `opacity`, `font.size` ), and the
24
+ text falls back through `cfg` -> i18n module -> a built-in dictionary ( en / zh-TW / zh-CN / ja ),
25
+ the same chain the legend uses. drawn by the core rather than by each chart on purpose: each
26
+ chart doing its own would misalign, and alignment is the one thing this kind of hint has to get
27
+ right. the core only ever has one message to give - it sees the raw the host handed it, so it
28
+ cannot tell "there was never any data" from "everything got filtered out", and that is fine:
29
+ the second is also "no data to show". a chart whose backdrop still means something without data
30
+ ( a map, say ) overrides `common.empty.enabled`'s default in its own `mod.config`.
31
+ charts with no empty data are untouched - no node is created at all
32
+
33
+
34
+ ## v3.12.0
35
+
36
+ - brush: `common.brush` styles the selection rectangle and the handles ( `fill`, `fillOpacity`,
37
+ `stroke`, `strokeOpacity`, `strokeWidth`, `handleFill`, `handleOpacity` ). d3's convention is a
38
+ light fill with a white edge, which on a dark theme reads as a glowing block, and until now the
39
+ only way out was for the host to reach into the chart block's own DOM with CSS. the node
40
+ structure ( `rect.overlay` / `rect.selection` / `rect.handle` ) belongs to `utils.brush`, so the
41
+ core styles it directly and charts pass nothing down. every property defaults to unset and an
42
+ unset property is not touched at all, so the serialized output of a chart that doesn't configure
43
+ it is unchanged - including `@makechart/bar`'s own `shape-rendering` on the selection.
44
+ the handles are transparent by default ( they are hit areas ); `handleFill` makes them visible
45
+
46
+
3
47
  ## v3.11.0
4
48
 
5
49
  - axis: `label.direction` gains `angled` and `auto`, plus `label.angle` ( degrees, default 45 ).
package/bundle.js CHANGED
@@ -901,8 +901,85 @@ var module = {exports: {}};
901
901
  if (this.mod.render) {
902
902
  this.mod.render.apply(this);
903
903
  }
904
+ this._brushStyle();
905
+ this._empty();
904
906
  return this._anno();
905
907
  },
908
+ _emptyText: function(){
909
+ var cfg, k, i, r, lng, res;
910
+ cfg = ((this.cfg || {}).common || {}).empty || {};
911
+ if (cfg.text) {
912
+ return cfg.text;
913
+ }
914
+ k = chart.i18nNs + ":text";
915
+ i = (typeof block != 'undefined' && block !== null) && block.i18n && block.i18n.module ? block.i18n.module : null;
916
+ if (i && i.t && (r = i.t(k)) && r !== k && r !== 'text') {
917
+ return r;
918
+ }
919
+ lng = (i ? i.language || i.lng : null) || (typeof document != 'undefined' && document !== null ? document.documentElement.lang : '') || 'en';
920
+ res = chart.i18n[lng] || chart.i18n[lng.split('-')[0]] || (/^zh/i.exec(lng) ? chart.i18n[/cn|hans|sg/i.exec(lng) ? 'zh-CN' : 'zh-TW'] : void 8) || chart.i18n.en;
921
+ return res.text;
922
+ },
923
+ _empty: function(){
924
+ var cfg, show, box, t;
925
+ cfg = ((this.cfg || {}).common || {}).empty || {};
926
+ show = (cfg.enabled != null ? !!cfg.enabled : true) && !(this.data || []).length;
927
+ if (!this.svg) {
928
+ return;
929
+ }
930
+ if (!show) {
931
+ if (this._emptyG && this._emptyG.parentNode) {
932
+ this._emptyG.parentNode.removeChild(this._emptyG);
933
+ }
934
+ return;
935
+ }
936
+ if (!this._emptyG) {
937
+ this._emptyG = document.createElementNS(svgns, 'g');
938
+ this._emptyG.setAttribute('data-type', 'empty');
939
+ this._emptyG.style.pointerEvents = 'none';
940
+ }
941
+ this.svg.appendChild(this._emptyG);
942
+ this._emptyG.textContent = '';
943
+ box = this._annoBox();
944
+ t = document.createElementNS(svgns, 'text');
945
+ t.setAttribute('x', box.x + box.width / 2);
946
+ t.setAttribute('y', box.y + box.height / 2);
947
+ t.setAttribute('text-anchor', 'middle');
948
+ t.setAttribute('dominant-baseline', 'middle');
949
+ t.style.fill = cfg.color || 'currentColor';
950
+ t.style.opacity = cfg.opacity != null ? cfg.opacity : 0.45;
951
+ if ((cfg.font || {}).size) {
952
+ t.style.fontSize = cfg.font.size + "";
953
+ }
954
+ t.textContent = this._emptyText();
955
+ return this._emptyG.appendChild(t);
956
+ },
957
+ _brushStyle: function(){
958
+ var cfg, set, i$, ref$, len$, n, results$ = [];
959
+ cfg = ((this.cfg || {}).common || {}).brush || {};
960
+ if (!this.svg) {
961
+ return;
962
+ }
963
+ set = function(n, k, v){
964
+ if (v != null && v !== '') {
965
+ return n.setAttribute(k, v);
966
+ }
967
+ };
968
+ for (i$ = 0, len$ = (ref$ = this.svg.querySelectorAll('rect.selection')).length; i$ < len$; ++i$) {
969
+ n = ref$[i$];
970
+ set(n, 'fill', cfg.fill);
971
+ set(n, 'fill-opacity', cfg.fillOpacity);
972
+ set(n, 'stroke', cfg.stroke);
973
+ set(n, 'stroke-opacity', cfg.strokeOpacity);
974
+ set(n, 'stroke-width', cfg.strokeWidth);
975
+ }
976
+ for (i$ = 0, len$ = (ref$ = this.svg.querySelectorAll('rect.handle')).length; i$ < len$; ++i$) {
977
+ n = ref$[i$];
978
+ set(n, 'fill', cfg.handleFill);
979
+ results$.push(set(n, 'fill-opacity', cfg.handleOpacity));
980
+ }
981
+ return results$;
982
+ },
906
983
  _annoBox: function(){
907
984
  var b;
908
985
  if (this.layout) {
@@ -1518,6 +1595,81 @@ var module = {exports: {}};
1518
1595
  'default': 'transparent'
1519
1596
  }
1520
1597
  },
1598
+ empty: {
1599
+ enabled: {
1600
+ type: 'boolean',
1601
+ 'default': true
1602
+ },
1603
+ text: {
1604
+ type: 'text',
1605
+ 'default': ''
1606
+ },
1607
+ color: {
1608
+ type: 'color',
1609
+ 'default': 'currentColor'
1610
+ },
1611
+ opacity: {
1612
+ type: 'number',
1613
+ 'default': 0.45,
1614
+ min: 0,
1615
+ max: 1,
1616
+ step: 0.05
1617
+ },
1618
+ font: {
1619
+ size: {
1620
+ type: 'quantity',
1621
+ 'default': '',
1622
+ units: [{
1623
+ name: 'em',
1624
+ max: 10,
1625
+ step: 0.01,
1626
+ 'default': 1
1627
+ }]
1628
+ }
1629
+ }
1630
+ },
1631
+ brush: {
1632
+ fill: {
1633
+ type: 'color',
1634
+ 'default': ''
1635
+ },
1636
+ fillOpacity: {
1637
+ type: 'number',
1638
+ 'default': '',
1639
+ min: 0,
1640
+ max: 1,
1641
+ step: 0.05
1642
+ },
1643
+ stroke: {
1644
+ type: 'color',
1645
+ 'default': ''
1646
+ },
1647
+ strokeOpacity: {
1648
+ type: 'number',
1649
+ 'default': '',
1650
+ min: 0,
1651
+ max: 1,
1652
+ step: 0.05
1653
+ },
1654
+ strokeWidth: {
1655
+ type: 'number',
1656
+ 'default': '',
1657
+ min: 0,
1658
+ max: 20,
1659
+ step: 0.5
1660
+ },
1661
+ handleFill: {
1662
+ type: 'color',
1663
+ 'default': ''
1664
+ },
1665
+ handleOpacity: {
1666
+ type: 'number',
1667
+ 'default': '',
1668
+ min: 0,
1669
+ max: 1,
1670
+ step: 0.05
1671
+ }
1672
+ },
1521
1673
  annotation: {
1522
1674
  enabled: {
1523
1675
  type: 'boolean',
@@ -1549,6 +1701,21 @@ var module = {exports: {}};
1549
1701
  }
1550
1702
  }
1551
1703
  };
1704
+ chart.i18nNs = "@plotdb/chart";
1705
+ chart.i18n = {
1706
+ en: {
1707
+ text: "No data to show"
1708
+ },
1709
+ "zh-TW": {
1710
+ text: "沒有可顯示的資料"
1711
+ },
1712
+ "zh-CN": {
1713
+ text: "没有可显示的数据"
1714
+ },
1715
+ ja: {
1716
+ text: "表示するデータがありません"
1717
+ }
1718
+ };
1552
1719
  chart.scopes = {
1553
1720
  space: {
1554
1721
  x: function(v){
@@ -4762,6 +4929,11 @@ var chart = module.exports;
4762
4929
  }
4763
4930
  },
4764
4931
  legend: {
4932
+ mode: {
4933
+ type: 'choice',
4934
+ values: ['list', 'direct'],
4935
+ 'default': 'list'
4936
+ },
4765
4937
  selectable: {
4766
4938
  type: 'boolean',
4767
4939
  'default': true
@@ -5314,6 +5486,100 @@ var chart = module.exports;
5314
5486
  (chart.utils || (chart.utils = {})).data = main;
5315
5487
  }
5316
5488
  }).call(this);
5489
+ (function(){
5490
+ var labels;
5491
+ labels = {};
5492
+ labels.place = function(items, opt){
5493
+ var gap, top, bottom, heightOf, order, blocks, i$, len$, i, b, a, pos, pushDown, last, hi, ret;
5494
+ items == null && (items = []);
5495
+ opt == null && (opt = {});
5496
+ if (!items.length) {
5497
+ return [];
5498
+ }
5499
+ gap = opt.gap || 0;
5500
+ top = opt.top != null ? opt.top : 0;
5501
+ bottom = opt.bottom != null ? opt.bottom : Infinity;
5502
+ heightOf = function(d){
5503
+ return (d || {}).height || 0;
5504
+ };
5505
+ order = items.map(function(d, i){
5506
+ return i;
5507
+ });
5508
+ order.sort(function(a, b){
5509
+ return items[a].y - items[b].y || a - b;
5510
+ });
5511
+ blocks = [];
5512
+ for (i$ = 0, len$ = order.length; i$ < len$; ++i$) {
5513
+ i = order[i$];
5514
+ blocks.push({
5515
+ list: [i],
5516
+ h: heightOf(items[i]),
5517
+ sum: items[i].y,
5518
+ n: 1
5519
+ });
5520
+ while (blocks.length > 1) {
5521
+ b = blocks[blocks.length - 1];
5522
+ a = blocks[blocks.length - 2];
5523
+ if (a.sum / a.n + a.h + gap <= b.sum / b.n) {
5524
+ break;
5525
+ }
5526
+ a.sum += b.sum - b.n * (a.h + gap);
5527
+ a.n += b.n;
5528
+ a.h += gap + b.h;
5529
+ a.list = a.list.concat(b.list);
5530
+ blocks.pop();
5531
+ }
5532
+ }
5533
+ pos = blocks.map(function(b){
5534
+ return b.sum / b.n;
5535
+ });
5536
+ pushDown = function(){
5537
+ var i$, to$, i, lo, results$ = [];
5538
+ for (i$ = 1, to$ = blocks.length; i$ < to$; ++i$) {
5539
+ i = i$;
5540
+ lo = pos[i - 1] + blocks[i - 1].h + gap;
5541
+ if (pos[i] < lo) {
5542
+ results$.push(pos[i] = lo);
5543
+ }
5544
+ }
5545
+ return results$;
5546
+ };
5547
+ pushDown();
5548
+ if (bottom < Infinity) {
5549
+ last = blocks.length - 1;
5550
+ if (pos[last] + blocks[last].h > bottom) {
5551
+ pos[last] = bottom - blocks[last].h;
5552
+ }
5553
+ for (i$ = blocks.length - 2; i$ >= 0; --i$) {
5554
+ i = i$;
5555
+ hi = pos[i + 1] - blocks[i].h - gap;
5556
+ if (pos[i] > hi) {
5557
+ pos[i] = hi;
5558
+ }
5559
+ }
5560
+ }
5561
+ if (pos[0] < top) {
5562
+ pos[0] = top;
5563
+ }
5564
+ pushDown();
5565
+ ret = [];
5566
+ blocks.map(function(b, bi){
5567
+ var y;
5568
+ y = pos[bi];
5569
+ return b.list.map(function(i){
5570
+ ret[i] = {
5571
+ y: y,
5572
+ moved: Math.abs(y - items[i].y) > 0.5
5573
+ };
5574
+ return y += heightOf(items[i]) + gap;
5575
+ });
5576
+ });
5577
+ return ret;
5578
+ };
5579
+ if (typeof chart != 'undefined' && chart !== null) {
5580
+ (chart.utils || (chart.utils = {})).labels = labels;
5581
+ }
5582
+ }).call(this);
5317
5583
  (function(){
5318
5584
  var svgns, legend;
5319
5585
  svgns = "http://www.w3.org/2000/svg";
@@ -5342,6 +5608,8 @@ var chart = module.exports;
5342
5608
  return this$.render();
5343
5609
  });
5344
5610
  this.id = Math.random().toString(36).substring(2);
5611
+ this._anchor = null;
5612
+ this._pos = {};
5345
5613
  nodeLayout = this.layout.getNode(this.name);
5346
5614
  nodeRender = this.layout.getGroup(this.name);
5347
5615
  if (!nodeLayout.querySelector('[ld-each=legend]')) {
@@ -5472,9 +5740,17 @@ var chart = module.exports;
5472
5740
  },
5473
5741
  handler: {
5474
5742
  "@": function(arg$){
5475
- var node, ctx;
5743
+ var node, ctx, p;
5476
5744
  node = arg$.node, ctx = arg$.ctx;
5477
- return node.style.opacity = !this$.cfg.selectable || ctx.type === 'func' || this$._picked[ctx.key] ? 1 : 0.3;
5745
+ node.style.opacity = !this$.cfg.selectable || ctx.type === 'func' || this$._picked[ctx.key] ? 1 : 0.3;
5746
+ if (!this$._direct()) {
5747
+ return node.removeAttribute('transform');
5748
+ }
5749
+ p = this$._pos[ctx.key];
5750
+ if (!p) {
5751
+ return node.removeAttribute('transform');
5752
+ }
5753
+ return node.setAttribute('transform', "translate(0," + p.dy + ")");
5478
5754
  },
5479
5755
  "shape": function(arg$){
5480
5756
  var node, ctx, n, box, rbox;
@@ -5573,7 +5849,119 @@ var chart = module.exports;
5573
5849
  return this._lngHandler = null;
5574
5850
  },
5575
5851
  _actions: function(){
5576
- return !!(this.cfg.selectable && (this.cfg.actions != null ? this.cfg.actions : true));
5852
+ return !!(this.cfg.selectable && (this.cfg.actions != null ? this.cfg.actions : true) && !this._direct());
5853
+ },
5854
+ _direct: function(){
5855
+ return this.cfg.mode === 'direct' && !!this._anchor && this.dir === 'vertical';
5856
+ },
5857
+ anchor: function(fn){
5858
+ if (!arguments.length) {
5859
+ return this._anchor;
5860
+ }
5861
+ this._anchor = fn;
5862
+ return this;
5863
+ },
5864
+ _solve: function(){
5865
+ var rbox, items, valid, got, this$ = this;
5866
+ this._pos = {};
5867
+ if (!this._direct()) {
5868
+ return;
5869
+ }
5870
+ rbox = this.layout.getBox(this.name);
5871
+ items = [];
5872
+ this._data.map(function(d){
5873
+ var nbox, y, e;
5874
+ nbox = this$.layout.getBox(this$._name(d, 'text'));
5875
+ y = (function(){
5876
+ try {
5877
+ return this._anchor(d);
5878
+ } catch (e$) {
5879
+ e = e$;
5880
+ return null;
5881
+ }
5882
+ }.call(this$));
5883
+ return items.push({
5884
+ d: d,
5885
+ nbox: nbox,
5886
+ y: y
5887
+ });
5888
+ });
5889
+ valid = items.filter(function(o){
5890
+ return o.y != null && !isNaN(o.y);
5891
+ });
5892
+ if (!valid.length) {
5893
+ return;
5894
+ }
5895
+ got = chart.utils.labels.place(valid.map(function(o){
5896
+ return {
5897
+ y: o.y - o.nbox.height / 2,
5898
+ height: o.nbox.height
5899
+ };
5900
+ }), {
5901
+ top: 0,
5902
+ bottom: rbox.height,
5903
+ gap: 2
5904
+ });
5905
+ return valid.map(function(o, i){
5906
+ var base;
5907
+ base = o.nbox.y - rbox.y;
5908
+ return this$._pos[o.d.key] = {
5909
+ dy: got[i].y - base,
5910
+ y: got[i].y,
5911
+ h: o.nbox.height,
5912
+ moved: got[i].moved,
5913
+ anchor: o.y
5914
+ };
5915
+ });
5916
+ },
5917
+ _leads: function(){
5918
+ var g, k, ref$, p, d, n, ref1$, y0, y1, x1, results$ = [];
5919
+ g = this.layout.getGroup(this.name);
5920
+ if (!this._lead) {
5921
+ this._lead = document.createElementNS(svgns, 'g');
5922
+ this._lead.setAttribute('data-type', 'leader');
5923
+ this._lead.style.pointerEvents = 'none';
5924
+ }
5925
+ if (!this._direct()) {
5926
+ if (this._lead.parentNode) {
5927
+ this._lead.parentNode.removeChild(this._lead);
5928
+ }
5929
+ return;
5930
+ }
5931
+ g.insertBefore(this._lead, g.firstChild);
5932
+ this._lead.textContent = '';
5933
+ for (k in ref$ = this._pos) {
5934
+ p = ref$[k];
5935
+ if (!p.moved) {
5936
+ continue;
5937
+ }
5938
+ d = this._data.filter(fn$)[0];
5939
+ if (!d) {
5940
+ continue;
5941
+ }
5942
+ n = document.createElementNS(svgns, 'path');
5943
+ ref1$ = [p.anchor, p.y + p.h / 2], y0 = ref1$[0], y1 = ref1$[1];
5944
+ x1 = this._shapeX();
5945
+ n.setAttribute('d', "M0 " + y0 + " L" + x1 / 2 + " " + y0 + " L" + x1 / 2 + " " + y1 + " L" + x1 + " " + y1);
5946
+ n.setAttribute('fill', 'none');
5947
+ n.setAttribute('stroke', 'currentColor');
5948
+ n.setAttribute('stroke-width', 1);
5949
+ n.setAttribute('opacity', this._picked[k] ? 0.35 : 0.12);
5950
+ results$.push(this._lead.appendChild(n));
5951
+ }
5952
+ return results$;
5953
+ function fn$(it){
5954
+ return it.key === k;
5955
+ }
5956
+ },
5957
+ _shapeX: function(){
5958
+ var d, rbox;
5959
+ d = this._data[0];
5960
+ if (!d) {
5961
+ return 0;
5962
+ }
5963
+ rbox = this.layout.getBox(this.name);
5964
+ return this.layout.getBox(this._name(d, 'shape')).x - rbox.x;
5577
5965
  },
5578
5966
  _name: function(ctx, type){
5579
5967
  type == null && (type = "s");
@@ -5606,9 +5994,12 @@ var chart = module.exports;
5606
5994
  enabled = this.enabled();
5607
5995
  g = this.layout.getGroup(this.name);
5608
5996
  g.style.display = enabled ? '' : 'none';
5609
- if (enabled) {
5610
- return this.view.render.render();
5997
+ if (!enabled) {
5998
+ return;
5611
5999
  }
6000
+ this._solve();
6001
+ this.view.render.render();
6002
+ return this._leads();
5612
6003
  },
5613
6004
  config: function(cfg){
5614
6005
  var node, this$ = this;