|
| 1 | +import React, { useState, useEffect, useRef, useMemo } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import Chart from 'chart.js' |
| 4 | +import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs' |
| 5 | +import '@coreui/chartjs/dist/css/coreui-chartjs.css' |
| 6 | + |
| 7 | +const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] |
| 8 | +const key = () => Math.random().toString(36).replace('0.', '') |
| 9 | + |
| 10 | +//component - CoreUI / CChart |
| 11 | +const CChart = props => { |
| 12 | + const { |
| 13 | + innerRef, |
| 14 | + datasets, |
| 15 | + labels, |
| 16 | + options, |
| 17 | + plugins, |
| 18 | + type, |
| 19 | + ...attributes |
| 20 | + } = props |
| 21 | + |
| 22 | + const compData = useRef({ firstRun: true }).current |
| 23 | + const [chart, setChart] = useState() |
| 24 | + const ref = useRef() |
| 25 | + const safeId = useState('safe_id_' + key())[0] |
| 26 | + |
| 27 | + // methods |
| 28 | + const renderChart = () => { |
| 29 | + destroyChart() |
| 30 | + setChart(new Chart( |
| 31 | + ref.current.getContext('2d'), |
| 32 | + chartConfig |
| 33 | + )) |
| 34 | + } |
| 35 | + |
| 36 | + const updateChart = () => { |
| 37 | + Object.assign(chart, chartConfig) |
| 38 | + chart.update() |
| 39 | + } |
| 40 | + |
| 41 | + const destroyChart = () => chart && chart.destroy() |
| 42 | + |
| 43 | + const dataset = (datasets && datasets[0] && datasets[0].data) || [] |
| 44 | + |
| 45 | + const computedLabels = useMemo(() => { |
| 46 | + if (labels && typeof labels !== 'string') { |
| 47 | + return labels |
| 48 | + } |
| 49 | + const emptyLabels = Array(dataset.length).fill('') |
| 50 | + console.log(emptyLabels, dataset) |
| 51 | + if (labels === 'indexes') { |
| 52 | + return emptyLabels.map((u, i) => i + 1) |
| 53 | + } else if (labels === 'months') { |
| 54 | + return emptyLabels.map((u, i) => months[i % 12]) |
| 55 | + } |
| 56 | + return emptyLabels |
| 57 | + }, [JSON.stringify(labels), dataset.length]) |
| 58 | + |
| 59 | + |
| 60 | + const customTooltips = (() => { |
| 61 | + if (options && options.tooltips) { |
| 62 | + return |
| 63 | + } |
| 64 | + return { |
| 65 | + tooltips: { |
| 66 | + enabled: false, |
| 67 | + custom: cuiCustomTooltips, |
| 68 | + intersect: true, |
| 69 | + mode: 'index', |
| 70 | + position: 'nearest', |
| 71 | + callbacks: { |
| 72 | + labelColor(tooltipItem, chart) { |
| 73 | + function getValue(prop) { |
| 74 | + return typeof prop === 'object' ? prop[tooltipItem.index] : prop |
| 75 | + } |
| 76 | + |
| 77 | + const dataset = chart.data.datasets[tooltipItem.datasetIndex] |
| 78 | + //tooltipLabelColor is coreUI custom prop used only here |
| 79 | + const backgroundColor = getValue( |
| 80 | + dataset.tooltipLabelColor || |
| 81 | + dataset.pointHoverBackgroundColor || |
| 82 | + dataset.borderColor || |
| 83 | + dataset.backgroundColor |
| 84 | + ) |
| 85 | + return { |
| 86 | + backgroundColor |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + })() |
| 93 | + |
| 94 | + const computedOptions = (() => { |
| 95 | + return Object.assign({}, options, customTooltips || {}) |
| 96 | + })() |
| 97 | + |
| 98 | + const chartConfig = { |
| 99 | + type, |
| 100 | + data: { |
| 101 | + datasets, |
| 102 | + labels: computedLabels |
| 103 | + }, |
| 104 | + options: computedOptions, |
| 105 | + plugins |
| 106 | + } |
| 107 | + |
| 108 | + useEffect(() => { |
| 109 | + if (compData.firstRun) return |
| 110 | + updateChart() |
| 111 | + }, [chartConfig]) |
| 112 | + |
| 113 | + useEffect(() => { |
| 114 | + renderChart() |
| 115 | + compData.firstRun = false |
| 116 | + return () => destroyChart() |
| 117 | + }, []) |
| 118 | + |
| 119 | + return ( |
| 120 | + <div {...attributes} ref={innerRef}> |
| 121 | + <canvas id={safeId} ref={ref} /> |
| 122 | + </div> |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +CChart.propTypes = { |
| 127 | + innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]), |
| 128 | + datasets: PropTypes.array, |
| 129 | + labels: PropTypes.oneOfType([PropTypes.array, PropTypes.string]), |
| 130 | + options: PropTypes.object, |
| 131 | + plugins: PropTypes.array, |
| 132 | + type: PropTypes.string |
| 133 | +} |
| 134 | + |
| 135 | +const CChartBar = props => <CChart {...props} type="bar"/> |
| 136 | +const CChartHorizontalBar = props => <CChart {...props} type="horizontalBar"/> |
| 137 | +const CChartLine = props => <CChart {...props} type="line"/> |
| 138 | +const CChartDoughnut = props => <CChart {...props} type="doughnut"/> |
| 139 | +const CChartRadar = props => <CChart {...props} type="radar"/> |
| 140 | +const CChartPie = props => <CChart {...props} type="pie"/> |
| 141 | +const CChartPolarArea = props => <CChart {...props} type="polarArea"/> |
| 142 | + |
| 143 | +export { |
| 144 | + CChart, |
| 145 | + CChartBar, |
| 146 | + CChartHorizontalBar, |
| 147 | + CChartLine, |
| 148 | + CChartDoughnut, |
| 149 | + CChartRadar, |
| 150 | + CChartPie, |
| 151 | + CChartPolarArea |
| 152 | +} |
| 153 | + |
0 commit comments