averages.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // vim: expandtab shiftwidth=4:
  2. var httpclient = function() {
  3. this.get = function(url, callback) {
  4. var req = new XMLHttpRequest();
  5. req.onreadystatechange = function () {
  6. if (req.readyState == 4 && req.status == 200) {
  7. callback(req.responseText)
  8. }
  9. }
  10. req.open('GET', url, true);
  11. req.send(null);
  12. }
  13. }
  14. var render_graph = function(url, chartid) {
  15. var client = new httpclient();
  16. client.get(url, function(jsondata) {
  17. var ctx = document.getElementById(chartid).getContext('2d');
  18. if (! ctx) {
  19. console.log('Unable to find chart element ' + chartid);
  20. return;
  21. }
  22. var data = JSON.parse(jsondata);
  23. var graphdata = {
  24. labels: data.labels,
  25. datasets: [{
  26. borderColor: 'rgba(255, 140, 51, 1)',
  27. fill : false,
  28. pointRadius: 2,
  29. label : 'Min Temperature',
  30. data : data.min_temp
  31. }, {
  32. borderColor: 'rgba(255, 140, 151, 1)',
  33. fill : '-1',
  34. pointRadius: 2,
  35. label : 'Max Temperature',
  36. data : data.max_temp
  37. }, {
  38. borderColor: 'rgba(0, 140, 255, 1)',
  39. fill : false,
  40. label : 'Avg Humidity',
  41. pointRadius: 2,
  42. data : data.avg_humidity
  43. }]
  44. };
  45. var chart = new Chart(ctx, {
  46. type: 'line',
  47. data: graphdata,
  48. options: {
  49. elements: {
  50. line: {
  51. //tension: 0, // disable bezier curves
  52. }
  53. },
  54. legend: {
  55. display: true,
  56. labels: {
  57. fontColor: 'rgb(5, 9, 3)'
  58. }
  59. },
  60. scales: {
  61. xAxes: [{
  62. type: 'time',
  63. distribution: 'linear',
  64. }]
  65. }
  66. }
  67. });
  68. });
  69. }