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.
@@ -14,6 +14,8 @@ export type HistogramPlotProps = {
14
14
  onDeselect?: () => void;
15
15
  plotId?: string;
16
16
  selectByBin?: boolean;
17
+ dateTickFormat?: string;
18
+ binSizeOverride?: number;
17
19
  };
18
20
  export declare const HistogramPlot: (props: HistogramPlotProps) => import("react/jsx-runtime").JSX.Element;
19
21
  export default HistogramPlot;
@@ -2,3 +2,5 @@ export declare const isNumberArray: (arr: unknown[]) => arr is number[];
2
2
  export declare const isDateArray: (arr: unknown[]) => arr is Date[];
3
3
  export declare function calculateMean(arr: number[] | Date[]): number | undefined;
4
4
  export declare const formatDateMDY: (timestamp: number) => string;
5
+ export declare const roundToNextDay: (timestamp: number) => string;
6
+ export declare const roundToPrevDay: (timestamp: number) => string;
package/dist/index.d.ts CHANGED
@@ -4,4 +4,5 @@ export type { HistogramPlotProps } from './components/Histogram';
4
4
  export { RadialHistogramPlot } from './components/RadialHistogram';
5
5
  export type { RadialHistogramPlotProps } from './components/RadialHistogram';
6
6
  export { default as TestPlot } from './components/TestPlot';
7
+ export { isDateArray, isNumberArray } from './components/Utils';
7
8
  export type { PlotParams } from 'react-plotly.js';
package/dist/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { __spreadArray, __assign } from 'tslib';
2
2
  import { jsx, Fragment } from 'react/jsx-runtime';
3
3
  import * as React from 'react';
4
- import { lazy, useRef, useState, useMemo, Suspense } from 'react';
4
+ import { lazy, useRef, useState, useEffect, useMemo, Suspense } from 'react';
5
5
  import emStyled from '@emotion/styled';
6
6
  import { serializeStyles } from '@emotion/serialize';
7
7
  import { css, keyframes } from '@emotion/react';
@@ -57,6 +57,18 @@ function calculateMean(arr) {
57
57
  return sum / arr.length;
58
58
  }
59
59
  }
60
+ // Utilities for rounding dates to the nearest day.
61
+ // Helpful for making nice bins
62
+ var roundToNextDay = function (timestamp) {
63
+ var date = new Date(timestamp);
64
+ date.setHours(23, 59, 59, 999); // End of day
65
+ return date.toISOString();
66
+ };
67
+ var roundToPrevDay = function (timestamp) {
68
+ var date = new Date(timestamp);
69
+ date.setHours(0, 0, 0, 0); // Start of day
70
+ return date.toISOString();
71
+ };
60
72
 
61
73
  function getDefaultExportFromCjs (x) {
62
74
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -5840,13 +5852,19 @@ var Loading = function () {
5840
5852
  var Plot$2 = lazy(function () { return import('react-plotly.js'); });
5841
5853
  var HistogramPlot = function (props) {
5842
5854
  var _a, _b;
5843
- 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;
5855
+ 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;
5844
5856
  // Ref for plot container
5845
5857
  var containerRef = useRef(null);
5846
5858
  // Track any selections made in this plot so we can style the selection box.
5847
5859
  var _l = useState(null), selectedRange = _l[0], setSelectedRange = _l[1];
5860
+ // If all the data becomes selected, we should forget any old selections.
5861
+ useEffect(function () {
5862
+ if (unselectedData.length === 0) {
5863
+ setSelectedRange(null); // Clear selection box if all data is selected
5864
+ }
5865
+ }, [unselectedData]);
5848
5866
  // Set the bins based on the entire data set.
5849
- var nBins = Math.ceil(Math.sqrt(data.length + unselectedData.length));
5867
+ var nBins = (data.length + unselectedData.length) >= 10 ? Math.ceil(Math.sqrt(data.length + unselectedData.length)) : 0;
5850
5868
  // Plotly determines "nice" bins which consequently means it internally decides the axis range. We need
5851
5869
  // to access that information once the plot has been initialized so that we can prevent the
5852
5870
  // axis range from changing during interaction. Dates use strings.
@@ -5863,10 +5881,7 @@ var HistogramPlot = function (props) {
5863
5881
  if (range[0] === -1 && range[1] === 1) {
5864
5882
  return;
5865
5883
  }
5866
- var computedRange = typeof (range[0]) === 'string'
5867
- ? [new Date(range[0]), new Date(range[1])]
5868
- : [range[0], range[1]];
5869
- setFixedXAxisRange(computedRange);
5884
+ setFixedXAxisRange(range);
5870
5885
  }
5871
5886
  }
5872
5887
  };
@@ -6020,10 +6035,59 @@ var HistogramPlot = function (props) {
6020
6035
  layer: 'above' // Ensure the selection box is above the bars
6021
6036
  }];
6022
6037
  }, [selectedRange, unselectedData]);
6038
+ // Calculate the mean of the selected data using normalized data
6039
+ var meanValue = (_a = calculateMean(data)) !== null && _a !== void 0 ? _a : 0; // Default to 0 if no data
6040
+ var meanLineRadius = 0.01; // distance from the top of the y axis to the top/bottom end of the mean line
6041
+ var meanLine = (showMeanLines && data.length > 0) ? [{
6042
+ type: 'line',
6043
+ x0: meanValue,
6044
+ y0: 1 - meanLineRadius,
6045
+ x1: meanValue,
6046
+ yref: 'paper',
6047
+ y1: 1 + meanLineRadius + 0.04, // Add extra length above to make the line look centered on the y-axis top.
6048
+ line: {
6049
+ color: barColor,
6050
+ width: 1.5,
6051
+ }
6052
+ }] : [];
6053
+ // Draw mean line for all data
6054
+ var allData = __spreadArray(__spreadArray([], data, true), unselectedData, true);
6055
+ var allDataMeanValue = (_b = calculateMean(allData)) !== null && _b !== void 0 ? _b : 0;
6056
+ var allDataMeanLine = (showMeanLines && unselectedData.length > 0 && data.length > 0) ? [{
6057
+ type: 'line',
6058
+ x0: allDataMeanValue,
6059
+ y0: 1 - meanLineRadius,
6060
+ x1: allDataMeanValue,
6061
+ yref: 'paper',
6062
+ y1: 1 + meanLineRadius + 0.04, // Add extra length above to make the line look centered on the y-axis top.
6063
+ line: {
6064
+ color: unselectedBarColor,
6065
+ width: 1.5,
6066
+ }
6067
+ }] : [];
6068
+ // If binSizeOverride is provided, use it to set the bin size and range explicitly.
6069
+ // Plotly does a better job of setting bins and ending them at nice numbers, so only use
6070
+ // this prop when necessary.
6071
+ var xBins = binSizeOverride
6072
+ ? (isDateArray(allData)
6073
+ ? {
6074
+ start: roundToPrevDay(Math.min.apply(Math, allData.map(function (d) { return d.getTime(); }))), // Find a nice round number as a starting point.
6075
+ end: roundToNextDay(Math.max.apply(Math, allData.map(function (d) { return d.getTime(); }))),
6076
+ size: binSizeOverride * 86400000 // bin size in milliseconds
6077
+ }
6078
+ : isNumberArray(allData)
6079
+ ? {
6080
+ start: Math.floor(Math.min.apply(Math, allData)),
6081
+ end: Math.ceil(Math.max.apply(Math, allData)),
6082
+ size: binSizeOverride
6083
+ }
6084
+ : undefined)
6085
+ : undefined;
6023
6086
  var unselectedTrace = {
6024
6087
  x: unselectedData,
6025
6088
  type: 'histogram',
6026
6089
  autobinx: false,
6090
+ xbins: xBins,
6027
6091
  // nbinsx is valid but not included in the type definition
6028
6092
  //@ts-ignore
6029
6093
  nbinsx: nBins, // Maximum number of bins. Plotly may adjust to make bins "nicer".
@@ -6041,6 +6105,7 @@ var HistogramPlot = function (props) {
6041
6105
  x: data,
6042
6106
  type: 'histogram',
6043
6107
  autobinx: false,
6108
+ xbins: xBins,
6044
6109
  // nbinsx is valid but not included in the type definition
6045
6110
  //@ts-ignore
6046
6111
  nbinsx: nBins, // Maximum number of bins. Plotly may adjust to make bins "nicer".
@@ -6055,36 +6120,6 @@ var HistogramPlot = function (props) {
6055
6120
  },
6056
6121
  unselectedTrace,
6057
6122
  ];
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 = __spreadArray(__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
6123
  var layout = {
6089
6124
  title: {
6090
6125
  text: title,
@@ -6118,6 +6153,8 @@ var HistogramPlot = function (props) {
6118
6153
  linewidth: 1,
6119
6154
  fixedrange: true, // Disable zooming
6120
6155
  ticklabelposition: 'outside',
6156
+ tickformat: isDateArray(data) ? dateTickFormat : undefined, // Format ticks for dates
6157
+ automargin: true, // Adjust margin if tick labels rotate
6121
6158
  },
6122
6159
  yaxis: {
6123
6160
  title: {
@@ -6303,5 +6340,5 @@ var TestPlot = function (props) {
6303
6340
  return jsx(Plot, { data: data, layout: layout });
6304
6341
  };
6305
6342
 
6306
- export { HistogramPlot, RadialHistogramPlot, TestPlot };
6343
+ export { HistogramPlot, RadialHistogramPlot, TestPlot, isDateArray, isNumberArray };
6307
6344
  //# sourceMappingURL=index.esm.js.map