Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitde1b51e

Browse files
committed
refactor: refactor library
- add specific chart type exports- optimize code- change exports- update typings
1 parentd15059e commitde1b51e

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+
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+

‎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-
importReactfrom'react'
2-
3-
interfaceCCharts{
1+
interfaceCharts{
42
innerRef?:any;
53
datasets?:Array<any>;
64
labels?:string|Array<any>;
75
options?:any;
86
plugins?:Array<any>;
7+
}
8+
9+
interfaceCChartextendsCharts{
910
type?:string;
1011
}
1112

12-
exportdeclareconstCCharts:(props:CCharts)=>React.SFC<CCharts>;
13+
interfaceCChartBarextendsCharts{}
14+
interfaceCChartHorizontalBarextendsCharts{}
15+
interfaceCChartLineextendsCharts{}
16+
interfaceCChartDoughnutextendsCharts{}
17+
interfaceCChartRadarextendsCharts{}
18+
interfaceCChartPieextendsCharts{}
19+
interfaceCChartPolarAreaextendsCharts{}
20+
21+
exportdeclareconstCChart:(props:CChart)=>any
22+
exportdeclareconstCChartBar:(props:CChartBar)=>any
23+
exportdeclareconstCChartHorizontalBar:(props:CChartHorizontalBar)=>any
24+
exportdeclareconstCChartLine:(props:CChartLine)=>any
25+
exportdeclareconstCChartDoughnut:(props:CChartDoughnut)=>any
26+
exportdeclareconstCChartRadar:(props:CChartRadar)=>any
27+
exportdeclareconstCChartPie:(props:CChartPie)=>any
28+
exportdeclareconstCChartPolarArea:(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{defaultasCCharts}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)

[8]ページ先頭

©2009-2025 Movatter.jp