Selaa lähdekoodia

Move JS into its own file

Add alternating row colours to transaction table
master
Chris Smith 13 vuotta sitten
vanhempi
commit
e98ef971e0
2 muutettua tiedostoa jossa 182 lisäystä ja 172 poistoa
  1. 177
    0
      analyser.js
  2. 5
    172
      index.html

+ 177
- 0
analyser.js Näytä tiedosto

@@ -0,0 +1,177 @@
1
+var previousPoint = null;
2
+var plots = {};
3
+var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
4
+
5
+function colourTableRows(table) {
6
+ $('tr', table).removeClass('alt');
7
+ $('tr:visible:even', table).addClass('alt');
8
+}
9
+
10
+function showTooltip(x, y, contents) {
11
+ $('<div id="tooltip">' + contents + '</div>').css( {
12
+  position: 'absolute',
13
+  display: 'none',
14
+  top: y + 5,
15
+  left: x + 5,
16
+  border: '1px solid #fdd',
17
+  padding: '2px',
18
+  'background-color': '#fee',
19
+ }).appendTo("body").fadeIn(200);
20
+}
21
+
22
+function getDateKey(date) {
23
+ return date.getFullYear() + '-' + (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1);
24
+}
25
+
26
+function showSelectedMonths(start, end, incoming, outgoing) {
27
+ $('#historytable tr.data').remove();
28
+ $('#historytable').show();
29
+
30
+ var startDate = new Date(start), endDate = new Date(end);
31
+
32
+ if (startDate.getDate() > 1) {
33
+  startDate.setDate(1);
34
+  startDate.getMonth() == 11 && startDate.setYear(startDate.getFullYear() + 1);
35
+  startDate.setMonth(startDate.getMonth() == 11 ? 0 : startDate.getMonth() + 1);
36
+ }
37
+
38
+ var startKey = getDateKey(startDate), endKey = getDateKey(endDate);
39
+
40
+ if (startKey == endKey) {
41
+  $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + ' ' + startDate.getFullYear());
42
+ } else if (startDate.getFullYear() == endDate.getFullYear()) {
43
+  $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + '-' + months[endDate.getMonth()] + ' ' + startDate.getFullYear());
44
+ } else {
45
+  $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + ' ' + startDate.getFullYear() + ' - ' +  months[endDate.getMonth()] + ' ' + endDate.getFullYear());
46
+ }
47
+
48
+ var pieData = {};
49
+ var table = $('#historytable table');
50
+ var include = false;
51
+ $.each(data, function(month, monthData) {
52
+  if (month == startKey) { include = true; }
53
+
54
+  if (include) {
55
+   $.each(monthData, function(index, trans) {
56
+    if (incoming != trans.Amount > 0) { return; }
57
+
58
+    var category = trans.Category ? trans.Category : 'Unsorted';
59
+
60
+    var tr = $('<tr/>').addClass('data').addClass('category' + category.replace(/[^a-zA-Z]*/g, '')).appendTo(table);
61
+ 
62
+    $('<td/>').text(trans.Date.date.split(' ')[0]).appendTo(tr);
63
+    $('<td/>').text(trans.Type ? trans.Type : 'Other').appendTo(tr);
64
+    $('<td/>').text(trans.Category ? trans.Category : '').appendTo(tr);
65
+    $('<td/>').text(trans.Description).appendTo(tr);
66
+    $('<td/>').text(trans.Amount).appendTo(tr);
67
+ 
68
+    if (category != '(Ignored)') {
69
+     if (!pieData[category]) { pieData[category] = 0; }
70
+     pieData[category] += Math.abs(trans.Amount);
71
+    }
72
+   });
73
+  }
74
+
75
+  if (month == endKey) { include = false; }
76
+ });
77
+ colourTableRows(table);
78
+
79
+ var seriesData = [];
80
+ $.each(pieData, function(category, amount) {
81
+  seriesData.push({ label: category + ' (' + Math.round(amount) + ')', data: amount });
82
+ });
83
+
84
+ seriesData.sort(function(a, b) { return b.data - a.data; });
85
+
86
+ plots.expense = $.plot($('#expense'), seriesData, {
87
+   series: { pie: { show: true, innerRadius: 0.5, highlight: { opacity: 0.5 } } },
88
+   grid: { clickable: true }
89
+ });
90
+}
91
+
92
+$(function() {
93
+ var transData = [{label: 'Income', data: []}, {label: 'Expense', data: []}, {label: 'Difference', data: []}];
94
+ var min = new Date().getTime(), max = 0;
95
+
96
+ $.each(data, function(month, entries) {
97
+  var split = month.split('-');
98
+  var timestamp = new Date(split[0], split[1] - 1).getTime();
99
+  var sum = [0, 0];
100
+
101
+  $.each(entries, function() {
102
+   if (this.Category == '(Ignored)') { return; }
103
+
104
+   sum[this.Amount < 0 ? 1 : 0] += this.Amount;
105
+  });
106
+
107
+  transData[0].data.push([timestamp, sum[0]]);
108
+  transData[1].data.push([timestamp, sum[1]]);
109
+  transData[2].data.push([timestamp, sum[0] + sum[1]]);
110
+  min = Math.min(min, timestamp);
111
+  max = Math.max(max, timestamp);
112
+ }); 
113
+
114
+ plots.history = $.plot($('#history'), transData, {
115
+   xaxis: { mode: 'time', timeformat: '%y/%m'},
116
+   series: {
117
+     lines: { show: true, fill: true },
118
+     points: { show: true }
119
+   },
120
+   grid: {
121
+     hoverable: true,
122
+     clickable: true,
123
+     markings: [{ color: '#000', lineWidth: 1, xaxis: { from: min, to: max }, yaxis: { from: 0, to: 0 } }]
124
+   },
125
+   selection: { mode : "x" }
126
+ });
127
+
128
+ $("#history").bind("plothover", function (event, pos, item) {
129
+  if (item) {
130
+   var id = {dataIndex: item.dataIndex, seriesIndex: item.seriesIndex};
131
+
132
+   if (previousPoint == null || previousPoint.dataIndex != id.dataIndex || previousPoint.seriesIndex != id.seriesIndex) {
133
+    previousPoint = id;
134
+             
135
+    $("#tooltip").remove();
136
+    var x = item.datapoint[0],
137
+        y = item.datapoint[1].toFixed(2);
138
+                 
139
+    var date = new Date(x);
140
+
141
+    var seriesTitles = ["Money in", "Money out", "Balance change"];
142
+    showTooltip(item.pageX, item.pageY, (seriesTitles[item.seriesIndex]) + " during " + months[date.getMonth()] + " " + date.getFullYear() + " = " + y);
143
+   }
144
+  } else {
145
+   $("#tooltip").remove();
146
+   previousPoint = null;            
147
+  }
148
+ });
149
+
150
+ $('#history').bind('plotselected', function(event, ranges) {
151
+  var startDate = parseInt(ranges.xaxis.from.toFixed());
152
+  var endDate = parseInt(ranges.xaxis.to.toFixed());
153
+ 
154
+  $.history.load('start:' + startDate + ';end:' + endDate + ';type:expenses'); 
155
+ });
156
+
157
+ $('#history').bind('plotclick', function(event, pos, item) {
158
+  if (item) {
159
+   $.history.load('start:' + item.datapoint[0] + ';end:' + item.datapoint[0] + ';type:' + (item.seriesIndex == 0 ? 'income' : 'expenses'));
160
+  }
161
+ });
162
+
163
+ $('#expense').bind('plotclick', function(event, pos, item) {
164
+  $('#historytable .data').hide();
165
+  $('#historytable .category' + item.series.label.replace(/[^a-zA-Z]*/g, '')).show();
166
+  colourTableRows($('#historytable'));
167
+ });
168
+
169
+ $.history.init(function(hash) {
170
+  var match = /start:([0-9]+);end:([0-9]+);type:(income|expenses)/.exec(hash);
171
+
172
+  if (match != null) {
173
+   showSelectedMonths(parseInt(match[1]), parseInt(match[2]), match[3] == 'income', match[3] == 'expenses');
174
+   plots.history.setSelection({ xaxis: { from: parseInt(match[1]), to: parseInt(match[2]) }});
175
+  }
176
+ });
177
+});

+ 5
- 172
index.html Näytä tiedosto

@@ -8,178 +8,7 @@
8 8
   <script src="externals/flot/jquery.flot.selection.js" type="text/javascript"></script>
9 9
   <script src="externals/jquery.history/jquery.history.js" type="text/javascript"></script>
10 10
   <script src="data.php" type="text/javascript"></script>
11
-  <script type="text/javascript">
12
-   var previousPoint = null;
13
-   var plots = {};
14
-   var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
15
-
16
-   function showTooltip(x, y, contents) {
17
-    $('<div id="tooltip">' + contents + '</div>').css( {
18
-     position: 'absolute',
19
-     display: 'none',
20
-     top: y + 5,
21
-     left: x + 5,
22
-     border: '1px solid #fdd',
23
-     padding: '2px',
24
-     'background-color': '#fee',
25
-    }).appendTo("body").fadeIn(200);
26
-   }
27
-
28
-   function getDateKey(date) {
29
-    return date.getFullYear() + '-' + (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1);
30
-   }
31
-
32
-   function showSelectedMonths(start, end, incoming, outgoing) {
33
-    $('#historytable tr.data').remove();
34
-    $('#historytable').show();
35
-
36
-    var startDate = new Date(start), endDate = new Date(end);
37
-
38
-    if (startDate.getDate() > 1) {
39
-     startDate.setDate(1);
40
-     startDate.getMonth() == 11 && startDate.setYear(startDate.getFullYear() + 1);
41
-     startDate.setMonth(startDate.getMonth() == 11 ? 0 : startDate.getMonth() + 1);
42
-    }
43
-
44
-    var startKey = getDateKey(startDate), endKey = getDateKey(endDate);
45
-
46
-    if (startKey == endKey) {
47
-     $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + ' ' + startDate.getFullYear());
48
-    } else if (startDate.getFullYear() == endDate.getFullYear()) {
49
-     $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + '-' + months[endDate.getMonth()] + ' ' + startDate.getFullYear());
50
-    } else {
51
-     $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + ' ' + startDate.getFullYear() + ' - ' +  months[endDate.getMonth()] + ' ' + endDate.getFullYear());
52
-    }
53
-
54
-    var pieData = {};
55
-    var table = $('#historytable table');
56
-    var include = false;
57
-    $.each(data, function(month, monthData) {
58
-     if (month == startKey) { include = true; }
59
-
60
-     if (include) {
61
-      $.each(monthData, function(index, trans) {
62
-       if (incoming != trans.Amount > 0) { return; }
63
-
64
-       var category = trans.Category ? trans.Category : 'Unsorted';
65
-
66
-       var tr = $('<tr/>').addClass('data').addClass('category' + category.replace(/[^a-zA-Z]*/g, '')).appendTo(table);
67
- 
68
-       $('<td/>').text(trans.Date.date.split(' ')[0]).appendTo(tr);
69
-       $('<td/>').text(trans.Type ? trans.Type : 'Other').appendTo(tr);
70
-       $('<td/>').text(trans.Category ? trans.Category : '').appendTo(tr);
71
-       $('<td/>').text(trans.Description).appendTo(tr);
72
-       $('<td/>').text(trans.Amount).appendTo(tr);
73
- 
74
-       if (category != '(Ignored)') {
75
-        if (!pieData[category]) { pieData[category] = 0; }
76
-        pieData[category] += Math.abs(trans.Amount);
77
-       }
78
-      });
79
-     }
80
-
81
-     if (month == endKey) { include = false; }
82
-    });
83
-
84
-    var seriesData = [];
85
-    $.each(pieData, function(category, amount) {
86
-     seriesData.push({ label: category + ' (' + Math.round(amount) + ')', data: amount });
87
-    });
88
-
89
-    seriesData.sort(function(a, b) { return b.data - a.data; });
90
-
91
-    plots.expense = $.plot($('#expense'), seriesData, {
92
-      series: { pie: { show: true, innerRadius: 0.5, highlight: { opacity: 0.5 } } },
93
-      grid: { clickable: true }
94
-    });
95
-   }
96
-
97
-   $(function() {
98
-    var transData = [{label: 'Income', data: []}, {label: 'Expense', data: []}, {label: 'Difference', data: []}];
99
-    var min = new Date().getTime(), max = 0;
100
-
101
-    $.each(data, function(month, entries) {
102
-     var split = month.split('-');
103
-     var timestamp = new Date(split[0], split[1] - 1).getTime();
104
-     var sum = [0, 0];
105
-
106
-     $.each(entries, function() {
107
-      if (this.Category == '(Ignored)') { return; }
108
-
109
-      sum[this.Amount < 0 ? 1 : 0] += this.Amount;
110
-     });
111
-
112
-     transData[0].data.push([timestamp, sum[0]]);
113
-     transData[1].data.push([timestamp, sum[1]]);
114
-     transData[2].data.push([timestamp, sum[0] + sum[1]]);
115
-     min = Math.min(min, timestamp);
116
-     max = Math.max(max, timestamp);
117
-    }); 
118
-
119
-    plots.history = $.plot($('#history'), transData, {
120
-      xaxis: { mode: 'time', timeformat: '%y/%m'},
121
-      series: {
122
-        lines: { show: true, fill: true },
123
-        points: { show: true }
124
-      },
125
-      grid: {
126
-        hoverable: true,
127
-        clickable: true,
128
-        markings: [{ color: '#000', lineWidth: 1, xaxis: { from: min, to: max }, yaxis: { from: 0, to: 0 } }]
129
-      },
130
-      selection: { mode : "x" }
131
-    });
132
-
133
-    $("#history").bind("plothover", function (event, pos, item) {
134
-     if (item) {
135
-      var id = {dataIndex: item.dataIndex, seriesIndex: item.seriesIndex};
136
-
137
-      if (previousPoint == null || previousPoint.dataIndex != id.dataIndex || previousPoint.seriesIndex != id.seriesIndex) {
138
-       previousPoint = id;
139
-                
140
-       $("#tooltip").remove();
141
-       var x = item.datapoint[0],
142
-           y = item.datapoint[1].toFixed(2);
143
-                    
144
-       var date = new Date(x);
145
-
146
-       var seriesTitles = ["Money in", "Money out", "Balance change"];
147
-       showTooltip(item.pageX, item.pageY, (seriesTitles[item.seriesIndex]) + " during " + months[date.getMonth()] + " " + date.getFullYear() + " = " + y);
148
-      }
149
-     } else {
150
-      $("#tooltip").remove();
151
-      previousPoint = null;            
152
-     }
153
-    });
154
-
155
-    $('#history').bind('plotselected', function(event, ranges) {
156
-     var startDate = parseInt(ranges.xaxis.from.toFixed());
157
-     var endDate = parseInt(ranges.xaxis.to.toFixed());
158
-    
159
-     $.history.load('start:' + startDate + ';end:' + endDate + ';type:expenses'); 
160
-    });
161
-
162
-    $('#history').bind('plotclick', function(event, pos, item) {
163
-     if (item) {
164
-      $.history.load('start:' + item.datapoint[0] + ';end:' + item.datapoint[0] + ';type:' + (item.seriesIndex == 0 ? 'income' : 'expenses'));
165
-     }
166
-    });
167
-
168
-    $('#expense').bind('plotclick', function(event, pos, item) {
169
-     $('#historytable .data').hide();
170
-     $('#historytable .category' + item.series.label.replace(/[^a-zA-Z]*/g, '')).show();
171
-    });
172
-
173
-    $.history.init(function(hash) {
174
-     var match = /start:([0-9]+);end:([0-9]+);type:(income|expenses)/.exec(hash);
175
-
176
-     if (match != null) {
177
-      showSelectedMonths(parseInt(match[1]), parseInt(match[2]), match[3] == 'income', match[3] == 'expenses');
178
-      plots.history.setSelection({ xaxis: { from: parseInt(match[1]), to: parseInt(match[2]) }});
179
-     }
180
-    });
181
-   });
182
-  </script>
11
+  <script src="analyser.js" type="text/javascript"></script>
183 12
   <style type="text/css">
184 13
    .categoryIgnored td { color: gray; }
185 14
    .left { width: 45%; padding: 20px; float: left; }
@@ -189,6 +18,10 @@
189 18
    #header h1 { color: white; margin: 0; font-size: 60px; position: absolute; top: 42px; left: 20px; z-index: 2; }
190 19
    #headershadow { font-size: 60px; position: absolute; top: 42px; left: 25px; font-weight: bold; z-index: 1; }
191 20
    h2 { margin-top: 0; }
21
+   table { border-collapse: collapse; }
22
+   tr.alt td { background-color: #ddd; }
23
+   td { padding: 3px; }
24
+   th { text-align: left; }
192 25
   </style>
193 26
  </head>
194 27
  <body>

Loading…
Peruuta
Tallenna