I need to create a bar or column chart inside a table cell of each row.
i.e I need a graph to be populated for each category inside table cell.
JSON:
JavaScript
x
83
83
1
[
2
{
3
"Chocolate": [
4
{
5
"kisses": [
6
{
7
"2022": {
8
"jan": 2000,
9
"feb": 1200,
10
"mar": 7000
11
}
12
},
13
{
14
"2021": {
15
"jan": 2000,
16
"feb": 1200,
17
"mar": 7000
18
}
19
}
20
]
21
},
22
{
23
"kitkat": [
24
{
25
"2022": {
26
"jan": 1000,
27
"feb": 3200,
28
"mar": 4500
29
}
30
},
31
{
32
"2021": {
33
"jan": 2000,
34
"feb": 200,
35
"mar": 7030
36
}
37
}
38
]
39
}
40
]
41
},
42
{
43
"Drinks": [
44
{
45
"Coco cola": [
46
{
47
"2022": {
48
"jan": 2000,
49
"feb": 1200,
50
"mar": 7000
51
}
52
},
53
{
54
"2021": {
55
"jan": 2200,
56
"feb": 1200,
57
"mar": 7100
58
}
59
}
60
]
61
},
62
{
63
"Pepsi": [
64
{
65
"2022": {
66
"jan": 1000,
67
"feb": 3200,
68
"mar": 4500
69
}
70
},
71
{
72
"2021": {
73
"jan": 4550,
74
"feb": 2100,
75
"mar": 3430
76
}
77
}
78
]
79
}
80
]
81
}
82
]
83
I need to create a bar or column chart inside a table cell of each row.
JavaScript
1
60
60
1
google.charts.load('current', {
2
packages: ['corechart', 'table']
3
}).then(function () {
4
var data = new google.visualization.DataTable();
5
data.addColumn('string', 'Category');
6
data.addColumn('number', 'Name');
7
data.addColumn('string', 'Chart');
8
data.addRows([
9
['Choclate', {v: 10000, f: 'Kisses'}, null],
10
['Drinks', {v: 12500, f: 'Pepsi'}, null],
11
['Food', {v: 7000, f: 'Pizza'}, null]
12
]);
13
14
var table = new google.visualization.Table(document.getElementById('table_div'));
15
16
google.visualization.events.addListener(table, 'ready', function () {
17
// table body
18
Array.prototype.forEach.call(table.getContainer().getElementsByTagName('tbody'), function(tableBody) {
19
// table rows
20
Array.prototype.forEach.call(tableBody.rows, function(tableRow, rowIndex) {
21
// table cells
22
Array.prototype.forEach.call(tableRow.cells, function(tableCell, cellIndex) {
23
// determine if last cell
24
if (cellIndex === (tableRow.cells.length - 1)) {
25
// add chart continer
26
var chartContainer = tableCell.appendChild(document.createElement('div'));
27
chartContainer.className = 'chart';
28
29
// build chart data table
30
var dataChart = new google.visualization.DataTable();
31
dataChart.addColumn('number', 'x');
32
dataChart.addColumn('number', 'y');
33
for (var i = 0; i <= rowIndex; i++) {
34
dataChart.addRow([i, data.getValue(i, 1)]);
35
}
36
37
// draw chart
38
var chart = new google.visualization.ColumnChart(chartContainer);
39
chart.draw(dataChart, {
40
chartArea: {
41
left: 24,
42
top: 16,
43
right: 24,
44
bottom: 16,
45
height: '100%',
46
width: '100%'
47
},
48
height: '100%',
49
legend: 'none',
50
pointSize: 6,
51
width: '100%'
52
});
53
}
54
});
55
});
56
});
57
});
58
59
table.draw(data, {showRowNumber: true, width: '60%', height: '70%'});
60
});
JavaScript
1
2
1
<script src="https://www.gstatic.com/charts/loader.js"></script>
2
<div id="table_div"></div>
I Want JSON values to be populated in the graph w.r.t each category.
Sample output: https://i.stack.imgur.com/H973g.png
If unable to pivot row based on category. I need to try atlease each row.
Please suggest me how to achieve
Advertisement
Answer
You can try something like the below.
- You need to prepare your JSON as per rows.
So you need to transform your data like below
processData
var which will contains proper dataset. - After each row, you need to merge cells based on category. I took reference from this SO
- In Chart, add data for each year and sum it with month wise
JavaScript
1
182
182
1
let chartData = [{
2
"Chocolate": [{
3
"kisses": [{
4
"2022": {
5
"jan": 2000,
6
"feb": 1200,
7
"mar": 7000
8
}
9
},
10
{
11
"2021": {
12
"jan": 2000,
13
"feb": 1200,
14
"mar": 7000
15
}
16
}
17
]
18
},
19
{
20
"kitkat": [{
21
"2022": {
22
"jan": 1000,
23
"feb": 3200,
24
"mar": 4500
25
}
26
},
27
{
28
"2021": {
29
"jan": 2000,
30
"feb": 200,
31
"mar": 7030
32
}
33
}
34
]
35
}
36
]
37
},
38
{
39
"Drinks": [{
40
"Coco cola": [{
41
"2022": {
42
"jan": 2000,
43
"feb": 1200,
44
"mar": 7000
45
}
46
},
47
{
48
"2021": {
49
"jan": 2200,
50
"feb": 1200,
51
"mar": 7100
52
}
53
}
54
]
55
},
56
{
57
"Pepsi": [{
58
"2022": {
59
"jan": 1000,
60
"feb": 3200,
61
"mar": 4500
62
}
63
},
64
{
65
"2021": {
66
"jan": 4550,
67
"feb": 2100,
68
"mar": 3430
69
}
70
}
71
]
72
}
73
]
74
}
75
];
76
77
78
const processData = chartData.reduce((prev, next) => {
79
let category = Object.keys(next)[0];
80
next[category].forEach(value => {
81
let key = Object.keys(value)[0]
82
let data = [category, key, null, value[key]];
83
prev.push(data);
84
})
85
return prev;
86
}, []);
87
88
const rowsData = [];
89
processData.forEach(row => {
90
rowsData.push(row.slice(0, -1));
91
})
92
93
google.charts.load('current', {
94
packages: ['corechart', 'table']
95
}).then(function() {
96
var data = new google.visualization.DataTable();
97
data.addColumn('string', 'Category');
98
data.addColumn('string', 'Name');
99
data.addColumn('string', 'Chart');
100
data.addRows(rowsData)
101
102
var table = new google.visualization.Table(document.getElementById('table_div'));
103
104
google.visualization.events.addListener(table, 'ready', function() {
105
// table body
106
Array.prototype.forEach.call(table.getContainer().getElementsByTagName('tbody'), function(tableBody) {
107
// table rows
108
var rowLabel = null;
109
var rowIndex;
110
var rowSpan;
111
Array.prototype.forEach.call(tableBody.rows, function(tableRow, rowIndex) {
112
// table cells
113
114
if (rowLabel !== tableRow.cells[0].innerHTML) {
115
rowLabel = tableRow.cells[0].innerHTML;
116
rowIndex = rowIndex;
117
if (rowSpan > 1) {
118
tableBody.rows[rowIndex - rowSpan].cells[0].rowSpan = rowSpan;
119
}
120
rowSpan = 1;
121
} else {
122
tableRow.removeChild(tableRow.cells[0]);
123
if (rowIndex === (tableBody.rows.length - 1)) {
124
tableBody.rows[rowIndex - rowSpan].cells[0].rowSpan = ++rowSpan;
125
} else {
126
rowSpan++;
127
}
128
}
129
130
131
Array.prototype.forEach.call(tableRow.cells, function(tableCell, cellIndex) {
132
133
134
// determine if last cell
135
if (cellIndex === (tableRow.cells.length - 1)) {
136
// add chart continer
137
var chartContainer = tableCell.appendChild(document.createElement('div'));
138
chartContainer.className = 'chart';
139
140
// build chart data table
141
var dataChart = new google.visualization.DataTable();
142
dataChart.addColumn('string', 'x');
143
dataChart.addColumn('number', 'y');
144
const row = processData[rowIndex];
145
146
row[3].forEach(value => {
147
148
const key = Object.keys(value)[0];
149
let sum = 0;
150
Object.keys(value[key]).forEach(month => {
151
sum += value[key][month]
152
})
153
dataChart.addRow([key, sum]);
154
})
155
156
// draw chart
157
var chart = new google.visualization.ColumnChart(chartContainer);
158
chart.draw(dataChart, {
159
chartArea: {
160
left: 24,
161
top: 16,
162
right: 24,
163
bottom: 16,
164
height: '100%',
165
width: '100%'
166
},
167
168
legend: 'none',
169
pointSize: 6,
170
width: '150'
171
});
172
}
173
});
174
});
175
});
176
});
177
178
table.draw(data, {
179
width: '60%',
180
height: '70%'
181
});
182
});
JavaScript
1
2
1
<script src="https://www.gstatic.com/charts/loader.js"></script>
2
<div id="table_div"></div>