Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lossplots.xml 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <window id="lossplots">
  2. <reporttitle>Production:->Weight Loss By Item and Operator</reporttitle>
  3. <layout type="vertical">
  4. <webview id="report" />
  5. </layout>
  6. <menu name="File">
  7. <item id="print" shortcut="Ctrl+P">Print...</item>
  8. </menu>
  9. <program>
  10. <![CDATA[
  11. this.windowTitle = TTR("lossplots", "Typica - Weight Loss By Item and Operator");
  12. var report = findChildObject(this, 'report');
  13. var printMenu = findChildObject(this, 'print');
  14. printMenu.triggered.connect(function() {
  15. report.print();
  16. });
  17. var query = new QSqlQuery;
  18. query.exec("WITH a AS (SELECT roasted_id, max(time) AS last_batch FROM roasting_log WHERE roasted_id IN (SELECT item FROM current_items) GROUP BY roasted_id), b AS (SELECT roasted_id, last_batch, (SELECT unroasted_id FROM roasting_log WHERE roasted_id = a.roasted_id AND time = last_batch) FROM a), c AS (SELECT roasting_log.roasted_id, ((unroasted_total_quantity - roasted_quantity)/unroasted_total_quantity) AS loss FROM roasting_log, b WHERE roasting_log.roasted_id = b.roasted_id AND roasting_log.unroasted_id = b.unroasted_id AND unroasted_total_quantity > 0 AND approval = true) SELECT roasted_id, (SELECT name FROM items WHERE id = roasted_id) AS name, count(1), min(loss), percentile_cont(0.25) WITHIN GROUP (ORDER BY loss) AS lower_quart, percentile_cont(0.5) WITHIN GROUP (ORDER BY loss) AS median, percentile_cont(0.75) WITHIN GROUP (ORDER BY loss) AS upper_quart, max(loss), avg(loss) FROM c GROUP BY roasted_id ORDER BY name ASC");
  19. var items = new Array;
  20. while(query.next()) {
  21. var item = new Object;
  22. item.roasted_id = query.value(0);
  23. item.name = query.value(1);
  24. item.mean = query.value(8);
  25. item.min = query.value(3);
  26. item.max = query.value(7);
  27. item.plots = new Array;
  28. var plot = new Object;
  29. plot.count = query.value(2);
  30. plot.min = item.min;
  31. plot.max = item.max;
  32. plot.lower_quart = query.value(4);
  33. plot.median = query.value(5);
  34. plot.upper_quart = query.value(6);
  35. plot.name = "All";
  36. item.plots.push(plot);
  37. items.push(item);
  38. }
  39. query.exec("WITH a AS (SELECT roasted_id, max(time) AS last_batch FROM roasting_log WHERE roasted_id IN (SELECT item FROM current_items) GROUP BY roasted_id), b AS (SELECT roasted_id, last_batch, (SELECT unroasted_id FROM roasting_log WHERE roasted_id = a.roasted_id AND time = last_batch) FROM a), c AS (SELECT roasting_log.roasted_id, ((unroasted_total_quantity - roasted_quantity)/unroasted_total_quantity) AS loss, person FROM roasting_log, b WHERE roasting_log.roasted_id = b.roasted_id AND roasting_log.unroasted_id = b.unroasted_id AND unroasted_total_quantity > 0 AND approval = true) SELECT roasted_id, person, count(1), min(loss), percentile_cont(0.25) WITHIN GROUP (ORDER BY loss) AS lower_quart, percentile_cont(0.5) WITHIN GROUP (ORDER BY loss) AS median, percentile_cont(0.75) WITHIN GROUP (ORDER BY loss) AS upper_quart, max(loss) FROM c GROUP BY roasted_id, person");
  40. while(query.next()) {
  41. var plot = new Object;
  42. plot.count = query.value(2);
  43. plot.min = query.value(3);
  44. plot.max = query.value(7);
  45. plot.lower_quart = query.value(4);
  46. plot.median = query.value(5);
  47. plot.upper_quart = query.value(6);
  48. plot.name = query.value(1);
  49. for(var i = 0; i < items.length; i++) {
  50. if(items[i].roasted_id == query.value(0)) {
  51. items[i].plots.push(plot);
  52. break;
  53. }
  54. }
  55. }
  56. query = query.invalidate();
  57. var buffer = new QBuffer;
  58. buffer.open(3);
  59. var output = new XmlWriter(buffer);
  60. output.writeStartDocument("1.0");
  61. 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">');
  62. output.writeStartElement("html");
  63. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  64. output.writeStartElement("head");
  65. output.writeTextElement("style", ".box {font: 10px sans-serif;} .whisker {font: 10px sans-serif;} .axis {font: 12px sans-serif;} .axis path {fill: none; stroke: #000; shape-rendering: crispEdges;} .axis line {fill: none; stroke: #000; shape-rendering: crispEdges;} .x.axis path {fill: none; stroke: #000; shape-rendering: crispEdges;}");
  66. output.writeEndElement(); //head
  67. output.writeStartElement("body");
  68. var cdt = new Date(Date.now());
  69. output.writeTextElement("p", cdt.toLocaleDateString(TTR("reports", "en-US")) + " " + cdt.toLocaleTimeString(TTR("reports", "en-US")));
  70. function position(value, min, max) {
  71. return 300 - (((Number(value)-Number(min))/(Number(max)-Number(min)))*300);
  72. }
  73. for(var i = 0; i < items.length; i++) {
  74. output.writeStartElement("svg");
  75. output.writeAttribute("xmlns", "http://www.w3.org/2000/svg");
  76. output.writeAttribute("height", "420");
  77. output.writeAttribute("width", 84 + (items[i].plots.length * 179));
  78. output.writeStartElement("g"); //plots
  79. output.writeAttribute("transform", "translate(50,30)");
  80. var plotMin = items[i].min;
  81. var plotMax = items[i].max;
  82. for(var j = 0; j < items[i].plots.length; j++) {
  83. output.writeStartElement("g"); //plot
  84. output.writeAttribute("transform", "translate(" + (55 + (179 * j)).toString() + ",30)");
  85. output.writeStartElement("line"); //center line
  86. output.writeAttribute("class", "center");
  87. output.writeAttribute("style", "fill: #000; stroke: #000; stroke-width: 1px;");
  88. output.writeAttribute("x1", "27");
  89. output.writeAttribute("x2", "27");
  90. output.writeAttribute("y1", position(items[i].plots[j].min, plotMin, plotMax));
  91. output.writeAttribute("y2", position(items[i].plots[j].max, plotMin, plotMax));
  92. output.writeEndElement(); //line
  93. output.writeStartElement("rect"); //upper and lower quartiles box
  94. output.writeAttribute("class", "box");
  95. output.writeAttribute("style", "fill: #12DD11; stroke: #000; stroke-width: 1px;");
  96. output.writeAttribute("x", "0");
  97. output.writeAttribute("width", "54");
  98. output.writeAttribute("y", position(items[i].plots[j].upper_quart, plotMin, plotMax));
  99. output.writeAttribute("height", position(items[i].plots[j].lower_quart, plotMin, plotMax) - position(items[i].plots[j].upper_quart, plotMin, plotMax));
  100. output.writeEndElement(); //rect
  101. output.writeStartElement("line"); //median line
  102. output.writeAttribute("class", "median");
  103. output.writeAttribute("style", "fill: #12DD11; stroke: #000; stroke-width: 1px;");
  104. output.writeAttribute("x1", "0");
  105. output.writeAttribute("x2", "54");
  106. output.writeAttribute("y1", position(items[i].plots[j].median, plotMin, plotMax));
  107. output.writeAttribute("y2", position(items[i].plots[j].median, plotMin, plotMax));
  108. output.writeEndElement(); //line
  109. output.writeStartElement("line"); //minimum whisker
  110. output.writeAttribute("class", "whisker");
  111. output.writeAttribute("style", "fill: #12DD11; stroke: #000; stroke-width: 1px;");
  112. output.writeAttribute("x1", "0");
  113. output.writeAttribute("x2", "54");
  114. output.writeAttribute("y1", position(items[i].plots[j].min, plotMin, plotMax));
  115. output.writeAttribute("y2", position(items[i].plots[j].min, plotMin, plotMax));
  116. output.writeEndElement(); //line
  117. output.writeStartElement("line"); //maximum whisker
  118. output.writeAttribute("class", "whisker");
  119. output.writeAttribute("style", "fill: #12DD11; stroke: #000; stroke-width: 1px;");
  120. output.writeAttribute("x1", "0");
  121. output.writeAttribute("x2", "54");
  122. output.writeAttribute("y1", position(items[i].plots[j].max, plotMin, plotMax));
  123. output.writeAttribute("y2", position(items[i].plots[j].max, plotMin, plotMax));
  124. output.writeEndElement(); //line
  125. output.writeStartElement("text"); //upper quartile label
  126. output.writeAttribute("class", "box");
  127. output.writeAttribute("dy", ".3em");
  128. output.writeAttribute("dx", "-6");
  129. output.writeAttribute("x", "0");
  130. output.writeAttribute("y", position(items[i].plots[j].upper_quart, plotMin, plotMax));
  131. output.writeAttribute("text-anchor", "end");
  132. output.writeCharacters(Number(items[i].plots[j].upper_quart * 100).toFixed(2) + "%");
  133. output.writeEndElement(); //text
  134. output.writeStartElement("text"); //lower quartile label
  135. output.writeAttribute("class", "box");
  136. output.writeAttribute("dy", ".3em");
  137. output.writeAttribute("dx", "-6");
  138. output.writeAttribute("x", "0");
  139. output.writeAttribute("y", position(items[i].plots[j].lower_quart, plotMin, plotMax));
  140. output.writeAttribute("text-anchor", "end");
  141. output.writeCharacters(Number(items[i].plots[j].lower_quart * 100).toFixed(2) + "%");
  142. output.writeEndElement(); //text
  143. output.writeStartElement("text"); //median label
  144. output.writeAttribute("class", "box");
  145. output.writeAttribute("dy", ".3em");
  146. output.writeAttribute("dx", "6");
  147. output.writeAttribute("x", "54");
  148. output.writeAttribute("y", position(items[i].plots[j].median, plotMin, plotMax));
  149. output.writeAttribute("text-anchor", "start");
  150. output.writeCharacters(Number(items[i].plots[j].median * 100).toFixed(2) + "%");
  151. output.writeEndElement(); //text
  152. output.writeStartElement("text"); //minimum label
  153. output.writeAttribute("class", "whisker");
  154. output.writeAttribute("dy", ".3em");
  155. output.writeAttribute("dx", "6");
  156. output.writeAttribute("x", "54");
  157. output.writeAttribute("y", position(items[i].plots[j].min, plotMin, plotMax));
  158. output.writeAttribute("text-anchor", "start");
  159. output.writeCharacters(Number(items[i].plots[j].min * 100).toFixed(2) + "%");
  160. output.writeEndElement(); //text
  161. output.writeStartElement("text"); //maximum label
  162. output.writeAttribute("class", "whisker");
  163. output.writeAttribute("dy", ".3em");
  164. output.writeAttribute("dx", "6");
  165. output.writeAttribute("x", "54");
  166. output.writeAttribute("y", position(items[i].plots[j].max, plotMin, plotMax));
  167. output.writeAttribute("text-anchor", "start");
  168. output.writeCharacters(Number(items[i].plots[j].max * 100).toFixed(2) + "%");
  169. output.writeEndElement(); //text
  170. output.writeEndElement(); //g
  171. }
  172. output.writeStartElement("g"); //mean line and label
  173. output.writeAttribute("transform", "translate(0, 30)");
  174. output.writeStartElement("line");
  175. output.writeAttribute("class", "center");
  176. output.writeAttribute("style", "stroke: #000; stroke-width: 1px;");
  177. output.writeAttribute("x1", "0");
  178. output.writeAttribute("x2", (items[i].plots.length * 179) - 16);
  179. output.writeAttribute("y1", position(items[i].mean, plotMin, plotMax));
  180. output.writeAttribute("y2", position(items[i].mean, plotMin, plotMax));
  181. output.writeEndElement(); //line
  182. output.writeStartElement("text");
  183. output.writeAttribute("class", "box");
  184. output.writeAttribute("dy", ".3em");
  185. output.writeAttribute("dx", "-6");
  186. output.writeAttribute("x", "0");
  187. output.writeAttribute("y", position(items[i].mean, plotMin, plotMax));
  188. output.writeAttribute("text-anchor", "end");
  189. output.writeCharacters(Number(items[i].mean * 100).toFixed(2) + "%");
  190. output.writeEndElement(); //text
  191. output.writeEndElement(); //g
  192. output.writeStartElement("g") //x axis
  193. output.writeAttribute("class", "x axis");
  194. output.writeAttribute("transform", "translate(0,340)");
  195. for(var j = 0; j < items[i].plots.length; j++) {
  196. output.writeStartElement("g"); //name and population size
  197. output.writeAttribute("class", "tick");
  198. output.writeAttribute("transform", "translate(" + (82 + (j * 179)) + ",0)");
  199. output.writeStartElement("line"); //tick
  200. output.writeAttribute("y2", "6");
  201. output.writeAttribute("x2", "0");
  202. output.writeEndElement(); //line
  203. output.writeStartElement("text"); //label
  204. output.writeAttribute("style", "text-anchor: middle");
  205. output.writeAttribute("y", "9");
  206. output.writeAttribute("x", "0");
  207. output.writeStartElement("tspan");
  208. output.writeAttribute("x", "0");
  209. output.writeAttribute("dy", ".71em");
  210. output.writeCharacters(items[i].plots[j].name);
  211. output.writeEndElement(); //tspan
  212. output.writeStartElement("tspan");
  213. output.writeAttribute("x", "0");
  214. output.writeAttribute("dy", "1.42em");
  215. output.writeCharacters("(n=" + items[i].plots[j].count + ")");
  216. output.writeEndElement(); //tspan
  217. output.writeEndElement(); //text
  218. output.writeEndElement(); //g
  219. }
  220. output.writeStartElement("path");
  221. output.writeAttribute("class", "domain");
  222. output.writeAttribute("d", "M0,6V0H" + ((items[i].plots.length * 179) - 16) + "V6");
  223. output.writeEndElement(); //path
  224. output.writeEndElement(); //g
  225. output.writeStartElement("text") //chart title
  226. output.writeAttribute("x", (84 + (items[i].plots.length * 179))/2);
  227. output.writeAttribute("y", "15");
  228. output.writeAttribute("text-anchor", "middle");
  229. output.writeAttribute("style", "font-size: 18px;");
  230. output.writeCharacters(items[i].name);
  231. output.writeEndElement(); //text
  232. output.writeEndElement(); //g
  233. output.writeEndElement(); //svg
  234. }
  235. output.writeEndElement(); //body
  236. output.writeEndElement(); //html
  237. output.writeEndDocument();
  238. report.setContent(buffer);
  239. buffer.close();
  240. ]]>
  241. </program>
  242. </window>