Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chart auto resize #2231

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ChartDataLabels from 'chartjs-plugin-datalabels';
import { Modal } from 'components/modal';
import { useReportEditContext } from 'features/my-reports/edit/ReportEditContext';
import React from 'react';
import { useModal } from 'tno-core';
import { IChartSectionSettingsModel, useModal } from 'tno-core';

import {
calcAutoSize,
Expand Down Expand Up @@ -72,7 +72,8 @@ export const ChartViewer: React.FC<IChartViewerProps> = ({
chart.sectionSettings.autoResize &&
shouldResizeChart(data.labels.length, chart.sectionSettings, minAxisColumnWidth)
) {
toggleModal();
// toggleModal();
// Commented out temporarily until I can present the fact the chart is being resized in a better way.
}
// Only update when values change in data or chart.
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand All @@ -87,37 +88,53 @@ export const ChartViewer: React.FC<IChartViewerProps> = ({
React.useEffect(() => {
if (canvasRef.current !== null && data) {
myChart[uid]?.destroy();
const widthValue = chart.sectionSettings.width ? chart.sectionSettings.width : 500;
if (chart.sectionSettings.height) canvasRef.current.height = chart.sectionSettings.height;
if (chart.sectionSettings.width) canvasRef.current.width = widthValue;

// Auto resize if it's on
let chartSectionSettings: IChartSectionSettingsModel = {
...chart.sectionSettings,
width: chart.sectionSettings.width ? chart.sectionSettings.width : 500,
};
if (
chart.sectionSettings.autoResize &&
shouldResizeChart(data.labels.length, chart.sectionSettings, minAxisColumnWidth)
) {
chartSectionSettings = calcAutoSize(
data.labels.length,
chart.sectionSettings,
minAxisColumnWidth,
);
}

if (chartSectionSettings.height) canvasRef.current.height = chartSectionSettings.height;
if (chartSectionSettings.width) canvasRef.current.width = chartSectionSettings.width;

myChart[uid] = new ChartJS(canvasRef.current, {
type: chart.sectionSettings.chartType as keyof ChartTypeRegistry,
type: chartSectionSettings.chartType as keyof ChartTypeRegistry,
data: {
...data,
datasets:
data?.datasets?.map(
(dataset: IChartDataset, index: number) =>
({
...dataset,
label: getSectionLabel(dataset.label, chart.sectionSettings, values.sections),
minBarLength: chart.sectionSettings.minBarLength,
label: getSectionLabel(dataset.label, chartSectionSettings, values.sections),
minBarLength: chartSectionSettings.minBarLength,
borderWidth: 2,
spanGaps: true,
backgroundColor: determineBackgroundColor(
index,
dataset.label,
chart.sectionSettings,
chartSectionSettings,
),
borderColor: determineBorderColor(index, dataset.label, chart.sectionSettings),
borderColor: determineBorderColor(index, dataset.label, chartSectionSettings),
} as any),
) ?? [],
},
options: generateChartOptions(data, chart.sectionSettings),
options: generateChartOptions(data, chartSectionSettings),
});
myChart[uid].resize(widthValue, chart.sectionSettings.height);
myChart[uid].resize(chartSectionSettings.width, chartSectionSettings.height);
}
}, [chart.sectionSettings, data, values.sections, uid]);
}, [chart.sectionSettings, data, values.sections, uid, minAxisColumnWidth]);

return (
<div>
Expand Down
Loading