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
Charts: Explicit import of chart.js dependency doesn't allow for single pages with inline scripting #3059
Comments
Thanks for reporting. I think that you might be correct that this won't work. I just recently fixed this: #2999 I think we have to try and make another fix to load it if its loaded via script. |
Looks like this: https://www.chartjs.org/docs/latest/getting-started/integration.html Just need to figure out how. |
Currently this doesn't look possible because the name of the React component |
I looked at your commit / attempt. Would we be able to leverage the import * as React from 'react';
import { useUnmountEffect } from '../hooks/Hooks';
import { classNames, ObjectUtils } from '../utils/Utils';
const ChartJS = function() {
try {
return Chart;
} catch {
return undefined;
}
}();
const PrimeReactChart = React.memo(React.forwardRef((props, ref) => {
const chartRef = React.useRef(null);
const canvasRef = React.useRef(null);
const initChart = () => {
// lines omitted
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);
}
}
});
}
}
// lines omitted
return (
<div id={props.id} style={style} className={className} {...otherProps}>
<canvas ref={canvasRef} width={props.width} height={props.height}></canvas>
</div>
);
}), (prevProps, nextProps) => prevProps.data === nextProps.data && prevProps.options === nextProps.options && prevProps.type === nextProps.type);
PrimeReactChart.displayName = 'Chart';
PrimeReactChart.defaultProps = {
__TYPE: 'Chart',
id: null,
type: null,
data: null,
options: null,
plugins: null,
width: null,
height: null,
style: null,
className: null
}
export { PrimeReactChart as Chart }; I was able to get it working with a change similar to this to the dist version of chart.js. |
Yes let me try this that looks really promising! |
I just edited the example above with some try/catch behavior to take into account the other use cases where we actually need to import the module. |
OK PR submitted! Tested and its still working fine in NextJS/CRA. |
8.3.0 is release @tailg8nj if you want to give it a try now? |
Confirmed working as expected. Thanks for the quick turnaround! |
Ohh no! I did not expect such a quick effect :) You are very fast :D |
Unless I'm misunderstanding something here, I believe the following line that imports
chart.js/auto
, means that in order to use the PrimeReact Graph components, you must be using a node-based installation. I have not found a way to successfully leverage them via script tags.primereact/components/lib/chart/Chart.js
Line 10 in f80c1fe
Example code that fails:
Stack:
Opening this issue to confirm and any suggested work-arounds. I have a fairly simple form that needs to query a rest server and display the chart. I'd love to use the charting available here instead of say, plotly or returning svg/png data.
The text was updated successfully, but these errors were encountered: