Преглед изворни кода

Frontend features

- Allow selection of ranges
- Allow clicking on a category pie slice to filter transactions
pull/1/head
Chris Smith пре 13 година
родитељ
комит
d67c3ea405
1 измењених фајлова са 85 додато и 43 уклоњено
  1. 85
    43
      index.html

+ 85
- 43
index.html Прегледај датотеку

@@ -5,6 +5,7 @@
5 5
   <script src="externals/flot/jquery.js" type="text/javascript"></script>
6 6
   <script src="externals/flot/jquery.flot.js" type="text/javascript"></script>
7 7
   <script src="externals/flot/jquery.flot.pie.js" type="text/javascript"></script>
8
+  <script src="externals/flot/jquery.flot.selection.js" type="text/javascript"></script>
8 9
   <script src="data.php" type="text/javascript"></script>
9 10
   <script type="text/javascript">
10 11
    var previousPoint = null;
@@ -22,6 +23,75 @@
22 23
     }).appendTo("body").fadeIn(200);
23 24
    }
24 25
 
26
+   function getDateKey(date) {
27
+    return date.getFullYear() + '-' + (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1);
28
+   }
29
+
30
+   function showSelectedMonths(start, end, incoming, outgoing) {
31
+    $('#historytable tr.data').remove();
32
+    $('#historytable').show();
33
+
34
+    var startDate = new Date(start), endDate = new Date(end);
35
+
36
+    if (startDate.getDate() > 1) {
37
+     startDate.setDate(1);
38
+     startDate.getMonth() == 11 && startDate.setYear(startDate.getFullYear() + 1);
39
+     startDate.setMonth(startDate.getMonth() == 11 ? 0 : startDate.getMonth() + 1);
40
+    }
41
+
42
+    var startKey = getDateKey(startDate), endKey = getDateKey(endDate);
43
+
44
+    if (startKey == endKey) {
45
+     $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + ' ' + startDate.getFullYear());
46
+    } else if (startDate.getFullYear() == endDate.getFullYear()) {
47
+     $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + '-' + months[endDate.getMonth()] + ' ' + startDate.getFullYear());
48
+    } else {
49
+     $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + ' ' + startDate.getFullYear() + ' - ' +  months[endDate.getMonth()] + ' ' + endDate.getFullYear());
50
+    }
51
+
52
+    var pieData = {};
53
+    var table = $('#historytable table');
54
+    var include = false;
55
+    $.each(data, function(month, monthData) {
56
+     if (month == startKey) { include = true; }
57
+
58
+     if (include) {
59
+      $.each(monthData, function(index, trans) {
60
+       if (incoming != trans.Amount > 0) { return; }
61
+
62
+       var category = trans.Category ? trans.Category : 'Unsorted';
63
+
64
+       var tr = $('<tr/>').addClass('data').addClass('category' + category.replace(/[^a-zA-Z]*/g, '')).appendTo(table);
65
+ 
66
+       $('<td/>').text(trans.Date.date.split(' ')[0]).appendTo(tr);
67
+       $('<td/>').text(trans.Type ? trans.Type : 'Other').appendTo(tr);
68
+       $('<td/>').text(trans.Category ? trans.Category : '').appendTo(tr);
69
+       $('<td/>').text(trans.Description).appendTo(tr);
70
+       $('<td/>').text(trans.Amount).appendTo(tr);
71
+ 
72
+       if (category != '(Ignored)') {
73
+        if (!pieData[category]) { pieData[category] = 0; }
74
+        pieData[category] += Math.abs(trans.Amount);
75
+       }
76
+      });
77
+     }
78
+
79
+     if (month == endKey) { include = false; }
80
+    });
81
+
82
+    var seriesData = [];
83
+    $.each(pieData, function(category, amount) {
84
+     seriesData.push({ label: category + ' (' + Math.round(amount) + ')', data: amount });
85
+    });
86
+
87
+    seriesData.sort(function(a, b) { return b.data - a.data; });
88
+
89
+    $.plot($('#expense'), seriesData, {
90
+      series: { pie: { show: true, innerRadius: 0.5, highlight: { opacity: 0.5 } } },
91
+      grid: { clickable: true }
92
+    });
93
+   }
94
+
25 95
    $(function() {
26 96
     var transData = [{label: 'Income', data: []}, {label: 'Expense', data: []}];
27 97
 
@@ -47,6 +117,7 @@
47 117
         points: { show: true }
48 118
       },
49 119
       grid: { hoverable: true, clickable: true },
120
+      selection: { mode : "x" }
50 121
     });
51 122
 
52 123
     $("#history").bind("plothover", function (event, pos, item) {
@@ -71,56 +142,27 @@
71 142
      }
72 143
     });
73 144
 
145
+    $('#history').bind('plotselected', function(event, ranges) {
146
+     var startDate = parseInt(ranges.xaxis.from.toFixed());
147
+     var endDate = parseInt(ranges.xaxis.to.toFixed());
148
+    
149
+     showSelectedMonths(startDate, endDate, 0); 
150
+    });
151
+
74 152
     $('#history').bind('plotclick', function(event, pos, item) {
75 153
      if (item) {
76
-      var incoming = item.seriesIndex == 0;
77
-      var dateOb = new Date(item.datapoint[0]);
78
-      var date = dateOb.getFullYear() + '-' + (dateOb.getMonth() < 9 ? '0' : '') + (dateOb.getMonth() + 1);
79
-
80
-      $('#historytable tr.data').remove();
81
-      $('#historytable').show();
82
-
83
-      $('#historytable h3').text('Transactions for ' + months[dateOb.getMonth()] + ' ' + dateOb.getFullYear());
84
-
85
-      var pieData = {};
86
-      var table = $('#historytable table');
87
-      $.each(data[date], function(index, trans) {
88
-       if (incoming != trans.Amount > 0) { return; }
89
-
90
-       var tr = $('<tr/>').addClass('data').appendTo(table);
91
-
92
-       trans.Category && trans.Category == '(Ignored)' && tr.addClass('ignored');
93
-       
94
-       $('<td/>').text(trans.Date.date.split(' ')[0]).appendTo(tr);
95
-       $('<td/>').text(trans.Type ? trans.Type : 'Other').appendTo(tr);
96
-       $('<td/>').text(trans.Category ? trans.Category : '').appendTo(tr);
97
-       $('<td/>').text(trans.Description).appendTo(tr);
98
-       $('<td/>').text(trans.Amount).appendTo(tr);
99
-
100
-       var category = trans.Category ? trans.Category : 'Unsorted';
101
-
102
-       if (category != '(Ignored)') {
103
-        if (!pieData[category]) { pieData[category] = 0; }
104
-        pieData[category] += Math.abs(trans.Amount);
105
-       }
106
-      });
107
-
108
-      var seriesData = [];
109
-      $.each(pieData, function(category, amount) {
110
-       seriesData.push({ label: category + ' (' + Math.round(amount) + ')', data: amount });
111
-      });
112
-
113
-      seriesData.sort(function(a, b) { return b.data - a.data; });
114
-
115
-      $.plot($('#expense'), seriesData, {
116
-        series: { pie: { show: true, innerRadius: 0.5 } }
117
-      });
154
+      showSelectedMonths(item.datapoint[0], item.datapoint[0], item.seriesIndex == 0, item.seriesIndex == 1);
118 155
      }
119 156
     });
157
+
158
+    $('#expense').bind('plotclick', function(event, pos, item) {
159
+     $('#historytable .data').hide();
160
+     $('#historytable .category' + item.series.label.replace(/[^a-zA-Z]*/g, '')).show();
161
+    });
120 162
    });
121 163
   </script>
122 164
   <style type="text/css">
123
-   .ignored td { color: gray; }
165
+   .categoryIgnored td { color: gray; }
124 166
    .left { width: 45%; float: left; }
125 167
    .right { margin-left: 50%; }
126 168
    h2 { margin-top: 0; }

Loading…
Откажи
Сачувај