forked from groodlooker/vega-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vega.js
713 lines (653 loc) · 23 KB
/
vega.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
looker.plugins.visualizations.add({
create: function(element, config){
container = element.appendChild(document.createElement("div"));
container.setAttribute("id","my-vega");
},
updateAsync: function(data, element, config, queryResponse, details, doneRendering){
var myData = [];
var dataProperties = {};
var dims = [];
var meas = [];
var allFields = [];
var options = createOptions(queryResponse)['options'];
this.trigger('registerOptions', options);
if (Object.keys(config).length > 2) {
var chartWidth = document.getElementById("my-vega").offsetWidth * 0.81;
var chartHeight = document.getElementById("my-vega").offsetHeight;
var parent = document.getElementById("my-vega").parentElement;
var chartHeight = parent.offsetHeight * 0.78;
for (var cell in data) {
var obj = data[cell];
var dataDict = {};
dataDict['links'] = [];
for (var key in obj){
var shortName = key.replace(".","?");
dataDict[shortName] = obj[key]['value'];
if (typeof obj[key]['links'] != "undefined") {
//create array of all links for a row of data
for(var l=0;l<obj[key]['links'].length;l++){
//grab link label and add field name for clarity in menu
var currentLabel = obj[key]['links'][l]['label'];
currentLabel = currentLabel + " (" + shortName.substring(shortName.indexOf("?")+1) + ")";
obj[key]['links'][l]['label'] = currentLabel;
}
//add links for field in row
dataDict['links'].push(obj[key]['links']);
}
}
//flatten to make single depth array
dataDict['links'] = dataDict['links'].flat();
myData.push(dataDict);
}
var rowValues = [];
var colValues = [];
//auto-size settings when a row facet included
if (typeof config['row'] != "undefined" && typeof config['row'] != "" && config['fixed_height'] == null) {
for (var x = 0; x < myData.length; x++) {
rowValues.push(myData[x][config['row']]);
}
let uniqueRows = new Set(rowValues);
chartHeight = Math.max( (chartHeight / uniqueRows.size), (chartHeight / 12) );
}
//auto-sizing settings when a column facet included
if (typeof config['column'] != "undefined" && config['column'] != "" && config['fixed_width'] == null) {
for (var x = 0; x < myData.length; x++) {
colValues.push(myData[x][config['column']]);
}
let uniqueCols = new Set(colValues);
chartWidth = Math.max( (chartWidth / uniqueCols.size), (chartWidth / 12) ) ;
}
//manual-sizing for chart height
if (typeof config['fixed_height'] != "undefined" && config['fixed_height'] != null) {
chartHeight = config['fixed_height'];
}
//manual-sizing for chart width
if (typeof config['fixed_width'] != "undefined" && config['fixed_width'] != null) {
chartWidth = config['fixed_width'];
}
//create array of all measures for lookup purposes
queryResponse.fields.measure_like.forEach(function(field){
var fieldName = (field.name).replace(".","?");
meas.push(fieldName);
});
//create array of all dimensions for lookup purposes
queryResponse.fields.dimension_like.forEach(function(field){
var fieldName = (field.name).replace(".","?");
dims.push(fieldName);
});
allFields = meas.concat(dims);
var dataFormatDict = {
"$#,##0" : "$,.0f",
"$#,##0.00" : "$,.2f",
"#,##0.00%" : ",.2%",
"#,##0.0%" : ",.1%",
"#,##0%" : ",.0%",
"null" : ""
};
//determine number format
for (var field in allFields) {
var lookerName = allFields[field].replace("?",".");
dataProperties[allFields[field]] = {};
//get friendly names for measures
queryResponse.fields.measure_like.forEach(function(measure){
if (lookerName == measure['name']) {
//get label short or label to handle table calcs
if (typeof measure['label_short'] != "undefined") {
dataProperties[allFields[field]]['title'] = measure['label_short'];
} else {
dataProperties[allFields[field]]['title'] = measure['label'];
}
dataProperties[allFields[field]]['valueFormat'] = dataFormatDict[String(measure['value_format'])];
dataProperties[allFields[field]]['dtype'] = "quantitative";
}
});
//get friendly names for dimensions
queryResponse.fields.dimension_like.forEach(function(dimension){
if (lookerName == dimension['name']) {
if (typeof dimension['label_short'] != "undefined") {
dataProperties[allFields[field]]['title'] = dimension['label_short'];
} else {
dataProperties[allFields[field]]['title'] = dimension['label'];
}
dataProperties[allFields[field]]['valueFormat'] = dataFormatDict[String(dimension['value_format'])];
dataProperties[allFields[field]]['dtype'] = "nominal";
}
});
}
//construct the tooltip with appropriate formatting
var tooltipFields = [];
for (datum in dataProperties) {
var tip = {};
tip['field'] = datum;
tip['type'] = dataProperties[datum]['dtype'];
tip['format'] = dataProperties[datum]['valueFormat'];
tip['title'] = dataProperties[datum]['title'];
tooltipFields.push(tip);
}
//switch to compatible mark if trying to utilize shape
if (config['shape'] != "") {
config['mark_type'] = "point";
}
var chart = {
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"data": {
"values": myData
},
// "autosize": {
// "type": "fit",
// "contains": "padding"
// },
// "selection": {
// "paintbrush": {
// "type": "single",
// "on": "mouseover", "empty": "all", "fields":[config['color']]
// }
// },
"config": {
"mark": {
"size": null,
"color": null,
"circle": null,
"bar": {"size":null},
"text": {"size":null},
"tick": {"size":null},
"point": {"size":2},
"square": {"size":null}
}
},
"mark": {
"type": config['mark_type'],
"fillOpacity": config['opacity'],
"stroke": config['border']
// "strokeWidth": 1
},
"width": chartWidth,
"height": chartHeight,
"encoding": {
"tooltip" : tooltipFields
}
};
//checks for building viz based on config selections//
//////////////////////////////////////////////////////
//add points to line to ensure proper drill through
var marksNeedingPoints = ["line","trail","area"];
// "mark": {"type": "line", "color": "green", "point": {"color": "red"}},
if (marksNeedingPoints.includes(config['mark_type'])) {
chart.mark.point = {"color":config['fixed_color']};
}
if (config['y'] != "" && typeof config['x'] != "undefined") {
chart.encoding.y = {
"field": config['y'],
"type": dataProperties[config['y']]['dtype'],
"title": dataProperties[config['y']]['title'],
"scale": {"zero": config['unpin_y']}};
}
if (config['x'] != "" && typeof config['x'] != "undefined") {
chart.encoding.x = {
"field": config['x'],
"type": dataProperties[config['x']]['dtype'],
"title": dataProperties[config['x']]['title'],
"scale": {"zero": config['unpin_x']}};
}
//row & column facets
if (config['column'] != "" && typeof config['column'] != "undefined") {
//add column facet
chart.encoding.column = {"field":config['column'],"type":dataProperties[config['column']]['dtype'],"title": dataProperties[config['column']]['title']};
// check for independent axes
if (config['resolve_y'] != "" && config['y'] != "") {
chart.resolve = {"scale": {"y":"independent"}};
}
if (config['resolve_x'] != "" && config['x'] != "") {
chart.resolve = {"scale": {"x":"independent"}};
}
}
if (config['row'] != "" && typeof config['column'] != "undefined") {
//add row facet
chart.encoding.row = {"field":config['row'],"type":dataProperties[config['row']]['dtype'],"title": dataProperties[config['row']]['title']};
// check for independent axes
if (config['resolve_y'] != "" && config['y'] != "") {
chart.resolve = {"scale": {"y":"independent"}};
}
if (config['resolve_x'] != "" && config['x'] != "") {
chart.resolve = {"scale": {"x":"independent"}};
}
}
//coloring properties//
//list of categorical schemes not compatible with sequential data
var myColorPalettes = ["tableau10","tableau20","dark2","category20b","set2"];
//add selection handler to chart
if (config['highlight'] != "" && typeof config['highlight'] != "undefined") {
chart.selection = {"paintbrush":{
"type" : "multi",
"on" : "mouseover", "empty":"all","fields":[config['highlight']]
}};
}
//change color domain based on user input
if (config['domain'] != "" && typeof config['domain'] != "undefined") {
var colorDomain = [];
//parse the string input by user into array of integers
for (num in config['domain'].split(",")) {
colorDomain.push(Number(config['domain'].split(",")[num]));
}
}
//set highlighting based on selection, use color selected on marks page
if (config['highlight'] != "" && typeof config['highlight'] != "undefined") {
//assign default color if none selected
if (config['color_scheme'] == "") {
config['color_scheme'] = "tableau10"
}
chart.encoding.color = {
"condition": {
"scale": {"type":"ordinal","scheme":config['color_scheme']},
//use aformentioned select
"selection" : "paintbrush",
//field to drive the highlight
"field":config['highlight'], "type": dataProperties[config['highlight']]['dtype'], "title": dataProperties[config['highlight']]['title']
},
//value if false
"value":"#B8B8B8"
};
} else if (config['color'] != "" && typeof config['color'] != "undefined") {
//if user has changed the field to color by
//add color setting based on data type
chart.encoding.color = {"field": config['color'], "type": dataProperties[config['color']]['dtype'],"title": dataProperties[config['color']]['title']};
//check if quantitative or nominal
if (dataProperties[config['color']]['dtype'] == 'quantitative') {
//determine scale and scheme based on data type
if (config['color_scheme'] == "") {
//if color scheme left on auto, assign the blues
chart.encoding.color.scale = {"type": "sequential", "scheme":"blues"};
} else {
//if color scheme is categorical, but used with quantitative data, switch it to the blues by default
if (myColorPalettes.includes(config['color_scheme'])) {config['color_scheme'] = "blues";}
//apply new color scheme for sequential data, include domain manually provided
chart.encoding.color.scale = {"type": "sequential", "scheme": {"name" : config['color_scheme']}, "domain" : colorDomain};
}
} else if (dataProperties[config['color']]['dtype'] == 'nominal') {
if (config['color_scheme'] == "") {
//if color scheme is auto, use tableau10
chart.encoding.color.scale = {"type": "ordinal", "scheme":"tableau10"};
} else {
//assign selected color scheme otherwise - ordinal will work with quantitative data so no checking required
chart.encoding.color.scale = {"type": "ordinal", "scheme":config['color_scheme']};
}
}
} else {
if (config['mark_type'] == "line") {
//color the stoke if it's a line to avoid it being filled
chart.mark.stroke = config['fixed_color'];
} else {
//color the fill of the mark if not a line
chart.mark.fill = config['fixed_color'];
}
}
//End coloring section//
var sizableMarks = ["point", "square", "circle", "tick", "bar", "text"];
//shape properties
if (config['shape'] != "") {
chart.encoding.shape = {"field": config['shape'], "type": dataProperties[config['shape']]['dtype'],"title": dataProperties[config['shape']]['title']};
}
//set style of line
if (config['line_style'] != "" && typeof config['line_style'] != "undefined" && config['mark_type'] == "line") {
chart.mark.interpolate = config['line_style'];
}
// chart.order = {"field": config['path'],"type": "temporal"};
//sizing properties
if (config['size'] != "") {
chart.encoding.size = {"field": config['size'], "type": dataProperties[config['size']]['dtype'],"title": dataProperties[config['size']]['title']};
} else {
chart.config.mark.size = config['fixed_size'];
}
//order properties for stacked bars
if (config['order'] != "" && typeof config['order'] != "undefined") {
chart.encoding.order = {"field":config['order'], "type":dataProperties[config['order']]['dtype'], "sort":config['order_type']};
}
//sorting properties for bar charts mainly
if (config['sort'] != "" && typeof config['sort'] != "undefined") {
if (dataProperties[config['y']]['dtype'] == "nominal") {
chart.encoding.y.sort = {"field":config['sort'],"op":"sum" ,"order":config['sort_type']}; //"type":dataProperties[config['sort']]['dtype'],
} else if (dataProperties[config['x']]['dtype'] == "nominal") {
chart.encoding.x.sort = {"field":config['sort'],"op":"sum","order":config['sort_type']}; //"type":dataProperties[config['sort']]['dtype'],
}
}
console.log(chart);
vegaEmbed("#my-vega", chart, {actions: false}).then(({spec, view}) => {
view.addEventListener('click', function (event, item) {
if (event.shiftKey) {
} else {
LookerCharts.Utils.openDrillMenu({
links: item.datum.links,
event: event
});
}
});
doneRendering();
});
}
}
});
function createOptions(queryResponse){
var masterList = [];
var dimensionList = [];
var measureList = [];
var options = {};
var defaultDim;
var defaultMes;
var optionsResponse = {};
optionsResponse['options'] = {};
optionsResponse['measures'] = [];
optionsResponse['dimensions'] = [];
optionsResponse['masterList'] = [];
var dimCounter = 1;
var mesCounter = 1;
queryResponse.fields.dimension_like.forEach(function(field){
var dimLib = {};
var fieldName = (field.name).replace(".","?");
if (typeof field.label_short != "undefined") {
dimLib[field.label_short] = fieldName; //store friendly label & field name
} else {
dimLib[field.label] = fieldName; //capture label, mainly for table calcs
}
if (dimCounter == 1) {
defaultDim = fieldName; //grab first dimension to use as default X value
}
optionsResponse['masterList'].push(dimLib); //add to master list of all fields
optionsResponse['dimensions'].push(dimLib);
dimCounter += 1;
});
queryResponse.fields.measure_like.forEach(function(field){
var mesLib = {};
var fieldName = (field.name).replace(".","?");
if (typeof field.label_short != "undefined") {
mesLib[field.label_short] = fieldName;
} else {
mesLib[field.label] = fieldName;
}
if (mesCounter == 1) {
defaultMes = fieldName //grab first measure as default Y value
}
optionsResponse['masterList'].push(mesLib);
optionsResponse['measures'].push(mesLib);
mesCounter += 1;
});
var mesLib = {};
mesLib['NA'] = "";
optionsResponse['masterList'].push(mesLib);
optionsResponse['measures'].push(mesLib);
optionsResponse['dimensions'].push(mesLib);
optionsResponse['options']['x'] = {
label: "X",
section: "1) Axes",
type: "string",
display: "select",
order: 1,
values: optionsResponse['masterList'],
default: defaultDim
}
optionsResponse['options']['y'] = {
label: "Y",
section: "1) Axes",
type: "string",
display: "select",
order: 2,
values: optionsResponse['masterList'],
default: defaultMes
}
optionsResponse['options']['size'] = {
label: "Size",
section: "2) Mark",
type: "string",
display: "select",
order: 5,
values: optionsResponse['measures'],
default: ""
}
optionsResponse['options']['color'] = {
label: "Color",
section: "2) Mark",
type: "string",
display: "select",
order: 2,
values: optionsResponse['masterList'],
default: ""
}
optionsResponse['options']['color_scheme'] = {
label: "Color Scheme",
section: "2) Mark",
type: "string",
display: "select",
order: 3,
values: [
{"Auto" : ""},
{"Default 10 (Categorical)" : "tableau10"},
{"Default 20 (Categorical)" : "tableau20"},
{"Dark 8 (Categorical)" : "dark2"},
{"Dark 20 (Categorical)" : "category20b"},
{"Light 8 (Categorical)" : "set2"},
{"Blues (Sequential)" : "blues"},
{"Greens (Sequential)" : "greens"},
{"Grays (Sequential)" : "greys"},
{"Purples (Sequential)" : "purples"},
{"Oranges (Sequential)" : "oranges"},
{"Viridis (Sequential Multi)" : "viridis"},
{"Inferno (Sequential Multi)" : "inferno"},
{"Magma (Sequential Multi)" : "magma"},
{"Plasma (Sequential Multi)" : "plasma"},
{"Blue Purple (Sequential Multi)" : "bluepurple"},
{"Purple Red (Sequential Multi)" : "purplered"},
{"Spectral (Diverging)" : "spectral"},
{"Red Blue (Diverging)" : "redblue"},
{"Red Gray (Diverging)" : "redgrey"},
{"Red Green (Diverging)" : "redyellowgreen"},
{"Brown Green (Diverging)" : "brownbluegreen"}
],
default: ""
}
optionsResponse['options']['domain'] = {
label: "Color Domain",
section: "2) Mark",
type: "string",
display: "text",
order: 4,
default: ""
}
optionsResponse['options']['row'] = {
label: "Row",
section: "1) Axes",
type: "string",
order: 3,
display: "select",
default: "",
values: optionsResponse['dimensions']
}
optionsResponse['options']['column'] = {
label: "Column",
section: "1) Axes",
type: "string",
order: 4,
display: "select",
default: "",
values: optionsResponse['dimensions']
}
optionsResponse['options']['resolve_x'] = {
label: "Independent X Axis",
section: "1) Axes",
type: "string",
display: "select",
default: "",
order: 5,
values: [
{"Yes":"independent"},
{"No":""}
]
}
optionsResponse['options']['resolve_y'] = {
label: "Independent Y Axis",
section: "1) Axes",
type: "string",
display: "select",
order: 7,
default: "",
values: [
{"Yes":"independent"},
{"No":""}
]
}
optionsResponse['options']['unpin_x'] = {
label: "Unpin X from Zero",
section: "1) Axes",
type: "string",
display: "select",
order: 6,
default: true,
values: [{"Yes":false},{"No":true}]
}
optionsResponse['options']['unpin_y'] = {
label: "Unpin Y from Zero",
section: "1) Axes",
type: "string",
display: "select",
order: 8,
default: true,
values: [{"Yes":false},{"No":true}]
}
optionsResponse['options']['fixed_size'] = {
label: "Fixed Size",
section: "3) Format",
type: "number",
display: "range",
default: 100,
min: 1,
max: 5000
}
optionsResponse['options']['opacity'] = {
label: "Opacity",
section: "3) Format",
type: "number",
display: "text",
default: 1,
step: 1,
min: 0,
max: 1
}
optionsResponse['options']['line_style'] = {
label: "Line Style",
section: "3) Format",
type: "string",
display: "select",
default: "",
values:[{"Default":""},{"Monotone":"monotone"},{"Step":"step-after"}]
}
optionsResponse['options']['fixed_height'] = {
label: "Chart Height",
section: "3) Format",
type: "number",
display: "text",
default: null,
}
optionsResponse['options']['fixed_width'] = {
label: "Chart Width",
section: "3) Format",
type: "number",
display: "text",
default: null,
}
optionsResponse['options']['fixed_color'] = {
label: "Color",
section: "3) Format",
type: "string",
display: "text",
default: "#4C78A8"
}
optionsResponse['options']['border'] = {
label: "Border (Enter color)",
section: "3) Format",
type: "string",
display: "text",
default: ""
}
optionsResponse['options']['shape'] = {
label: "Shape",
section: "2) Mark",
order: 6,
type: "string",
display: "select",
values: optionsResponse['dimensions'],
default: ""
}
optionsResponse['options']['path'] = {
label: "Path",
section: "2) Mark",
order: 7,
type: "string",
default: "",
display: "select",
values: optionsResponse['dimensions']
}
optionsResponse['options']['mark_type'] = {
label: "Mark Type",
section: "2) Mark",
type: "string",
order: 1,
display: "select",
default: "bar",
values: [
{"Bar" : "bar"},
{"Rule" : "rule"},
{"Circle" : "circle"},
{"Tick" : "tick"},
// {"Text" : "text"},
{"Line" : "line"},
{"Rect" : "rect"},
{"Area" : "area"},
{"Point" : "point"},
{"Trail" : "trail"}
]
}
optionsResponse['options']['highlight'] = {
label: "Highlight Action",
section: "4) Advanced",
type: "string",
order: 1,
display: "select",
default: "",
values: optionsResponse['dimensions']
}
optionsResponse['options']['order'] = {
label: "Order by (stacked only)",
section: "4) Advanced",
type: "string",
order: 4,
display: "select",
default: "",
values: optionsResponse['measures']
}
optionsResponse['options']['order_type'] = {
label: "Order by Type",
section: "4) Advanced",
type: "string",
order: 5,
display: "select",
default: "descending",
values: [{"Descending":"descending"},{"Ascending":"ascending"}]
}
optionsResponse['options']['sort'] = {
label: "Sort Value",
order: 2,
section: "4) Advanced",
type: "string",
display: "select",
default: "",
values: optionsResponse['measures']
}
optionsResponse['options']['sort_type'] = {
label: "Sort by Type",
order: 3,
section: "4) Advanced",
type: "string",
display: "select",
default: "descending",
values: [{"Descending":"descending"},{"Ascending":"ascending"}]
}
return optionsResponse;
}