@cryptotaxi247 / netdata-1 / commits / f7df0c7b8

cleanup; added support for loading charts into other sites

Costa Tsaousis (ktsaou) committed Apr 3, 2014 at 02:36 UTC f7df0c7b8be88c76d7f923461d23bd1d37e6f056
2 files changed +5 -106
web/index.html
+1 -1
@@ -139,7 +139,7 @@
139 var width = thumbWidth();
140 var height = TARGET_THUMB_GRAPH_HEIGHT;
141
142 - loadCharts(function(c) {
142 + loadCharts(null, function(c) {
143 mycharts = c;
144
145 if(mycharts == null || mycharts.length == 0) {
web/netdata.js
+4 -105
@@ -16,6 +16,7 @@ function canChartBeRefreshed(chart) {
16 if((now - chart.last_updated) < (chart.group * chart.update_every * 1000)) return false;
17
18 // is the chart in the visible area?
19 + //console.log(chart.div);
20 if($('#' + chart.div).visible(true) == false) return false;
21
22 // ok, do it
@@ -78,9 +79,9 @@ function refreshChart(chart, doNext) {
79 // returns an array of objects, containing all the server metadata
80 // (not the values of the graphs - just the info about the graphs)
81
81 -function loadCharts(doNext) {
82 +function loadCharts(base_url, doNext) {
83 $.ajax({
83 - url: '/all.json',
84 + url: ((base_url)?base_url:'') + '/all.json',
85 dataType: 'json',
86 cache: false
87 }).done(function(json) {
@@ -91,6 +92,7 @@ function loadCharts(doNext) {
92
93 // make sure we have the proper values
94 if(!json.charts[i].update_every) chart.update_every = 1;
95 + if(base_url) json.charts[i].url = base_url + json.charts[i].url;
96
97 json.charts[i].last_updated = 0;
98 json.charts[i].thumbnail = false;
@@ -223,106 +225,3 @@ function loadCharts(doNext) {
225 if(typeof doNext == "function") doNext(json.charts);
226 });
227 };
226 -
227 -var charts = new Array();
228 -function addChart(name, div, width, height, jsonurl, title, vtitle) {
229 - var i = charts.length;
230 -
231 - console.log('Creating new objects for chart ' + name);
232 - charts[i] = [];
233 -
234 - charts[i].name = name;
235 - charts[i].title = title;
236 - charts[i].vtitle = vtitle;
237 - charts[i].url = jsonurl;
238 -
239 - charts[i].div = div;
240 -
241 - charts[i].refreshCount = 0;
242 - charts[i].update_every = 1;
243 - charts[i].last_updated = 0;
244 - charts[i].enabled = true;
245 - charts[i].group = 1;
246 -
247 - charts[i].chart = null;
248 - charts[i].jsondata = null;
249 - charts[i].datatable = null;
250 -
251 - charts[i].chartType = "AreaChart";
252 - charts[i].chartOptions = {
253 - width: width,
254 - height: height,
255 - title: charts[i].title,
256 - hAxis: {title: "Time of Day"},
257 - vAxis: {title: charts[i].vtitle, minValue: 10},
258 - focusTarget: 'category',
259 - };
260 -}
261 -
262 -var charts_last_drawn = 999999999;
263 -function refreshCharts(howmany) {
264 -
265 - if(charts.length == 0) return;
266 -
267 - var h = howmany;
268 - if(h == 0) h = charts.length;
269 - if(h > charts.length) h = charts.length;
270 - //console.log('Will run for ' + h + ' charts');
271 -
272 - var width = Math.round(Math.sqrt(charts.length));
273 - var height = Math.round(Math.sqrt(charts.length));
274 - while((width * height) < charts.length) {
275 - if((height + 1) <= width) height++;
276 - else width++;
277 - }
278 - // console.log('all: ' + charts.length + ', optimal: width = ' + width + ', height = ' + height);
279 -
280 - while(width > 1 && width >= height) {
281 - width--;
282 - height++;
283 - }
284 - if(width * height < charts.length) height++;
285 - // console.log('final: width = ' + width + ', height = ' + height);
286 -
287 - ww = (window.innerWidth < document.documentElement.clientWidth)?window.innerWidth:document.documentElement.clientWidth;
288 - wh = (window.innerHeight < document.documentElement.clientHeight)?window.innerHeight:document.documentElement.clientHeight;
289 -
290 - if(width == 0) width = (ww - 40) / 2;
291 - if(width <= 10) width = (ww - 40) / width;
292 - if(width < 200) width = 200;
293 -
294 - if(height == 0) height = (wh - 20) / 2;
295 - if(height <= 10) height = (wh - 20) / height;
296 - if(height < 100) height = 100;
297 -
298 - // console.log('width = ' + width + ', height = ' + height);
299 -
300 - var i;
301 - for(i = 0; i < h; i++) {
302 - var zeroDimensions = 0;
303 -
304 - charts_last_drawn++;
305 - if(charts_last_drawn >= charts.length) charts_last_drawn = 0;
306 -
307 - if(charts[charts_last_drawn].chartOptions.width == 0 && charts[charts_last_drawn].height == 0) {
308 - charts[charts_last_drawn].chartOptions.width = width;
309 - charts[charts_last_drawn].chartOptions.height = height;
310 - zeroDimensions = 1;
311 - }
312 -
313 - try {
314 -
315 - console.log('Refreshing chart ' + charts[charts_last_drawn].name);
316 - refreshChart(charts[charts_last_drawn]);
317 - }
318 - catch(err) {
319 - console.log('Cannot refresh chart for ' + charts[charts_last_drawn].url);
320 - }
321 -
322 - if(zeroDimensions == 1) {
323 - charts[charts_last_drawn].chartOptions.width = 0;
324 - charts[charts_last_drawn].chartOptions.height = 0;
325 - }
326 - }
327 - return 0;
328 -}