This repository was archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathcore.js
171 lines (150 loc) · 4.89 KB
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var React = require('react')
var ReactDOM = require('react-dom')
module.exports = {
createClass: function (chartType, methodNames, dataKey) {
var classData = {
displayName: chartType + 'Chart',
getInitialState: function () {
return {}
},
render: function () {
var _props = {
ref: 'canvass'
}
for (var name in this.props) {
if (this.props.hasOwnProperty(name)) {
if (name !== 'data' && name !== 'options') {
_props[name] = this.props[name]
}
}
}
return React.createElement('canvas', _props)
}
}
var extras = ['clear', 'stop', 'resize', 'toBase64Image', 'generateLegend', 'update', 'addData', 'removeData']
function extra (type) {
classData[type] = function () {
return this.state.chart[type].apply(this.state.chart, arguments)
}
}
classData.componentDidMount = function () {
this.initializeChart(this.props)
}
classData.componentWillUnmount = function () {
var chart = this.state.chart
chart.destroy()
}
classData.componentWillReceiveProps = function (nextProps) {
var chart = this.state.chart
if (nextProps.redraw) {
chart.destroy()
this.initializeChart(nextProps)
} else {
dataKey = dataKey || dataKeys[chart.name]
updatePoints(nextProps, chart, dataKey)
if (chart.scale) {
chart.scale.xLabels = nextProps.data.labels
chart.scale.calculateXLabelRotation()
}
chart.update()
}
}
classData.initializeChart = function (nextProps) {
var Chart = require('chart.js')
var el = ReactDOM.findDOMNode(this)
var ctx = el.getContext('2d')
Chart.types.Doughnut.extend({
name: 'DoughnutTextInside',
showTooltip: function () {
this.chart.ctx.save()
Chart.types.Doughnut.prototype.showTooltip.apply(this, arguments)
this.chart.ctx.restore()
},
defaults: {
labelScale: 100
},
draw: function () {
Chart.types.Doughnut.prototype.draw.apply(this, arguments)
var width = this.chart.width,
height = this.chart.height
var fontSize = (height / this.options.labelScale).toFixed(2)
this.chart.ctx.fillStyle = '#000'
this.chart.ctx.font = fontSize + "em 'Open Sans'"
this.chart.ctx.textBaseline = 'middle'
var total = this.total
var clone = this.segments.slice()
clone.pop()
var filled = clone.reduce(function (a, val) {
return a + val.value
}, 0)
var text = Math.round(total > 0 ? (100 / total) * filled : 0) + '%'
var textX = Math.round((width - this.chart.ctx.measureText(text).width) / 2),
textY = height / 2
this.chart.ctx.fillText(text, textX, textY)
}
})
var chart = new Chart(ctx)[chartType](nextProps.data, nextProps.options || {})
this.state.chart = chart
}
// return the chartjs instance
classData.getChart = function () {
return this.state.chart
}
// return the canvass element that contains the chart
classData.getCanvass = function () {
return this.refs.canvass
}
classData.getCanvas = classData.getCanvass
var i
for (i = 0; i < extras.length; i++) {
extra(extras[i])
}
for (i = 0; i < methodNames.length; i++) {
extra(methodNames[i])
}
return React.createClass(classData)
}
}
var dataKeys = {
'Line': 'points',
'Radar': 'points',
'Bar': 'bars'
}
var updatePoints = function (nextProps, chart, dataKey) {
var name = chart.name
if (name === 'PolarArea' || name === 'Pie' || name === 'Doughnut' || name === 'DoughnutTextInside') {
nextProps.data.forEach(function (segment, segmentIndex) {
if (!chart.segments[segmentIndex]) {
chart.addData(segment)
} else {
Object.keys(segment).forEach(function (key) {
chart.segments[segmentIndex][key] = segment[key]
})
}
})
} else {
if (chart.scale) {
while (chart.scale.xLabels.length > nextProps.data.labels.length) {
chart.removeData()
}
}
if (nextProps.data && nextProps.data.datasets) {
nextProps.data.datasets.forEach(function (set, setIndex) {
set.data.forEach(function (val, pointIndex) {
if (typeof (chart.datasets[setIndex][dataKey][pointIndex]) == 'undefined') {
addData(nextProps, chart, setIndex, pointIndex)
} else {
chart.datasets[setIndex][dataKey][pointIndex].value = val
}
})
})
}
}
}
var addData = function (nextProps, chart, setIndex, pointIndex) {
var values = []
nextProps.data.datasets.forEach(function (set) {
values.push(set.data[pointIndex])
})
chart.addData(values, nextProps.data.labels[setIndex])
}