@axdspub/axiom-charts 0.0.11 → 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.
@@ -1,5 +1,5 @@
1
1
  import { RequestItem } from '@axdspub/axiom-ui-data-services';
2
- import { extent, sum } from 'd3-array';
2
+ import { extent, bisector, sum } from 'd3-array';
3
3
  import { groupBy, debounce } from 'lodash';
4
4
  import { create } from 'd3-selection';
5
5
  import { scaleUtc, scaleLinear, scaleRadial } from 'd3-scale';
@@ -123,6 +123,7 @@ var PlotRoot = /** @class */ (function (_super) {
123
123
  _this.chart = props.chart;
124
124
  _this.dimensions = props.dimensions;
125
125
  _this.style = props.style;
126
+ _this.scrubbing = props.scrubbing;
126
127
  _this.axisSettings = props.axisSettings;
127
128
  _this.runtime = {};
128
129
  return _this;
@@ -249,6 +250,13 @@ var PlotRoot = /** @class */ (function (_super) {
249
250
  });
250
251
  });
251
252
  };
253
+ PlotRoot.prototype.drawScrubbing = function (mouseX) {
254
+ console.log('drawScrubbing not implemented');
255
+ };
256
+ PlotRoot.prototype.getScrubPosition = function (mouseX) {
257
+ console.log('getScrubPosition not implemented');
258
+ return undefined;
259
+ };
252
260
  PlotRoot.prototype.destroy = function () {
253
261
  };
254
262
  return PlotRoot;
@@ -259,6 +267,7 @@ var ChartRoot = /** @class */ (function () {
259
267
  this.plots = props.plots.map(function (p) { return (__assign({}, p)); });
260
268
  this.settings = props.settings;
261
269
  this.element = props.element;
270
+ this.onScrub = props.onScrub;
262
271
  this.runtime = {
263
272
  prevWidth: 0,
264
273
  prevHeight: 0
@@ -493,6 +502,8 @@ var ChartRoot = /** @class */ (function () {
493
502
  };
494
503
  ChartRoot.prototype.showXAxes = function (xAxes) {
495
504
  };
505
+ ChartRoot.prototype.adjustYAxis = function () {
506
+ };
496
507
  ChartRoot.prototype.draw = function () {
497
508
  return __awaiter(this, void 0, void 0, function () {
498
509
  var plotImplementations;
@@ -549,7 +560,9 @@ var AxiomPlotRoot = /** @class */ (function (_super) {
549
560
  var AxiomLine = /** @class */ (function (_super) {
550
561
  __extends(AxiomLine, _super);
551
562
  function AxiomLine() {
552
- return _super !== null && _super.apply(this, arguments) || this;
563
+ var _this = _super !== null && _super.apply(this, arguments) || this;
564
+ _this.addedScrubbingListener = false;
565
+ return _this;
553
566
  }
554
567
  AxiomLine.prototype.getDomainAccessor = function (axis) {
555
568
  var _a, _b, _c;
@@ -564,15 +577,19 @@ var AxiomLine = /** @class */ (function (_super) {
564
577
  AxiomLine.prototype.draw = function () {
565
578
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
566
579
  return __awaiter(this, void 0, void 0, function () {
567
- var style, data, parsedData, xAccessor, yAccessor, chart, ctx, xAxisScale, yAxisScale, offset, xScale, xPosition, yScale, yPosition, isDefined, line$1;
580
+ var style, data, xAccessor, yAccessor, chart, ctx, xAxisScale, yAxisScale, xPosition, yPosition, isDefined, line$1;
581
+ var _this = this;
568
582
  return __generator(this, function (_m) {
569
583
  switch (_m.label) {
570
584
  case 0:
571
585
  style = this.style !== undefined ? this.style : {};
586
+ if (!(this.parsedData === undefined)) return [3 /*break*/, 2];
572
587
  return [4 /*yield*/, this.getData()];
573
588
  case 1:
574
589
  data = _m.sent();
575
- parsedData = (_a = data === null || data === void 0 ? void 0 : data.parsed) === null || _a === void 0 ? void 0 : _a.data;
590
+ this.parsedData = (_a = data === null || data === void 0 ? void 0 : data.parsed) === null || _a === void 0 ? void 0 : _a.data;
591
+ _m.label = 2;
592
+ case 2:
576
593
  xAccessor = this.getAccessorAtAxis('x');
577
594
  yAccessor = this.getAccessorAtAxis('y');
578
595
  chart = this.chart;
@@ -582,33 +599,36 @@ var AxiomLine = /** @class */ (function (_super) {
582
599
  }
583
600
  xAxisScale = (_c = (_b = this === null || this === void 0 ? void 0 : this.axes) === null || _b === void 0 ? void 0 : _b.x) === null || _c === void 0 ? void 0 : _c.scale;
584
601
  yAxisScale = (_e = (_d = this === null || this === void 0 ? void 0 : this.axes) === null || _d === void 0 ? void 0 : _d.y) === null || _e === void 0 ? void 0 : _e.scale;
585
- if (xAxisScale === undefined || yAxisScale === undefined || parsedData === undefined || xAccessor === undefined || yAccessor === undefined) {
602
+ if (xAxisScale === undefined || yAxisScale === undefined || this.parsedData === undefined || xAccessor === undefined || yAccessor === undefined) {
586
603
  return [2 /*return*/, undefined];
587
604
  }
588
- offset = {
605
+ this.offset = {
589
606
  x: ((_h = (_g = (_f = this === null || this === void 0 ? void 0 : this.axes) === null || _f === void 0 ? void 0 : _f.x) === null || _g === void 0 ? void 0 : _g.offset) === null || _h === void 0 ? void 0 : _h.x) !== undefined ? this.axes.x.offset.x : 0,
590
607
  y: ((_l = (_k = (_j = this === null || this === void 0 ? void 0 : this.axes) === null || _j === void 0 ? void 0 : _j.y) === null || _k === void 0 ? void 0 : _k.offset) === null || _l === void 0 ? void 0 : _l.y) !== undefined ? this.axes.y.offset.y : 0
591
608
  };
592
- xScale = chart.getD3XScale(xAxisScale);
609
+ this.xScale = chart.getD3XScale(xAxisScale);
593
610
  xPosition = function (d) {
611
+ if (_this.xScale === undefined || _this.offset === undefined)
612
+ return 0;
594
613
  var v = xAccessor(d);
595
- var p = xScale(v);
596
- return p === undefined ? 0 : (p + offset.x);
614
+ var p = _this.xScale(v);
615
+ return p === undefined ? 0 : (p + _this.offset.x);
597
616
  };
598
- yScale = chart.getD3YScale(yAxisScale);
617
+ this.yScale = chart.getD3YScale(yAxisScale);
599
618
  yPosition = function (d) {
619
+ if (_this.yScale === undefined || _this.offset === undefined)
620
+ return 0;
600
621
  var v = yAccessor(d);
601
- var p = yScale(v);
602
- return p === undefined ? 0 : (p + offset.y);
622
+ var p = _this.yScale(v);
623
+ return p === undefined ? 0 : (p + _this.offset.y);
603
624
  };
604
625
  isDefined = function (d) {
605
626
  var v = yAccessor(d);
606
627
  return !!((v !== undefined && v !== null && !isNaN(+v)));
607
628
  };
608
- // settings?.style?.strokeColor !== undefined ? settings.style.strokeColor :
609
- // settings?.style?.strokeWidth ||
610
- ctx.strokeStyle = style.strokeColor !== undefined ? style.strokeColor : '#333';
611
- ctx.lineWidth = style.strokeWidth !== undefined ? style.strokeWidth : 1;
629
+ ctx.strokeStyle = this.getStrokeStyle();
630
+ ctx.lineWidth = this.getLineWidth();
631
+ ctx.setLineDash([]);
612
632
  line$1 = line()
613
633
  .x(xPosition)
614
634
  .y(yPosition)
@@ -618,13 +638,74 @@ var AxiomLine = /** @class */ (function (_super) {
618
638
  if ((style === null || style === void 0 ? void 0 : style.strokeDash) !== undefined) {
619
639
  ctx.setLineDash(style.strokeDash);
620
640
  }
621
- line$1(parsedData);
641
+ line$1(this.parsedData);
622
642
  ctx.stroke();
623
643
  return [2 /*return*/];
624
644
  }
625
645
  });
626
646
  });
627
647
  };
648
+ AxiomLine.prototype.getScrubPosition = function (mouseX) {
649
+ if (this.xScale === undefined || this.yScale === undefined || this.parsedData === undefined || this.offset === undefined) {
650
+ return undefined;
651
+ }
652
+ var xAccessor = this.getAccessorAtAxis('x');
653
+ var yAccessor = this.getAccessorAtAxis('y');
654
+ if (xAccessor === undefined || yAccessor === undefined) {
655
+ return undefined;
656
+ }
657
+ var scrubValue = this.xScale.invert(mouseX - this.offset.x);
658
+ var bisect = bisector(function (row) { return xAccessor(row); }).center;
659
+ var i = bisect(this.parsedData, scrubValue);
660
+ var bisected = this.parsedData[i];
661
+ var xValue = xAccessor(bisected);
662
+ var xPosition = this.xScale(xValue) + this.offset.x;
663
+ var yValue = this.parsedData[i].value;
664
+ var yPosition = this.yScale(yValue) + this.offset.y;
665
+ return {
666
+ xPosition: xPosition,
667
+ xValue: xValue,
668
+ yPosition: yPosition,
669
+ yValue: yValue
670
+ };
671
+ };
672
+ AxiomLine.prototype.drawScrubbing = function (mouseX) {
673
+ var chart = this.chart;
674
+ var ctx = chart.getCanvasContext();
675
+ if (ctx === null) {
676
+ return undefined;
677
+ }
678
+ var scrubPosition = this.getScrubPosition(mouseX);
679
+ if (scrubPosition === undefined)
680
+ return;
681
+ var xPosition = scrubPosition.xPosition, yPosition = scrubPosition.yPosition;
682
+ this.drawScrubCircle(xPosition, yPosition, ctx);
683
+ this.drawScrubLine(xPosition, ctx);
684
+ };
685
+ AxiomLine.prototype.drawScrubCircle = function (x, y, ctx) {
686
+ ctx.beginPath();
687
+ ctx.setLineDash([]);
688
+ ctx.arc(x, y, 6, 0, 2 * Math.PI, true);
689
+ ctx.strokeStyle = this.getStrokeStyle();
690
+ ctx.lineWidth = this.getLineWidth();
691
+ ctx.stroke();
692
+ };
693
+ AxiomLine.prototype.drawScrubLine = function (x, ctx) {
694
+ var lineHeight = this.chart.settings.height - this.chart.settings.margin.bottom;
695
+ ctx.setLineDash([1, 1]);
696
+ ctx.beginPath();
697
+ ctx.moveTo(x, 0);
698
+ ctx.lineTo(x, lineHeight);
699
+ ctx.stroke();
700
+ };
701
+ AxiomLine.prototype.getStrokeStyle = function () {
702
+ var style = this.style !== undefined ? this.style : {};
703
+ return style.strokeColor !== undefined ? style.strokeColor : '#333';
704
+ };
705
+ AxiomLine.prototype.getLineWidth = function () {
706
+ var style = this.style !== undefined ? this.style : {};
707
+ return style.strokeWidth !== undefined ? style.strokeWidth : 1;
708
+ };
628
709
  return AxiomLine;
629
710
  }(AxiomPlotRoot));
630
711
 
@@ -742,6 +823,13 @@ var AxiomArea = /** @class */ (function (_super) {
742
823
  });
743
824
  });
744
825
  };
826
+ AxiomArea.prototype.drawScrubbing = function (mouseX) {
827
+ console.error('AxiomArea.drawScrubbing not implemented');
828
+ };
829
+ AxiomArea.prototype.getScrubPosition = function (mouseX) {
830
+ console.error('AxiomArea.getScrubPosition not implemented');
831
+ return undefined;
832
+ };
745
833
  return AxiomArea;
746
834
  }(AxiomPlotRoot));
747
835
 
@@ -823,6 +911,12 @@ var AxiomScatter = /** @class */ (function (_super) {
823
911
  });
824
912
  });
825
913
  };
914
+ AxiomScatter.prototype.getScrubPosition = function (mouseX) {
915
+ throw new Error('Method not implemented.');
916
+ };
917
+ AxiomScatter.prototype.drawScrubbing = function (mouseX) {
918
+ throw new Error('Method not implemented.');
919
+ };
826
920
  return AxiomScatter;
827
921
  }(AxiomPlotRoot));
828
922
 
@@ -929,6 +1023,22 @@ var AxiomChart = /** @class */ (function (_super) {
929
1023
  };
930
1024
  return axisG;
931
1025
  };
1026
+ AxiomChart.prototype.adjustYAxis = function () {
1027
+ console.log('here');
1028
+ var chartElements = this.getChartElements();
1029
+ if (chartElements === undefined)
1030
+ return;
1031
+ var svg = chartElements.svg, canvas = chartElements.canvas;
1032
+ var node = svg.selectAll('.y-axis').nodes()[0];
1033
+ if (node === null || node === undefined)
1034
+ return;
1035
+ node = node;
1036
+ // const yAxisWidth = node.getBoundingClientRect()
1037
+ console.log(node);
1038
+ svg.selectAll('.y-axis').attr('translate', 'transform(100px, 100px)').attr('width', 100);
1039
+ canvas.attr('width', this.getWidth() - 45.7);
1040
+ canvas.attr('transform', "translate(".concat(45.7, ", 0)"));
1041
+ };
932
1042
  AxiomChart.prototype.showYAxes = function (axes) {
933
1043
  var _this = this;
934
1044
  var chartElements = this.getChartElements();
@@ -1077,6 +1187,8 @@ var AxiomChart = /** @class */ (function (_super) {
1077
1187
  this.element.append(canvasNode);
1078
1188
  }
1079
1189
  }
1190
+ // TODO: conditional
1191
+ this.addScrubbing();
1080
1192
  };
1081
1193
  AxiomChart.prototype.createPlot = function (plot) {
1082
1194
  var props = this.getPlotImplementationProps(plot);
@@ -1091,6 +1203,67 @@ var AxiomChart = /** @class */ (function (_super) {
1091
1203
  }
1092
1204
  return new PlotRoot(props);
1093
1205
  };
1206
+ AxiomChart.prototype.clearCanvas = function () {
1207
+ var ctx = this.getCanvasContext();
1208
+ if (ctx !== null) {
1209
+ ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
1210
+ }
1211
+ };
1212
+ AxiomChart.prototype.addScrubbing = function () {
1213
+ var _this = this;
1214
+ var ctx = this.getCanvasContext();
1215
+ if (ctx === null) {
1216
+ return;
1217
+ }
1218
+ ctx.canvas.addEventListener('touchmove', function (event) {
1219
+ var touch = event.touches[0];
1220
+ var touchOffset = 20; // TODO: figure out why touch.clientX is always off to the right
1221
+ _this.onScrubTriggered(touch.clientX - touchOffset);
1222
+ });
1223
+ ctx.canvas.addEventListener('mousemove', function (event) { _this.onScrubTriggered(event.offsetX); });
1224
+ };
1225
+ AxiomChart.prototype.onScrubTriggered = function (cursorX) {
1226
+ var _this = this;
1227
+ if (this.plotImplementations === undefined)
1228
+ return;
1229
+ var plotsWithScrubbingEnabled = this.plotImplementations.filter(function (plot) {
1230
+ return plot.scrubbing !== undefined && plot.scrubbing && plot.type === EPlotTypes.line;
1231
+ });
1232
+ var plotScrubPositions = this.getScrubPositions(cursorX, plotsWithScrubbingEnabled);
1233
+ // console.log(plotScrubPositions)
1234
+ if (this.onScrub !== undefined) {
1235
+ this.onScrub(plotScrubPositions);
1236
+ }
1237
+ this.runtime.busy = true;
1238
+ this.clearCanvas();
1239
+ var drawPromises = this.plotImplementations.map(function (plot) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
1240
+ switch (_a.label) {
1241
+ case 0: return [4 /*yield*/, plot.draw()];
1242
+ case 1:
1243
+ _a.sent();
1244
+ return [2 /*return*/];
1245
+ }
1246
+ }); }); });
1247
+ Promise.all(drawPromises).then(function () {
1248
+ plotsWithScrubbingEnabled.forEach(function (plot) {
1249
+ var position = plotScrubPositions[plot.id];
1250
+ plot.drawScrubbing(position.xPosition);
1251
+ });
1252
+ _this.runtime.busy = false;
1253
+ }).catch(function (error) {
1254
+ console.error(error);
1255
+ });
1256
+ };
1257
+ AxiomChart.prototype.getScrubPositions = function (mouseX, plots) {
1258
+ var plotScrubPositions = {};
1259
+ plots.forEach(function (plot) {
1260
+ var scrubPosition = plot.getScrubPosition(mouseX);
1261
+ if (scrubPosition !== undefined) {
1262
+ plotScrubPositions[plot.id] = scrubPosition;
1263
+ }
1264
+ });
1265
+ return plotScrubPositions;
1266
+ };
1094
1267
  AxiomChart.prototype.init = function (drawImmediately) {
1095
1268
  if (drawImmediately === void 0) { drawImmediately = false; }
1096
1269
  return __awaiter(this, void 0, void 0, function () {
@@ -1318,6 +1491,13 @@ var AxiomRadialArea = /** @class */ (function (_super) {
1318
1491
  });
1319
1492
  });
1320
1493
  };
1494
+ AxiomRadialArea.prototype.drawScrubbing = function (mouseX) {
1495
+ console.error('AxiomRadialArea.drawScrubbing not implemented');
1496
+ };
1497
+ AxiomRadialArea.prototype.getScrubPosition = function (mouseX) {
1498
+ console.error('AxiomRadialArea.getScrubPosition not implemented');
1499
+ return undefined;
1500
+ };
1321
1501
  return AxiomRadialArea;
1322
1502
  }(AxiomArea));
1323
1503