Skip to content

Commit

Permalink
custom button added to all graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
williamw04 committed Oct 19, 2024
1 parent 0ad2d19 commit 1355633
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 18 deletions.
26 changes: 12 additions & 14 deletions src/client/app/components/BarChartComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@ export default function BarChartComponent() {
const raw = useAppSelector(selectIsRaw);
const unitLabel = useAppSelector(selectBarUnitLabel);

// Display Plotly Buttons Feature
// The number of items in defaultButtons and advancedButtons must differ as discussed below
const defaultButtons: Plotly.ModeBarDefaultButtons[] = [
'zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d'];
const advancedButtons: Plotly.ModeBarDefaultButtons[] = [
'select2d', 'lasso2d', 'autoScale2d', 'resetScale2d'
];

const defaultButtons: Plotly.ModeBarDefaultButtons[] = ['zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d',
'resetScale2d'];
const advancedButtons: Plotly.ModeBarDefaultButtons[] = ['select2d', 'lasso2d', 'autoScale2d', 'resetScale2d'];
// Manage button states with useState
const [listOfButtons, setListOfButtons] = React.useState(defaultButtons);

// useQueryHooks for data fetching
const datasets: Partial<Plotly.PlotData>[] = meterReadings.concat(groupData);

Expand Down Expand Up @@ -113,14 +111,14 @@ export default function BarChartComponent() {
displayModeBar: true,
modeBarButtonsToRemove: listOfButtons,
modeBarButtonsToAdd: [{
name: 'more-options',
title: 'More Options',
icon: Icons.pencil,
click: function () {
name: 'more-options',
title: 'More Options',
icon: Icons.pencil,
click: function () {
// # of items must differ so the length can tell which list of buttons is being set
setListOfButtons(listOfButtons.length === defaultButtons.length ? advancedButtons : defaultButtons); // Update the state
}
}],
setListOfButtons(listOfButtons.length === defaultButtons.length ? advancedButtons : defaultButtons); // Update the state
}
}],
// Current Locale
locale,
// Available Locales
Expand Down
20 changes: 19 additions & 1 deletion src/client/app/components/LineChartComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { utc } from 'moment';
import { PlotRelayoutEvent } from 'plotly.js';
import * as React from 'react';
import Plot from 'react-plotly.js';
import { Icons } from 'plotly.js';
import { TimeInterval } from '../../../common/TimeInterval';
import { updateSliderRange } from '../redux/actions/extraActions';
import { readingsApi, stableEmptyLineReadings } from '../redux/api/readingsApi';
Expand Down Expand Up @@ -56,6 +57,14 @@ export default function LineChartComponent() {
// Use Query Data to derive plotly datasets memoized selector
const unitLabel = useAppSelector(selectLineUnitLabel);

// Display Plotly Buttons Feature
// The number of items in defaultButtons and advancedButtons must differ as discussed below
const defaultButtons: Plotly.ModeBarDefaultButtons[] = ['zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d',
'zoomOut2d', 'autoScale2d','resetScale2d'];
const advancedButtons: Plotly.ModeBarDefaultButtons[] = ['select2d', 'lasso2d','autoScale2d','resetScale2d'];
// Manage button states with useState
const [listOfButtons, setListOfButtons] = React.useState(defaultButtons);

const data: Partial<Plotly.PlotData>[] = React.useMemo(() => meterPlotlyData.concat(groupPlotlyData), [meterPlotlyData, groupPlotlyData]);


Expand Down Expand Up @@ -87,7 +96,16 @@ export default function LineChartComponent() {
config={{
responsive: true,
displayModeBar: true,
modeBarButtonsToRemove: ['select2d','lasso2d','autoScale2d','resetScale2d'], // Current Locale
modeBarButtonsToRemove: listOfButtons,
modeBarButtonsToAdd: [{
name: 'more-options',
title: 'More Options',
icon: Icons.pencil,
click: function () {
// # of items must differ so the length can tell which list of buttons is being set
setListOfButtons(listOfButtons.length === defaultButtons.length ? advancedButtons : defaultButtons); // Update the state
}
}],
locale,
// Available Locales
locales: Locales
Expand Down
20 changes: 19 additions & 1 deletion src/client/app/components/RadarChartComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as moment from 'moment';
import { Layout } from 'plotly.js';
import * as React from 'react';
import Plot from 'react-plotly.js';
import { Icons } from 'plotly.js';
import { selectGroupDataById } from '../redux/api/groupsApi';
import { selectMeterDataById } from '../redux/api/metersApi';
import { readingsApi } from '../redux/api/readingsApi';
Expand Down Expand Up @@ -67,6 +68,14 @@ export default function RadarChartComponent() {
// The rate will be 1 if it is per hour (since state readings are per hour) or no rate scaling so no change.
const rateScaling = needsRateScaling ? currentSelectedRate.rate : 1;

// Display Plotly Buttons Feature
// The number of items in defaultButtons and advancedButtons must differ as discussed below
const defaultButtons: Plotly.ModeBarDefaultButtons[] = ['zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d',
'resetScale2d'];
const advancedButtons: Plotly.ModeBarDefaultButtons[] = ['select2d', 'lasso2d', 'autoScale2d', 'resetScale2d'];
// Manage button states with useState
const [listOfButtons, setListOfButtons] = React.useState(defaultButtons);

// Add all valid data from existing meters to the radar plot
for (const meterID of selectedMeters) {
if (meterReadings) {
Expand Down Expand Up @@ -328,7 +337,16 @@ export default function RadarChartComponent() {
useResizeHandler={true}
config={{
displayModeBar: true,
modeBarButtonsToRemove: ['select2d','lasso2d','autoScale2d','resetScale2d'],
modeBarButtonsToRemove: listOfButtons,
modeBarButtonsToAdd: [{
name: 'more-options',
title: 'More Options',
icon: Icons.pencil,
click: function () {
// # of items must differ so the length can tell which list of buttons is being set
setListOfButtons(listOfButtons.length === defaultButtons.length ? advancedButtons : defaultButtons); // Update the state
}
}],
responsive: true,
locales: Locales // makes locales available for use
}}
Expand Down
19 changes: 18 additions & 1 deletion src/client/app/components/ThreeDComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import translate from '../utils/translate';
import SpinnerComponent from './SpinnerComponent';
import ThreeDPillComponent from './ThreeDPillComponent';
import Plot from 'react-plotly.js';
import { Icons } from 'plotly.js';
import { selectSelectedLanguage } from '../redux/slices/appStateSlice';
import Locales from '../types/locales';

Expand All @@ -47,6 +48,13 @@ export default function ThreeDComponent() {
let layout = {};
let dataToRender = null;

// Display Plotly Buttons Feature
// The number of items in defaultButtons and advancedButtons must differ as discussed below
const defaultButtons: Plotly.ModeBarDefaultButtons[] = ['zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d',
'resetScale2d'];
const advancedButtons: Plotly.ModeBarDefaultButtons[] = ['resetCameraDefault3d'];
// Manage button states with useState
const [listOfButtons, setListOfButtons] = React.useState(defaultButtons);

if (!meterOrGroupID) {
// No selected Meters
Expand Down Expand Up @@ -81,7 +89,16 @@ export default function ThreeDComponent() {
config={{
responsive: true,
displayModeBar: true,
modeBarButtonsToRemove: ['resetCameraDefault3d'],
modeBarButtonsToRemove: listOfButtons,
modeBarButtonsToAdd: [{
name: 'more-options',
title: 'More Options',
icon: Icons.pencil,
click: function () {
// # of items must differ so the length can tell which list of buttons is being set
setListOfButtons(listOfButtons.length === defaultButtons.length ? advancedButtons : defaultButtons); // Update the state
}
}],
// Current Locale
locale,
// Available Locales
Expand Down
21 changes: 20 additions & 1 deletion src/client/app/containers/CompareChartContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import { connect } from 'react-redux';
import { getComparePeriodLabels, getCompareChangeSummary, calculateCompareShift } from '../utils/calculateCompare';
import translate from '../utils/translate';
import * as React from 'react';
import Plot from 'react-plotly.js';
import { Icons } from 'plotly.js';
import Locales from '../types/locales';
import * as moment from 'moment';
import { UnitRepresentType } from '../types/redux/units';
Expand Down Expand Up @@ -84,6 +86,14 @@ function mapStateToProps(state: RootState, ownProps: CompareChartContainerProps)
}
}

// Display Plotly Buttons Feature
// The number of items in defaultButtons and advancedButtons must differ as discussed below
const defaultButtons: Plotly.ModeBarDefaultButtons[] = ['zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d',
'resetScale2d'];
const advancedButtons: Plotly.ModeBarDefaultButtons[] = ['select2d', 'lasso2d', 'autoScale2d', 'resetScale2d'];
// Manage button states with useState
const [listOfButtons, setListOfButtons] = React.useState(defaultButtons);

// Get the time shift for this comparison as a moment duration
const compareShift = calculateCompareShift(comparePeriod);
// The start and end of this time period. Need to create new moment objects since subtraction mutates the original.
Expand Down Expand Up @@ -231,7 +241,16 @@ function mapStateToProps(state: RootState, ownProps: CompareChartContainerProps)
layout,
config: {
displayModeBar: true,
modeBarButtonsToRemove: ['select2d','lasso2d','autoScale2d','resetScale2d'],
modeBarButtonsToRemove: listOfButtons,
modeBarButtonsToAdd: [{
name: 'more-options',
title: 'More Options',
icon: Icons.pencil,
click: function () {
// # of items must differ so the length can tell which list of buttons is being set
setListOfButtons(listOfButtons.length === defaultButtons.length ? advancedButtons : defaultButtons); // Update the state
}
}],
locale,
locales: Locales // makes locales available for use
}
Expand Down

0 comments on commit 1355633

Please sign in to comment.