td-plots 1.5.1 → 1.5.2

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/dist/index.js CHANGED
@@ -57,6 +57,7 @@ var css_248z = ".plot-container{height:100%;max-width:100%;min-height:300px;over
57
57
  styleInject(css_248z);
58
58
 
59
59
  // Utility functions for our components
60
+ var ONEAVGMONTH = 2629800000; // Average month length in ms, copied from plotly constants in plotly.js/src/constants/numerical.js
60
61
  // Type guard to check if array contains only numbers
61
62
  var isNumberArray = function (arr) {
62
63
  return arr.every(function (item) { return typeof item === 'number' && !isNaN(item); });
@@ -109,6 +110,44 @@ var roundToPrevDay = function (timestamp) {
109
110
  date.setHours(0, 0, 0, 0); // Start of day
110
111
  return date.toISOString();
111
112
  };
113
+ // Convert plotly M# string to number of milliseconds
114
+ // According to the docs, plotly will use M# whenever the bin size is
115
+ // larger than one average month. Even if the bin size is 2 years, plotly
116
+ // will still use M24 instead of Y2 (plotly.js/src/plots/cartesian/axes.js).
117
+ var plotlyMToMilliseconds = function (mString) {
118
+ var match = mString.match(/^M(\d+)$/);
119
+ if (match) {
120
+ var months = parseInt(match[1], 10);
121
+ return months * ONEAVGMONTH;
122
+ }
123
+ else {
124
+ console.error("plotlyMToMilliseconds: Invalid M# string: ".concat(mString));
125
+ }
126
+ return 0;
127
+ };
128
+ // Convert a string specifying a day (with no time) to a timestamp
129
+ var convertDateDescriptionToTimestamp = function (startValue) {
130
+ if (typeof startValue === 'number') {
131
+ // Then we're assuming it's already a timestamp
132
+ return startValue;
133
+ }
134
+ if (typeof startValue === 'string') {
135
+ // Try to parse the date string.
136
+ var date = new Date(startValue);
137
+ // Sometimes the date will fail to be a proper Date if it does not
138
+ // have a time component. Add a time stamp if necessary.
139
+ if (isNaN(date.getTime())) {
140
+ // Then date did not have a time and is just a day. Try adding a time.
141
+ var dateWithTime = new Date(startValue + 'T00:00:00');
142
+ if (isNaN(dateWithTime.getTime())) {
143
+ throw new Error("Cannot parse date: ".concat(startValue));
144
+ }
145
+ return dateWithTime.getTime();
146
+ }
147
+ return date.getTime();
148
+ }
149
+ throw new Error("Unexpected type for start value: ".concat(typeof startValue));
150
+ };
112
151
 
113
152
  function getDefaultExportFromCjs (x) {
114
153
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -6113,8 +6152,8 @@ var HistogramPlot = function (props) {
6113
6152
  // This is the start of the first bin out of all the traces. The global first bin.
6114
6153
  var globalFirstBinStart = void 0;
6115
6154
  if (isDateArray(data)) {
6116
- // Date bins are represented as strings. We'll need to convert them to timestamps.
6117
- globalFirstBinStart = new Date(event.points[0].data.xbins.start).getTime();
6155
+ // Date bins are represented as strings (sometimes). We'll need to convert whatever plotly gives us to timestamps.
6156
+ globalFirstBinStart = convertDateDescriptionToTimestamp(event.points[0].data.xbins.start);
6118
6157
  minTraceValue = Math.min.apply(Math, data.map(function (d) { return d.getTime(); }));
6119
6158
  }
6120
6159
  else {
@@ -6127,10 +6166,14 @@ var HistogramPlot = function (props) {
6127
6166
  minTraceValue = Math.min.apply(Math, data);
6128
6167
  globalFirstBinStart = event.points[0].data.xbins.start;
6129
6168
  }
6169
+ // Finally, we need to calculate the min and max values of the clicked bin.
6170
+ // If the bin size is a month or more, plotly records it in their "mstring" format like "M3" for 3 months.
6171
+ // We then must convert it back to milliseconds. Otherwise, it's always ms.
6172
+ var binSize = typeof event.points[0].data.xbins.size === 'string'
6173
+ ? plotlyMToMilliseconds(event.points[0].data.xbins.size)
6174
+ : event.points[0].data.xbins.size;
6130
6175
  // This minTraceValue is in the 0th bin of this trace. Find the index of this bin in the whole plot.
6131
- var clickedBinGlobalIndex = clickedBinIndex + Math.floor((minTraceValue - globalFirstBinStart) / event.points[0].data.xbins.size);
6132
- // Finally we can calculate the min and max values of the clicked bin.
6133
- var binSize = event.points[0].data.xbins.size;
6176
+ var clickedBinGlobalIndex = clickedBinIndex + Math.floor((minTraceValue - globalFirstBinStart) / binSize);
6134
6177
  var _a = [
6135
6178
  globalFirstBinStart + clickedBinGlobalIndex * binSize,
6136
6179
  globalFirstBinStart + (clickedBinGlobalIndex + 1) * binSize
@@ -6163,7 +6206,11 @@ var HistogramPlot = function (props) {
6163
6206
  // Means at least one bin has been selected
6164
6207
  var firstBinMidPoint = new Date(event.points[0].x).getTime();
6165
6208
  var lastBinMidPoint = new Date(event.points[event.points.length - 1].x).getTime();
6166
- var binSize = event.points[0].data.xbins.size;
6209
+ // If the bin size is a month or more, plotly records it in their "mstring" format like "M3" for 3 months.
6210
+ // We then must convert it back to milliseconds. Otherwise, it's always ms.
6211
+ var binSize = typeof event.points[0].data.xbins.size === 'string'
6212
+ ? plotlyMToMilliseconds(event.points[0].data.xbins.size)
6213
+ : event.points[0].data.xbins.size;
6167
6214
  minValue = new Date(firstBinMidPoint - binSize / 2);
6168
6215
  maxValue = new Date(lastBinMidPoint + binSize / 2);
6169
6216
  }
@@ -6328,7 +6375,7 @@ var HistogramPlot = function (props) {
6328
6375
  // If binSizeOverride is provided, use it to set the bin size and range explicitly.
6329
6376
  // Plotly does a better job of setting bins and ending them at nice numbers, so only use
6330
6377
  // this prop when necessary.
6331
- var xBins = binSizeOverride
6378
+ var xBins = (binSizeOverride && allData.length > 0)
6332
6379
  ? (isDateArray(allData)
6333
6380
  ? {
6334
6381
  start: roundToPrevDay(Math.min.apply(Math, allData.map(function (d) { return d.getTime(); }))), // Find a nice round number as a starting point.
@@ -6358,7 +6405,7 @@ var HistogramPlot = function (props) {
6358
6405
  width: 0.5,
6359
6406
  },
6360
6407
  },
6361
- hovertemplate: '[%{x})<br>Count: %{y}<extra></extra>', // Custom hover text
6408
+ hovertemplate: '[%{x})<br>Count: %{y}<extra></extra>',
6362
6409
  };
6363
6410
  var meanAnnotation = (statsAnnotations.includes('mean') && meanLine && data.length > 0) ? [{
6364
6411
  x: meanValue,
@@ -6449,6 +6496,7 @@ var HistogramPlot = function (props) {
6449
6496
  ticklabelposition: 'outside',
6450
6497
  tickformat: isDateArray(data) ? dateTickFormat : undefined, // Format ticks for dates
6451
6498
  automargin: true, // Adjust margin if tick labels rotate
6499
+ hoverformat: (isNumberArray(allData) && Math.max.apply(Math, allData) - Math.min.apply(Math, allData) > 3) ? '.1~f' : undefined,
6452
6500
  },
6453
6501
  yaxis: {
6454
6502
  title: {