td-plots 1.2.2 → 1.2.3

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
@@ -5860,25 +5860,35 @@ var Loading = function () {
5860
5860
  var Plot$2 = React.lazy(function () { return import('react-plotly.js'); });
5861
5861
  var HistogramPlot = function (props) {
5862
5862
  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.showMeanLine, showMeanLine = _f === void 0 ? true : _f, _g = props.showAllDataMeanLine, showAllDataMeanLine = _g === void 0 ? true : _g, containerStyleOverrides = props.containerStyleOverrides, _h = props.unselectedData, unselectedData = _h === void 0 ? [] : _h, _j = props.handleClickOrSelection, handleClickOrSelection = _j === void 0 ? function () { } : _j, _k = props.onDeselect, onDeselect = _k === void 0 ? function () { } : _k, plotId = props.plotId;
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.showMeanLine, showMeanLine = _f === void 0 ? true : _f, _g = props.showAllDataMeanLine, showAllDataMeanLine = _g === void 0 ? true : _g, containerStyleOverrides = props.containerStyleOverrides, _h = props.unselectedData, unselectedData = _h === void 0 ? [] : _h, _j = props.handleClickOrSelection, handleClickOrSelection = _j === void 0 ? function () { } : _j, _k = props.onDeselect, onDeselect = _k === void 0 ? function () { } : _k, plotId = props.plotId, _l = props.selectByBin, selectByBin = _l === void 0 ? false : _l;
5864
5864
  // Ref for plot container
5865
5865
  var containerRef = React.useRef(null);
5866
5866
  // Track any selections made in this plot so we can style the selection box.
5867
- var _l = React.useState(null), selectedRange = _l[0], setSelectedRange = _l[1];
5867
+ var _m = React.useState(null), selectedRange = _m[0], setSelectedRange = _m[1];
5868
5868
  // Set the bins based on the entire data set.
5869
5869
  var nBins = Math.ceil(Math.sqrt(data.length + unselectedData.length));
5870
5870
  // Plotly determines "nice" bins which consequently means it internally decides the axis range. We need
5871
5871
  // to access that information once the plot has been initialized so that we can prevent the
5872
5872
  // axis range from changing during interaction. Dates use strings.
5873
- var _m = React.useState(undefined), fixedXAxisRange = _m[0], setFixedXAxisRange = _m[1];
5873
+ var _o = React.useState(undefined), fixedXAxisRange = _o[0], setFixedXAxisRange = _o[1];
5874
+ // Once the plot is drawn, record the initial axis range so we can keep it fixed.
5874
5875
  // figure should be Readonly<Plotly.Figure> but react-plotly.js doesn't expose that type, so we use any.
5875
- var handlePlotInitialized = function (figure, graphDiv) {
5876
- var _a, _b;
5877
- if (((_b = (_a = figure.layout) === null || _a === void 0 ? void 0 : _a.xaxis) === null || _b === void 0 ? void 0 : _b.range) && !fixedXAxisRange) {
5878
- var computedRange = typeof (figure.layout.xaxis.range[0]) === 'string'
5879
- ? [new Date(figure.layout.xaxis.range[0]), new Date(figure.layout.xaxis.range[1])]
5880
- : [figure.layout.xaxis.range[0], figure.layout.xaxis.range[1]];
5881
- setFixedXAxisRange(computedRange);
5876
+ var handlePlotUpdate = function (figure, graphDiv) {
5877
+ var _a;
5878
+ if (!fixedXAxisRange) {
5879
+ var currentLayout = graphDiv._fullLayout;
5880
+ if ((_a = currentLayout === null || currentLayout === void 0 ? void 0 : currentLayout.xaxis) === null || _a === void 0 ? void 0 : _a.range) {
5881
+ var range = currentLayout.xaxis.range;
5882
+ // Skip temporary [-1, 1] range
5883
+ if (range[0] === -1 && range[1] === 1) {
5884
+ return;
5885
+ }
5886
+ var computedRange = typeof (range[0]) === 'string'
5887
+ ? [new Date(range[0]), new Date(range[1])]
5888
+ : [range[0], range[1]];
5889
+ console.log('Captured range from onUpdate:', computedRange);
5890
+ setFixedXAxisRange(computedRange);
5891
+ }
5882
5892
  }
5883
5893
  };
5884
5894
  // Create handler for click event that can use event data to update the plot if desired.
@@ -5947,18 +5957,68 @@ var HistogramPlot = function (props) {
5947
5957
  var minValue;
5948
5958
  var maxValue;
5949
5959
  if (typeof event.range.x[0] === 'string' && typeof event.range.x[1] === 'string') {
5950
- // Then we must be dealing with dates
5951
- minValue = new Date(event.range.x[0]);
5952
- maxValue = new Date(event.range.x[1]);
5953
- setSelectedRange([minValue, maxValue]);
5960
+ // Then we are must be dealing with dates
5961
+ if (selectByBin) {
5962
+ // Set selected range to include the whole bin if at least half the bin is within the explicit range
5963
+ // Plotly already tells us which bins were selected based on this logic. These are the points in the event.
5964
+ if (event.points && event.points.length > 0) {
5965
+ // Means at least one bin has been selected
5966
+ var firstBinMidPoint = new Date(event.points[0].x).getTime();
5967
+ var lastBinMidPoint = new Date(event.points[event.points.length - 1].x).getTime();
5968
+ var binSize = event.points[0].data.xbins.size;
5969
+ minValue = new Date(firstBinMidPoint - binSize / 2);
5970
+ maxValue = new Date(lastBinMidPoint + binSize / 2);
5971
+ }
5972
+ else {
5973
+ // No bins selected, so the range should be empty.
5974
+ minValue = new Date(event.range.x[0]);
5975
+ maxValue = minValue;
5976
+ }
5977
+ }
5978
+ else {
5979
+ // Set the range based on the explicit selection boundaries
5980
+ minValue = new Date(event.range.x[0]);
5981
+ maxValue = new Date(event.range.x[1]);
5982
+ }
5954
5983
  }
5955
5984
  else {
5956
- minValue = event.range.x[0];
5957
- maxValue = event.range.x[1];
5958
- setSelectedRange([minValue, maxValue]);
5985
+ // Handle selection for numbers
5986
+ if (selectByBin) {
5987
+ // Set the range based on the bins selected. Plotly will include a bin in event.points if
5988
+ // at least half of it is selected.
5989
+ if (event.points && event.points.length > 0) {
5990
+ var firstBinMidPoint = event.points[0].x;
5991
+ var lastBinMidPoint = event.points[event.points.length - 1].x;
5992
+ var binSize = event.points[0].data.xbins.size;
5993
+ var roundedMinValue = firstBinMidPoint - binSize / 2;
5994
+ var roundedMaxValue = lastBinMidPoint + binSize / 2;
5995
+ minValue = roundedMinValue;
5996
+ maxValue = roundedMaxValue;
5997
+ }
5998
+ else {
5999
+ // No bins selected, so set the range to be empty.
6000
+ minValue = event.range.x[0];
6001
+ maxValue = minValue;
6002
+ }
6003
+ }
6004
+ else {
6005
+ // Set the range based on the explicit selection boundaries
6006
+ minValue = event.range.x[0];
6007
+ maxValue = event.range.x[1];
6008
+ }
6009
+ }
6010
+ if (minValue !== undefined && maxValue !== undefined) {
6011
+ // Update selected range. Have to be strict about types.
6012
+ if (typeof minValue === 'number' && typeof maxValue === 'number') {
6013
+ setSelectedRange([minValue, maxValue]);
6014
+ }
6015
+ else if (minValue instanceof Date && maxValue instanceof Date) {
6016
+ setSelectedRange([minValue, maxValue]);
6017
+ }
6018
+ handleClickOrSelection(minValue, maxValue);
5959
6019
  }
5960
- handleClickOrSelection(minValue, maxValue);
5961
6020
  };
6021
+ console.log('fixed range', fixedXAxisRange);
5962
6022
  // Create the selected range box
5963
6023
  var selectedRangeBox = React.useMemo(function () {
5964
6024
  if (!selectedRange)
@@ -6131,7 +6191,7 @@ var HistogramPlot = function (props) {
6131
6191
  return (jsxRuntime.jsx("div", { ref: containerRef, className: "plot-container ".concat(plotId), style: tslib.__assign({ '--selection-color': selectorsColor }, containerStyles), children: jsxRuntime.jsx(React.Suspense, { fallback: jsxRuntime.jsx(Loading, {}), children: jsxRuntime.jsx(jsxRuntime.Fragment, { children: jsxRuntime.jsx(Plot$2, { data: plotlyData, layout: layout, config: config, onSelected: handleSelection, onClick: handleClick, onDeselect: function () {
6132
6192
  onDeselect();
6133
6193
  setSelectedRange(null); // Remove selected box
6134
- }, onInitialized: handlePlotInitialized, useResizeHandler: true, style: {
6194
+ }, onUpdate: handlePlotUpdate, useResizeHandler: true, style: {
6135
6195
  width: "100%",
6136
6196
  height: "100%",
6137
6197
  display: "block"