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

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