Skip to content

Commit

Permalink
Allow showing Y axis
Browse files Browse the repository at this point in the history
  • Loading branch information
danmindru committed Dec 19, 2024
1 parent 1a9010f commit eca8a62
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/share/pages/share/[result].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ const Result = ({ ogData }: { ogData?: string }) => {

<ResultChart
chartDownSampleThreshold={1000}
showYAxis={true}
item={item}
className="mt-4"
/>
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import ThemeToggle from 'Settings/ThemeToggle/ThemeToggle';
import StickySearchToggle from 'Settings/StickySearchToggle/StickySearchToggle';
import TrendlineToggle from 'Settings/TrendlineToggle/TrendlineToggle';
import BarChartToggle from 'Settings/BarChartToggle/BarChartToggle';
import YAxisToggle from 'Settings/YAxisToggle/YAxisToggle';
import CollectResponseDataToggle from 'Settings/CollectResponseDataToggle/CollectResponseDataToggle';
import CollectResponseErrorsToggle from 'Settings/CollectResponseErrorsToggle/CollectResponseErrorsToggle';
import { useToastStore } from 'toasts/state/toastStore';
Expand Down Expand Up @@ -205,6 +206,7 @@ export const Settings = ({ dismissModal }: { dismissModal: () => void }) => {
Chart visualization settings
</Typography>

<YAxisToggle />
<TrendlineToggle />
<BarChartToggle />
</FormControl>
Expand Down
60 changes: 60 additions & 0 deletions packages/ui/src/Settings/YAxisToggle/YAxisToggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useEffect, useContext, useState } from 'react';

import { FormControlLabel, FormGroup } from '@mui/material';

import { GlobalStore } from 'app/globalContext';
import { EDbStores } from 'storage/EDbStores';
import { SK } from 'storage/storageKeys';
import { getDb } from 'storage/storage';
import { useStoredPreferences } from 'shared/hooks/useStoredPreferences';

import AppleSwitch from 'shared/components/AppleSwitch/AppleSwitch';

const YAxisToggle = () => {
const globalStore = useContext(GlobalStore);

const [preferencesApplied, setPreferencesApplied] = useState(false);
const { preferences, preferencesLoaded } = useStoredPreferences();

useEffect(() => {
const resultDb = getDb(EDbStores.MAIN_STORE_NAME);
resultDb.setItem(
SK.PREFERENCES.SHOW_Y_AXIS,
globalStore.appSettings.showYAxis
);
}, [globalStore.appSettings.showYAxis]);

useEffect(() => {
if (preferencesApplied || !preferencesLoaded) {
return;
}

if (preferences) {
if (preferences.showYAxis) {
globalStore.appSettings.setShowYAxis(preferences.showYAxis);
}
}

setPreferencesApplied(true);
}, [preferences, preferencesApplied, preferencesLoaded, globalStore]);

return (
<GlobalStore.Consumer>
{({ appSettings }) => (
<FormGroup>
<FormControlLabel
control={
<AppleSwitch
onChange={appSettings.toggleShowYAxis}
checked={appSettings.showYAxis}
/>
}
label="Show chart labels (Y-Axis)"
/>
</FormGroup>
)}
</GlobalStore.Consumer>
);
};

export default YAxisToggle;
4 changes: 4 additions & 0 deletions packages/ui/src/app/globalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export const DEFAULT_GLOBAL_STORE = {
setShowBarCharts: (showBarCharts: boolean) => {},
toggleShowBarCharts() {},

showYAxis: false,
setShowYAxis: (showYAxis: boolean) => {},
toggleShowYAxis() {},

chartDownSampleThreshold: 500,
setChartDownSampleThreshold: (chartDownSampleThreshold: number) => {},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const ResultContent = ({

const showTrendline = globalStore.appSettings.showTrendline;
const showBarCharts = globalStore.appSettings.showBarCharts;
const showYAxis = globalStore.appSettings.showYAxis;
const chartDownSampleThreshold =
globalStore.appSettings.chartDownSampleThreshold;

Expand Down Expand Up @@ -189,6 +190,7 @@ const ResultContent = ({
item={item}
showTrendline={showTrendline}
showBarCharts={showBarCharts}
showYAxis={showYAxis}
chartDownSampleThreshold={chartDownSampleThreshold}
className="mt-4"
/>
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/src/results/ResultChart/ResultChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export const ResultChart = ({
item,
showTrendline,
showBarCharts,
showYAxis = false,
chartDownSampleThreshold,
className
}: {
item: ClobbrUIListItem;
showTrendline?: boolean;
showBarCharts?: boolean;
showYAxis?: boolean;
chartDownSampleThreshold: number;
className?: string;
}) => {
Expand Down Expand Up @@ -106,7 +108,7 @@ export const ResultChart = ({
downsampleThreshold={chartDownSampleThreshold}
suggestedYMax={paddedDuration}
responsive={true}
hideYAxis={true}
hideYAxis={!showYAxis}
hideXAxis={true}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export const GenericChart = ({
downsampleThreshold = 1000,
numberOfDownSamplePoints = 1000,
colorMap,
bgColorMap
bgColorMap,
xAxisType
}: {
data: ChartData<'line' | 'bar'>;
width?: number | string;
Expand All @@ -129,14 +130,15 @@ export const GenericChart = ({
numberOfDownSamplePoints?: number;
colorMap?: { [key: number]: string };
bgColorMap?: { [key: number]: string };
xAxisType?: 'category' | 'linear';
}) => {
console.log({ downsampleThreshold, numberOfDownSamplePoints });
const chartWidth = width || 300;
const chartHeight = height || 100;
const gradientColorMap = colorMap || DEFAULT_COLOR_MAP;
const gradientBgColorMap = bgColorMap || DEFAULT_BG_COLOR_MAP;
const threshold = downsampleThreshold;
const samples = numberOfDownSamplePoints;
const scaleXType = xAxisType || 'linear';

const chartRef = useRef<ChartJS>(null);
const [chartData, setChartData] = useState<ChartData<'line' | 'bar'>>({
Expand Down Expand Up @@ -207,19 +209,47 @@ export const GenericChart = ({
size: 1
}
},
type: 'linear'
type: scaleXType
}
}
: {}),
: {
x: {
type: scaleXType
}
}),
...(hideYAxis
? {
y: {
display: false,
display: false
}
}
: {
y: {
display: true,
position: 'right',
ticks: {
color: 'rgba(160,160,160,0.5)', // Color of the tick labels
font: {
size: 12, // Font size of the tick labels
weight: 'normal'
},
stepSize: 100
},

grid: {
// display: false,
color: 'rgba(160,160,160,0.2)', // Color of the tick labels
drawBorder: false,
drawOnChartArea: true,
borderDash: [3, 3]
},
title: {
display: false
},
suggestedMin: 50,
...(suggestedYMax ? { suggestedMax: suggestedYMax } : {})
}
}
: {})
})
},
indexAxis: 'x',
plugins: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const PreferenceLoader = () => {
if (isBoolean(preferences.showBarCharts)) {
globalStore.appSettings.setShowBarCharts(preferences.showBarCharts);
}

if (isBoolean(preferences.showYAxis)) {
globalStore.appSettings.setShowYAxis(preferences.showYAxis);
}
}

setPreferencesApplied(true);
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/src/shared/hooks/useStoredPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Preferences = {
maxIterations?: number;
showTrendline?: boolean;
showBarCharts?: boolean;
showYAxis?: boolean;
collectResponseData?: boolean;
collectResponseErrors?: boolean;
};
Expand All @@ -28,6 +29,7 @@ export const useStoredPreferences = () => {
const showBarCharts = await resultDb.getItem(
SK.PREFERENCES.SHOW_BAR_CHARTS
);
const showYAxis = await resultDb.getItem(SK.PREFERENCES.SHOW_Y_AXIS);
const collectResponseData = await resultDb.getItem(
SK.PREFERENCES.COLLECT_RESPONSE_DATA
);
Expand All @@ -41,6 +43,7 @@ export const useStoredPreferences = () => {
maxIterations,
showTrendline,
showBarCharts,
showYAxis,
collectResponseData,
collectResponseErrors
};
Expand All @@ -58,6 +61,7 @@ export const useStoredPreferences = () => {
maxIterations: storedPreferences.value.maxIterations,
showTrendline: storedPreferences.value.showTrendline,
showBarCharts: storedPreferences.value.showBarCharts,
showYAxis: storedPreferences.value.showYAxis,
collectResponseData: storedPreferences.value.collectResponseData,
collectResponseErrors: storedPreferences.value.collectResponseErrors
});
Expand Down
11 changes: 11 additions & 0 deletions packages/ui/src/state/useAppSettingsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const useAppSettingsState = ({
const [showBarCharts, setShowBarCharts] = useState(
initialState.appSettings.showBarCharts
);
const [showYAxis, setShowYAxis] = useState(
initialState.appSettings.showYAxis
);
const [chartDownSampleThreshold, setChartDownSampleThreshold] = useState(
initialState.appSettings.chartDownSampleThreshold
);
Expand All @@ -37,6 +40,10 @@ export const useAppSettingsState = ({
setShowBarCharts(!showBarCharts);
};

const toggleShowYAxis = () => {
setShowYAxis(!showYAxis);
};

const toggleCollectResponseData = () => {
setCollectResponseData(!collectResponseData);
};
Expand All @@ -61,6 +68,10 @@ export const useAppSettingsState = ({
setShowBarCharts,
toggleShowBarCharts,

showYAxis,
setShowYAxis,
toggleShowYAxis,

chartDownSampleThreshold,
setChartDownSampleThreshold,

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/storage/storageKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const SK = {
MAX_ITERATIONS: 'preferences.maxIterations',
SHOW_TRENDLINE: 'preferences.showTrendline',
SHOW_BAR_CHARTS: 'preferences.showBarCharts',
SHOW_Y_AXIS: 'preferences.showYAxis',
CHART_DOWNSAMPLE_THRESHOLD: 'preferences.chartDownSampleThreshold',
COLLECT_RESPONSE_DATA: 'preferences.collectResponseData',
COLLECT_RESPONSE_ERRORS: 'preferences.collectResponseErrors',
Expand Down

0 comments on commit eca8a62

Please sign in to comment.