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 12KB

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