-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_viz_scatterplot.js
152 lines (129 loc) · 5.13 KB
/
custom_viz_scatterplot.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
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
/**
* This is a Custom Visualization for Looker
*
* It shows the first 2 dimension columns as a title and a paragraph
*
* CREATED BY: Egbert Schroeder
*
* DEPENDENCIES:
*
*
* TODO:
*
*
**/
const customVizScatterPlot = {
options: {},
/**
* The create function gets called when the visualization is mounted but before any
* data is passed to it.
**/
create: function (element, config) {
// Insert Highcharts css file
// Create a container element to let us center the text.
this._vizContainer = element.appendChild(document.createElement("div"));
},
/**
* UpdateAsync is the function that gets called (potentially) multiple times. It receives
* the data and should update the visualization with the new data.
**/
updateAsync: function (data, element, config, queryResponse, details, doneRendering) {
// set the dimensions and margins of the graph
var $dimension_keys = ["name", "country", "column3"];
var $measure_keys = ["x", "y", "z"];
var $dataArray = [];
// Create list of unique values from first column
for (let i = 0; i < data.length; i++) {
var row = data[i];
var $rowData = {};
for (var key in row) {
for (var a = 0; a < queryResponse.fields.dimension_like.length; a++) {
if (queryResponse.fields.dimension_like[a]["name"] == key && queryResponse.fields.dimension_like[a]["hidden"] == false) {
$rowData.name = LookerCharts.Utils.textForCell(row[key]);
}
}
for (var b = 0; b < queryResponse.fields.measure_like.length; b++) {
if (queryResponse.fields.measure_like[b]["name"] == key && queryResponse.fields.measure_like[b]["hidden"] == false) {
if (b == 0) {
$rowData.x = row[key].value;
} else if (b == 1) {
$rowData.y = row[key].value;
} else {
$rowData.z = row[key].value;
}
}
}
}
$dataArray.push($rowData);
}
console.log($dataArray);
//console.log(data);
var html = '<div id="scatterPlotContainer"></div>';
// Insert the generated html into the page
this._vizContainer.innerHTML = html;
$(document).ready(function () {
// Create the chart
var chart = Highcharts.chart('scatterPlotContainer', {
chart: {
type: 'bubble',
zoomType: 'xy'
},
title: {
text: ''
},
subtitle: {
text: ''
},
xAxis: {
min: 0,
gridLineWidth: 1,
title: {
text: 'xAxis'
},
labels: {
format: '{value}'
}
},
yAxis: {
min: 0,
title: {
text: 'yAxis'
},
labels: {
format: '{value}'
}
},
tooltip: {
useHTML: true,
headerFormat: '<table>',
pointFormat: '<tr><th colspan="2"><h3>{point.country}</h3></th></tr>' +
'<tr><th>X:</th><td>{point.x}</td></tr>' +
'<tr><th>Y:</th><td>{point.y}</td></tr>' +
'<tr><th>Z:</th><td>{point.z}</td></tr>',
footerFormat: '</table>',
followPointer: true
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
format: '{point.name}'
}
}
},
series: [{
data: $dataArray
/*[
{x: 95, y: 95, name: 'BE', country: 'Belgium'},
{x: 86.5, y: 102.9, z: 14.7, name: 'DE', country: 'Germany'},
{x: 80.8, y: 91.5, z: 15.8, name: 'FI', country: 'Finland'},
{x: 80.4, y: 102.5, z: 12, name: 'NL', country: 'Netherlands'}
]*/
}]
});
});
doneRendering()
}
}
;
looker.plugins.visualizations.add(customVizScatterPlot);