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 #3059: ChartJS allow loading by script tag #3061

Merged
merged 1 commit into from
Jul 14, 2022
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
58 changes: 35 additions & 23 deletions components/lib/chart/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@ import * as React from 'react';
import { useUnmountEffect } from '../hooks/Hooks';
import { classNames, ObjectUtils } from '../utils/Utils';

export const Chart = React.memo(React.forwardRef((props, ref) => {
// GitHub #3059 wrapper if loaded by script tag
const ChartJS = function() {try {return Chart;} catch {return null;}}();

const PrimeReactChart = React.memo(React.forwardRef((props, ref) => {
const chartRef = React.useRef(null);
const canvasRef = React.useRef(null);

const initChart = () => {
import('chart.js/auto').then((module) => {
if (chartRef.current) {
chartRef.current.destroy();
chartRef.current = null;
}

const configuration = {
type: props.type,
data: props.data,
options: props.options,
plugins: props.plugins
};
if (chartRef.current) {
chartRef.current.destroy();
chartRef.current = null;
}

if (module) {
if (module.default) {
chartRef.current = new module.default(canvasRef.current, configuration);
} else {
chartRef.current = new module(canvasRef.current, configuration);
const configuration = {
type: props.type,
data: props.data,
options: props.options,
plugins: props.plugins
};
if (ChartJS) {
// GitHub #3059 loaded by script only
chartRef.current = new ChartJS(canvasRef.current, configuration);
}
else {
import('chart.js/auto').then((module) => {
if (module) {
if (module.default) {
// WebPack
chartRef.current = new module.default(canvasRef.current, configuration);
} else {
// ParcelJS
chartRef.current = new module(canvasRef.current, configuration);
}
}
}
});
});
}
}

React.useImperativeHandle(ref, () => ({
Expand All @@ -50,7 +60,7 @@ export const Chart = React.memo(React.forwardRef((props, ref) => {
}
});

const otherProps = ObjectUtils.findDiffKeys(props, Chart.defaultProps);
const otherProps = ObjectUtils.findDiffKeys(props, PrimeReactChart.defaultProps);
const className = classNames('p-chart', props.className);
const style = Object.assign({ width: props.width, height: props.height }, props.style);

Expand All @@ -61,8 +71,8 @@ export const Chart = React.memo(React.forwardRef((props, ref) => {
);
}), (prevProps, nextProps) => prevProps.data === nextProps.data && prevProps.options === nextProps.options && prevProps.type === nextProps.type);

Chart.displayName = 'Chart';
Chart.defaultProps = {
PrimeReactChart.displayName = 'Chart';
PrimeReactChart.defaultProps = {
__TYPE: 'Chart',
id: null,
type: null,
Expand All @@ -74,3 +84,5 @@ Chart.defaultProps = {
style: null,
className: null
}

export { PrimeReactChart as Chart };