///////////////////////////////////////////////////////////// // // pgAdmin 4 - PostgreSQL Tools // // Copyright (C) 2013 - 2021, The pgAdmin Development Team // This software is released under the PostgreSQL Licence // ////////////////////////////////////////////////////////////// import React, { useEffect } from 'react'; import Chart from 'chart.js'; import PropTypes from 'prop-types'; const defaultOptions = { responsive: true, maintainAspectRatio: false, elements: { line: { tension: 0, borderWidth: 2, fill: false, }, }, layout: { padding: 8, }, hover: { animationDuration:0, }, }; export default function BaseChart({type='line', id, options, data, redraw=false, ...props}) { const chartRef = React.useRef(); const chartObj = React.useRef(); let optionsMerged = Chart.helpers.configMerge(defaultOptions, options); const initChart = function() { let chartContext = chartRef.current.getContext('2d'); chartObj.current = new Chart(chartContext, { type: type, data: data, options: optionsMerged, }); props.onInit && props.onInit(chartObj.current); }; const destroyChart = function() { chartObj.current && chartObj.current.destroy(); }; useEffect(()=>{ initChart(); return destroyChart; }, []); useEffect(()=>{ if(typeof(chartObj.current) != 'undefined') { chartObj.current.data = data; for(let i=0; i{ if(redraw) { destroyChart(); initChart(); } }, [redraw]); return ( ); } BaseChart.propTypes = { type: PropTypes.string.isRequired, id: PropTypes.string, data: PropTypes.object.isRequired, options: PropTypes.object, redraw: PropTypes.bool, updateOptions: PropTypes.object, onInit: PropTypes.func, onUpdate: PropTypes.func, }; export function LineChart(props) { return ( ); }