forked from w3f/polkadot-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChart.jsx
123 lines (115 loc) · 3.4 KB
/
Chart.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
import React from "react";
import { Line, Scatter } from "react-chartjs-2";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
} from 'chart.js';
import openGovVariables from "./utilities/openGovVariables";
// Maps a type and props to a JSX charts.js component
function mapTypeToComponent(type, key, network, maxY, maxX) {
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend
);
// Configure props using data
let props = configureProps(key, network, maxY, maxX);
ChartJS.defaults.font.size = 18;
switch (type) {
case 'line':
return <Line datasetIdKey={props.datasetIdKey} data={props.data} options={props.options} />
case 'scatter':
return <Scatter datasetIdKey={props.datasetIdKey} data={props.data} options={props.options} />
}
}
function configureProps(key, network, maxY, maxX) {
console.log(`${network}${key}`);
let govData = openGovVariables[`${network}${key}`];
const approvals = govData.map((_) => { return { x: _.time_hours, y: _.approval } });
const support = govData.map((_) => { return { x: _.time_hours, y: _.support } });
const props = {
data: {
labels: ['Approval', 'Support'],
datasets: [{
label: "Approval",
data: approvals,
backgroundColor: '#00B2FF'
},
{
label: "Support",
data: support,
backgroundColor: '#56F39A'
}
],
},
options: {
animation: false,
normalized: true,
parsing: false,
scales: {
y: {
type: 'linear',
min: 0,
max: maxY,
ticks: {
callback: function (value) {
return `${value}%`;
}
}
},
x: {
type: 'linear',
min: 0,
max: maxX,
title: {
display: true,
text: "Hours",
},
ticks: {
callback: function (value) {
return `${value}`;
}
}
}
},
plugins: {
tooltip: {
callbacks: {
title: function (context) {
return `Hour: ${context[0].label}`;
},
label: function (context) {
return `${context.dataset.label}: ${context.parsed.y}%`;
}
}
}
}
}
};
// Return props
return props;
}
function Chart({ title, type, dataId, network, maxY, maxX}) {
let chart = mapTypeToComponent(type, dataId, network, maxY, maxX);
return (
<div className="App">
<header className="App-header">
<h3>
{title}
</h3>
</header>
{chart}
</div>
);
}
export default Chart;