kasownik-plot/js/plot.js

81 lines
2.1 KiB
JavaScript
Raw Normal View History

2013-08-07 14:09:02 +00:00
window.onload = function() {
2013-08-21 20:39:22 +00:00
var required = [], paid = [], influx = [];
2013-08-07 14:09:02 +00:00
var today = new Date(),
year = 1900 + today.getYear(),
2013-08-21 20:39:22 +00:00
month = today.getMonth() + 1,
2013-09-23 14:47:58 +00:00
urlBase = 'https://kasownik.hackerspace.pl/api/',
modified;
2013-08-07 14:09:02 +00:00
for(i = 0; i < 12; ++i) {
var xhr = new XMLHttpRequest();
2013-08-21 20:39:22 +00:00
xhr.open("GET", urlBase + 'month/'+ year + '/' + month + '.json', false);
2013-08-07 14:09:02 +00:00
xhr.send();
2013-09-23 14:47:58 +00:00
var data = JSON.parse(xhr.response),
res = data.content,
date = new Date(year, month, 1);
modified = modified || data.modified;
2013-08-07 14:09:02 +00:00
required.unshift({ x: date.getTime() / 1000, y: res.required });
paid.unshift({ x: date.getTime() / 1000, y: res.paid });
2013-08-21 20:39:22 +00:00
xhr = new XMLHttpRequest();
xhr.open("GET", urlBase + 'cashflow/'+ year + '/' + month + '.json', false);
xhr.send();
res = JSON.parse(xhr.response).content,
influx.unshift({ x: date.getTime() / 1000, y: res.in });
2013-08-07 14:09:02 +00:00
month -= 1;
if(month == 0) {
month = 12;
year -= 1;
}
}
console.log(required, paid);
2013-09-23 14:47:58 +00:00
var lastmod = document.getElementById("lastmod");
lastmod.innerHTML = modified;
2013-08-21 20:39:22 +00:00
var palette = new Rickshaw.Color.Palette( { scheme: 'munin' } );
2013-08-07 14:09:02 +00:00
var graph = new Rickshaw.Graph({
element: document.getElementById("plot"),
width: 800,
height: 600,
renderer: 'line',
stroke: true,
series: [
{
color: palette.color(),
data: required,
name: 'Required',
},
{
color: palette.color(),
data: paid,
name: 'Paid',
2013-08-21 20:39:22 +00:00
},
{
color: palette.color(),
data: influx,
name: 'Cash Influx',
},
2013-08-07 14:09:02 +00:00
]
});
graph.render();
var yAxis = new Rickshaw.Graph.Axis.Y({
graph: graph,
});
yAxis.render();
var xAxis = new Rickshaw.Graph.Axis.Time({
graph: graph,
});
xAxis.render();
var legend = new Rickshaw.Graph.Legend({
element: document.getElementById("legend"),
graph: graph,
});
var hoverDetail = new Rickshaw.Graph.HoverDetail( {
graph: graph,
xFormatter: function(x) {
var date = new Date(x * 1000);
return (1900 + date.getYear()) + '/' + date.getMonth();
}
});
}