-
Notifications
You must be signed in to change notification settings - Fork 51
/
ContributionChart.jsx
196 lines (185 loc) · 5.07 KB
/
ContributionChart.jsx
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import React from 'react'
import PropTypes from 'prop-types'
import utilsDate from '../../utils/DateHelper'
import { Line as LineChart } from 'react-chartjs-2'
const defaultFonts = '"Source Sans Pro", "Helvetica Neue", HelveticaNeue, ' +
'Helvetica, Arial, sans-serif'
const defaultChartOptions = {
animation: {
easing: 'easeOutQuint'
},
legend: {
display: false
},
elements: {
line: {
// curviness
tension: 0.4,
// thickness of line
borderWidth: 2
},
point: {
radius: 3,
hoverRadius: 4,
hitRadius: 10,
borderWidth: 0,
borderColor: '#fff'
}
},
responsive: true,
scales: {
xAxes: [{
ticks: {
fontColor: 'rgb(84, 102, 122)',
fontFamily: defaultFonts
},
gridLines: {
drawOnChartArea: false
}
}],
yAxes: [{
ticks: {
fontColor: 'rgb(84, 102, 122)',
fontFamily: defaultFonts,
// chart should always start at 0, someone can't have negative contrib.
min: 0,
suggestedMax: 10
},
gridLines: {
color: 'rgba(198, 210, 219, .1)',
drawBorder: true
}
}]
},
tooltips: {
mode: 'index',
intersect: false,
backgroundColor: 'rgba(255,255,255,0.92)',
borderColor: '#ccc',
borderWidth: 1,
yPadding: 8,
xPadding: 6,
caretSize: 6,
cornerRadius: 2,
titleFontFamily: defaultFonts,
titleFontSize: 14,
titleFontStyle: '400',
titleFontColor: 'rgb(84, 102, 122)',
titleSpacing: 0,
titleMarginBottom: 12,
bodyFontFamily: defaultFonts,
bodyFontSize: 14,
bodyFontStyle: '400',
bodyFontColor: 'rgb(132, 168, 196)',
bodySpacing: 6,
// http://www.chartjs.org/docs/latest/configuration/tooltip.html
callbacks: {
label: ({ datasetIndex, index }, { datasets }) => {
const dataset = datasets[datasetIndex]
const { data, label } = dataset
const val = data[index]
return ` ${val} (${label})`
}
}
}
}
function convertMatrixDataToChartData (matrixData) {
var chartData = {
labels: [],
datasets: [
{
label: 'Approved',
// fill colour under the line
backgroundColor: 'rgba(27, 167, 217, .05)',
// colour of the line
borderColor: 'rgb(27, 167, 217)',
pointBackgroundColor: 'rgb(27, 167, 217)',
pointHighlightStroke: 'rgb(27, 167, 217)',
data: []
},
{
label: 'Needs Work',
// fill colour under the line
backgroundColor: 'rgba(235, 236, 21, .05)',
// colour of the line
borderColor: 'rgb(235, 236, 21)',
pointBackgroundColor: 'rgb(235, 236, 21)',
pointHighlightStroke: 'rgb(235, 236, 21)',
data: []
},
{
label: 'Translated',
// fill colour under the line
backgroundColor: 'rgba(36, 200, 137, .05)',
// colour of the line
borderColor: 'rgb(36, 200, 137)',
pointBackgroundColor: 'rgb(36, 200, 137)',
pointHighlightStroke: 'rgb(36, 200, 137)',
data: []
},
{
label: 'Total',
// fill colour under the line
backgroundColor: 'rgba(84, 102, 122, .05)',
// colour of the line
borderColor: 'rgb(84, 102, 122)',
pointBackgroundColor: 'rgb(84, 102, 122)',
pointHighlightStroke: 'rgb(84, 102, 122)',
data: []
}
]
}
var numOfDays = matrixData.length
var intoTheFuture = false
matrixData.forEach(function (value) {
var date = value['date']
intoTheFuture = intoTheFuture || utilsDate.isInFuture(date)
chartData.labels.push(utilsDate.dayAsLabel(date, numOfDays))
if (!intoTheFuture) {
chartData['datasets'][0]['data'].push(value['totalApproved'])
chartData['datasets'][1]['data'].push(value['totalNeedsWork'])
chartData['datasets'][2]['data'].push(value['totalTranslated'])
chartData['datasets'][3]['data'].push(value['totalActivity'])
}
})
return chartData
}
class ContributionChart extends React.Component {
static propTypes = {
dateRange: PropTypes.object.isRequired,
wordCountForEachDay: PropTypes.arrayOf(
PropTypes.shape(
{
date: PropTypes.string.isRequired,
totalActivity: PropTypes.number.isRequired,
totalApproved: PropTypes.number.isRequired,
totalTranslated: PropTypes.number.isRequired,
totalNeedsWork: PropTypes.number.isRequired
})
).isRequired,
chartOptions: PropTypes.object
}
static defaultProps = {
chartOptions: defaultChartOptions
}
shouldComponentUpdate (nextProps, nextState) {
return this.props.dateRange !== nextProps.dateRange ||
this.props.wordCountForEachDay.length !==
nextProps.wordCountForEachDay.length
}
render () {
const {
wordCountForEachDay,
chartOptions
} = this.props
var chartData = convertMatrixDataToChartData(wordCountForEachDay)
return (
<LineChart
data={chartData}
options={chartOptions}
width={800}
height={250} />
)
}
}
export default ContributionChart