|
| 1 | +// /** |
| 2 | +// * Created by gkarak on 23/11/2016. |
| 3 | +// */ |
| 4 | + |
| 5 | +angular |
| 6 | + .module('gaDashboardGraphFlot') |
| 7 | + .component('gaDashboardGraphFlot', { |
| 8 | + templateUrl: 'static/ng-gentelella/ga-dashboard-graph-flot/ga-dashboard-graph-flot.template.html', |
| 9 | + bindings: { |
| 10 | + graphTitle: '@', |
| 11 | + graphSubTitle: '@', |
| 12 | + graphRange: '@', |
| 13 | + graphId: '@', |
| 14 | + graphLegendTitle: '@', |
| 15 | + graphData: '<' |
| 16 | + }, |
| 17 | + transclude: true, |
| 18 | + controller: [ |
| 19 | + function GaGraphFlotController() { |
| 20 | + var self = this; |
| 21 | + |
| 22 | + // Initialise |
| 23 | + self.$onInit = function () { |
| 24 | + if (!self.graphId) self.graphId = 'main-graph'; |
| 25 | + self.plotted = false; |
| 26 | + }; |
| 27 | + |
| 28 | + // Run on every digest cycle |
| 29 | + // The only suitable event, because the id in template is set after any other event |
| 30 | + // This is why we call plot only once with self.plotted |
| 31 | + self.$doCheck = function () { |
| 32 | + var canvas = $('.' + self.graphId); |
| 33 | + |
| 34 | + if (!self.plotted && self.graphData && canvas.length) { |
| 35 | + |
| 36 | + // Transform mongo data to flot data |
| 37 | + var data = []; |
| 38 | + self.graphData.forEach(function (row) { |
| 39 | + var series = []; |
| 40 | + row.forEach(function (val) { |
| 41 | + series.push([ |
| 42 | + gd(val._id.year, val._id.month, val._id.day), |
| 43 | + val.count |
| 44 | + ]); |
| 45 | + }); |
| 46 | + data.push(series); |
| 47 | + }); |
| 48 | + |
| 49 | + function gd(year, month, day) { |
| 50 | + return new Date(year, month - 1, day + 1).getTime(); |
| 51 | + } |
| 52 | + |
| 53 | + // PLOT |
| 54 | + // !self.plotted && self.graphData && canvas.length && $.plot(canvas, self.graphData, { |
| 55 | + $.plot(canvas, data, { |
| 56 | + series: { |
| 57 | + lines: { |
| 58 | + show: false, |
| 59 | + fill: true |
| 60 | + }, |
| 61 | + splines: { |
| 62 | + show: true, |
| 63 | + tension: 0.4, |
| 64 | + lineWidth: 1, |
| 65 | + fill: 0.4 |
| 66 | + }, |
| 67 | + points: { |
| 68 | + radius: 0, |
| 69 | + show: true |
| 70 | + }, |
| 71 | + shadowSize: 2 |
| 72 | + }, |
| 73 | + grid: { |
| 74 | + verticalLines: true, |
| 75 | + hoverable: true, |
| 76 | + clickable: true, |
| 77 | + tickColor: "#d5d5d5", |
| 78 | + borderWidth: 1, |
| 79 | + color: '#fff' |
| 80 | + }, |
| 81 | + colors: ["rgba(38, 185, 154, 0.38)", "rgba(3, 88, 106, 0.38)"], |
| 82 | + xaxis: { |
| 83 | + tickColor: "rgba(51, 51, 51, 0.06)", |
| 84 | + mode: "time", |
| 85 | + tickSize: [1, "day"], |
| 86 | + //tickLength: 10, |
| 87 | + axisLabel: "Date", |
| 88 | + axisLabelUseCanvas: true, |
| 89 | + axisLabelFontSizePixels: 12, |
| 90 | + axisLabelFontFamily: 'Verdana, Arial', |
| 91 | + axisLabelPadding: 10 |
| 92 | + }, |
| 93 | + yaxis: { |
| 94 | + ticks: 8, |
| 95 | + tickColor: "rgba(51, 51, 51, 0.06)" |
| 96 | + }, |
| 97 | + tooltip: false |
| 98 | + }); |
| 99 | + |
| 100 | + self.plotted = true; |
| 101 | + } |
| 102 | + }; |
| 103 | + } |
| 104 | + ] |
| 105 | + }); |
0 commit comments