Typica is a free program for professional coffee roasters. https://typica.us
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

chart.xml 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <window id="pytdprodcomp">
  2. <reporttitle>Production:->Previous Year Production Comparison</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <daterange id="dates" initial="19" /><!-- Current Year to Date -->
  6. <label>Days to Average</label>
  7. <line validator="integer" id="days">7</line>
  8. <label>Weight Unit:</label>
  9. <sqldrop id="unit" />
  10. <stretch />
  11. </layout>
  12. <webview id="report" />
  13. </layout>
  14. <menu name="File">
  15. <item id="print" shortcut="Ctrl+P">Print</item>
  16. </menu>
  17. <program>
  18. <![CDATA[
  19. this.windowTitle = "Typica - Previous Year Production Comparison";
  20. var dateSelect = findChildObject(this, 'dates');
  21. dateSelect.removeIndex(23); // Remove Lifetime range
  22. var view = findChildObject(this, 'report');
  23. var printMenu = findChildObject(this, 'print');
  24. printMenu.triggered.connect(function() {
  25. view.print();
  26. });
  27. var avgField = findChildObject(this, 'days');
  28. var unitBox = findChildObject(this, 'unit');
  29. unitBox.addItem("Kg");
  30. unitBox.addItem("Lb");
  31. unitBox.currentIndex = QSettings.value("script/report_unit", 1);
  32. unitBox['currentIndexChanged(int)'].connect(function() {
  33. QSettings.setValue("script/report_unit", unitBox.currentIndex);
  34. refresh();
  35. });
  36. function refresh() {
  37. var conversion = 1;
  38. if(unitBox.currentIndex == 0) {
  39. conversion = 2.2;
  40. }
  41. var buffer = new QBuffer;
  42. buffer.open(3);
  43. var output = new XmlWriter(buffer);
  44. output.writeStartDocument("1.0");
  45. output.writeDTD('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg.dtd">');
  46. output.writeStartElement("html");
  47. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  48. output.writeStartElement("head");
  49. output.writeTextElement("title", "Previous Year Production Comparison");
  50. output.writeEndElement();
  51. output.writeStartElement("body");
  52. output.writeTextElement("h1", "Previous Year Production Comparison");
  53. output.writeTextElement("p", "This report provides an itemized and overall comparison of roasted coffee production for the dates specified with those dates in the previous year. A chart of this data along with percent change and rolling average of the percent change is also produced.");
  54. output.writeStartElement("table");
  55. output.writeAttribute("style", "page-break-after:auto;");
  56. output.writeAttribute("rules", "groups");
  57. output.writeAttribute("cellpadding", "3px");
  58. output.writeStartElement("thead");
  59. output.writeStartElement("tr");
  60. output.writeTextElement("th", "Coffee");
  61. switch(unitBox.currentIndex) {
  62. case 0:
  63. output.writeTextElement("th", "Previous (Kg)");
  64. output.writeTextElement("th", "Current (Kg)");
  65. output.writeTextElement("th", "Change (Kg)");
  66. break;
  67. case 1:
  68. output.writeTextElement("th", "Previous (Lb)");
  69. output.writeTextElement("th", "Current (Lb)");
  70. output.writeTextElement("th", "Change (Lb)");
  71. break;
  72. }
  73. output.writeEndElement();
  74. output.writeEndElement();
  75. output.writeStartElement("tbody");
  76. var query = new QSqlQuery();
  77. query.exec("START TRANSACTION");
  78. var dateRange = dateSelect.currentRange();
  79. var curStartDate = "'"+dateRange[0]+"'";
  80. var curEndDate = "'"+dateRange[dateRange.length - 1]+"'";
  81. query.exec("SELECT "+curStartDate+"::date - interval '1 year', "+curEndDate+"::date - interval '1 year' + interval '1 day', "+curEndDate+"::date + interval '1 day'");
  82. query.next();
  83. curEndDate = "'"+query.value(2)+"'";
  84. var prevStartDate = "'"+query.value(0)+"'";
  85. var prevEndDate = "'"+query.value(1)+"'";
  86. 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";
  87. query.exec(q);
  88. 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";
  89. query.exec(q);
  90. query.exec("INSERT INTO previous SELECT roasted_id, 0 FROM current WHERE roasted_id NOT IN (SELECT roasted_id FROM previous)");
  91. query.exec("INSERT INTO current SELECT roasted_id, 0 FROM previous WHERE roasted_id NOT IN (SELECT roasted_id FROM current)");
  92. 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");
  93. 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");
  94. while(query.next())
  95. {
  96. output.writeStartElement("tr");
  97. output.writeTextElement("td", query.value(0));
  98. output.writeTextElement("td", (query.value(1) / conversion).toFixed(2));
  99. output.writeTextElement("td", (query.value(2) / conversion).toFixed(2));
  100. output.writeTextElement("td", (query.value(3) / conversion).toFixed(2));
  101. output.writeEndElement();
  102. }
  103. output.writeEndElement();
  104. output.writeStartElement("tfoot");
  105. output.writeTextElement("th", "Totals");
  106. query.exec("SELECT sum(p), sum(c), sum(c-p) FROM comp");
  107. query.next();
  108. output.writeTextElement("td", (query.value(0) / conversion).toFixed(2));
  109. output.writeTextElement("td", (query.value(1) / conversion).toFixed(2));
  110. output.writeTextElement("td", (query.value(2) / conversion).toFixed(2));
  111. output.writeEndElement();
  112. query.exec("ABORT");
  113. output.writeEndElement();
  114. output.writeStartElement("p");
  115. output.writeAttribute("style", "page-break-inside: avoid");
  116. output.writeStartElement("svg");
  117. output.writeAttribute("xmlns", "http://www.w3.org/2000/svg");
  118. output.writeAttribute("width", "7.5in");
  119. output.writeAttribute("height", "6.3in");
  120. output.writeStartElement("g");
  121. output.writeAttribute("transform", "translate(0,470)");
  122. output.writeStartElement("g");
  123. output.writeAttribute("transform", "scale(1,-1)");
  124. output.writeStartElement("line");
  125. output.writeAttribute("x1", "40");
  126. output.writeAttribute("x2", "40");
  127. output.writeAttribute("y1", "0");
  128. output.writeAttribute("y2", "450");
  129. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  130. output.writeEndElement();
  131. output.writeStartElement("line");
  132. output.writeAttribute("x1", "50");
  133. output.writeAttribute("x2", "650");
  134. output.writeAttribute("y1", "-10");
  135. output.writeAttribute("y2", "-10");
  136. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  137. output.writeEndElement();
  138. output.writeStartElement("line");
  139. output.writeAttribute("x1", "660");
  140. output.writeAttribute("x2", "660");
  141. output.writeAttribute("y1", "0");
  142. output.writeAttribute("y2", "450");
  143. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  144. output.writeEndElement();
  145. var q = "SELECT "+curEndDate+"::date - "+curStartDate+"::date";
  146. query.exec(q);
  147. query.next();
  148. var days = query.value(0);
  149. var dates = new Array();
  150. var curpounds = new Array();
  151. var prevpounds = new Array();
  152. var change = new Array();
  153. var avgchange = new Array();
  154. var i;
  155. for(i = 0; i < days; i++)
  156. {
  157. q = "SELECT "+curStartDate+"::date + "+(i+1);
  158. query.exec(q);
  159. query.next();
  160. dates[i] = query.value(0);
  161. q = "SELECT sum(roasted_quantity) FROM roasting_log WHERE time > "+curStartDate+" AND time < '"+dates[i]+"'";
  162. query.exec(q);
  163. if(query.next())
  164. {
  165. curpounds[i] = query.value(0);
  166. }
  167. else
  168. {
  169. curpounds[i] = 0;
  170. }
  171. q = "SELECT sum(roasted_quantity) FROM roasting_log WHERE time > "+curStartDate+"::date - '1 year'::interval AND time < '"+dates[i]+"'::date - '1 year'::interval";
  172. query.exec(q);
  173. if(query.next())
  174. {
  175. prevpounds[i] = query.value(0);
  176. }
  177. else
  178. {
  179. prevpounds[i] = 0;
  180. }
  181. if(curpounds[i] > 0)
  182. {
  183. change[i] = (curpounds[i] - prevpounds[i])/curpounds[i];
  184. }
  185. else
  186. {
  187. if(prevpounds[i] > 0)
  188. {
  189. change[i] = -1;
  190. }
  191. else
  192. {
  193. change[i] = 0;
  194. }
  195. }
  196. if(i > (parseInt(avgField.text)-2))
  197. {
  198. var sum = 0;
  199. var j;
  200. for(j = 0; j < parseInt(avgField.text); j++)
  201. {
  202. sum += change[i-j];
  203. }
  204. avgchange[i] = sum / parseInt(avgField.text);
  205. }
  206. }
  207. // Calculate the domain of the primary y axis.
  208. var maxy1 = 0;
  209. var increment = 100; // Starting increment is 100 Lb.
  210. if(unitBox.currentIndex == 0) {
  211. increment = 110; // Starting increment is 50 Kg if Kg is requested.
  212. }
  213. while(maxy1 < Math.max(curpounds[days - 1], prevpounds[days - 1]))
  214. {
  215. maxy1 += increment;
  216. }
  217. // Calculate the number of grid lines and loosen spacing if there are too many.
  218. var divisions = maxy1 / increment;
  219. while(divisions > 10) {
  220. increment *= 2;
  221. divisions = maxy1 / increment;
  222. }
  223. // Draw y axis grid lines.
  224. var pos = 0;
  225. while(pos <= maxy1)
  226. {
  227. output.writeStartElement("line");
  228. output.writeAttribute("x1", "35");
  229. output.writeAttribute("x2", "45");
  230. output.writeAttribute("y1", (450/maxy1)*pos);
  231. output.writeAttribute("y2", (450/maxy1)*pos);
  232. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  233. output.writeEndElement();
  234. pos += increment;
  235. }
  236. var n = Math.min.apply(Math, change);
  237. var m = Math.max.apply(Math, change);
  238. var miny2 = 0;
  239. var maxy2;
  240. if(n < 0)
  241. {
  242. while(miny2 > n)
  243. {
  244. miny2 -= 0.1;
  245. }
  246. maxy2 = miny2;
  247. }
  248. else
  249. {
  250. while(miny2 < n)
  251. {
  252. miny2 += 0.1;
  253. }
  254. miny2 -= 0.1;
  255. maxy2 = miny2;
  256. }
  257. while(maxy2 < m)
  258. {
  259. maxy2 += 0.1;
  260. }
  261. var range = maxy2 - miny2;
  262. pos = miny2;
  263. while(pos <= maxy2)
  264. {
  265. output.writeStartElement("line");
  266. output.writeAttribute("x1", "655");
  267. output.writeAttribute("x2", "665");
  268. output.writeAttribute("y1", (pos-miny2)*(4.5/(range*100))*10000);
  269. output.writeAttribute("y2", (pos-miny2)*(4.5/(range*100))*10000);
  270. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  271. output.writeEndElement();
  272. pos += 0.1;
  273. }
  274. pos = 0;
  275. while(pos < days)
  276. {
  277. output.writeStartElement("line");
  278. output.writeAttribute("x1", 50+((600/(days-1))*pos));
  279. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  280. output.writeAttribute("y1", "-5");
  281. output.writeAttribute("y2", "-15");
  282. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  283. output.writeEndElement();
  284. pos += 7;
  285. }
  286. output.writeStartElement("line");
  287. output.writeAttribute("x1", 50+((600/(days-1))*(days-1)));
  288. output.writeAttribute("x2", 50+((600/(days-1))*(days-1)));
  289. output.writeAttribute("y1", "-5");
  290. output.writeAttribute("y2", "-15");
  291. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  292. output.writeEndElement();
  293. pos = 1;
  294. while(pos < days)
  295. {
  296. output.writeStartElement("line");
  297. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  298. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  299. output.writeAttribute("y1", (prevpounds[pos-1]/maxy1)*450);
  300. output.writeAttribute("y2", (prevpounds[pos]/maxy1)*450);
  301. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  302. output.writeEndElement();
  303. output.writeStartElement("line");
  304. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  305. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  306. output.writeAttribute("y1", (curpounds[pos-1]/maxy1)*450);
  307. output.writeAttribute("y2", (curpounds[pos]/maxy1)*450);
  308. output.writeAttribute("style", "stroke:rgb(255,0,0);stroke-width:1;");
  309. output.writeEndElement();
  310. output.writeStartElement("line");
  311. output.writeAttribute("x1", 50+((600/(days-1))*(pos-1)));
  312. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  313. output.writeAttribute("y1", (change[pos-1]-miny2)*(4.5/(range*100))*10000);
  314. output.writeAttribute("y2", (change[pos]-miny2)*(4.5/(range*100))*10000);
  315. output.writeAttribute("style", "stroke:rgb(0,255,0);stroke-width:1;");
  316. output.writeEndElement();
  317. if(pos > (parseInt(avgField.text)-1))
  318. {
  319. output.writeStartElement("line");
  320. output.writeAttribute("x1", 50+((600/(days-1))*(pos-1)));
  321. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  322. output.writeAttribute("y1", (avgchange[pos-1]-miny2)*(4.5/(range*100))*10000);
  323. output.writeAttribute("y2", (avgchange[pos]-miny2)*(4.5/(range*100))*10000);
  324. output.writeAttribute("style", "stroke:rgb(0,0,255);stroke-width:1;");
  325. output.writeEndElement();
  326. }
  327. pos++;
  328. }
  329. output.writeEndElement();
  330. i = 0;
  331. while(i <= maxy1)
  332. {
  333. output.writeStartElement("text");
  334. output.writeAttribute("x", "0");
  335. output.writeAttribute("y", -((450/maxy1)*i)+5);
  336. output.writeAttribute("font-size", "12");
  337. switch(unitBox.currentIndex) {
  338. case 0:
  339. output.writeCharacters((i / 2.2).toFixed(0));
  340. break;
  341. case 1:
  342. default:
  343. output.writeCharacters(i);
  344. break;
  345. }
  346. output.writeEndElement();
  347. i += increment;
  348. }
  349. i = miny2;
  350. while(i <= maxy2)
  351. {
  352. output.writeStartElement("text");
  353. output.writeAttribute("x", "670");
  354. output.writeAttribute("y", -((i-miny2)*(4.5/(range*100))*10000)+5);
  355. output.writeAttribute("font-size", "12");
  356. output.writeCharacters(Number(i*100).toFixed(0)+"%");
  357. output.writeEndElement();
  358. i += 0.1;
  359. }
  360. i = 0;
  361. while(i <= days-1)
  362. {
  363. output.writeStartElement("text");
  364. output.writeAttribute("x", 45+((600/(days-1))*i));
  365. output.writeAttribute("y", "20");
  366. output.writeAttribute("font-size", "12");
  367. output.writeAttribute("transform", "rotate(90 "+(45+((600/(days-1))*i))+",20)");
  368. q = "SELECT ('"+dates[i]+"'::date - '1 day'::interval)::date";
  369. query.exec(q);
  370. query.next();
  371. var ds = query.value(0);
  372. var parts = ds.split("-");
  373. switch(parts[1])
  374. {
  375. case '01':
  376. ds = "January ";
  377. break;
  378. case '02':
  379. ds = "February ";
  380. break;
  381. case '03':
  382. ds = "March ";
  383. break;
  384. case '04':
  385. ds = "April ";
  386. break;
  387. case '05':
  388. ds = "May ";
  389. break;
  390. case '06':
  391. ds = "June ";
  392. break;
  393. case '07':
  394. ds = "July ";
  395. break;
  396. case '08':
  397. ds = "August ";
  398. break;
  399. case '09':
  400. ds = "September ";
  401. break;
  402. case '10':
  403. ds = "October ";
  404. break;
  405. case '11':
  406. ds = "November ";
  407. break;
  408. case '12':
  409. ds = "December ";
  410. break;
  411. }
  412. ds = ds + Number(parts[2]);
  413. output.writeCharacters(ds);
  414. output.writeEndElement();
  415. if(i == days-1)
  416. {
  417. break;
  418. }
  419. i += 7;
  420. if(i > days-1)
  421. {
  422. i = days - 1;
  423. }
  424. }
  425. query = query.invalidate();
  426. output.writeStartElement("rect");
  427. output.writeAttribute("fill", "rgb(0,0,0)");
  428. output.writeAttribute("x", "45");
  429. output.writeAttribute("y", "110");
  430. output.writeAttribute("width", "24");
  431. output.writeAttribute("height", "12");
  432. output.writeEndElement();
  433. output.writeStartElement("text");
  434. output.writeAttribute("x", "75");
  435. output.writeAttribute("y", "120");
  436. output.writeAttribute("font-size", "12");
  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. }
  445. output.writeEndElement();
  446. output.writeStartElement("rect");
  447. output.writeAttribute("fill", "rgb(255,0,0)");
  448. output.writeAttribute("x", "195");
  449. output.writeAttribute("y", "110");
  450. output.writeAttribute("width", "24");
  451. output.writeAttribute("height", "12");
  452. output.writeEndElement();
  453. output.writeStartElement("text");
  454. output.writeAttribute("x", "225");
  455. output.writeAttribute("y", "120");
  456. output.writeAttribute("font-size", "12");
  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. }
  465. output.writeEndElement();
  466. output.writeStartElement("rect");
  467. output.writeAttribute("fill", "rgb(0,255,0)");
  468. output.writeAttribute("x", "345");
  469. output.writeAttribute("y", "110");
  470. output.writeAttribute("width", "24");
  471. output.writeAttribute("height", "12");
  472. output.writeEndElement();
  473. output.writeStartElement("text");
  474. output.writeAttribute("x", "375");
  475. output.writeAttribute("y", "120");
  476. output.writeAttribute("font-size", "12");
  477. output.writeCharacters("% Change");
  478. output.writeEndElement();
  479. output.writeStartElement("rect");
  480. output.writeAttribute("fill", "rgb(0,0,255)");
  481. output.writeAttribute("x", "495");
  482. output.writeAttribute("y", "110");
  483. output.writeAttribute("width", "24");
  484. output.writeAttribute("height", "12");
  485. output.writeEndElement();
  486. output.writeStartElement("text");
  487. output.writeAttribute("x", "525");
  488. output.writeAttribute("y", "120");
  489. output.writeAttribute("font-size", "12");
  490. output.writeCharacters("Average % Change");
  491. output.writeEndElement();
  492. output.writeEndElement();
  493. output.writeEndElement();
  494. output.writeEndElement();
  495. output.writeEndElement();
  496. output.writeEndElement();
  497. output.writeEndDocument();
  498. view.setContent(buffer);
  499. buffer.close();
  500. }
  501. refresh();
  502. dateSelect.rangeUpdated.connect(function() {
  503. refresh();
  504. });
  505. avgField.editingFinished.connect(function() {
  506. refresh();
  507. });
  508. ]]>
  509. </program>
  510. </window>