@plotdb/chart 3.13.0 → 3.15.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,38 @@
1
1
  # Change Logs
2
2
 
3
+ ## v3.15.0
4
+
5
+ - axis: `grid` ( `show`, `color`, `opacity`, `width`, `dash` ) draws one line per tick across the
6
+ plot area. three decisions worth stating:
7
+ - the config lives on the axis, because that is where the ticks are, but the drawing happens in
8
+ the plot cell, because that is where the length comes from. the axis takes the cell name as a
9
+ construction option ( `grid`, default `view` - the convention every chart here already
10
+ follows ), so no chart had to change
11
+ - the lines follow the ticks, not the labels. labels that don't fit are only made transparent;
12
+ the ticks stay. a dense faint grid under sparse labels is the normal result and the right one
13
+ - `show` defaults to false. a band axis would draw one line per category, which is noise, so
14
+ which axis deserves a grid is the caller's call - and nothing existing moves by a pixel.
15
+ turning the grid on usually means turning `tick.inner` to 0: a grid and tick marks say the
16
+ same thing twice
17
+
18
+
19
+ ## v3.14.0
20
+
21
+ - new `utils.labels`: `labels.place(items, {top, bottom, gap})` resolves a set of labels that each
22
+ want to sit next to their own anchor but must not overlap or leave the box. overlapping labels
23
+ are merged into a block and the block is moved to the mean of its members' wanted positions,
24
+ which minimises total displacement - "everyone stays put except the ones that were piled up".
25
+ a plain push-down pass instead accumulates the error onto the last label, pushing it far from
26
+ its own data, which is the one thing this kind of label has to get right. returns `moved` per
27
+ label so the caller only draws a leader where one is needed
28
+ - legend: new `mode` ( `list` | `direct`, default `list` ). `direct` writes each name next to its
29
+ own data instead of stacking a list: the chart supplies the anchors via `legend.anchor(fn)`,
30
+ `utils.labels` resolves the overlaps, and a leader is drawn for each label that had to move.
31
+ falls back to `list` when the chart supplies no anchor or the legend is horizontal, and
32
+ `actions` is off in `direct` ( the two shortcuts would have to sit somewhere fixed, which
33
+ contradicts the whole idea ). `@makechart/line` implements the anchors
34
+
35
+
3
36
  ## v3.13.0
4
37
 
5
38
  - empty state: when there is no data, the core writes one line in the middle of the plot area.
package/bundle.js CHANGED
@@ -4929,6 +4929,11 @@ var chart = module.exports;
4929
4929
  }
4930
4930
  },
4931
4931
  legend: {
4932
+ mode: {
4933
+ type: 'choice',
4934
+ values: ['list', 'direct'],
4935
+ 'default': 'list'
4936
+ },
4932
4937
  selectable: {
4933
4938
  type: 'boolean',
4934
4939
  'default': true
@@ -5027,6 +5032,34 @@ var chart = module.exports;
5027
5032
  type: 'boolean',
5028
5033
  'default': true
5029
5034
  },
5035
+ grid: {
5036
+ show: {
5037
+ type: 'boolean',
5038
+ 'default': false
5039
+ },
5040
+ color: {
5041
+ type: 'color',
5042
+ 'default': 'currentColor'
5043
+ },
5044
+ opacity: {
5045
+ type: 'number',
5046
+ 'default': 0.12,
5047
+ min: 0,
5048
+ max: 1,
5049
+ step: 0.01
5050
+ },
5051
+ width: {
5052
+ type: 'number',
5053
+ 'default': 1,
5054
+ min: 0,
5055
+ max: 10,
5056
+ step: 0.5
5057
+ },
5058
+ dash: {
5059
+ type: 'text',
5060
+ 'default': ''
5061
+ }
5062
+ },
5030
5063
  center: {
5031
5064
  type: 'boolean',
5032
5065
  'default': false
@@ -5481,6 +5514,100 @@ var chart = module.exports;
5481
5514
  (chart.utils || (chart.utils = {})).data = main;
5482
5515
  }
5483
5516
  }).call(this);
5517
+ (function(){
5518
+ var labels;
5519
+ labels = {};
5520
+ labels.place = function(items, opt){
5521
+ var gap, top, bottom, heightOf, order, blocks, i$, len$, i, b, a, pos, pushDown, last, hi, ret;
5522
+ items == null && (items = []);
5523
+ opt == null && (opt = {});
5524
+ if (!items.length) {
5525
+ return [];
5526
+ }
5527
+ gap = opt.gap || 0;
5528
+ top = opt.top != null ? opt.top : 0;
5529
+ bottom = opt.bottom != null ? opt.bottom : Infinity;
5530
+ heightOf = function(d){
5531
+ return (d || {}).height || 0;
5532
+ };
5533
+ order = items.map(function(d, i){
5534
+ return i;
5535
+ });
5536
+ order.sort(function(a, b){
5537
+ return items[a].y - items[b].y || a - b;
5538
+ });
5539
+ blocks = [];
5540
+ for (i$ = 0, len$ = order.length; i$ < len$; ++i$) {
5541
+ i = order[i$];
5542
+ blocks.push({
5543
+ list: [i],
5544
+ h: heightOf(items[i]),
5545
+ sum: items[i].y,
5546
+ n: 1
5547
+ });
5548
+ while (blocks.length > 1) {
5549
+ b = blocks[blocks.length - 1];
5550
+ a = blocks[blocks.length - 2];
5551
+ if (a.sum / a.n + a.h + gap <= b.sum / b.n) {
5552
+ break;
5553
+ }
5554
+ a.sum += b.sum - b.n * (a.h + gap);
5555
+ a.n += b.n;
5556
+ a.h += gap + b.h;
5557
+ a.list = a.list.concat(b.list);
5558
+ blocks.pop();
5559
+ }
5560
+ }
5561
+ pos = blocks.map(function(b){
5562
+ return b.sum / b.n;
5563
+ });
5564
+ pushDown = function(){
5565
+ var i$, to$, i, lo, results$ = [];
5566
+ for (i$ = 1, to$ = blocks.length; i$ < to$; ++i$) {
5567
+ i = i$;
5568
+ lo = pos[i - 1] + blocks[i - 1].h + gap;
5569
+ if (pos[i] < lo) {
5570
+ results$.push(pos[i] = lo);
5571
+ }
5572
+ }
5573
+ return results$;
5574
+ };
5575
+ pushDown();
5576
+ if (bottom < Infinity) {
5577
+ last = blocks.length - 1;
5578
+ if (pos[last] + blocks[last].h > bottom) {
5579
+ pos[last] = bottom - blocks[last].h;
5580
+ }
5581
+ for (i$ = blocks.length - 2; i$ >= 0; --i$) {
5582
+ i = i$;
5583
+ hi = pos[i + 1] - blocks[i].h - gap;
5584
+ if (pos[i] > hi) {
5585
+ pos[i] = hi;
5586
+ }
5587
+ }
5588
+ }
5589
+ if (pos[0] < top) {
5590
+ pos[0] = top;
5591
+ }
5592
+ pushDown();
5593
+ ret = [];
5594
+ blocks.map(function(b, bi){
5595
+ var y;
5596
+ y = pos[bi];
5597
+ return b.list.map(function(i){
5598
+ ret[i] = {
5599
+ y: y,
5600
+ moved: Math.abs(y - items[i].y) > 0.5
5601
+ };
5602
+ return y += heightOf(items[i]) + gap;
5603
+ });
5604
+ });
5605
+ return ret;
5606
+ };
5607
+ if (typeof chart != 'undefined' && chart !== null) {
5608
+ (chart.utils || (chart.utils = {})).labels = labels;
5609
+ }
5610
+ }).call(this);
5484
5611
  (function(){
5485
5612
  var svgns, legend;
5486
5613
  svgns = "http://www.w3.org/2000/svg";
@@ -5509,6 +5636,8 @@ var chart = module.exports;
5509
5636
  return this$.render();
5510
5637
  });
5511
5638
  this.id = Math.random().toString(36).substring(2);
5639
+ this._anchor = null;
5640
+ this._pos = {};
5512
5641
  nodeLayout = this.layout.getNode(this.name);
5513
5642
  nodeRender = this.layout.getGroup(this.name);
5514
5643
  if (!nodeLayout.querySelector('[ld-each=legend]')) {
@@ -5639,9 +5768,17 @@ var chart = module.exports;
5639
5768
  },
5640
5769
  handler: {
5641
5770
  "@": function(arg$){
5642
- var node, ctx;
5771
+ var node, ctx, p;
5643
5772
  node = arg$.node, ctx = arg$.ctx;
5644
- return node.style.opacity = !this$.cfg.selectable || ctx.type === 'func' || this$._picked[ctx.key] ? 1 : 0.3;
5773
+ node.style.opacity = !this$.cfg.selectable || ctx.type === 'func' || this$._picked[ctx.key] ? 1 : 0.3;
5774
+ if (!this$._direct()) {
5775
+ return node.removeAttribute('transform');
5776
+ }
5777
+ p = this$._pos[ctx.key];
5778
+ if (!p) {
5779
+ return node.removeAttribute('transform');
5780
+ }
5781
+ return node.setAttribute('transform', "translate(0," + p.dy + ")");
5645
5782
  },
5646
5783
  "shape": function(arg$){
5647
5784
  var node, ctx, n, box, rbox;
@@ -5740,7 +5877,119 @@ var chart = module.exports;
5740
5877
  return this._lngHandler = null;
5741
5878
  },
5742
5879
  _actions: function(){
5743
- return !!(this.cfg.selectable && (this.cfg.actions != null ? this.cfg.actions : true));
5880
+ return !!(this.cfg.selectable && (this.cfg.actions != null ? this.cfg.actions : true) && !this._direct());
5881
+ },
5882
+ _direct: function(){
5883
+ return this.cfg.mode === 'direct' && !!this._anchor && this.dir === 'vertical';
5884
+ },
5885
+ anchor: function(fn){
5886
+ if (!arguments.length) {
5887
+ return this._anchor;
5888
+ }
5889
+ this._anchor = fn;
5890
+ return this;
5891
+ },
5892
+ _solve: function(){
5893
+ var rbox, items, valid, got, this$ = this;
5894
+ this._pos = {};
5895
+ if (!this._direct()) {
5896
+ return;
5897
+ }
5898
+ rbox = this.layout.getBox(this.name);
5899
+ items = [];
5900
+ this._data.map(function(d){
5901
+ var nbox, y, e;
5902
+ nbox = this$.layout.getBox(this$._name(d, 'text'));
5903
+ y = (function(){
5904
+ try {
5905
+ return this._anchor(d);
5906
+ } catch (e$) {
5907
+ e = e$;
5908
+ return null;
5909
+ }
5910
+ }.call(this$));
5911
+ return items.push({
5912
+ d: d,
5913
+ nbox: nbox,
5914
+ y: y
5915
+ });
5916
+ });
5917
+ valid = items.filter(function(o){
5918
+ return o.y != null && !isNaN(o.y);
5919
+ });
5920
+ if (!valid.length) {
5921
+ return;
5922
+ }
5923
+ got = chart.utils.labels.place(valid.map(function(o){
5924
+ return {
5925
+ y: o.y - o.nbox.height / 2,
5926
+ height: o.nbox.height
5927
+ };
5928
+ }), {
5929
+ top: 0,
5930
+ bottom: rbox.height,
5931
+ gap: 2
5932
+ });
5933
+ return valid.map(function(o, i){
5934
+ var base;
5935
+ base = o.nbox.y - rbox.y;
5936
+ return this$._pos[o.d.key] = {
5937
+ dy: got[i].y - base,
5938
+ y: got[i].y,
5939
+ h: o.nbox.height,
5940
+ moved: got[i].moved,
5941
+ anchor: o.y
5942
+ };
5943
+ });
5944
+ },
5945
+ _leads: function(){
5946
+ var g, k, ref$, p, d, n, ref1$, y0, y1, x1, results$ = [];
5947
+ g = this.layout.getGroup(this.name);
5948
+ if (!this._lead) {
5949
+ this._lead = document.createElementNS(svgns, 'g');
5950
+ this._lead.setAttribute('data-type', 'leader');
5951
+ this._lead.style.pointerEvents = 'none';
5952
+ }
5953
+ if (!this._direct()) {
5954
+ if (this._lead.parentNode) {
5955
+ this._lead.parentNode.removeChild(this._lead);
5956
+ }
5957
+ return;
5958
+ }
5959
+ g.insertBefore(this._lead, g.firstChild);
5960
+ this._lead.textContent = '';
5961
+ for (k in ref$ = this._pos) {
5962
+ p = ref$[k];
5963
+ if (!p.moved) {
5964
+ continue;
5965
+ }
5966
+ d = this._data.filter(fn$)[0];
5967
+ if (!d) {
5968
+ continue;
5969
+ }
5970
+ n = document.createElementNS(svgns, 'path');
5971
+ ref1$ = [p.anchor, p.y + p.h / 2], y0 = ref1$[0], y1 = ref1$[1];
5972
+ x1 = this._shapeX();
5973
+ n.setAttribute('d', "M0 " + y0 + " L" + x1 / 2 + " " + y0 + " L" + x1 / 2 + " " + y1 + " L" + x1 + " " + y1);
5974
+ n.setAttribute('fill', 'none');
5975
+ n.setAttribute('stroke', 'currentColor');
5976
+ n.setAttribute('stroke-width', 1);
5977
+ n.setAttribute('opacity', this._picked[k] ? 0.35 : 0.12);
5978
+ results$.push(this._lead.appendChild(n));
5979
+ }
5980
+ return results$;
5981
+ function fn$(it){
5982
+ return it.key === k;
5983
+ }
5984
+ },
5985
+ _shapeX: function(){
5986
+ var d, rbox;
5987
+ d = this._data[0];
5988
+ if (!d) {
5989
+ return 0;
5990
+ }
5991
+ rbox = this.layout.getBox(this.name);
5992
+ return this.layout.getBox(this._name(d, 'shape')).x - rbox.x;
5744
5993
  },
5745
5994
  _name: function(ctx, type){
5746
5995
  type == null && (type = "s");
@@ -5773,9 +6022,12 @@ var chart = module.exports;
5773
6022
  enabled = this.enabled();
5774
6023
  g = this.layout.getGroup(this.name);
5775
6024
  g.style.display = enabled ? '' : 'none';
5776
- if (enabled) {
5777
- return this.view.render.render();
6025
+ if (!enabled) {
6026
+ return;
5778
6027
  }
6028
+ this._solve();
6029
+ this.view.render.render();
6030
+ return this._leads();
5779
6031
  },
5780
6032
  config: function(cfg){
5781
6033
  var node, this$ = this;
@@ -5927,6 +6179,7 @@ var chart = module.exports;
5927
6179
  this.layout = opt.layout || new layout({
5928
6180
  root: this.root
5929
6181
  });
6182
+ this._gridName = opt.grid || 'view';
5930
6183
  this._ticks = [];
5931
6184
  this._scale = opt.scale || function(it){
5932
6185
  return it;
@@ -5991,6 +6244,7 @@ var chart = module.exports;
5991
6244
  var ref$, this$ = this;
5992
6245
  c == null && (c = {});
5993
6246
  this.cfg = {
6247
+ grid: {},
5994
6248
  tick: {
5995
6249
  inner: 8
5996
6250
  },
@@ -6013,7 +6267,7 @@ var chart = module.exports;
6013
6267
  padding: 1
6014
6268
  }
6015
6269
  };
6016
- ['tick', 'baseline', 'label', 'caption', 'sideNoteStart', 'sideNoteEnd'].map(function(n){
6270
+ ['grid', 'tick', 'baseline', 'label', 'caption', 'sideNoteStart', 'sideNoteEnd'].map(function(n){
6017
6271
  return import$(this$.cfg[n], c[n] || {});
6018
6272
  });
6019
6273
  (ref$ = this.cfg.label).font || (ref$.font = {});
@@ -6373,6 +6627,7 @@ var chart = module.exports;
6373
6627
  shown = ticks.filter(function(tick, i){
6374
6628
  return picked[i];
6375
6629
  });
6630
+ this._grid(ticks);
6376
6631
  this._n.label.style.fill = this.cfg.caption.color;
6377
6632
  this._n.label.style.fontSize = this.cfg.caption.font.size ? this.cfg.caption.font.size + "" || '' : void 8;
6378
6633
  this._n.label.setAttribute('class', (this.cfg.caption.font.family || {}).className || '');
@@ -6436,6 +6691,58 @@ var chart = module.exports;
6436
6691
  }
6437
6692
  }
6438
6693
  });
6694
+ axis.prototype._grid = function(ticks){
6695
+ var cfg, g, box, abox, ref$, ox, oy, i$, len$, tick, n, x1, y1, x2, y2, results$ = [];
6696
+ cfg = this.cfg.grid || {};
6697
+ g = cfg.show ? (function(){
6698
+ try {
6699
+ return this.layout.getGroup(this._gridName);
6700
+ } catch (e$) {}
6701
+ }.call(this)) : null;
6702
+ box = g ? (function(){
6703
+ try {
6704
+ return this.layout.getBox(this._gridName);
6705
+ } catch (e$) {}
6706
+ }.call(this)) : null;
6707
+ if (!(g && box && box.width && box.height)) {
6708
+ if (this._n.grid && this._n.grid.parentNode) {
6709
+ this._n.grid.parentNode.removeChild(this._n.grid);
6710
+ }
6711
+ return;
6712
+ }
6713
+ if (!this._n.grid) {
6714
+ this._n.grid = ceNS('g');
6715
+ this._n.grid.setAttribute('data-type', 'grid');
6716
+ this._n.grid.style.pointerEvents = 'none';
6717
+ }
6718
+ if (this._n.grid.parentNode !== g) {
6719
+ g.insertBefore(this._n.grid, g.firstChild);
6720
+ }
6721
+ this._n.grid.textContent = '';
6722
+ abox = this.layout.getBox(this.name);
6723
+ ref$ = [abox.x - box.x, abox.y - box.y], ox = ref$[0], oy = ref$[1];
6724
+ for (i$ = 0, len$ = ticks.length; i$ < len$; ++i$) {
6725
+ tick = ticks[i$];
6726
+ n = ceNS('line');
6727
+ ref$ = this.dir < 2
6728
+ ? [ox + tick._c, 0, ox + tick._c, box.height]
6729
+ : [0, oy + tick._c, box.width, oy + tick._c], x1 = ref$[0], y1 = ref$[1], x2 = ref$[2], y2 = ref$[3];
6730
+ n.setAttribute('x1', x1);
6731
+ n.setAttribute('y1', y1);
6732
+ n.setAttribute('x2', x2);
6733
+ n.setAttribute('y2', y2);
6734
+ import$(n.style, {
6735
+ stroke: cfg.color || 'currentColor',
6736
+ strokeWidth: cfg.width != null ? cfg.width : 1,
6737
+ opacity: cfg.opacity != null ? cfg.opacity : 0.12
6738
+ });
6739
+ if (cfg.dash) {
6740
+ n.style.strokeDasharray = cfg.dash;
6741
+ }
6742
+ results$.push(this._n.grid.appendChild(n));
6743
+ }
6744
+ return results$;
6745
+ };
6439
6746
  if (typeof chart != 'undefined' && chart !== null) {
6440
6747
  (chart.utils || (chart.utils = {})).axis = axis;
6441
6748
  }