-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
156 lines (128 loc) · 5.29 KB
/
Code.gs
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
function generateChart(chartData) {
if (!chartData || !chartData.data)
return "Invalid data set";
var chartImage;
switch (chartData.type) {
case 0: // timeline chart
chartImage = generateArea(chartData.data);
break;
case 1: // stacked timeline chart
chartImage = generateStackedArea(chartData.data);
break;
case 2: // pie chart
chartImage = generatePie(chartData.data);
break;
case 3: // vertical bar data
chartImage = generateVerticalBar(chartData.data);
break;
case 4: // horizontal bar data
chartImage = generateHorizontalBar(chartData.data);
break;
default:
chartImage = null;
}
if (!chartImage)
return "Invalid data set";
return chartImage.build().getAs("image/png").getBytes();
}
function generateArea(chartData) {
var data = Charts.newDataTable().addColumn(Charts.ColumnType.STRING, 'Label');
var legendTextStyleBuilder = Charts.newTextStyle().setColor('#767171').setFontName('noto sans');
var legendTextStyle = legendTextStyleBuilder.build();
for (var i = 0; i < chartData.data.length; i++)
data = data.addColumn(Charts.ColumnType.NUMBER, chartData.series[i]);
for (var i = 0; i < chartData.data[0].length; i++) {
data = data.addRow([chartData.labels[i]]);
for (var j = 0; j < chartData.data.length; j++)
data = data.setValue(i, j+1, chartData.data[j][i]);
}
var chart = Charts.newAreaChart()
.setColors(['#5cbae5','#f29b1d'])
.setLegendPosition(Charts.Position.TOP)
.setLegendTextStyle(legendTextStyle)
.setDimensions(800, 510)
.setOption('areaOpacity', 0.05)
.setOption('chartArea.left','60')
.setOption('chartArea.right', '60')
.setOption('hAxis.gridlines.color', '#ffffff')
.setOption('vAxis.gridlines.color', '#d6d6d6')
.setOption('vAxis.baselineColor', '#767171')
.setOption('hAxis.baselineColor', '#767171')
.setOption('hAxis.textStyle', {color: '#767171'})
.setOption('vAxis.textStyle', {color: '#767171'})
.setDataTable(data);
return chart;
}
function generateStackedArea(chartData) {
return generateArea(chartData).setStacked();
}
function generatePie(chartData) {
var data = Charts.newDataTable().addColumn(Charts.ColumnType.STRING, 'Label').addColumn(Charts.ColumnType.NUMBER, 'Value');
var allSum = 0;
for (var i = 0; i < chartData.data.length; i++)
allSum += chartData.data[i];
for (var i = 0; i < chartData.data.length; i++)
data = data.addRow([chartData.labels[i] + ' ('+percentageRound( chartData.data[i] / allSum * 100) + '%)', chartData.data[i]]);
var pieSliceTextColor = '#FFFFFF';
if (chartData.data.length < 2)
pieSliceTextColor = '#767171';
var chart = Charts.newPieChart()
.setColors(['#6534ff', '#FFD454', '#62bcfa', '#cdd422', '#e05915', '#1B9D17', '#bccbde', '#431c5d', '#e6e9f0', '#fccdd3','#10A3C2'])
.setDimensions(400, 300)
.setDataTable(data)
.setOption('pieHole', 0.45)
.setOption('pieSliceTextStyle', { color: pieSliceTextColor, fontName: "noto sans", format: 'short', bold: true})
.setOption('pieSliceText', 'value')
.setOption('chartArea.width', '98%')
.setOption('chartArea.height', '95%')
.setOption('legend', {alignment: 'center', position: 'right', textStyle: {color:'#767171'}})
return chart;
}
function generateVerticalBar(chartData) {
var data = Charts.newDataTable().addColumn(Charts.ColumnType.STRING, 'Label')
var seriesCount = chartData.series.length;
for(var i = 0; i < seriesCount; i++)
data = data.addColumn(Charts.ColumnType.NUMBER, chartData.series[i]);
for (var i = 0; i < chartData.data[0].length; i++) {
data = data.addRow([chartData.labels[i]]);
for (var j = 0; j < seriesCount; j++)
data = data.setValue(i, j+1, chartData.data[j][i]);
}
var chart = Charts.newColumnChart()
.setColors(['#6534ff', '#FFD454', '#62bcfa', '#cdd422', '#e05915', '#1B9D17', '#bccbde', '#431c5d', '#e6e9f0', '#fccdd3'])
.setDimensions(400, 300)
.setOption('chartArea.left','60')
.setOption('chartArea.right','60')
.setOption('vAxis.gridlines.color', '#d6d6d6')
.setOption('vAxis.baselineColor', '#767171')
.setOption('hAxis.baselineColor', '#767171')
.setOption('legend', {textStyle: {color: '#767171'}, position: 'top'})
.setOption('hAxis.textStyle', {color: '#767171'})
.setOption('vAxis.textStyle', {color: '#767171'})
.setDataTable(data);
return chart;
}
function generateHorizontalBar(chartData) {
var data = Charts.newDataTable().addColumn(Charts.ColumnType.STRING, 'Label')
var seriesCount = chartData.series.length;
for(var i = 0; i < seriesCount; i++)
data = data.addColumn(Charts.ColumnType.NUMBER, chartData.series[i]);
for (var i = 0; i < chartData.data[0].length; i++) {
data = data.addRow([chartData.labels[i]]);
for (var j = 0; j < seriesCount; j++)
data = data.setValue(i, j+1, chartData.data[j][i]);
}
var chart = Charts.newBarChart()
.setColors(['#6534ff', '#FFD454', '#62bcfa', '#cdd422', '#e05915', '#1B9D17', '#bccbde', '#431c5d', '#e6e9f0', '#fccdd3'])
.setDimensions(800, 400)
.setLegendPosition(Charts.Position.TOP)
.setDataTable(data)
.setRange(0, 40)
return chart;
}
function percentageRound(num) {
if (num >= 0.1)
return Math.round(num * 10) / 10;
else
return "<0.1";
}