Browse Source

Merge branch 'unitselect' into development

Neal Wilson 11 years ago
parent
commit
d815137f08
1 changed files with 72 additions and 25 deletions
  1. 72
    25
      config/Reports/chart.xml

+ 72
- 25
config/Reports/chart.xml View File

@@ -8,6 +8,8 @@
8 8
             <calendar id="enddate" />
9 9
             <label>Days to Average</label>
10 10
             <line validator="integer" id="days">7</line>
11
+			<label>Weight Unit:</label>
12
+			<sqldrop id="unit" />
11 13
             <stretch />
12 14
         </layout>
13 15
         <webview id="report" />
@@ -27,7 +29,19 @@
27 29
                 view.print();
28 30
             });
29 31
             var avgField = findChildObject(this, 'days');
32
+			var unitBox = findChildObject(this, 'unit');
33
+			unitBox.addItem("Kg");
34
+			unitBox.addItem("Lb");
35
+			unitBox.currentIndex = QSettings.value("script/report_unit", 1);
36
+			unitBox['currentIndexChanged(int)'].connect(function() {
37
+				QSettings.setValue("script/report_unit", unitBox.currentIndex);
38
+				refresh();
39
+			});
30 40
             function refresh() {
41
+				var conversion = 1;
42
+				if(unitBox.currentIndex == 0) {
43
+					conversion = 2.2;
44
+				}
31 45
                 var buffer = new QBuffer;
32 46
                 buffer.open(3);
33 47
                 var output = new XmlWriter(buffer);
@@ -48,57 +62,56 @@
48 62
                 output.writeStartElement("thead");
49 63
                 output.writeStartElement("tr");
50 64
                 output.writeTextElement("th", "Coffee");
51
-                output.writeTextElement("th", "Previous");
52
-                output.writeTextElement("th", "Current");
53
-                output.writeTextElement("th", "Change");
65
+				switch(unitBox.currentIndex) {
66
+					case 0:
67
+						output.writeTextElement("th", "Previous (Kg)");
68
+						output.writeTextElement("th", "Current (Kg)");
69
+						output.writeTextElement("th", "Change (Kg)");
70
+						break;
71
+					case 1:
72
+						output.writeTextElement("th", "Previous (Lb)");
73
+						output.writeTextElement("th", "Current (Lb)");
74
+						output.writeTextElement("th", "Change (Lb)");
75
+						break;
76
+				}
54 77
                 output.writeEndElement();
55 78
                 output.writeEndElement();
56 79
                 output.writeStartElement("tbody");
57 80
                 var query = new QSqlQuery();
58 81
                 query.exec("START TRANSACTION");
59
-                print(query.executedQuery());
60 82
                 var curStartDate = "'"+startDateField.year()+"-"+startDateField.month()+"-"+startDateField.day()+"'";
61 83
                 query.exec("SELECT "+curStartDate+"::date - interval '1 year', '"+endDateField.year()+"-"+endDateField.month()+"-"+endDateField.day()+"'::date - interval '1 year' + interval '1 day', '"+endDateField.year()+"-"+endDateField.month()+"-"+endDateField.day()+"'::date + interval '1 day'");
62
-                print(query.executedQuery());
63 84
                 query.next();
64 85
                 var curEndDate = "'"+query.value(2)+"'";
65 86
                 var prevStartDate = "'"+query.value(0)+"'";
66 87
                 var prevEndDate = "'"+query.value(1)+"'";
67 88
                 var q = "CREATE TEMPORARY TABLE previous ON COMMIT DROP AS SELECT roasted_id, sum(roasted_quantity) AS p FROM roasting_log WHERE time > "+prevStartDate+" AND time < "+prevEndDate+" AND roasted_id IS NOT NULL GROUP BY roasted_id";
68 89
                 query.exec(q);
69
-                print(query.executedQuery());
70 90
                 q = "CREATE TEMPORARY TABLE current ON COMMIT DROP AS SELECT roasted_id, sum(roasted_quantity) AS c FROM roasting_log WHERE time > "+curStartDate+" AND time < "+curEndDate+" AND roasted_id IS NOT NULL GROUP BY roasted_id";
71 91
                 query.exec(q);
72
-                print(query.executedQuery());
73 92
                 query.exec("INSERT INTO previous SELECT roasted_id, 0 FROM current WHERE roasted_id NOT IN (SELECT roasted_id FROM previous)");
74
-                print(query.executedQuery());
75 93
                 query.exec("INSERT INTO current SELECT roasted_id, 0 FROM previous WHERE roasted_id NOT IN (SELECT roasted_id FROM current)");
76
-                print(query.executedQuery());
77 94
                 query.exec("CREATE TEMPORARY TABLE comp ON COMMIT DROP AS SELECT previous.roasted_id, p, c FROM previous LEFT OUTER JOIN current ON previous.roasted_id = current.roasted_id");
78
-                print(query.executedQuery());
79 95
                 query.exec("SELECT (SELECT name FROM items WHERE id = roasted_id) AS name, p, c, (c-p) FROM comp WHERE p > 0 OR c > 0 ORDER BY name");
80
-                print(query.executedQuery());
81 96
                 while(query.next())
82 97
                 {
83 98
                     output.writeStartElement("tr");
84 99
                     output.writeTextElement("td", query.value(0));
85
-                    output.writeTextElement("td", query.value(1));
86
-                    output.writeTextElement("td", query.value(2));
87
-                    output.writeTextElement("td", query.value(3));
100
+					output.writeTextElement("td", (query.value(1) / conversion).toFixed(2));
101
+					output.writeTextElement("td", (query.value(2) / conversion).toFixed(2));
102
+					output.writeTextElement("td", (query.value(3) / conversion).toFixed(2));
88 103
                     output.writeEndElement();
89 104
                 }
90 105
                 output.writeEndElement();
91 106
                 output.writeStartElement("tfoot");
92 107
                 output.writeTextElement("th", "Totals");
93 108
                 query.exec("SELECT sum(p), sum(c), sum(c-p) FROM comp");
94
-                print(query.executedQuery());
95 109
                 query.next();
96
-                output.writeTextElement("td", query.value(0));
97
-                output.writeTextElement("td", query.value(1));
98
-                output.writeTextElement("td", query.value(2));
110
+				output.writeTextElement("td", (query.value(0) / conversion).toFixed(2));
111
+				output.writeTextElement("td", (query.value(1) / conversion).toFixed(2));
112
+				output.writeTextElement("td", (query.value(2) / conversion).toFixed(2));
99 113
                 output.writeEndElement();
100 114
                 query.exec("ABORT");
101
-                print(query.executedQuery());
102 115
                 output.writeEndElement();
103 116
                 output.writeStartElement("svg");
104 117
                 output.writeAttribute("xmlns", "http://www.w3.org/2000/svg");
@@ -192,11 +205,23 @@
192 205
                         avgchange[i] = sum / parseInt(avgField.text);
193 206
                     }
194 207
                 }
208
+				// Calculate the domain of the primary y axis.
195 209
                 var maxy1 = 0;
210
+				var increment = 100; // Starting increment is 100 Lb.
211
+				if(unitBox.currentIndex == 0) {
212
+					increment = 110; // Starting increment is 50 Kg if Kg is requested.
213
+				}
196 214
                 while(maxy1 < Math.max(curpounds[days - 1], prevpounds[days - 1]))
197 215
                 {
198
-                    maxy1 += 100;
216
+					maxy1 += increment;
199 217
                 }
218
+				// Calculate the number of grid lines and loosen spacing if there are too many.
219
+				var divisions = maxy1 / increment;
220
+				while(divisions > 10) {
221
+					increment *= 2;
222
+					divisions = maxy1 / increment;
223
+				}
224
+				// Draw y axis grid lines.
200 225
                 var pos = 0;
201 226
                 while(pos <= maxy1)
202 227
                 {
@@ -207,7 +232,7 @@
207 232
                     output.writeAttribute("y2", (450/maxy1)*pos);
208 233
                     output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
209 234
                     output.writeEndElement();
210
-                    pos += 100;
235
+					pos += increment;
211 236
                 }
212 237
                 var n = Math.min.apply(Math, change);
213 238
                 var m = Math.max.apply(Math, change);
@@ -310,9 +335,17 @@
310 335
                     output.writeAttribute("x", "0");
311 336
                     output.writeAttribute("y", -((450/maxy1)*i)+5);
312 337
                     output.writeAttribute("font-size", "12");
313
-                    output.writeCharacters(i);
338
+					switch(unitBox.currentIndex) {
339
+						case 0:
340
+							output.writeCharacters((i / 2.2).toFixed(0));
341
+							break;
342
+						case 1:
343
+						default:
344
+							output.writeCharacters(i);
345
+							break;
346
+					}
314 347
                     output.writeEndElement();
315
-                    i += 100;
348
+                    i += increment;
316 349
                 }
317 350
                 i = miny2;
318 351
                 while(i <= maxy2)
@@ -401,7 +434,14 @@
401 434
                 output.writeAttribute("x", "75");
402 435
                 output.writeAttribute("y", "120");
403 436
                 output.writeAttribute("font-size", "12");
404
-                output.writeCharacters("Previous Year Pounds");
437
+				switch(unitBox.currentIndex) {
438
+					case 0:
439
+						output.writeCharacters("Previous Year Kg");
440
+						break;
441
+					case 1:
442
+						output.writeCharacters("Previous Year Lb");
443
+						break;
444
+				}
405 445
                 output.writeEndElement();
406 446
                 output.writeStartElement("rect");
407 447
                 output.writeAttribute("fill", "rgb(255,0,0)");
@@ -414,7 +454,14 @@
414 454
                 output.writeAttribute("x", "225");
415 455
                 output.writeAttribute("y", "120");
416 456
                 output.writeAttribute("font-size", "12");
417
-                output.writeCharacters("Current Year Pounds");
457
+				switch(unitBox.currentIndex) {
458
+					case 0:
459
+						output.writeCharacters("Current Year Kg");
460
+						break;
461
+					case 1:
462
+						output.writeCharacters("Current Year Lb");
463
+						break;
464
+				}
418 465
                 output.writeEndElement();
419 466
                 output.writeStartElement("rect");
420 467
                 output.writeAttribute("fill", "rgb(0,255,0)");

Loading…
Cancel
Save