|
| 1 | +importReact,{useState,useEffect,useRef,useMemo}from'react' |
| 2 | +importPropTypesfrom'prop-types' |
| 3 | +importChartfrom'chart.js' |
| 4 | +import{customTooltipsascuiCustomTooltips}from'@coreui/chartjs' |
| 5 | +import'@coreui/chartjs/dist/css/coreui-chartjs.css' |
| 6 | + |
| 7 | +constmonths=['January','February','March','April','May','June','July','August','September','October','November','December'] |
| 8 | +constkey=()=>Math.random().toString(36).replace('0.','') |
| 9 | + |
| 10 | +//component - CoreUI / CChart |
| 11 | +constCChart=props=>{ |
| 12 | +const{ |
| 13 | + innerRef, |
| 14 | + datasets, |
| 15 | + labels, |
| 16 | + options, |
| 17 | + plugins, |
| 18 | + type, |
| 19 | + ...attributes |
| 20 | +}=props |
| 21 | + |
| 22 | +constcompData=useRef({firstRun:true}).current |
| 23 | +const[chart,setChart]=useState() |
| 24 | +constref=useRef() |
| 25 | +constsafeId=useState('safe_id_'+key())[0] |
| 26 | + |
| 27 | +// methods |
| 28 | +constrenderChart=()=>{ |
| 29 | +destroyChart() |
| 30 | +setChart(newChart( |
| 31 | +ref.current.getContext('2d'), |
| 32 | +chartConfig |
| 33 | +)) |
| 34 | +} |
| 35 | + |
| 36 | +constupdateChart=()=>{ |
| 37 | +Object.assign(chart,chartConfig) |
| 38 | +chart.update() |
| 39 | +} |
| 40 | + |
| 41 | +constdestroyChart=()=>chart&&chart.destroy() |
| 42 | + |
| 43 | +constdataset=(datasets&&datasets[0]&&datasets[0].data)||[] |
| 44 | + |
| 45 | +constcomputedLabels=useMemo(()=>{ |
| 46 | +if(labels&&typeoflabels!=='string'){ |
| 47 | +returnlabels |
| 48 | +} |
| 49 | +constemptyLabels=Array(dataset.length).fill('') |
| 50 | +console.log(emptyLabels,dataset) |
| 51 | +if(labels==='indexes'){ |
| 52 | +returnemptyLabels.map((u,i)=>i+1) |
| 53 | +}elseif(labels==='months'){ |
| 54 | +returnemptyLabels.map((u,i)=>months[i%12]) |
| 55 | +} |
| 56 | +returnemptyLabels |
| 57 | +},[JSON.stringify(labels),dataset.length]) |
| 58 | + |
| 59 | + |
| 60 | +constcustomTooltips=(()=>{ |
| 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 | +functiongetValue(prop){ |
| 74 | +returntypeofprop==='object' ?prop[tooltipItem.index] :prop |
| 75 | +} |
| 76 | + |
| 77 | +constdataset=chart.data.datasets[tooltipItem.datasetIndex] |
| 78 | +//tooltipLabelColor is coreUI custom prop used only here |
| 79 | +constbackgroundColor=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 | +constcomputedOptions=(()=>{ |
| 95 | +returnObject.assign({},options,customTooltips||{}) |
| 96 | +})() |
| 97 | + |
| 98 | +constchartConfig={ |
| 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 | +<canvasid={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 | +constCChartBar=props=><CChart{...props}type="bar"/> |
| 136 | +constCChartHorizontalBar=props=><CChart{...props}type="horizontalBar"/> |
| 137 | +constCChartLine=props=><CChart{...props}type="line"/> |
| 138 | +constCChartDoughnut=props=><CChart{...props}type="doughnut"/> |
| 139 | +constCChartRadar=props=><CChart{...props}type="radar"/> |
| 140 | +constCChartPie=props=><CChart{...props}type="pie"/> |
| 141 | +constCChartPolarArea=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 | + |