PHP/JavaScript webapp to analyse spending habits
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.html 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Spending Analyser</title>
  5. <script src="externals/flot/jquery.js" type="text/javascript"></script>
  6. <script src="externals/flot/jquery.flot.js" type="text/javascript"></script>
  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>
  9. <script src="data.php" type="text/javascript"></script>
  10. <script type="text/javascript">
  11. var previousPoint = null;
  12. var plots = {};
  13. var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  14. function showTooltip(x, y, contents) {
  15. $('<div id="tooltip">' + contents + '</div>').css( {
  16. position: 'absolute',
  17. display: 'none',
  18. top: y + 5,
  19. left: x + 5,
  20. border: '1px solid #fdd',
  21. padding: '2px',
  22. 'background-color': '#fee',
  23. }).appendTo("body").fadeIn(200);
  24. }
  25. function getDateKey(date) {
  26. return date.getFullYear() + '-' + (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1);
  27. }
  28. function showSelectedMonths(start, end, incoming, outgoing) {
  29. $('#historytable tr.data').remove();
  30. $('#historytable').show();
  31. var startDate = new Date(start), endDate = new Date(end);
  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. var startKey = getDateKey(startDate), endKey = getDateKey(endDate);
  38. if (startKey == endKey) {
  39. $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + ' ' + startDate.getFullYear());
  40. } else if (startDate.getFullYear() == endDate.getFullYear()) {
  41. $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + '-' + months[endDate.getMonth()] + ' ' + startDate.getFullYear());
  42. } else {
  43. $('#historytable h3').text('Transactions for ' + months[startDate.getMonth()] + ' ' + startDate.getFullYear() + ' - ' + months[endDate.getMonth()] + ' ' + endDate.getFullYear());
  44. }
  45. var pieData = {};
  46. var table = $('#historytable table');
  47. var include = false;
  48. $.each(data, function(month, monthData) {
  49. if (month == startKey) { include = true; }
  50. if (include) {
  51. $.each(monthData, function(index, trans) {
  52. if (incoming != trans.Amount > 0) { return; }
  53. var category = trans.Category ? trans.Category : 'Unsorted';
  54. var tr = $('<tr/>').addClass('data').addClass('category' + category.replace(/[^a-zA-Z]*/g, '')).appendTo(table);
  55. $('<td/>').text(trans.Date.date.split(' ')[0]).appendTo(tr);
  56. $('<td/>').text(trans.Type ? trans.Type : 'Other').appendTo(tr);
  57. $('<td/>').text(trans.Category ? trans.Category : '').appendTo(tr);
  58. $('<td/>').text(trans.Description).appendTo(tr);
  59. $('<td/>').text(trans.Amount).appendTo(tr);
  60. if (category != '(Ignored)') {
  61. if (!pieData[category]) { pieData[category] = 0; }
  62. pieData[category] += Math.abs(trans.Amount);
  63. }
  64. });
  65. }
  66. if (month == endKey) { include = false; }
  67. });
  68. var seriesData = [];
  69. $.each(pieData, function(category, amount) {
  70. seriesData.push({ label: category + ' (' + Math.round(amount) + ')', data: amount });
  71. });
  72. seriesData.sort(function(a, b) { return b.data - a.data; });
  73. plots.expense = $.plot($('#expense'), seriesData, {
  74. series: { pie: { show: true, innerRadius: 0.5, highlight: { opacity: 0.5 } } },
  75. grid: { clickable: true }
  76. });
  77. }
  78. $(function() {
  79. var transData = [{label: 'Income', data: []}, {label: 'Expense', data: []}];
  80. $.each(data, function(month, entries) {
  81. var split = month.split('-');
  82. var timestamp = new Date(split[0], split[1] - 1).getTime();
  83. var sum = [0, 0];
  84. $.each(entries, function() {
  85. if (this.Category == '(Ignored)') { return; }
  86. sum[this.Amount < 0 ? 1 : 0] += this.Amount;
  87. });
  88. transData[0].data.push([timestamp, sum[0]]);
  89. transData[1].data.push([timestamp, sum[1]]);
  90. });
  91. plots.history = $.plot($('#history'), transData, {
  92. xaxis: { mode: 'time', timeformat: '%y/%m'},
  93. series: {
  94. lines: { show: true },
  95. points: { show: true }
  96. },
  97. grid: { hoverable: true, clickable: true },
  98. selection: { mode : "x" }
  99. });
  100. $("#history").bind("plothover", function (event, pos, item) {
  101. if (item) {
  102. var id = {dataIndex: item.dataIndex, seriesIndex: item.seriesIndex};
  103. if (previousPoint == null || previousPoint.dataIndex != id.dataIndex || previousPoint.seriesIndex != id.seriesIndex) {
  104. previousPoint = id;
  105. $("#tooltip").remove();
  106. var x = item.datapoint[0],
  107. y = item.datapoint[1].toFixed(2);
  108. var date = new Date(x);
  109. showTooltip(item.pageX, item.pageY,
  110. (item.seriesIndex == 0 ? "Money in" : "Money out") + " during " + months[date.getMonth()] + " " + date.getFullYear() + " = " + y);
  111. }
  112. } else {
  113. $("#tooltip").remove();
  114. previousPoint = null;
  115. }
  116. });
  117. $('#history').bind('plotselected', function(event, ranges) {
  118. var startDate = parseInt(ranges.xaxis.from.toFixed());
  119. var endDate = parseInt(ranges.xaxis.to.toFixed());
  120. showSelectedMonths(startDate, endDate, 0);
  121. });
  122. $('#history').bind('plotclick', function(event, pos, item) {
  123. if (item) {
  124. showSelectedMonths(item.datapoint[0], item.datapoint[0], item.seriesIndex == 0, item.seriesIndex == 1);
  125. }
  126. });
  127. $('#expense').bind('plotclick', function(event, pos, item) {
  128. $('#historytable .data').hide();
  129. $('#historytable .category' + item.series.label.replace(/[^a-zA-Z]*/g, '')).show();
  130. });
  131. });
  132. </script>
  133. <style type="text/css">
  134. .categoryIgnored td { color: gray; }
  135. .left { width: 45%; padding: 20px; float: left; }
  136. .right { margin-left: 50%; padding: 20px; }
  137. body { margin: 0; font-family: sans-serif; }
  138. #header { background: url('res/gradient.png') repeat-x; height: 100px; margin: 0 0 10px 0; overflow: hidden; z-index: 0; }
  139. #header h1 { color: white; margin: 0; font-size: 60px; position: absolute; top: 42px; left: 20px; z-index: 2; }
  140. #headershadow { font-size: 60px; position: absolute; top: 42px; left: 25px; font-weight: bold; z-index: 1; }
  141. h2 { margin-top: 0; }
  142. </style>
  143. </head>
  144. <body>
  145. <div id="header">
  146. <h1>Money Analyser</h1>
  147. </div>
  148. <span id="headershadow">Money Analyser</span>
  149. <div class="left">
  150. <h2>Transaction History</h2>
  151. <div id="history" style="width: 600px; height: 300px;"></div>
  152. <div id="historytable" style="display: none;"><h3>Transactions for ??</h3><table><tr><th>Date</th><th>Type</th><th>Category</th><th>Description</th><th>Amount</th></tr></table></div>
  153. </div>
  154. <div class="right">
  155. <h2>Categories</h2>
  156. <div id="expense" style="width: 600px; height: 300px;"></div>
  157. </div>
  158. </body>
  159. </html>