td-plots 1.2.3 → 1.3.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.
@@ -3,8 +3,7 @@ import './plotStyles.scss';
3
3
  export type HistogramPlotProps = {
4
4
  data: number[] | Date[];
5
5
  unselectedData?: number[] | Date[];
6
- showMeanLine?: boolean;
7
- showAllDataMeanLine?: boolean;
6
+ showMeanLines?: boolean;
8
7
  title?: string;
9
8
  xAxisTitle?: string;
10
9
  barColor?: string;
@@ -15,6 +14,7 @@ export type HistogramPlotProps = {
15
14
  onDeselect?: () => void;
16
15
  plotId?: string;
17
16
  selectByBin?: boolean;
17
+ dateTickFormat?: string;
18
18
  };
19
19
  export declare const HistogramPlot: (props: HistogramPlotProps) => import("react/jsx-runtime").JSX.Element;
20
20
  export default HistogramPlot;
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';
@@ -5840,17 +5840,23 @@ var Loading = function () {
5840
5840
  var Plot$2 = lazy(function () { return import('react-plotly.js'); });
5841
5841
  var HistogramPlot = function (props) {
5842
5842
  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.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;
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, dateTickFormat = props.dateTickFormat;
5844
5844
  // Ref for plot container
5845
5845
  var containerRef = useRef(null);
5846
5846
  // Track any selections made in this plot so we can style the selection box.
5847
- var _m = useState(null), selectedRange = _m[0], setSelectedRange = _m[1];
5847
+ var _l = useState(null), selectedRange = _l[0], setSelectedRange = _l[1];
5848
+ // If all the data becomes selected, we should forget any old selections.
5849
+ useEffect(function () {
5850
+ if (unselectedData.length === 0) {
5851
+ setSelectedRange(null); // Clear selection box if all data is selected
5852
+ }
5853
+ }, [unselectedData]);
5848
5854
  // Set the bins based on the entire data set.
5849
- var nBins = Math.ceil(Math.sqrt(data.length + unselectedData.length));
5855
+ var nBins = (data.length + unselectedData.length) >= 10 ? Math.ceil(Math.sqrt(data.length + unselectedData.length)) : 0;
5850
5856
  // Plotly determines "nice" bins which consequently means it internally decides the axis range. We need
5851
5857
  // to access that information once the plot has been initialized so that we can prevent the
5852
5858
  // axis range from changing during interaction. Dates use strings.
5853
- var _o = useState(undefined), fixedXAxisRange = _o[0], setFixedXAxisRange = _o[1];
5859
+ var _m = useState(undefined), fixedXAxisRange = _m[0], setFixedXAxisRange = _m[1];
5854
5860
  // Once the plot is drawn, record the initial axis range so we can keep it fixed.
5855
5861
  // figure should be Readonly<Plotly.Figure> but react-plotly.js doesn't expose that type, so we use any.
5856
5862
  var handlePlotUpdate = function (figure, graphDiv) {
@@ -5863,11 +5869,7 @@ var HistogramPlot = function (props) {
5863
5869
  if (range[0] === -1 && range[1] === 1) {
5864
5870
  return;
5865
5871
  }
5866
- var computedRange = typeof (range[0]) === 'string'
5867
- ? [new Date(range[0]), new Date(range[1])]
5868
- : [range[0], range[1]];
5869
- console.log('Captured range from onUpdate:', computedRange);
5870
- setFixedXAxisRange(computedRange);
5872
+ setFixedXAxisRange(range);
5871
5873
  }
5872
5874
  }
5873
5875
  };
@@ -5998,11 +6000,12 @@ var HistogramPlot = function (props) {
5998
6000
  handleClickOrSelection(minValue, maxValue);
5999
6001
  }
6000
6002
  };
6001
- console.log('fixed range', fixedXAxisRange);
6002
6003
  // Create the selected range box
6003
6004
  var selectedRangeBox = useMemo(function () {
6004
6005
  if (!selectedRange)
6005
6006
  return [];
6007
+ if (unselectedData.length === 0)
6008
+ return []; // Don't show the box if the entire dataset is selected.
6006
6009
  // Create a multiply-like effect by using a semi-transparent dark overlay
6007
6010
  var multiplyColor = 'rgba(29, 104, 185, 0.1)';
6008
6011
  return [{
@@ -6019,7 +6022,7 @@ var HistogramPlot = function (props) {
6019
6022
  },
6020
6023
  layer: 'above' // Ensure the selection box is above the bars
6021
6024
  }];
6022
- }, [selectedRange]);
6025
+ }, [selectedRange, unselectedData]);
6023
6026
  var unselectedTrace = {
6024
6027
  x: unselectedData,
6025
6028
  type: 'histogram',
@@ -6058,7 +6061,7 @@ var HistogramPlot = function (props) {
6058
6061
  // Calculate the mean of the selected data using normalized data
6059
6062
  var meanValue = (_a = calculateMean(data)) !== null && _a !== void 0 ? _a : 0; // Default to 0 if no data
6060
6063
  var meanLineRadius = 0.01; // distance from the top of the y axis to the top/bottom end of the mean line
6061
- var meanLine = showMeanLine ? [{
6064
+ var meanLine = (showMeanLines && data.length > 0) ? [{
6062
6065
  type: 'line',
6063
6066
  x0: meanValue,
6064
6067
  y0: 1 - meanLineRadius,
@@ -6073,7 +6076,7 @@ var HistogramPlot = function (props) {
6073
6076
  // Draw mean line for all data
6074
6077
  var allData = __spreadArray(__spreadArray([], data, true), unselectedData, true);
6075
6078
  var allDataMeanValue = (_b = calculateMean(allData)) !== null && _b !== void 0 ? _b : 0;
6076
- var allDataMeanLine = (showAllDataMeanLine && unselectedData.length > 0) ? [{
6079
+ var allDataMeanLine = (showMeanLines && unselectedData.length > 0 && data.length > 0) ? [{
6077
6080
  type: 'line',
6078
6081
  x0: allDataMeanValue,
6079
6082
  y0: 1 - meanLineRadius,
@@ -6118,6 +6121,8 @@ var HistogramPlot = function (props) {
6118
6121
  linewidth: 1,
6119
6122
  fixedrange: true, // Disable zooming
6120
6123
  ticklabelposition: 'outside',
6124
+ tickformat: isDateArray(data) ? dateTickFormat : undefined, // Format ticks for dates
6125
+ automargin: true, // Adjust margin if tick labels rotate
6121
6126
  },
6122
6127
  yaxis: {
6123
6128
  title: {
@@ -6142,7 +6147,7 @@ var HistogramPlot = function (props) {
6142
6147
  dragmode: 'select', // Enable drag to select
6143
6148
  selectdirection: 'h', // User can select in horizontal direction only
6144
6149
  shapes: __spreadArray(__spreadArray(__spreadArray([], allDataMeanLine, true), meanLine, true), selectedRangeBox, true), // Add the mean line and selection box
6145
- annotations: (showMeanLine && meanLine) ? [{
6150
+ annotations: (showMeanLines && meanLine && data.length > 0) ? [{
6146
6151
  x: meanValue,
6147
6152
  y: 1 + meanLineRadius + 0.04, // Position above the mean line. Value set with respect to the paper coordinates.
6148
6153
  yref: 'paper',