plottable 4.0.0 → 4.1.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.
Files changed (33) hide show
  1. package/build/src/components/legend.js +10 -2
  2. package/build/src/core/version.js +1 -1
  3. package/build/src/dispatchers/dispatcher.js +8 -1
  4. package/build/src/interactions/clickInteraction.js +7 -0
  5. package/build/src/interactions/dragInteraction.js +6 -0
  6. package/build/src/interactions/interaction.d.ts +2 -1
  7. package/build/src/interactions/interaction.js +11 -1
  8. package/build/src/interactions/keyInteraction.js +7 -0
  9. package/build/src/interactions/panZoomInteraction.js +6 -0
  10. package/build/src/interactions/pointerInteraction.js +7 -0
  11. package/build/src/plots/barPlot.d.ts +6 -0
  12. package/build/src/plots/barPlot.js +12 -0
  13. package/build/src/plots/piePlot.js +1 -0
  14. package/build/src/plots/plot.d.ts +2 -0
  15. package/build/src/plots/plot.js +3 -1
  16. package/build/src/plots/rectanglePlot.d.ts +5 -0
  17. package/build/src/plots/rectanglePlot.js +11 -0
  18. package/build/src/plots/scatterPlot.d.ts +11 -0
  19. package/build/src/plots/scatterPlot.js +27 -0
  20. package/build/src/plots/xyPlot.js +1 -0
  21. package/build/src/scales/categoryScale.d.ts +1 -0
  22. package/build/src/scales/categoryScale.js +7 -4
  23. package/build/src/scales/colorScale.d.ts +4 -3
  24. package/build/src/scales/colorScale.js +29 -55
  25. package/build/src/scales/interpolatedColorScale.js +5 -4
  26. package/build/src/utils/entityStore.d.ts +6 -3
  27. package/build/src/utils/rTree.d.ts +11 -2
  28. package/build/src/utils/rTree.js +15 -3
  29. package/build/src/utils/stackingUtils.js +34 -0
  30. package/package.json +4 -4
  31. package/plottable.css +4 -3
  32. package/plottable.js +209 -73
  33. package/plottable.js.map +1 -1
@@ -211,12 +211,20 @@ var Legend = /** @class */ (function (_super) {
211
211
  return _this;
212
212
  }
213
213
  Legend.prototype._setup = function () {
214
+ var _this = this;
214
215
  _super.prototype._setup.call(this);
215
- var fakeLegendRow = this.content().append("g").classed(Legend.LEGEND_ROW_CLASS, true);
216
+ // Keep the row out of content() except while measuring, since renderImmediately() clears content()
217
+ var fakeLegendRow = d3.create("svg:g").classed(Legend.LEGEND_ROW_CLASS, true);
216
218
  var fakeLegendEntry = fakeLegendRow.append("g").classed(Legend.LEGEND_ENTRY_CLASS, true);
217
219
  fakeLegendEntry.append("text");
218
220
  var context = new Typesettable.SvgContext(fakeLegendRow.node(), null, Configs.ADD_TITLE_ELEMENTS);
219
- this._measurer = new Typesettable.CacheMeasurer(context);
221
+ var ruler = context.createRuler();
222
+ this._measurer = new Typesettable.CacheMeasurer(function (text) {
223
+ _this.content().append(function () { return fakeLegendRow.node(); });
224
+ var dimensions = ruler(text);
225
+ fakeLegendRow.remove();
226
+ return dimensions;
227
+ });
220
228
  this._wrapper = new Typesettable.Wrapper().maxLines(this.maxLinesPerEntry());
221
229
  this._writer = new Typesettable.Writer(this._measurer, context, this._wrapper);
222
230
  };
@@ -10,4 +10,4 @@ exports.version = void 0;
10
10
  * real version number during the dist phase for for npm module publishing. Modifying this line should
11
11
  * be accompanied by modifying the "sed-version" task in package.json accordingly.
12
12
  */
13
- exports.version = "4.0.0";
13
+ exports.version = "4.1.0";
@@ -78,7 +78,14 @@ var Dispatcher = /** @class */ (function () {
78
78
  }
79
79
  var callbackSet = this._eventNameToCallbackSet[eventName];
80
80
  if (callbackSet != null) {
81
- callbackSet.callCallbacks.apply(callbackSet, args);
81
+ // Interactions can resubscribe while handling an event, which Set.forEach would visit again
82
+ var callbacks_1 = [];
83
+ callbackSet.forEach(function (callback) { return callbacks_1.push(callback); });
84
+ callbacks_1.forEach(function (callback) {
85
+ if (callbackSet.has(callback)) {
86
+ callback.apply(callbackSet, args);
87
+ }
88
+ });
82
89
  }
83
90
  };
84
91
  return Dispatcher;
@@ -41,7 +41,14 @@ var Click = /** @class */ (function (_super) {
41
41
  this._touchDispatcher.onTouchCancel(this._touchCancelCallback);
42
42
  };
43
43
  Click.prototype._unanchor = function () {
44
+ var _this = this;
44
45
  _super.prototype._unanchor.call(this);
46
+ // Preserve a press through synchronous renderTo(), but discard it if its release can be missed.
47
+ Promise.resolve().then(function () {
48
+ if (!_this._isAnchored) {
49
+ _this._clickedDown = false;
50
+ }
51
+ });
45
52
  this._mouseDispatcher.offMouseDown(this._mouseDownCallback);
46
53
  this._mouseDispatcher.offMouseUp(this._mouseUpCallback);
47
54
  this._mouseDispatcher.offDblClick(this._dblClickCallback);
@@ -45,7 +45,13 @@ var Drag = /** @class */ (function (_super) {
45
45
  this._touchDispatcher.onTouchEnd(this._touchEndCallback);
46
46
  };
47
47
  Drag.prototype._unanchor = function () {
48
+ var _this = this;
48
49
  _super.prototype._unanchor.call(this);
50
+ Promise.resolve().then(function () {
51
+ if (!_this._isAnchored) {
52
+ _this._dragging = false;
53
+ }
54
+ });
49
55
  this._mouseDispatcher.offMouseDown(this._mouseDownCallback);
50
56
  this._mouseDispatcher.offMouseMove(this._mouseMoveCallback);
51
57
  this._mouseDispatcher.offMouseUp(this._mouseUpCallback);
@@ -6,7 +6,8 @@ import { Component } from "../components/component";
6
6
  import { Point } from "../core/interfaces";
7
7
  export declare class Interaction {
8
8
  private _anchorCallback;
9
- private _isAnchored;
9
+ private _detachCallback;
10
+ protected _isAnchored: boolean;
10
11
  private _enabled;
11
12
  protected _componentAttachedTo: Component;
12
13
  /**
@@ -8,7 +8,15 @@ exports.Interaction = void 0;
8
8
  var Interaction = /** @class */ (function () {
9
9
  function Interaction() {
10
10
  var _this = this;
11
- this._anchorCallback = function (component) { return _this._anchor(component); };
11
+ this._anchorCallback = function (component) {
12
+ _this._detachCallback();
13
+ _this._anchor(component);
14
+ };
15
+ this._detachCallback = function () {
16
+ if (_this._isAnchored) {
17
+ _this._unanchor();
18
+ }
19
+ };
12
20
  this._enabled = true;
13
21
  }
14
22
  /**
@@ -87,6 +95,7 @@ var Interaction = /** @class */ (function () {
87
95
  Interaction.prototype._connect = function () {
88
96
  if (this.enabled() && this._componentAttachedTo != null && !this._isAnchored) {
89
97
  this._componentAttachedTo.onAnchor(this._anchorCallback);
98
+ this._componentAttachedTo.onDetach(this._detachCallback);
90
99
  }
91
100
  };
92
101
  Interaction.prototype._disconnect = function () {
@@ -95,6 +104,7 @@ var Interaction = /** @class */ (function () {
95
104
  }
96
105
  if (this._componentAttachedTo != null) {
97
106
  this._componentAttachedTo.offAnchor(this._anchorCallback);
107
+ this._componentAttachedTo.offDetach(this._detachCallback);
98
108
  }
99
109
  };
100
110
  return Interaction;
@@ -30,7 +30,14 @@ var Key = /** @class */ (function (_super) {
30
30
  this._keyDispatcher.onKeyUp(this._keyUpCallback);
31
31
  };
32
32
  Key.prototype._unanchor = function () {
33
+ var _this = this;
33
34
  _super.prototype._unanchor.call(this);
35
+ // Preserve held keys through synchronous renderTo(), but discard them if keyup can be missed.
36
+ Promise.resolve().then(function () {
37
+ if (!_this._isAnchored) {
38
+ _this._downedKeys = new Utils.Set();
39
+ }
40
+ });
34
41
  this._positionDispatcher.offMouseMove(this._mouseMoveCallback);
35
42
  this._positionDispatcher = null;
36
43
  this._keyDispatcher.offKeyDown(this._keyDownCallback);
@@ -140,7 +140,13 @@ var PanZoom = /** @class */ (function (_super) {
140
140
  this._touchDispatcher.onTouchCancel(this._touchCancelCallback);
141
141
  };
142
142
  PanZoom.prototype._unanchor = function () {
143
+ var _this = this;
143
144
  _super.prototype._unanchor.call(this);
145
+ Promise.resolve().then(function () {
146
+ if (!_this._isAnchored) {
147
+ _this._touchIds.clear();
148
+ }
149
+ });
144
150
  this._mouseDispatcher.offWheel(this._wheelCallback);
145
151
  this._mouseDispatcher = null;
146
152
  this._touchDispatcher.offTouchStart(this._touchStartCallback);
@@ -29,7 +29,14 @@ var Pointer = /** @class */ (function (_super) {
29
29
  this._touchDispatcher.onTouchStart(this._touchStartCallback);
30
30
  };
31
31
  Pointer.prototype._unanchor = function () {
32
+ var _this = this;
32
33
  _super.prototype._unanchor.call(this);
34
+ // Preserve hover through synchronous renderTo(), but discard state if pointer events can be missed.
35
+ Promise.resolve().then(function () {
36
+ if (!_this._isAnchored) {
37
+ _this._overComponent = false;
38
+ }
39
+ });
33
40
  this._mouseDispatcher.offMouseMove(this._mouseMoveCallback);
34
41
  this._mouseDispatcher = null;
35
42
  this._touchDispatcher.offTouchStart(this._touchStartCallback);
@@ -199,6 +199,7 @@ export declare class Bar<X, Y> extends XYPlot<X, Y> {
199
199
  * - If the query point is inside a bar, returns the PlotEntity for that bar.
200
200
  * - Otherwise, gets the nearest PlotEntity by the positional direction (X for vertical, Y for horizontal),
201
201
  * breaking ties with the secondary direction.
202
+ * - Remaining ties, such as overlapping bars, go to the bar drawn last (the last Dataset, then the last datum).
202
203
  * Returns undefined if no PlotEntity can be found.
203
204
  *
204
205
  * @param {Point} queryPoint
@@ -243,6 +244,11 @@ export declare class Bar<X, Y> extends XYPlot<X, Y> {
243
244
  protected _updateThicknessAttr(): void;
244
245
  private _barPixelThickness;
245
246
  entities(datasets?: Dataset[]): IPlotEntity[];
247
+ /**
248
+ * Undrawn bars stay in `_getDataToDraw` as null placeholders, so the drawer
249
+ * indexes each bar's element by its dataset index
250
+ */
251
+ protected _buildLightweightPlotEntities(datasets: Dataset[]): Plots.ILightweightPlotEntity[];
246
252
  protected _entityBounds(entity: Plots.IPlotEntity | Plots.ILightweightPlotEntity): Pick<DOMRect, "height" | "width" | "x" | "y">;
247
253
  /**
248
254
  * The rectangular bounds of a bar. Note that the x/y coordinates are not the
@@ -255,6 +255,7 @@ var Bar = /** @class */ (function (_super) {
255
255
  * - If the query point is inside a bar, returns the PlotEntity for that bar.
256
256
  * - Otherwise, gets the nearest PlotEntity by the positional direction (X for vertical, Y for horizontal),
257
257
  * breaking ties with the secondary direction.
258
+ * - Remaining ties, such as overlapping bars, go to the bar drawn last (the last Dataset, then the last datum).
258
259
  * Returns undefined if no PlotEntity can be found.
259
260
  *
260
261
  * @param {Point} queryPoint
@@ -631,6 +632,17 @@ var Bar = /** @class */ (function (_super) {
631
632
  var entities = _super.prototype.entities.call(this, datasets);
632
633
  return entities;
633
634
  };
635
+ /**
636
+ * Undrawn bars stay in `_getDataToDraw` as null placeholders, so the drawer
637
+ * indexes each bar's element by its dataset index
638
+ */
639
+ Bar.prototype._buildLightweightPlotEntities = function (datasets) {
640
+ var lightweightPlotEntities = _super.prototype._buildLightweightPlotEntities.call(this, datasets);
641
+ lightweightPlotEntities.forEach(function (lightweightPlotEntity) {
642
+ lightweightPlotEntity.validDatumIndex = lightweightPlotEntity.index;
643
+ });
644
+ return lightweightPlotEntities;
645
+ };
634
646
  Bar.prototype._entityBounds = function (entity) {
635
647
  var datum = entity.datum, index = entity.index, dataset = entity.dataset;
636
648
  return this._pixelBounds(datum, index, dataset);
@@ -55,6 +55,7 @@ var Pie = /** @class */ (function (_super) {
55
55
  if (this.outerRadius().scale != null) {
56
56
  this.outerRadius().scale.range([0, radiusLimit]);
57
57
  }
58
+ this._updateExtents();
58
59
  return this;
59
60
  };
60
61
  Pie.prototype.addDataset = function (dataset) {
@@ -306,6 +306,8 @@ export declare class Plot extends Component {
306
306
  * Returns the {Plots.PlotEntity} nearest to the query point,
307
307
  * or undefined if no {Plots.PlotEntity} can be found.
308
308
  *
309
+ * If several Entities are equally near, returns the one drawn last (the last Dataset, then the last datum).
310
+ *
309
311
  * @param {Point} queryPoint
310
312
  * @returns {Plots.PlotEntity} The nearest PlotEntity, or undefined if no {Plots.PlotEntity} can be found.
311
313
  */
@@ -120,7 +120,7 @@ var Plot = /** @class */ (function (_super) {
120
120
  };
121
121
  Plot.prototype.setBounds = function (width, height, originX, originY) {
122
122
  _super.prototype.setBounds.call(this, width, height, originX, originY);
123
- this._updateExtents();
123
+ this._resetEntityStore();
124
124
  if (this._canvas != null) {
125
125
  if (this._bufferCanvas && !this._bufferCanvasValid) {
126
126
  // copy current canvas to buffer 1:1
@@ -685,6 +685,8 @@ var Plot = /** @class */ (function (_super) {
685
685
  * Returns the {Plots.PlotEntity} nearest to the query point,
686
686
  * or undefined if no {Plots.PlotEntity} can be found.
687
687
  *
688
+ * If several Entities are equally near, returns the one drawn last (the last Dataset, then the last datum).
689
+ *
688
690
  * @param {Point} queryPoint
689
691
  * @returns {Plots.PlotEntity} The nearest PlotEntity, or undefined if no {Plots.PlotEntity} can be found.
690
692
  */
@@ -101,6 +101,11 @@ export declare class Rectangle<X, Y> extends XYPlot<X, Y> {
101
101
  * @returns {PlotEntity[]} The PlotEntities at the particular point
102
102
  */
103
103
  entitiesAt(point: Point): Plots.IPlotEntity[];
104
+ /**
105
+ * Undrawn rectangles stay in `_getDataToDraw` as null placeholders, so the
106
+ * drawer indexes each rectangle's element by its dataset index
107
+ */
108
+ protected _buildLightweightPlotEntities(datasets: Dataset[]): Plots.ILightweightPlotEntity[];
104
109
  protected _entityBounds(entity: Plots.IPlotEntity | Plots.ILightweightPlotEntity): Pick<DOMRect, "height" | "width" | "x" | "y">;
105
110
  private _entityBBox;
106
111
  /**
@@ -170,6 +170,17 @@ var Rectangle = /** @class */ (function (_super) {
170
170
  return x <= point.x && point.x <= x + width && y <= point.y && point.y <= y + height;
171
171
  });
172
172
  };
173
+ /**
174
+ * Undrawn rectangles stay in `_getDataToDraw` as null placeholders, so the
175
+ * drawer indexes each rectangle's element by its dataset index
176
+ */
177
+ Rectangle.prototype._buildLightweightPlotEntities = function (datasets) {
178
+ var lightweightPlotEntities = _super.prototype._buildLightweightPlotEntities.call(this, datasets);
179
+ lightweightPlotEntities.forEach(function (lightweightPlotEntity) {
180
+ lightweightPlotEntity.validDatumIndex = lightweightPlotEntity.index;
181
+ });
182
+ return lightweightPlotEntities;
183
+ };
173
184
  Rectangle.prototype._entityBounds = function (entity) {
174
185
  var datum = entity.datum, index = entity.index, dataset = entity.dataset;
175
186
  return this._entityBBox(datum, index, dataset, this._getAttrToProjector());
@@ -77,6 +77,17 @@ export declare class Scatter<X, Y> extends XYPlot<X, Y> {
77
77
  * @returns {PlotEntity[]}
78
78
  */
79
79
  entitiesAt(p: Point): IPlotEntity[];
80
+ /**
81
+ * Returns the {Plots.PlotEntity} nearest to the query point,
82
+ * or undefined if no {Plots.PlotEntity} can be found.
83
+ *
84
+ * If the query point is inside several Entities' bounding boxes, returns the
85
+ * one whose position is nearest, then the one drawn last.
86
+ *
87
+ * @param {Point} queryPoint
88
+ * @returns {Plots.PlotEntity} The nearest PlotEntity, or undefined if no {Plots.PlotEntity} can be found.
89
+ */
90
+ entityNearest(queryPoint: Point): IPlotEntity;
80
91
  /**
81
92
  * Get whether bar labels are enabled.
82
93
  *
@@ -143,6 +143,33 @@ var Scatter = /** @class */ (function (_super) {
143
143
  return x - size / 2 <= p.x && p.x <= x + size / 2 && y - size / 2 <= p.y && p.y <= y + size / 2;
144
144
  });
145
145
  };
146
+ /**
147
+ * Returns the {Plots.PlotEntity} nearest to the query point,
148
+ * or undefined if no {Plots.PlotEntity} can be found.
149
+ *
150
+ * If the query point is inside several Entities' bounding boxes, returns the
151
+ * one whose position is nearest, then the one drawn last.
152
+ *
153
+ * @param {Point} queryPoint
154
+ * @returns {Plots.PlotEntity} The nearest PlotEntity, or undefined if no {Plots.PlotEntity} can be found.
155
+ */
156
+ Scatter.prototype.entityNearest = function (queryPoint) {
157
+ var containing = this._getEntityStore().entitiesInBounds({ x: queryPoint.x, y: queryPoint.y, width: 0, height: 0 });
158
+ if (containing.length === 0) {
159
+ return _super.prototype.entityNearest.call(this, queryPoint);
160
+ }
161
+ containing.sort(function (a, b) { return a.datasetIndex - b.datasetIndex || a.index - b.index; });
162
+ var nearest;
163
+ var minDistSquared = Infinity;
164
+ containing.forEach(function (entity) {
165
+ var distSquared = Utils.Math.distanceSquared(entity.position, queryPoint);
166
+ if (distSquared <= minDistSquared) {
167
+ nearest = entity;
168
+ minDistSquared = distSquared;
169
+ }
170
+ });
171
+ return this._lightweightPlotEntityToPlotEntity(nearest);
172
+ };
146
173
  Scatter.prototype.labelsEnabled = function (enabled) {
147
174
  if (enabled == null) {
148
175
  return this._labelsEnabled;
@@ -196,6 +196,7 @@ var XYPlot = /** @class */ (function (_super) {
196
196
  yScale.range([this.height(), 0]);
197
197
  }
198
198
  }
199
+ this._updateExtents();
199
200
  return this;
200
201
  };
201
202
  XYPlot.prototype._updateXExtentsAndAutodomain = function () {
@@ -22,6 +22,7 @@ export declare class Category extends Scale<string, number> implements ITransfor
22
22
  */
23
23
  private _d3TransformationScale;
24
24
  private _d3Scale;
25
+ private _domain;
25
26
  private _range;
26
27
  private _innerPadding;
27
28
  private _outerPadding;
@@ -20,6 +20,7 @@ var Category = /** @class */ (function (_super) {
20
20
  */
21
21
  function Category() {
22
22
  var _this = _super.call(this) || this;
23
+ _this._domain = [];
23
24
  _this._range = [0, 1];
24
25
  _this._d3Scale = d3.scaleBand();
25
26
  _this._d3Scale.range(TRANSFORMATION_SPACE);
@@ -69,7 +70,7 @@ var Category = /** @class */ (function (_super) {
69
70
  var normalizedDomain = domain.map(function (d) { return _this._d3Scale(d) + rangeBand / 2; });
70
71
  var domainStart = d3.bisect(normalizedDomain, domainStartNormalized);
71
72
  var domainEnd = d3.bisect(normalizedDomain, domainEndNormalized);
72
- return domain.slice(domainStart, domainEnd);
73
+ return this._domain.slice(domainStart, domainEnd);
73
74
  };
74
75
  Category.prototype.range = function (values) {
75
76
  return _super.prototype.range.call(this, values);
@@ -127,8 +128,9 @@ var Category = /** @class */ (function (_super) {
127
128
  return this;
128
129
  };
129
130
  Category.prototype.scale = function (value) {
131
+ // Accessors read untyped data, so values can be numbers, and 1 and "1" must share a band
130
132
  // Determine the middle of the range band for the value
131
- var untransformed = this._d3Scale(value) + this._d3Scale.bandwidth() / 2;
133
+ var untransformed = this._d3Scale(String(value)) + this._d3Scale.bandwidth() / 2;
132
134
  // Convert to screen space
133
135
  return this._d3TransformationScale(untransformed);
134
136
  };
@@ -169,10 +171,11 @@ var Category = /** @class */ (function (_super) {
169
171
  };
170
172
  Category.prototype._backingScaleDomain = function (values) {
171
173
  if (values == null) {
172
- return this._d3Scale.domain();
174
+ return this._domain.slice();
173
175
  }
174
176
  else {
175
- this._d3Scale.domain(values);
177
+ this._domain = Utils.Array.uniq(values);
178
+ this._d3Scale.domain(this._domain.map(String));
176
179
  this._setBands();
177
180
  return this;
178
181
  }
@@ -7,9 +7,9 @@ export declare class Color extends Scale<string, string> {
7
7
  private static _LOOP_LIGHTEN_FACTOR;
8
8
  private static _MAXIMUM_COLORS_FROM_CSS;
9
9
  private static _plottableColorCache;
10
- private _d3Scale;
11
- private _rangeLength;
12
- private _tracker;
10
+ private _domain;
11
+ private _domainIndices;
12
+ private _range;
13
13
  /**
14
14
  * A Color Scale maps string values to color hex values expressed as a string.
15
15
  *
@@ -33,6 +33,7 @@ export declare class Color extends Scale<string, string> {
33
33
  * @returns {string}
34
34
  */
35
35
  scale(value: string): string;
36
+ private _domainIndex;
36
37
  protected _getDomain(): string[];
37
38
  protected _backingScaleDomain(): string[];
38
39
  protected _backingScaleDomain(values: string[]): this;
@@ -9,41 +9,6 @@ var tslib_1 = require("tslib");
9
9
  var d3 = require("d3");
10
10
  var Utils = require("../utils");
11
11
  var scale_1 = require("./scale");
12
- /**
13
- * Workaround for bad d3 behavior.
14
- *
15
- * d3's color scales, which are oridinal scales, have side-effects when invoked.
16
- * When you call the `scale(value)` as a function, it will implicitly add new
17
- * values to the domain.
18
- *
19
- * These side-effects cause us to rely on looking up the current scale state
20
- * with the `.domain()` and `.range()` methods. However, these methods have poor
21
- * performance implications since they will always slice their internal values
22
- * before returning. Inside the inner render loop of color scatter plot points,
23
- * these slices add up.
24
- */
25
- var ImplicitSeriesTracker = /** @class */ (function () {
26
- function ImplicitSeriesTracker() {
27
- this.count = 0;
28
- this.tracker = {};
29
- }
30
- ImplicitSeriesTracker.prototype.getIndex = function (value) {
31
- if (this.tracker[value] != null) {
32
- return this.tracker[value];
33
- }
34
- else {
35
- var idx = this.count;
36
- this.tracker[value] = idx;
37
- this.count += 1;
38
- return idx;
39
- }
40
- };
41
- ImplicitSeriesTracker.prototype.clear = function () {
42
- this.count = 0;
43
- this.tracker = {};
44
- };
45
- return ImplicitSeriesTracker;
46
- }());
47
12
  // Preserve the public D3 4 palette names removed in D3 5.
48
13
  var CATEGORY20 = [
49
14
  "#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#2ca02c",
@@ -75,43 +40,41 @@ var Color = /** @class */ (function (_super) {
75
40
  */
76
41
  function Color(scaleType) {
77
42
  var _this = _super.call(this) || this;
78
- // Cache the number of range value to avoid calling `scale.range().length`
79
- _this._rangeLength = 1;
80
- _this._tracker = new ImplicitSeriesTracker();
81
- var scale;
43
+ _this._domain = [];
44
+ _this._domainIndices = new Map();
45
+ var range;
82
46
  switch (scaleType) {
83
47
  case null:
84
48
  case undefined:
85
49
  if (Color._plottableColorCache == null) {
86
50
  Color._plottableColorCache = Color._getPlottableColors();
87
51
  }
88
- scale = d3.scaleOrdinal().range(Color._plottableColorCache);
52
+ range = Color._plottableColorCache;
89
53
  break;
90
54
  case "Category10":
91
55
  case "category10":
92
56
  case "10":
93
- scale = d3.scaleOrdinal(d3.schemeCategory10);
57
+ range = d3.schemeCategory10;
94
58
  break;
95
59
  case "Category20":
96
60
  case "category20":
97
61
  case "20":
98
- scale = d3.scaleOrdinal(CATEGORY20);
62
+ range = CATEGORY20;
99
63
  break;
100
64
  case "Category20b":
101
65
  case "category20b":
102
66
  case "20b":
103
- scale = d3.scaleOrdinal(CATEGORY20B);
67
+ range = CATEGORY20B;
104
68
  break;
105
69
  case "Category20c":
106
70
  case "category20c":
107
71
  case "20c":
108
- scale = d3.scaleOrdinal(CATEGORY20C);
72
+ range = CATEGORY20C;
109
73
  break;
110
74
  default:
111
75
  throw new Error("Unsupported ColorScale type");
112
76
  }
113
- _this._d3Scale = scale;
114
- _this._rangeLength = _this._d3Scale.range().length;
77
+ _this._range = range.slice();
115
78
  return _this;
116
79
  }
117
80
  Color.prototype.extentOfValues = function (values) {
@@ -151,34 +114,45 @@ var Color = /** @class */ (function (_super) {
151
114
  * @returns {string}
152
115
  */
153
116
  Color.prototype.scale = function (value) {
154
- var color = this._d3Scale(value);
155
- var index = this._tracker.getIndex(value);
156
- var numLooped = Math.floor(index / this._rangeLength);
117
+ var index = this._domainIndex(value);
118
+ var color = this._range[index % this._range.length];
119
+ var numLooped = Math.floor(index / this._range.length);
157
120
  if (numLooped === 0) {
158
121
  return color;
159
122
  }
160
123
  var modifyFactor = Math.log(numLooped * Color._LOOP_LIGHTEN_FACTOR + 1);
161
124
  return Utils.Color.lightenColor(color, modifyFactor);
162
125
  };
126
+ Color.prototype._domainIndex = function (value) {
127
+ // Accessors read untyped data, so values can be numbers, and 1 and "1" must share a color
128
+ var key = String(value);
129
+ var index = this._domainIndices.get(key);
130
+ if (index == null) {
131
+ index = this._domain.push(value) - 1;
132
+ this._domainIndices.set(key, index);
133
+ }
134
+ return index;
135
+ };
163
136
  Color.prototype._getDomain = function () {
164
137
  return this._backingScaleDomain();
165
138
  };
166
139
  Color.prototype._backingScaleDomain = function (values) {
140
+ var _this = this;
167
141
  if (values == null) {
168
- return this._d3Scale.domain();
142
+ return this._domain.slice();
169
143
  }
170
144
  else {
171
- this._d3Scale.domain(values);
172
- this._tracker.clear();
145
+ this._domain = [];
146
+ this._domainIndices.clear();
147
+ values.forEach(function (value) { return _this._domainIndex(value); });
173
148
  return this;
174
149
  }
175
150
  };
176
151
  Color.prototype._getRange = function () {
177
- return this._d3Scale.range();
152
+ return this._range.slice();
178
153
  };
179
154
  Color.prototype._setRange = function (values) {
180
- this._d3Scale.range(values);
181
- this._rangeLength = values.length;
155
+ this._range = values.slice();
182
156
  };
183
157
  Color._LOOP_LIGHTEN_FACTOR = 1.6;
184
158
  // The maximum number of colors we are getting from CSS stylesheets
@@ -9,6 +9,7 @@ var tslib_1 = require("tslib");
9
9
  var d3 = require("d3");
10
10
  var Utils = require("../utils");
11
11
  var scale_1 = require("./scale");
12
+ var UNKNOWN_COLOR = "rgb(0, 0, 0)";
12
13
  var InterpolatedColor = /** @class */ (function (_super) {
13
14
  tslib_1.__extends(InterpolatedColor, _super);
14
15
  /**
@@ -21,16 +22,16 @@ var InterpolatedColor = /** @class */ (function (_super) {
21
22
  var _this = _super.call(this) || this;
22
23
  switch (scaleType) {
23
24
  case "linear":
24
- _this._colorScale = d3.scaleLinear();
25
+ _this._colorScale = d3.scaleLinear().unknown(UNKNOWN_COLOR);
25
26
  break;
26
27
  case "log":
27
- _this._colorScale = d3.scaleLog();
28
+ _this._colorScale = d3.scaleLog().unknown(UNKNOWN_COLOR);
28
29
  break;
29
30
  case "sqrt":
30
- _this._colorScale = d3.scaleSqrt();
31
+ _this._colorScale = d3.scaleSqrt().unknown(UNKNOWN_COLOR);
31
32
  break;
32
33
  case "pow":
33
- _this._colorScale = d3.scalePow();
34
+ _this._colorScale = d3.scalePow().unknown(UNKNOWN_COLOR);
34
35
  break;
35
36
  }
36
37
  if (_this._colorScale == null) {
@@ -27,7 +27,8 @@ export interface IEntityStore<T extends IPositionedEntity> {
27
27
  */
28
28
  addAll(entities: T[], entityBoundsFactory: (entity: T) => IEntityBounds, bounds?: Bounds): void;
29
29
  /**
30
- * Returns the entity closest to a given {Point}
30
+ * Returns the entity closest to a given {Point}. Ties are broken in favor of
31
+ * the entity added last.
31
32
  *
32
33
  * Note that if a {Bounds} was provided to the `addAll` method, entities
33
34
  * outside those bounds will not be returned by this method.
@@ -38,7 +39,8 @@ export interface IEntityStore<T extends IPositionedEntity> {
38
39
  entityNearest(point: Point): T;
39
40
  /**
40
41
  * Returns the entity closest to a given {Point} in the x-dimension. Ties are
41
- * broken with a sort in the y-dimension.
42
+ * broken with a sort in the y-dimension, then in favor of the entity added
43
+ * last.
42
44
  *
43
45
  * @param {Point} [point] Point around which to search for a closest entity
44
46
  * @returns {T} Will return the nearest entity or undefined if none are found
@@ -46,7 +48,8 @@ export interface IEntityStore<T extends IPositionedEntity> {
46
48
  entityNearestX(point: Point): T;
47
49
  /**
48
50
  * Returns the entity closest to a given {Point} in the y-dimension. Ties are
49
- * broken with a sort in the x-dimension.
51
+ * broken with a sort in the x-dimension, then in favor of the entity added
52
+ * last.
50
53
  *
51
54
  * @param {Point} [point] Point around which to search for a closest entity
52
55
  * @returns {T} Will return the nearest entity or undefined if none are found