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.

SimplePlanDrawer.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.plan;
  19. import java.text.DateFormat;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import org.w3c.dom.DOMImplementation;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.Element;
  26. import org.apache.batik.dom.svg.SVGDOMImplementation;
  27. import org.apache.fop.svg.SVGUtilities;
  28. /**
  29. * Simple plan drawer implementation.
  30. */
  31. public class SimplePlanDrawer implements PlanDrawer {
  32. private static final String SVG_NAMESPACE = SVGDOMImplementation.SVG_NAMESPACE_URI;
  33. private float fontSize;
  34. private HashMap hints;
  35. private java.awt.Font font = null;
  36. private boolean bord = false;
  37. private float lSpace = 15;
  38. private float width;
  39. private float height;
  40. private float topEdge;
  41. private float rightEdge;
  42. private String[] colours;
  43. private String[] darkcolours;
  44. private Date startDate;
  45. private Date endDate;
  46. /**
  47. * Sets the start date.
  48. * @param sd start date
  49. */
  50. public void setStartDate(Date sd) {
  51. startDate = sd;
  52. }
  53. /**
  54. * Sets the end date.
  55. * @param ed end date
  56. */
  57. public void setEndDate(Date ed) {
  58. endDate = ed;
  59. }
  60. /**
  61. * @see org.apache.fop.plan.PlanDrawer#createDocument(EventList, float, float, HashMap)
  62. */
  63. public Document createDocument(EventList data, float w, float h,
  64. HashMap hints) {
  65. this.width = w;
  66. this.height = h;
  67. this.hints = hints;
  68. fontSize = ((Float) hints.get(PlanHints.FONT_SIZE)).floatValue();
  69. bord = ((Boolean) hints.get(PlanHints.PLAN_BORDER)).booleanValue();
  70. String title = "";
  71. DOMImplementation impl =
  72. SVGDOMImplementation.getDOMImplementation();
  73. Document doc = impl.createDocument(SVG_NAMESPACE, "svg", null);
  74. Element svgRoot = doc.getDocumentElement();
  75. svgRoot.setAttributeNS(null, "width", "" + width);
  76. svgRoot.setAttributeNS(null, "height", "" + height);
  77. svgRoot.setAttributeNS(null, "style",
  78. "font-size:" + 8
  79. + ";font-family:"
  80. + hints.get(PlanHints.FONT_FAMILY));
  81. font = new java.awt.Font((String)hints.get(PlanHints.FONT_FAMILY),
  82. java.awt.Font.PLAIN, (int)fontSize);
  83. if (bord) {
  84. Element border =
  85. SVGUtilities.createRect(doc, 0, 0, width, height);
  86. border.setAttributeNS(null, "style", "stroke:black;fill:none");
  87. svgRoot.appendChild(border);
  88. }
  89. float strwidth = SVGUtilities.getStringWidth(title, font);
  90. Element text;
  91. float pos = (float)(80 - strwidth / 2.0);
  92. if (pos < 5) {
  93. pos = 5;
  94. }
  95. text = SVGUtilities.createText(doc, pos, 18, title);
  96. text.setAttributeNS(null, "style", "font-size:14");
  97. svgRoot.appendChild(text);
  98. topEdge = SVGUtilities.getStringHeight(title, font) + 5;
  99. // add the actual pie chart
  100. addPlan(doc, svgRoot, data);
  101. //addLegend(doc, svgRoot, data);
  102. return doc;
  103. }
  104. protected void addPlan(Document doc, Element svgRoot, EventList data) {
  105. Date currentDate = new Date();
  106. Date lastWeek = startDate;
  107. Date future = endDate;
  108. Calendar lw = Calendar.getInstance();
  109. if (lastWeek == null || future == null) {
  110. int dow = lw.get(Calendar.DAY_OF_WEEK);
  111. lw.add(Calendar.DATE, -dow - 6);
  112. lastWeek = lw.getTime();
  113. lw.add(Calendar.DATE, 5 * 7);
  114. future = lw.getTime();
  115. }
  116. long totalDays = (long)((future.getTime() - lastWeek.getTime() + 43200000) / 86400000);
  117. lw.setTime(lastWeek);
  118. int startDay = lw.get(Calendar.DAY_OF_WEEK);
  119. float graphTop = topEdge;
  120. Element g;
  121. Element line;
  122. line = SVGUtilities.createLine(doc, 0, topEdge, width, topEdge);
  123. line.setAttributeNS(null, "style", "fill:none;stroke:black");
  124. svgRoot.appendChild(line);
  125. Element clip1 = SVGUtilities.createClip(doc,
  126. SVGUtilities.createPath(doc,
  127. "m0 0l126 0l0 " + height + "l-126 0z"), "clip1");
  128. Element clip2 = SVGUtilities.createClip(doc,
  129. SVGUtilities.createPath(doc,
  130. "m130 0l66 0l0 " + height + "l-66 0z"), "clip2");
  131. Element clip3 = SVGUtilities.createClip(doc,
  132. SVGUtilities.createPath(doc,
  133. "m200 0l" + (width - 200) + " 0l0 " + height + "l-"
  134. + (width - 200) + " 0z"), "clip3");
  135. svgRoot.appendChild(clip1);
  136. svgRoot.appendChild(clip2);
  137. svgRoot.appendChild(clip3);
  138. DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
  139. Element text;
  140. text = SVGUtilities.createText(doc, 201, topEdge - 1,
  141. df.format(lastWeek));
  142. svgRoot.appendChild(text);
  143. text = SVGUtilities.createText(doc, width, topEdge - 1,
  144. df.format(future));
  145. text.setAttributeNS(null, "text-anchor", "end");
  146. svgRoot.appendChild(text);
  147. line = SVGUtilities.createLine(doc, 128, topEdge, 128, height);
  148. line.setAttributeNS(null, "style", "fill:none;stroke:rgb(150,150,150)");
  149. svgRoot.appendChild(line);
  150. int offset = 0;
  151. for (int count = startDay; count < startDay + totalDays - 1; count++) {
  152. offset++;
  153. if (count % 7 == 0 || count % 7 == 1) {
  154. Element rect = SVGUtilities.createRect(doc,
  155. 200 + (offset - 1) * (width - 200) / (totalDays - 2),
  156. (float)(topEdge + 0.5),
  157. (width - 200) / (totalDays - 3),
  158. height - 1 - topEdge);
  159. rect.setAttributeNS(null, "style", "stroke:none;fill:rgb(230,230,230)");
  160. svgRoot.appendChild(rect);
  161. }
  162. line = SVGUtilities.createLine(doc,
  163. 200 + (offset - 1) * (width - 200) / (totalDays - 2),
  164. (float)(topEdge + 0.5),
  165. 200 + (offset - 1) * (width - 200) / (totalDays - 2),
  166. (float)(height - 0.5));
  167. line.setAttributeNS(null, "style", "fill:none;stroke:rgb(200,200,200)");
  168. svgRoot.appendChild(line);
  169. }
  170. for (int count = 0; count < data.getSize(); count++) {
  171. GroupInfo gi = data.getGroupInfo(count);
  172. g = SVGUtilities.createG(doc);
  173. text = SVGUtilities.createText(doc, 1, topEdge + 12,
  174. gi.getName());
  175. text.setAttributeNS(null, "style", "clip-path:url(#clip1)");
  176. g.appendChild(text);
  177. if (count > 0) {
  178. line = SVGUtilities.createLine(doc, 0, topEdge + 2,
  179. width, topEdge + 2);
  180. line.setAttributeNS(null, "style", "fill:none;stroke:rgb(100,100,100)");
  181. g.appendChild(line);
  182. }
  183. float lastTop = topEdge;
  184. topEdge += 14;
  185. boolean showing = false;
  186. for (int count1 = 0; count1 < gi.getSize(); count1++) {
  187. ActionInfo act = gi.getActionInfo(count1);
  188. String name = act.getOwner();
  189. String label = act.getLabel();
  190. text = SVGUtilities.createText(doc, 8, topEdge + 12, label);
  191. text.setAttributeNS(null, "style", "clip-path:url(#clip1)");
  192. g.appendChild(text);
  193. text = SVGUtilities.createText(doc, 130, topEdge + 12,
  194. name);
  195. text.setAttributeNS(null, "style", "clip-path:url(#clip2)");
  196. g.appendChild(text);
  197. int type = act.getType();
  198. Date start = act.getStartDate();
  199. Date end = act.getEndDate();
  200. if (end.after(lastWeek) && start.before(future)) {
  201. showing = true;
  202. int left = 200;
  203. int right = 500;
  204. int daysToStart = (int)((start.getTime()
  205. - lastWeek.getTime() + 43200000) / 86400000);
  206. int days = (int)((end.getTime() - start.getTime()
  207. + 43200000) / 86400000);
  208. int daysFromEnd =
  209. (int)((future.getTime() - end.getTime()
  210. + 43200000) / 86400000);
  211. Element taskGraphic;
  212. switch (type) {
  213. case ActionInfo.TASK:
  214. taskGraphic = SVGUtilities.createRect(doc,
  215. left + daysToStart * 300 / (totalDays - 2),
  216. topEdge + 2, days * 300 / (totalDays - 2), 10);
  217. taskGraphic.setAttributeNS(null,
  218. "style",
  219. "stroke:black;fill:blue;stroke-width:1;clip-path:url(#clip3)");
  220. g.appendChild(taskGraphic);
  221. break;
  222. case ActionInfo.MILESTONE:
  223. taskGraphic = SVGUtilities.createPath(doc,
  224. "m " + (left
  225. + daysToStart * 300 / (totalDays - 2) - 6)
  226. + " " + (topEdge + 6) + "l6 6l6-6l-6-6z");
  227. taskGraphic.setAttributeNS(null,
  228. "style",
  229. "stroke:black;fill:black;stroke-width:1;clip-path:url(#clip3)");
  230. g.appendChild(taskGraphic);
  231. text = SVGUtilities.createText(doc,
  232. left + daysToStart * 300 / (totalDays - 2) + 8,
  233. topEdge + 9, df.format(start));
  234. g.appendChild(text);
  235. break;
  236. case ActionInfo.GROUPING:
  237. taskGraphic = SVGUtilities.createPath(doc,
  238. "m " + (left
  239. + daysToStart * 300 / (totalDays - 2) - 6)
  240. + " " + (topEdge + 6) + "l6 -6l"
  241. + (days * 300 / (totalDays - 2))
  242. + " 0l6 6l-6 6l-4-4l"
  243. + -(days * 300 / (totalDays - 2) - 8)
  244. + " 0l-4 4l-6-6z");
  245. taskGraphic.setAttributeNS(null,
  246. "style",
  247. "stroke:black;fill:black;stroke-width:1;clip-path:url(#clip3)");
  248. g.appendChild(taskGraphic);
  249. break;
  250. default:
  251. break;
  252. }
  253. }
  254. topEdge += 14;
  255. }
  256. if (showing) {
  257. svgRoot.appendChild(g);
  258. } else {
  259. topEdge = lastTop;
  260. }
  261. }
  262. int currentDays =
  263. (int)((currentDate.getTime() - lastWeek.getTime()
  264. + 43200000) / 86400000);
  265. text = SVGUtilities.createText(doc,
  266. (float)(200 + (currentDays + 0.5) * 300 / 35),
  267. graphTop - 1, df.format(currentDate));
  268. text.setAttributeNS(null, "text-anchor", "middle");
  269. text.setAttributeNS(null, "style", "stroke:rgb(100,100,100)");
  270. svgRoot.appendChild(text);
  271. line = SVGUtilities.createLine(doc,
  272. (float)(200 + (currentDays + 0.5) * 300 / 35), graphTop,
  273. (float)(200 + (currentDays + 0.5) * 300 / 35), height);
  274. line.setAttributeNS(null, "style", "fill:none;stroke:rgb(200,50,50);stroke-dasharray:5,5");
  275. svgRoot.appendChild(line);
  276. }
  277. }