@axdspub/axiom-charts 0.0.13 → 0.0.15

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.
@@ -268,6 +268,7 @@ var ChartRoot = /** @class */ (function () {
268
268
  this.settings = props.settings;
269
269
  this.element = props.element;
270
270
  this.onScrub = props.onScrub;
271
+ this.onScrubEnd = props.onScrubEnd;
271
272
  this.runtime = {
272
273
  prevWidth: 0,
273
274
  prevHeight: 0
@@ -656,11 +657,12 @@ var AxiomLine = /** @class */ (function (_super) {
656
657
  }
657
658
  var scrubValue = this.xScale.invert(mouseX - this.offset.x);
658
659
  var bisect = bisector(function (row) { return xAccessor(row); }).center;
659
- var i = bisect(this.parsedData, scrubValue);
660
- var bisected = this.parsedData[i];
660
+ var bisectIndex = bisect(this.parsedData, scrubValue);
661
+ var nearestRow = this.getNearestRow(bisectIndex, scrubValue, this.parsedData, xAccessor, yAccessor);
662
+ var bisected = nearestRow;
661
663
  var xValue = xAccessor(bisected);
662
664
  var xPosition = this.xScale(xValue) + this.offset.x;
663
- var yValue = this.parsedData[i].value;
665
+ var yValue = yAccessor(nearestRow);
664
666
  var yPosition = this.yScale(yValue) + this.offset.y;
665
667
  return {
666
668
  xPosition: xPosition,
@@ -669,6 +671,20 @@ var AxiomLine = /** @class */ (function (_super) {
669
671
  yValue: yValue
670
672
  };
671
673
  };
674
+ AxiomLine.prototype.getNearestRow = function (bisectIndex, scrubValue, parsedData, xAccessor, yAccessor) {
675
+ var nonEmptyRows = parsedData.filter(function (row) { return yAccessor(row) !== null && yAccessor(row) !== undefined; });
676
+ var minDistance = Number.MAX_VALUE;
677
+ var nearestRow = parsedData[bisectIndex];
678
+ for (var i = 0; i < nonEmptyRows.length; i += 1) {
679
+ var current = xAccessor(nonEmptyRows[i]);
680
+ var distance = Math.abs(+current - +scrubValue);
681
+ if (distance < minDistance) {
682
+ minDistance = distance;
683
+ nearestRow = nonEmptyRows[i];
684
+ }
685
+ }
686
+ return nearestRow;
687
+ };
672
688
  AxiomLine.prototype.drawScrubbing = function (mouseX) {
673
689
  var chart = this.chart;
674
690
  var ctx = chart.getCanvasContext();
@@ -1220,7 +1236,9 @@ var AxiomChart = /** @class */ (function (_super) {
1220
1236
  var touchOffset = 20; // TODO: figure out why touch.clientX is always off to the right
1221
1237
  _this.onScrubTriggered(touch.clientX - touchOffset);
1222
1238
  });
1239
+ ctx.canvas.addEventListener('touchend', function (event) { _this.onScrubEndTriggered(); });
1223
1240
  ctx.canvas.addEventListener('mousemove', function (event) { _this.onScrubTriggered(event.offsetX); });
1241
+ ctx.canvas.addEventListener('mouseleave', function (event) { _this.onScrubEndTriggered(); });
1224
1242
  };
1225
1243
  AxiomChart.prototype.onScrubTriggered = function (cursorX) {
1226
1244
  var _this = this;
@@ -1230,7 +1248,6 @@ var AxiomChart = /** @class */ (function (_super) {
1230
1248
  return plot.scrubbing !== undefined && plot.scrubbing && plot.type === EPlotTypes.line;
1231
1249
  });
1232
1250
  var plotScrubPositions = this.getScrubPositions(cursorX, plotsWithScrubbingEnabled);
1233
- // console.log(plotScrubPositions)
1234
1251
  if (this.onScrub !== undefined) {
1235
1252
  this.onScrub(plotScrubPositions);
1236
1253
  }
@@ -1254,6 +1271,28 @@ var AxiomChart = /** @class */ (function (_super) {
1254
1271
  console.error(error);
1255
1272
  });
1256
1273
  };
1274
+ AxiomChart.prototype.onScrubEndTriggered = function () {
1275
+ this.removeScrubbers();
1276
+ if (this.onScrubEnd !== undefined) {
1277
+ this.onScrubEnd();
1278
+ }
1279
+ };
1280
+ AxiomChart.prototype.removeScrubbers = function () {
1281
+ var _this = this;
1282
+ if (this.plotImplementations === undefined)
1283
+ return;
1284
+ this.runtime.busy = true;
1285
+ this.clearCanvas();
1286
+ var drawPromises = this.plotImplementations.map(function (plot) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
1287
+ switch (_a.label) {
1288
+ case 0: return [4 /*yield*/, plot.draw()];
1289
+ case 1:
1290
+ _a.sent();
1291
+ return [2 /*return*/];
1292
+ }
1293
+ }); }); });
1294
+ void Promise.all(drawPromises);
1295
+ };
1257
1296
  AxiomChart.prototype.getScrubPositions = function (mouseX, plots) {
1258
1297
  var plotScrubPositions = {};
1259
1298
  plots.forEach(function (plot) {