From c0634569d0e5e525186086a841872970cb3ff2ed Mon Sep 17 00:00:00 2001 From: plaume8 Date: Mon, 16 Dec 2024 22:48:05 +0100 Subject: [PATCH 1/8] fix: fixed theme switch -> parts of chart styling do not change on theme switch AND made sure the charts update when passed data changes --- src/components/Charts/CategoricalChart.tsx | 23 +++++++++----- src/components/Charts/LineChart.tsx | 36 ++++++++++++++++------ src/domain/props/ChartContainerProps.tsx | 2 +- src/domain/props/ChartModalProps.tsx | 2 +- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/components/Charts/CategoricalChart.tsx b/src/components/Charts/CategoricalChart.tsx index 24fe166a..810c27f5 100644 --- a/src/components/Charts/CategoricalChart.tsx +++ b/src/components/Charts/CategoricalChart.tsx @@ -39,17 +39,26 @@ export function CategoricalChart({ }: CategoricalChartProps) { const { theme } = useTheme(); - // build chart options for 'Highcharts' - const defaultChartOptions: Highcharts.Options = CategoricalChartOperations.getHighChartOptions(data); - // controlling if a bar or pie chart is rendered; bar chart is the default - const [showPieChart, setShowPieChart] = useState(false); - const [chartOptions, setChartOptions] = useState(defaultChartOptions); + const [showPieChart, setShowPieChart] = useState(false); + const [chartOptions, setChartOptions] = useState( + CategoricalChartOperations.getHighChartOptions(data, showPieChart) + ); + + // handling the theme switch + useEffect(() => { + // `theme` change does not guarantee that the NextUI CSS colors have already been changed; + // therefore we synchronize the update with the next repaint cycle, ensuring the CSS variables are updated + const rafId = requestAnimationFrame(() => { + setChartOptions(CategoricalChartOperations.getHighChartOptions(data, showPieChart)); + }); + return () => cancelAnimationFrame(rafId); + }, [theme]); - // handling the bar and pie chart switch and the theme switch; + // handling the bar and pie chart switch and data changes useEffect(() => { setChartOptions(CategoricalChartOperations.getHighChartOptions(data, showPieChart)); - }, [showPieChart, theme]); + }, [theme, data]); const alternativeSwitchButtonProps = disablePieChartSwitch ? undefined diff --git a/src/components/Charts/LineChart.tsx b/src/components/Charts/LineChart.tsx index 52891746..fa41d3ba 100644 --- a/src/components/Charts/LineChart.tsx +++ b/src/components/Charts/LineChart.tsx @@ -49,23 +49,24 @@ export function LineChart({ }: LineChartProps) { const { theme } = useTheme(); - // convert data to `LineChartData` and build chart options for 'Highcharts' (line and bar chart) + // make sure data is converted to `LineChartData` const lineChartData: LineChartData = LineChartOperations.convertToLineChartData(data); - const lineChartOptions: Highcharts.Options = LineChartOperations.getHighChartOptions(lineChartData); // the `selectedXAxisRange` saves the to be rendered x-axis range of the chart // can be changed using the `LinkeChartXAxisSlider` if the param `xAxisSlider==true` const xAxisLength: number = LineChartOperations.getDistinctXAxisValues(lineChartData).length; - const [selectedXAxisRange, setSelectedXAxisRange] = useState([0, xAxisLength - 1]); + const [selectedXAxisRange, setSelectedXAxisRange] = useState([0, xAxisLength - 1]); // controlling if a line or bar chart is rendered; line chart is the default - const [showBarChart, setShowBarChart] = useState(false); - const [chartOptions, setChartOptions] = useState(lineChartOptions); + const [showBarChart, setShowBarChart] = useState(false); + const [chartOptions, setChartOptions] = useState( + LineChartOperations.getHighChartOptions(lineChartData) + ); - // handling the line and bar chart switch and the theme switch; - // also handling changing the x-axis range using the `LineChartXAxisSlider`; - // special: if the selected x-axis range has length 1 -> bar chart is displayed - useEffect(() => { + // general function to update the chart options -> only used by the following `useEffect` hooks + const updateChartOptions = () => { + // also handling changing the x-axis range using the `LineChartXAxisSlider`; + // special: if the selected x-axis range has length 1 -> bar chart is displayed if (showBarChart || selectedXAxisRange[1] - selectedXAxisRange[0] === 0) { setChartOptions( LineChartOperations.getHighChartOptions(lineChartData, selectedXAxisRange[0], selectedXAxisRange[1], true) @@ -75,7 +76,22 @@ export function LineChart({ LineChartOperations.getHighChartOptions(lineChartData, selectedXAxisRange[0], selectedXAxisRange[1]) ); } - }, [showBarChart, theme, selectedXAxisRange]); + }; + + // handling the line and bar chart switch, selected x-axis range changes and data changes + useEffect(() => { + updateChartOptions(); + }, [showBarChart, selectedXAxisRange, data]); + + // handling the theme switch + useEffect(() => { + // `theme` change does not guarantee that the NextUI CSS colors have already been changed; + // therefore we synchronize the update with the next repaint cycle, ensuring the CSS variables are updated + const rafId = requestAnimationFrame(() => { + updateChartOptions(); + }); + return () => cancelAnimationFrame(rafId); + }, [theme]); // chart slider props - to manipulate the shown x-axis range const sliderProps = disableXAxisSlider diff --git a/src/domain/props/ChartContainerProps.tsx b/src/domain/props/ChartContainerProps.tsx index 6b021d1f..cd6a8e51 100644 --- a/src/domain/props/ChartContainerProps.tsx +++ b/src/domain/props/ChartContainerProps.tsx @@ -43,7 +43,7 @@ export interface ChartDownloadButtonProps { */ export default interface ChartContainerProps { - chartOptions: Highcharts.Options; + chartOptions?: Highcharts.Options; chartData: LineChartData | CategoricalChartData; title?: string; diff --git a/src/domain/props/ChartModalProps.tsx b/src/domain/props/ChartModalProps.tsx index b3f6f338..647623a4 100644 --- a/src/domain/props/ChartModalProps.tsx +++ b/src/domain/props/ChartModalProps.tsx @@ -6,7 +6,7 @@ import { LineChartData } from '@/domain/entities/charts/LineChartData.ts'; import { ChartAlternativeSwitchButtonProps, ChartSliderProps } from '@/domain/props/ChartContainerProps'; export default interface ChartModalProps { - chartOptions: Highcharts.Options; + chartOptions?: Highcharts.Options; chartData: LineChartData | CategoricalChartData; title?: string; From a17758e49bad7cbc6da6564af7aa05a1f6d3d2cf Mon Sep 17 00:00:00 2001 From: plaume8 Date: Mon, 16 Dec 2024 22:59:49 +0100 Subject: [PATCH 2/8] fix: disabling pie chart interactivity (pie sections are not clickable any more) --- src/components/Charts/CategoricalChart.tsx | 2 +- src/operations/charts/CategoricalChartOperations.ts | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/components/Charts/CategoricalChart.tsx b/src/components/Charts/CategoricalChart.tsx index 810c27f5..4534d614 100644 --- a/src/components/Charts/CategoricalChart.tsx +++ b/src/components/Charts/CategoricalChart.tsx @@ -58,7 +58,7 @@ export function CategoricalChart({ // handling the bar and pie chart switch and data changes useEffect(() => { setChartOptions(CategoricalChartOperations.getHighChartOptions(data, showPieChart)); - }, [theme, data]); + }, [showPieChart, data]); const alternativeSwitchButtonProps = disablePieChartSwitch ? undefined diff --git a/src/operations/charts/CategoricalChartOperations.ts b/src/operations/charts/CategoricalChartOperations.ts index 3c59b3b1..0f025f4d 100644 --- a/src/operations/charts/CategoricalChartOperations.ts +++ b/src/operations/charts/CategoricalChartOperations.ts @@ -160,8 +160,8 @@ export default class CategoricalChartOperations { }, pie: { animation: true, - allowPointSelect: true, - cursor: 'pointer', + allowPointSelect: false, + cursor: 'default', innerSize: '60%', borderWidth: 0, dataLabels: { @@ -176,6 +176,13 @@ export default class CategoricalChartOperations { fontWeight: 'light', }, }, + states: { + hover: { + halo: { + size: 4, // shrinking the halo/glow size on hover + }, + }, + }, }, }, }; From ca1f4fec3f486b28c206f354cfc17a45959c2723 Mon Sep 17 00:00:00 2001 From: plaume8 Date: Mon, 16 Dec 2024 23:09:25 +0100 Subject: [PATCH 3/8] fix: update chart options to chart container - reduces code redundancy --- src/components/Charts/CategoricalChart.tsx | 24 +++++------------ src/components/Charts/LineChart.tsx | 27 ++++--------------- .../Charts/helpers/ChartContainer.tsx | 25 ++++++++++++++--- src/domain/props/ChartContainerProps.tsx | 3 ++- 4 files changed, 34 insertions(+), 45 deletions(-) diff --git a/src/components/Charts/CategoricalChart.tsx b/src/components/Charts/CategoricalChart.tsx index 4534d614..7a75a8af 100644 --- a/src/components/Charts/CategoricalChart.tsx +++ b/src/components/Charts/CategoricalChart.tsx @@ -1,8 +1,7 @@ 'use client'; import Highcharts from 'highcharts'; -import { useTheme } from 'next-themes'; -import { useEffect, useState } from 'react'; +import { useState } from 'react'; import { ChartContainer } from '@/components/Charts/helpers/ChartContainer'; import { ChartType } from '@/domain/enums/ChartType.ts'; @@ -37,28 +36,16 @@ export function CategoricalChart({ disablePieChartSwitch, disableDownload, }: CategoricalChartProps) { - const { theme } = useTheme(); - // controlling if a bar or pie chart is rendered; bar chart is the default const [showPieChart, setShowPieChart] = useState(false); const [chartOptions, setChartOptions] = useState( CategoricalChartOperations.getHighChartOptions(data, showPieChart) ); - // handling the theme switch - useEffect(() => { - // `theme` change does not guarantee that the NextUI CSS colors have already been changed; - // therefore we synchronize the update with the next repaint cycle, ensuring the CSS variables are updated - const rafId = requestAnimationFrame(() => { - setChartOptions(CategoricalChartOperations.getHighChartOptions(data, showPieChart)); - }); - return () => cancelAnimationFrame(rafId); - }, [theme]); - - // handling the bar and pie chart switch and data changes - useEffect(() => { + // function to update/recalculate the chart options + const recalculateChartOptions = () => { setChartOptions(CategoricalChartOperations.getHighChartOptions(data, showPieChart)); - }, [showPieChart, data]); + }; const alternativeSwitchButtonProps = disablePieChartSwitch ? undefined @@ -71,8 +58,9 @@ export function CategoricalChart({ return ( only used by the following `useEffect` hooks - const updateChartOptions = () => { + // function to update/recalculate the chart options + const recalculateChartOptions = () => { // also handling changing the x-axis range using the `LineChartXAxisSlider`; // special: if the selected x-axis range has length 1 -> bar chart is displayed if (showBarChart || selectedXAxisRange[1] - selectedXAxisRange[0] === 0) { @@ -78,21 +75,6 @@ export function LineChart({ } }; - // handling the line and bar chart switch, selected x-axis range changes and data changes - useEffect(() => { - updateChartOptions(); - }, [showBarChart, selectedXAxisRange, data]); - - // handling the theme switch - useEffect(() => { - // `theme` change does not guarantee that the NextUI CSS colors have already been changed; - // therefore we synchronize the update with the next repaint cycle, ensuring the CSS variables are updated - const rafId = requestAnimationFrame(() => { - updateChartOptions(); - }); - return () => cancelAnimationFrame(rafId); - }, [theme]); - // chart slider props - to manipulate the shown x-axis range const sliderProps = disableXAxisSlider ? undefined @@ -115,8 +97,9 @@ export function LineChart({ return ( (null); // full screen modal state handling const { isOpen, onOpen, onClose, onOpenChange } = useDisclosure(); + // handling the theme switch + useEffect(() => { + // `theme` change does not guarantee that the NextUI CSS colors have already been changed; + // therefore we synchronize the update with the next repaint cycle, ensuring the CSS variables are updated + const rafId = requestAnimationFrame(() => { + recalculateChartOptions(); + }); + return () => cancelAnimationFrame(rafId); + }, [theme]); + + // handling chart type switch, data changes and slider changes + useEffect(() => { + recalculateChartOptions(); + }, [alternativeSwitchButtonProps?.showAlternativeChart, sliderProps?.selectedSliderRange, chartData]); + // handling the x-axis range slider visibility const [showSlider, setShowSlider] = useState(false); return ( diff --git a/src/domain/props/ChartContainerProps.tsx b/src/domain/props/ChartContainerProps.tsx index cd6a8e51..27cda93a 100644 --- a/src/domain/props/ChartContainerProps.tsx +++ b/src/domain/props/ChartContainerProps.tsx @@ -43,8 +43,9 @@ export interface ChartDownloadButtonProps { */ export default interface ChartContainerProps { - chartOptions?: Highcharts.Options; chartData: LineChartData | CategoricalChartData; + chartOptions?: Highcharts.Options; + recalculateChartOptions: () => void; title?: string; description?: string; From c036648a4048949a839eb44229039694a01d1703 Mon Sep 17 00:00:00 2001 From: plaume8 Date: Mon, 16 Dec 2024 23:25:21 +0100 Subject: [PATCH 4/8] fix: trigger the correct animation on chart type switch (e.g. if switching to pie chart -> the correct pie chart animation should be triggered) --- src/components/Charts/helpers/ChartContainer.tsx | 4 ++++ src/components/Charts/helpers/ChartModal.tsx | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/components/Charts/helpers/ChartContainer.tsx b/src/components/Charts/helpers/ChartContainer.tsx index 38a05f70..d7deb80d 100644 --- a/src/components/Charts/helpers/ChartContainer.tsx +++ b/src/components/Charts/helpers/ChartContainer.tsx @@ -47,6 +47,7 @@ export function ChartContainer({ const { theme } = useTheme(); const chartRef = useRef(null); + const [chartKey, setChartKey] = useState(0); // full screen modal state handling const { isOpen, onOpen, onClose, onOpenChange } = useDisclosure(); @@ -57,6 +58,7 @@ export function ChartContainer({ // therefore we synchronize the update with the next repaint cycle, ensuring the CSS variables are updated const rafId = requestAnimationFrame(() => { recalculateChartOptions(); + setChartKey((prev) => prev + 1); // forces chart to remount -> chart animation will be re-triggered }); return () => cancelAnimationFrame(rafId); }, [theme]); @@ -64,6 +66,7 @@ export function ChartContainer({ // handling chart type switch, data changes and slider changes useEffect(() => { recalculateChartOptions(); + setChartKey((prev) => prev + 1); // forces chart to remount -> chart animation will be re-triggered }, [alternativeSwitchButtonProps?.showAlternativeChart, sliderProps?.selectedSliderRange, chartData]); // handling the x-axis range slider visibility @@ -123,6 +126,7 @@ export function ChartContainer({ highcharts={Highcharts} options={chartOptions} ref={chartRef} + key={chartKey} containerProps={{ style: { width: '100%', diff --git a/src/components/Charts/helpers/ChartModal.tsx b/src/components/Charts/helpers/ChartModal.tsx index eb10f838..4768f87d 100644 --- a/src/components/Charts/helpers/ChartModal.tsx +++ b/src/components/Charts/helpers/ChartModal.tsx @@ -3,7 +3,8 @@ import { Modal, ModalBody, ModalContent, ModalFooter, ModalHeader } from '@nextu import Highcharts from 'highcharts'; import HighchartsReact from 'highcharts-react-official'; import { Minus } from 'iconsax-react'; -import { useRef } from 'react'; +import { useTheme } from 'next-themes'; +import { useEffect, useRef, useState } from 'react'; import ChartAlternativeSwitchButton from '@/components/Charts/helpers/buttons/ChartAlternativeSwitchButton'; import ChartDownloadButton from '@/components/Charts/helpers/buttons/ChartDownloadButton'; @@ -31,7 +32,15 @@ export function ChartModal({ showSlider, setShowSlider, }: ChartModalProps) { + const { theme } = useTheme(); + const chartRef = useRef(null); + const [chartKey, setChartKey] = useState(0); + + // if chart changes -> force chart to remount -> chart animation will be re-triggered + useEffect(() => { + setChartKey((prev) => prev + 1); + }, [theme, alternativeSwitchButtonProps?.showAlternativeChart, sliderProps?.selectedSliderRange, chartData]); return ( From b1ff9e7ff2da7645c43e8c481d62421a6d1b2d3c Mon Sep 17 00:00:00 2001 From: plaume8 Date: Thu, 19 Dec 2024 13:36:54 +0100 Subject: [PATCH 5/8] feat: renaming: LineChart -> ContinuousChart (and all related components, functions, etc. --- public/sitemap-0.xml | 16 ++--- src/app/elements/page.tsx | 40 +++++------ src/components/Charts/CategoricalChart.tsx | 2 +- .../{LineChart.tsx => ContinuousChart.tsx} | 47 +++++++------ .../Charts/helpers/ChartContainer.tsx | 4 +- .../helpers/buttons/ChartSliderButton.tsx | 4 +- .../ComparisonPortal/NoDataHint.tsx | 4 +- src/components/Map/FcsAccordion.tsx | 12 ++-- src/components/Map/FcsRegionTooltip.tsx | 4 +- .../entities/charts/BalanceOfTradeGraph.ts | 4 +- ...ineChartData.ts => ContinuousChartData.ts} | 16 ++--- .../entities/charts/CurrencyExchangeGraph.ts | 4 +- src/domain/entities/charts/InflationGraphs.ts | 4 +- .../comparison/CountryComparisonChartdata.ts | 12 ++-- ...DataType.ts => ContinuousChartDataType.ts} | 2 +- src/domain/props/ChartContainerProps.tsx | 8 +-- src/domain/props/ChartModalProps.tsx | 4 +- ...hartProps.tsx => ContinuousChartProps.tsx} | 6 +- src/domain/props/NoDataHintProps.ts | 4 +- .../charts/ChartDownloadButtonOperations.ts | 4 +- ...ations.ts => ContinuousChartOperations.ts} | 66 +++++++++---------- .../CountryComparisonOperations.tsx | 40 ++++++----- src/operations/map/FcsAccordionOperations.tsx | 18 ++--- .../map/FcsRegionTooltipOperations.ts | 8 +-- 24 files changed, 172 insertions(+), 161 deletions(-) rename src/components/Charts/{LineChart.tsx => ContinuousChart.tsx} (69%) rename src/domain/entities/charts/{LineChartData.ts => ContinuousChartData.ts} (74%) rename src/domain/enums/{LineChartDataType.ts => ContinuousChartDataType.ts} (83%) rename src/domain/props/{LineChartProps.tsx => ContinuousChartProps.tsx} (69%) rename src/operations/charts/{LineChartOperations.ts => ContinuousChartOperations.ts} (84%) diff --git a/public/sitemap-0.xml b/public/sitemap-0.xml index 796b60bf..8a098c9d 100644 --- a/public/sitemap-0.xml +++ b/public/sitemap-0.xml @@ -1,11 +1,11 @@ -https://localhost:3000/disclaimer2024-12-19T12:18:06.105Zdaily0.7 -https://localhost:3000/wiki2024-12-19T12:18:06.105Zdaily0.7 -https://localhost:3000/about2024-12-19T12:18:06.105Zdaily0.7 -https://localhost:3000/data_sources2024-12-19T12:18:06.105Zdaily0.7 -https://localhost:30002024-12-19T12:18:06.105Zdaily0.7 -https://localhost:3000/elements2024-12-19T12:18:06.105Zdaily0.7 -https://localhost:3000/download-portal2024-12-19T12:18:06.105Zdaily0.7 -https://localhost:3000/comparison-portal2024-12-19T12:18:06.105Zdaily0.7 +https://localhost:3000/about2024-12-19T12:36:14.394Zdaily0.7 +https://localhost:3000/disclaimer2024-12-19T12:36:14.394Zdaily0.7 +https://localhost:3000/wiki2024-12-19T12:36:14.394Zdaily0.7 +https://localhost:3000/data_sources2024-12-19T12:36:14.394Zdaily0.7 +https://localhost:30002024-12-19T12:36:14.394Zdaily0.7 +https://localhost:3000/elements2024-12-19T12:36:14.394Zdaily0.7 +https://localhost:3000/comparison-portal2024-12-19T12:36:14.394Zdaily0.7 +https://localhost:3000/download-portal2024-12-19T12:36:14.394Zdaily0.7 \ No newline at end of file diff --git a/src/app/elements/page.tsx b/src/app/elements/page.tsx index db7181ae..dc9a26e5 100644 --- a/src/app/elements/page.tsx +++ b/src/app/elements/page.tsx @@ -5,12 +5,12 @@ import { useState } from 'react'; import AccordionContainer from '@/components/Accordions/AccordionContainer'; import { CustomButton } from '@/components/Buttons/CustomButton'; import { CategoricalChart } from '@/components/Charts/CategoricalChart'; -import { LineChart } from '@/components/Charts/LineChart'; +import { ContinuousChart } from '@/components/Charts/ContinuousChart'; import MapSkeleton from '@/components/Map/MapSkeleton'; import SearchBar from '@/components/Search/SearchBar'; import { CategoricalChartData } from '@/domain/entities/charts/CategoricalChartData.ts'; -import { LineChartData } from '@/domain/entities/charts/LineChartData.ts'; -import { LineChartDataType } from '@/domain/enums/LineChartDataType.ts'; +import { ContinuousChartData } from '@/domain/entities/charts/ContinuousChartData.ts'; +import { ContinuousChartDataType } from '@/domain/enums/ContinuousChartDataType.ts'; import AccordionsOperations from '@/operations/accordions/AccordionOperations'; import { ReactComponent as FoodSvg } from '../../../public/Images/FoodConsumption.svg'; @@ -21,8 +21,8 @@ import { ReactComponent as FoodSvg } from '../../../public/Images/FoodConsumptio */ export default async function Elements() { const [searchTerm, setSearchTerm] = useState(''); - const simpleAndSmallLineChartData: LineChartData = { - type: LineChartDataType.LINE_CHART_DATA, + const simpleAndSmallContinuousChartData: ContinuousChartData = { + type: ContinuousChartDataType.LINE_CHART_DATA, xAxisType: 'linear', lines: [ { @@ -37,8 +37,8 @@ export default async function Elements() { ], }; - const maxedOutLineChartData: LineChartData = { - type: LineChartDataType.LINE_CHART_DATA, + const maxedOutContinuousChartData: ContinuousChartData = { + type: ContinuousChartDataType.LINE_CHART_DATA, xAxisType: 'linear', yAxisLabel: 'yield', lines: [ @@ -97,8 +97,8 @@ export default async function Elements() { ], }; - const predictionDummyChartData: LineChartData = { - type: LineChartDataType.LINE_CHART_DATA, + const predictionDummyChartData: ContinuousChartData = { + type: ContinuousChartDataType.LINE_CHART_DATA, xAxisType: 'linear', yAxisLabel: 'Mill', predictionVerticalLineX: 3, @@ -129,7 +129,7 @@ export default async function Elements() { ], }; - const categoricalDummyChartData1: CategoricalChartData = { + const categoricalDummyChartData: CategoricalChartData = { yAxisLabel: 'Mill', categories: [ { @@ -143,8 +143,8 @@ export default async function Elements() { ], }; - const emptyDummyChartData: LineChartData = { - type: LineChartDataType.LINE_CHART_DATA, + const emptyDummyChartData: ContinuousChartData = { + type: ContinuousChartDataType.LINE_CHART_DATA, xAxisType: 'linear', yAxisLabel: 'Mill', predictionVerticalLineX: 3, @@ -174,8 +174,8 @@ export default async function Elements() {
-
-
- +
- +
- +
diff --git a/src/components/Charts/CategoricalChart.tsx b/src/components/Charts/CategoricalChart.tsx index 7a75a8af..ed80743b 100644 --- a/src/components/Charts/CategoricalChart.tsx +++ b/src/components/Charts/CategoricalChart.tsx @@ -10,7 +10,7 @@ import CategoricalChartOperations from '@/operations/charts/CategoricalChartOper /** * The `CategoricalChart` component is a box that primarily renders a title, description text, and a bar chart. - * It should be used to plot categorical data. For continues data please use the `LineChart` component. + * It should be used to plot categorical data. For continues data please use the `ContinuousChart` component. * This component has a width of 100%, so it adjusts to the width of its parent element in which it is used. * The height of the entire box depends on the provided text, while the chart itself has a fixed height. * It also provides the option to open the chart in a full-screen modal, where one can download the data as well. diff --git a/src/components/Charts/LineChart.tsx b/src/components/Charts/ContinuousChart.tsx similarity index 69% rename from src/components/Charts/LineChart.tsx rename to src/components/Charts/ContinuousChart.tsx index 63b5d101..16b38519 100644 --- a/src/components/Charts/LineChart.tsx +++ b/src/components/Charts/ContinuousChart.tsx @@ -4,29 +4,29 @@ import Highcharts from 'highcharts'; import { useEffect, useMemo, useState } from 'react'; import { ChartContainer } from '@/components/Charts/helpers/ChartContainer'; -import { LineChartData } from '@/domain/entities/charts/LineChartData'; +import { ContinuousChartData } from '@/domain/entities/charts/ContinuousChartData.ts'; import { ChartType } from '@/domain/enums/ChartType.ts'; -import LineChartProps from '@/domain/props/LineChartProps'; -import LineChartOperations from '@/operations/charts/LineChartOperations'; +import ContinuousChartProps from '@/domain/props/ContinuousChartProps'; +import ContinuousChartOperations from '@/operations/charts/ContinuousChartOperations.ts'; /** - * The LineChart component is a box that primarily renders a title, description text, and a line chart. + * The ContinuousChart component is a box that primarily renders a title, description text, and a line chart. * It should be used to plot categorical data. For continues data please use the `CategoricalChart` component. * This component has a width of 100%, so it adjusts to the width of its parent element in which it is used. * The height of the entire box depends on the provided text, while the chart itself has a fixed height. * It also provides the option to open the chart in a full-screen modal, where one can download the data as well. * - * The data to be displayed in the chart can be provided in different types (see `LineChartProps.data`). - * However, the preferred type is `LineChartData`. + * The data to be displayed in the chart can be provided in different types (see `ContinuousChartProps.data`). + * However, the preferred type is `ContinuousChartData`. * - * To enable the LineChart component to support an additional `data` type, the following steps are required: - * 1. Define an interface and add it to `LineChartProps.data`. - * 2. Add another switch case in `LineChartOperations.convertToLineChartData` to convert the new interface to `LineChartData`. + * To enable the ContinuousChart component to support an additional `data` type, the following steps are required: + * 1. Define an interface and add it to `ContinuousChartProps.data`. + * 2. Add another switch case in `ContinuousChartOperations.convertToContinuousChartData` to convert the new interface to `ContinuousChartData`. * * @param data the actual data to be used in the chart * @param title chart title (optional) * @param description chart description text (optional) - * @param small when selected, all components in the line chart box become slightly smaller (optional) + * @param small when selected, all components in the main box become slightly smaller (optional) * @param noPadding when selected, the main box has no padding on all sides (optional) * @param transparentBackground when selected, the background of the entire component is transparent (optional) * @param disableExpandable when selected, the functionality to open the chart in a larger modal is disabled (optional) @@ -34,7 +34,7 @@ import LineChartOperations from '@/operations/charts/LineChartOperations'; * @param disableXAxisSlider when selected, the functionality to change the x-axis range via a slider is disabled (optional) * @param disableDownload when selected, the functionality to download the chart is disabled (optional) */ -export function LineChart({ +export function ContinuousChart({ data, title, description, @@ -45,15 +45,15 @@ export function LineChart({ disableBarChartSwitch, disableXAxisSlider, disableDownload, -}: LineChartProps) { - // make sure data is converted to `LineChartData` - const lineChartData: LineChartData = LineChartOperations.convertToLineChartData(data); +}: ContinuousChartProps) { + // make sure data is converted to `continuousChartData` + const continuousChartData: ContinuousChartData = ContinuousChartOperations.convertToContinuousChartData(data); // the `selectedXAxisRange` saves the to be rendered x-axis range of the chart // can be changed using the `LinkeChartXAxisSlider` if the param `xAxisSlider==true` const xAxisLength = useMemo(() => { - return LineChartOperations.getDistinctXAxisValues(lineChartData).length; - }, [lineChartData]); + return ContinuousChartOperations.getDistinctXAxisValues(continuousChartData).length; + }, [continuousChartData]); const [selectedXAxisRange, setSelectedXAxisRange] = useState([0, xAxisLength - 1]); // we have to make sure that if the data changes we update the XAxis slider configuration as well @@ -64,20 +64,25 @@ export function LineChart({ // controlling if a line or bar chart is rendered; line chart is the default const [showBarChart, setShowBarChart] = useState(false); const [chartOptions, setChartOptions] = useState( - LineChartOperations.getHighChartOptions(lineChartData) + ContinuousChartOperations.getHighChartOptions(continuousChartData) ); // function to update/recalculate the chart options const recalculateChartOptions = () => { - // also handling changing the x-axis range using the `LineChartXAxisSlider`; + // also handling changing the x-axis range using the `ChartXAxisSlider`; // special: if the selected x-axis range has length 1 -> bar chart is displayed if (showBarChart || selectedXAxisRange[1] - selectedXAxisRange[0] === 0) { setChartOptions( - LineChartOperations.getHighChartOptions(lineChartData, selectedXAxisRange[0], selectedXAxisRange[1], true) + ContinuousChartOperations.getHighChartOptions( + continuousChartData, + selectedXAxisRange[0], + selectedXAxisRange[1], + true + ) ); } else { setChartOptions( - LineChartOperations.getHighChartOptions(lineChartData, selectedXAxisRange[0], selectedXAxisRange[1]) + ContinuousChartOperations.getHighChartOptions(continuousChartData, selectedXAxisRange[0], selectedXAxisRange[1]) ); } }; @@ -104,7 +109,7 @@ export function LineChart({ return (