td-plots 1.3.1 → 1.4.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/dist/index.js CHANGED
@@ -77,6 +77,18 @@ function calculateMean(arr) {
77
77
  return sum / arr.length;
78
78
  }
79
79
  }
80
+ // Utilities for rounding dates to the nearest day.
81
+ // Helpful for making nice bins
82
+ var roundToNextDay = function (timestamp) {
83
+ var date = new Date(timestamp);
84
+ date.setHours(23, 59, 59, 999); // End of day
85
+ return date.toISOString();
86
+ };
87
+ var roundToPrevDay = function (timestamp) {
88
+ var date = new Date(timestamp);
89
+ date.setHours(0, 0, 0, 0); // Start of day
90
+ return date.toISOString();
91
+ };
80
92
 
81
93
  function getDefaultExportFromCjs (x) {
82
94
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -5860,13 +5872,19 @@ var Loading = function () {
5860
5872
  var Plot$2 = React.lazy(function () { return import('react-plotly.js'); });
5861
5873
  var HistogramPlot = function (props) {
5862
5874
  var _a, _b;
5863
- var data = props.data, title = props.title, xAxisTitle = props.xAxisTitle, _c = props.barColor, barColor = _c === void 0 ? 'rgb(72, 72, 74)' : _c, _d = props.unselectedBarColor, unselectedBarColor = _d === void 0 ? 'rgba(203, 195, 195, 0.88)' : _d, _e = props.selectorsColor, selectorsColor = _e === void 0 ? 'black' : _e, _f = props.showMeanLines, showMeanLines = _f === void 0 ? true : _f, containerStyleOverrides = props.containerStyleOverrides, _g = props.unselectedData, unselectedData = _g === void 0 ? [] : _g, _h = props.handleClickOrSelection, handleClickOrSelection = _h === void 0 ? function () { } : _h, _j = props.onDeselect, onDeselect = _j === void 0 ? function () { } : _j, plotId = props.plotId, _k = props.selectByBin, selectByBin = _k === void 0 ? false : _k;
5875
+ var data = props.data, title = props.title, xAxisTitle = props.xAxisTitle, _c = props.barColor, barColor = _c === void 0 ? 'rgb(72, 72, 74)' : _c, _d = props.unselectedBarColor, unselectedBarColor = _d === void 0 ? 'rgba(203, 195, 195, 0.88)' : _d, _e = props.selectorsColor, selectorsColor = _e === void 0 ? 'black' : _e, _f = props.showMeanLines, showMeanLines = _f === void 0 ? true : _f, containerStyleOverrides = props.containerStyleOverrides, _g = props.unselectedData, unselectedData = _g === void 0 ? [] : _g, _h = props.handleClickOrSelection, handleClickOrSelection = _h === void 0 ? function () { } : _h, _j = props.onDeselect, onDeselect = _j === void 0 ? function () { } : _j, plotId = props.plotId, _k = props.selectByBin, selectByBin = _k === void 0 ? false : _k, dateTickFormat = props.dateTickFormat, binSizeOverride = props.binSizeOverride;
5864
5876
  // Ref for plot container
5865
5877
  var containerRef = React.useRef(null);
5866
5878
  // Track any selections made in this plot so we can style the selection box.
5867
5879
  var _l = React.useState(null), selectedRange = _l[0], setSelectedRange = _l[1];
5880
+ // If all the data becomes selected, we should forget any old selections.
5881
+ React.useEffect(function () {
5882
+ if (unselectedData.length === 0) {
5883
+ setSelectedRange(null); // Clear selection box if all data is selected
5884
+ }
5885
+ }, [unselectedData]);
5868
5886
  // Set the bins based on the entire data set.
5869
- var nBins = Math.ceil(Math.sqrt(data.length + unselectedData.length));
5887
+ var nBins = (data.length + unselectedData.length) >= 10 ? Math.ceil(Math.sqrt(data.length + unselectedData.length)) : 0;
5870
5888
  // Plotly determines "nice" bins which consequently means it internally decides the axis range. We need
5871
5889
  // to access that information once the plot has been initialized so that we can prevent the
5872
5890
  // axis range from changing during interaction. Dates use strings.
@@ -5883,10 +5901,7 @@ var HistogramPlot = function (props) {
5883
5901
  if (range[0] === -1 && range[1] === 1) {
5884
5902
  return;
5885
5903
  }
5886
- var computedRange = typeof (range[0]) === 'string'
5887
- ? [new Date(range[0]), new Date(range[1])]
5888
- : [range[0], range[1]];
5889
- setFixedXAxisRange(computedRange);
5904
+ setFixedXAxisRange(range);
5890
5905
  }
5891
5906
  }
5892
5907
  };
@@ -6040,10 +6055,59 @@ var HistogramPlot = function (props) {
6040
6055
  layer: 'above' // Ensure the selection box is above the bars
6041
6056
  }];
6042
6057
  }, [selectedRange, unselectedData]);
6058
+ // Calculate the mean of the selected data using normalized data
6059
+ var meanValue = (_a = calculateMean(data)) !== null && _a !== void 0 ? _a : 0; // Default to 0 if no data
6060
+ var meanLineRadius = 0.01; // distance from the top of the y axis to the top/bottom end of the mean line
6061
+ var meanLine = (showMeanLines && data.length > 0) ? [{
6062
+ type: 'line',
6063
+ x0: meanValue,
6064
+ y0: 1 - meanLineRadius,
6065
+ x1: meanValue,
6066
+ yref: 'paper',
6067
+ y1: 1 + meanLineRadius + 0.04, // Add extra length above to make the line look centered on the y-axis top.
6068
+ line: {
6069
+ color: barColor,
6070
+ width: 1.5,
6071
+ }
6072
+ }] : [];
6073
+ // Draw mean line for all data
6074
+ var allData = tslib.__spreadArray(tslib.__spreadArray([], data, true), unselectedData, true);
6075
+ var allDataMeanValue = (_b = calculateMean(allData)) !== null && _b !== void 0 ? _b : 0;
6076
+ var allDataMeanLine = (showMeanLines && unselectedData.length > 0 && data.length > 0) ? [{
6077
+ type: 'line',
6078
+ x0: allDataMeanValue,
6079
+ y0: 1 - meanLineRadius,
6080
+ x1: allDataMeanValue,
6081
+ yref: 'paper',
6082
+ y1: 1 + meanLineRadius + 0.04, // Add extra length above to make the line look centered on the y-axis top.
6083
+ line: {
6084
+ color: unselectedBarColor,
6085
+ width: 1.5,
6086
+ }
6087
+ }] : [];
6088
+ // If binSizeOverride is provided, use it to set the bin size and range explicitly.
6089
+ // Plotly does a better job of setting bins and ending them at nice numbers, so only use
6090
+ // this prop when necessary.
6091
+ var xBins = binSizeOverride
6092
+ ? (isDateArray(allData)
6093
+ ? {
6094
+ start: roundToPrevDay(Math.min.apply(Math, allData.map(function (d) { return d.getTime(); }))), // Find a nice round number as a starting point.
6095
+ end: roundToNextDay(Math.max.apply(Math, allData.map(function (d) { return d.getTime(); }))),
6096
+ size: binSizeOverride * 86400000 // bin size in milliseconds
6097
+ }
6098
+ : isNumberArray(allData)
6099
+ ? {
6100
+ start: Math.floor(Math.min.apply(Math, allData)),
6101
+ end: Math.ceil(Math.max.apply(Math, allData)),
6102
+ size: binSizeOverride
6103
+ }
6104
+ : undefined)
6105
+ : undefined;
6043
6106
  var unselectedTrace = {
6044
6107
  x: unselectedData,
6045
6108
  type: 'histogram',
6046
6109
  autobinx: false,
6110
+ xbins: xBins,
6047
6111
  // nbinsx is valid but not included in the type definition
6048
6112
  //@ts-ignore
6049
6113
  nbinsx: nBins, // Maximum number of bins. Plotly may adjust to make bins "nicer".
@@ -6061,6 +6125,7 @@ var HistogramPlot = function (props) {
6061
6125
  x: data,
6062
6126
  type: 'histogram',
6063
6127
  autobinx: false,
6128
+ xbins: xBins,
6064
6129
  // nbinsx is valid but not included in the type definition
6065
6130
  //@ts-ignore
6066
6131
  nbinsx: nBins, // Maximum number of bins. Plotly may adjust to make bins "nicer".
@@ -6075,36 +6140,6 @@ var HistogramPlot = function (props) {
6075
6140
  },
6076
6141
  unselectedTrace,
6077
6142
  ];
6078
- // Calculate the mean of the selected data using normalized data
6079
- var meanValue = (_a = calculateMean(data)) !== null && _a !== void 0 ? _a : 0; // Default to 0 if no data
6080
- var meanLineRadius = 0.01; // distance from the top of the y axis to the top/bottom end of the mean line
6081
- var meanLine = (showMeanLines && data.length > 0) ? [{
6082
- type: 'line',
6083
- x0: meanValue,
6084
- y0: 1 - meanLineRadius,
6085
- x1: meanValue,
6086
- yref: 'paper',
6087
- y1: 1 + meanLineRadius + 0.04, // Add extra length above to make the line look centered on the y-axis top.
6088
- line: {
6089
- color: barColor,
6090
- width: 1.5,
6091
- }
6092
- }] : [];
6093
- // Draw mean line for all data
6094
- var allData = tslib.__spreadArray(tslib.__spreadArray([], data, true), unselectedData, true);
6095
- var allDataMeanValue = (_b = calculateMean(allData)) !== null && _b !== void 0 ? _b : 0;
6096
- var allDataMeanLine = (showMeanLines && unselectedData.length > 0 && data.length > 0) ? [{
6097
- type: 'line',
6098
- x0: allDataMeanValue,
6099
- y0: 1 - meanLineRadius,
6100
- x1: allDataMeanValue,
6101
- yref: 'paper',
6102
- y1: 1 + meanLineRadius + 0.04, // Add extra length above to make the line look centered on the y-axis top.
6103
- line: {
6104
- color: unselectedBarColor,
6105
- width: 1.5,
6106
- }
6107
- }] : [];
6108
6143
  var layout = {
6109
6144
  title: {
6110
6145
  text: title,
@@ -6138,6 +6173,8 @@ var HistogramPlot = function (props) {
6138
6173
  linewidth: 1,
6139
6174
  fixedrange: true, // Disable zooming
6140
6175
  ticklabelposition: 'outside',
6176
+ tickformat: isDateArray(data) ? dateTickFormat : undefined, // Format ticks for dates
6177
+ automargin: true, // Adjust margin if tick labels rotate
6141
6178
  },
6142
6179
  yaxis: {
6143
6180
  title: {
@@ -6326,4 +6363,6 @@ var TestPlot = function (props) {
6326
6363
  exports.HistogramPlot = HistogramPlot;
6327
6364
  exports.RadialHistogramPlot = RadialHistogramPlot;
6328
6365
  exports.TestPlot = TestPlot;
6366
+ exports.isDateArray = isDateArray;
6367
+ exports.isNumberArray = isNumberArray;
6329
6368
  //# sourceMappingURL=index.js.map