Skip to content

Commit de1b51e

Browse files
committed
refactor: refactor library
- add specific chart type exports - optimize code - change exports - update typings
1 parent d15059e commit de1b51e

File tree

4 files changed

+193
-173
lines changed

4 files changed

+193
-173
lines changed

src/CChart.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+

src/CCharts.js

Lines changed: 0 additions & 167 deletions
This file was deleted.

src/index.d.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
import React from 'react'
2-
3-
interface CCharts {
1+
interface Charts {
42
innerRef?: any;
53
datasets?: Array<any>;
64
labels?: string | Array<any>;
75
options?: any;
86
plugins?: Array<any>;
7+
}
8+
9+
interface CChart extends Charts {
910
type?: string;
1011
}
1112

12-
export declare const CCharts: (props: CCharts) => React.SFC<CCharts>;
13+
interface CChartBar extends Charts {}
14+
interface CChartHorizontalBar extends Charts {}
15+
interface CChartLine extends Charts {}
16+
interface CChartDoughnut extends Charts {}
17+
interface CChartRadar extends Charts {}
18+
interface CChartPie extends Charts {}
19+
interface CChartPolarArea extends Charts {}
20+
21+
export declare const CChart: (props: CChart) => any
22+
export declare const CChartBar: (props: CChartBar) => any
23+
export declare const CChartHorizontalBar: (props: CChartHorizontalBar) => any
24+
export declare const CChartLine: (props: CChartLine) => any
25+
export declare const CChartDoughnut: (props: CChartDoughnut) => any
26+
export declare const CChartRadar: (props: CChartRadar) => any
27+
export declare const CChartPie: (props: CChartPie) => any
28+
export declare const CChartPolarArea: (props: CChartPolarArea) => any

src/index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
// CoreUI 3 index
1+
import {
2+
CChart,
3+
CChartBar,
4+
CChartHorizontalBar,
5+
CChartLine,
6+
CChartDoughnut,
7+
CChartRadar,
8+
CChartPie,
9+
CChartPolarArea,
10+
} from './CChart'
211

3-
export {default as CCharts} from './CCharts';
12+
export {
13+
CChart,
14+
CChartBar,
15+
CChartHorizontalBar,
16+
CChartLine,
17+
CChartDoughnut,
18+
CChartRadar,
19+
CChartPie,
20+
CChartPolarArea
21+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy