-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchartify.js
More file actions
86 lines (75 loc) · 2.62 KB
/
chartify.js
File metadata and controls
86 lines (75 loc) · 2.62 KB
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
'use strict'
$(document).ready(function() {
function getCurrentTabUrl(callback) {
let queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
let tab = tabs[0];
let url = tab.url;
console.assert(typeof url == 'string', 'tab.url should be a string');
callback(url);
});
}
function generateChart() {
getCurrentTabUrl(function(url) {
console.log("Fetching for: ", url);
$.getJSON(url, undefined, buildChartFromData);
});
}
function buildChartFromData(data) {
let layout = DataCrawler.getLayoutFor(data)
console.log(layout)
if(layout === "undecidable") {
$("#content").text("Result is not a supported timeseries format. Sorry.");
} else {
let xName, sampleLocation
let xSplit = layout.x.split('.')
let keyNames = []
$("#debug").text(layout.x + " " + layout.keys + " " + layout.type);
sampleLocation = data
// Fast forward sample location to the data array.
for(let i = 0; i < xSplit.length -2; i++) {
sampleLocation = sampleLocation[xSplit[i]]
}
// Use the last name as the key for the x axis.
xName = xSplit[xSplit.length - 1]
// Go through each of the keys and extract their names for c3
layout.keys.forEach((key) => {
let locSplit = key.split('.')
let keyName = locSplit[locSplit.length - 1]
keyNames.push(keyName)
})
let samples = sampleLocation.map((sample) => {
// Translate dates to javascript date objects (Helps c3)
if(layout.type === "timeseries") {
sample[xName] = new Date(sample[xName])
}
return sample
})
let chartConfig = {
data: {
x: 'x',
keys: {
x: xName,
value: keyNames
},
json: samples
},
axis: {
x: {
type: layout.type,
tick: {
format: '%Y-%m-%d'
}
}
},
bindto: $("#content")[0]
}
$("#debug").text("xName: " + xName + " keyNames : " + keyNames);
let chart = c3.generate(chartConfig);
}
}
generateChart();
});