-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreateGoogleCharts.js
64 lines (52 loc) · 1.58 KB
/
createGoogleCharts.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
import startCase from 'lodash/startCase';
import voxlens from '../../../src';
const createGoogleCharts = (options) => {
let { data, fillColor, title, withVoxLens, xKey, yKey } = options;
const voxlensOptions = {
...options,
x: xKey,
y: yKey,
};
const container = document.getElementById('chart');
const margin = { top: 20, right: 40, bottom: 20, left: 70 };
const height = 700 - margin.top - margin.bottom;
const width = container.offsetWidth - margin.left - margin.right;
const google = window.google;
const drawChart = () => {
const dataTable = new google.visualization.DataTable();
let Chart = google.visualization.ColumnChart;
dataTable.addColumn('string', startCase(xKey));
dataTable.addColumn('number', startCase(yKey));
dataTable.addRows(
data.map((d) => [d[xKey].toString(), parseFloat(d[yKey])])
);
const options = {
legend: { position: 'none' },
bars: 'horizontal',
colors: [fillColor],
title,
width,
height,
hAxis: {
title: startCase(xKey),
slantedText: false,
},
vAxis: {
title: startCase(yKey),
baseline: 0,
gridlines: {
color: 'black',
},
minorGridlines: {
color: 'black',
},
},
};
const chart = new Chart(container);
chart.draw(dataTable, options);
if (withVoxLens) voxlens('googlecharts', chart, data, voxlensOptions);
};
google.charts.load('current', { packages: ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
};
export default createGoogleCharts;