/sigmah/src/test/java/org/sigmah/server/report/generator/map/GraphTest.java

http://sigma-h.googlecode.com/ · Java · 167 lines · 109 code · 43 blank · 15 comment · 10 complexity · 3f14027d4cc4ba50e7ff8f72aedfbfa8 MD5 · raw file

  1. /*
  2. * All Sigmah code is released under the GNU General Public License v3
  3. * See COPYRIGHT.txt and LICENSE.txt.
  4. */
  5. package org.sigmah.server.report.generator.map;
  6. import org.junit.Test;
  7. import org.sigmah.shared.report.content.Point;
  8. import java.io.File;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. /*
  14. * @author Alex Bertram
  15. */
  16. public class GraphTest {
  17. @Test
  18. public void testGraph() throws Exception {
  19. List<PointValue> points = new ArrayList<PointValue>();
  20. points.add(new PointValue(null, null, 100, new Point(380, 530)));
  21. points.add(new PointValue(null, null, 200, new Point(500, 500)));
  22. points.add(new PointValue(null, null, 50, new Point(650, 550)));
  23. points.add(new PointValue(null, null, 300, new Point(600, 600)));
  24. points.add(new PointValue(null, null, 200, new Point(650, 650)));
  25. points.add(new PointValue(null, null, 30, new Point(500, 1300)));
  26. points.add(new PointValue(null, null, 150, new Point(200, 200)));
  27. points.add(new PointValue(null, null, 200, new Point(350, 200)));
  28. points.add(new PointValue(null, null, 150, new Point(500, 200)));
  29. points.add(new PointValue(null, null, 30, new Point(700, 200)));
  30. // MarkerGraph graph = new MarkerGraph(points, new GraduatedLogCalculator(50, 100), 100);
  31. }
  32. protected void saveGraphImage(String testName, MarkerGraph graph, int maxRadius) throws Exception {
  33. File outputDir = new File("target/report-tests");
  34. outputDir.mkdirs();
  35. File outputFile = new File("target/report-tests/" + testName + ".svg");
  36. FileWriter svg = new FileWriter(outputFile);
  37. svg.write("<svg width='100%' height='100%' transform='scale(50)' version='1.1' xmlns='http://www.w3.org/2000/svg'>");
  38. for(MarkerGraph.Edge edge : graph.getEdges()) {
  39. svg.write(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' " +
  40. "style='stroke:rgb(99,99,99);stroke-width:1'/>",
  41. edge.a.getPoint().getX(),
  42. edge.a.getPoint().getY(),
  43. edge.b.getPoint().getX(),
  44. edge.b.getPoint().getY()));
  45. }
  46. for(MarkerGraph.Node node : graph.getNodes()) {
  47. svg.write(String.format("<circle cx='%d' cy='%d' r='1.5' " +
  48. "style='stroke:none; fill: blue'/>",
  49. node.getPoint().getX(),
  50. node.getPoint().getY()));
  51. // svg.write(String.format("<circle cx='%d' cy='%d' r='%d' " +
  52. // "style='stroke:blue; stroke-width:1; fill: none' title='node%d'/>",
  53. // node.getPoint().getX(),
  54. // node.getPoint().getY(),
  55. // maxRadius,
  56. // node.index));
  57. }
  58. svg.write("</svg>");
  59. svg.close();
  60. }
  61. protected void saveClusters(MarkerGraph graph, String fileName, List<Cluster> clusters) throws IOException {
  62. File outputDir = new File("target/report-tests");
  63. outputDir.mkdirs();
  64. File outputFile = new File("target/report-tests/" + fileName + ".svg");
  65. FileWriter svg = new FileWriter(outputFile);
  66. svg.write("<svg width='100%' height='100%' transform='scale(500)' version='1.1' " +
  67. "xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' >\n");
  68. for(MarkerGraph.Edge edge : graph.getEdges()) {
  69. svg.write(String.format("<path d='M%d %d L%d %d' " +
  70. "style='stroke:rgb(92,92,92);stroke-width:0.25'/>\n",
  71. edge.a.getPoint().getX(),
  72. edge.a.getPoint().getY(),
  73. edge.b.getPoint().getX(),
  74. edge.b.getPoint().getY()));
  75. }
  76. String[] colors = new String[] {
  77. "antiquewhite", "blue", "brown", "chartreuse", "cornflowerblue",
  78. "crimson", "darkkhaki", "darkorange", "darkorchid", "lightpink",
  79. "lightseagreen", "lightskyblue", "lime", "limegreen", "magenta",
  80. "mediumaqua" };
  81. int colorIndex =0;
  82. for(int i=0; i!= clusters.size();++i) {
  83. Cluster cluster = clusters.get(i);
  84. svg.write(String.format("<circle cx='%d' cy='%d' r='%f' " +
  85. "style='fill: %s; fill-opacity: 0.5; stroke:none'/>\n",
  86. cluster.getPoint().getX(),
  87. cluster.getPoint().getY(),
  88. cluster.getRadius(),
  89. colors[colorIndex]));
  90. for(PointValue pointValue : cluster.getPointValues()) {
  91. svg.write(String.format("<circle cx='%d' cy='%d' r='1.5' " +
  92. "style='stroke:rgb(92,92,92); stroke-width:0.1; fill: %s' title='%f'/>\n",
  93. pointValue.px.getX(),
  94. pointValue.px.getY(),
  95. colors[colorIndex],
  96. pointValue.value));
  97. }
  98. colorIndex++;
  99. if(colorIndex == colors.length) {
  100. colorIndex = 0;
  101. }
  102. }
  103. // label subgraphs
  104. List<List<MarkerGraph.Node>> subgraphs = graph.getSubgraphs();
  105. for(int i=0; i!=subgraphs.size();++i) {
  106. Point p = findLR(subgraphs.get(i));
  107. svg.write(String.format("<text x='%d' y='%d'>%d</text>\n",
  108. p.getX() + 5,
  109. p.getY(),
  110. i));
  111. }
  112. svg.write("</svg>\n");
  113. svg.close();
  114. }
  115. private Point findLR(List<MarkerGraph.Node> nodes) {
  116. Point lr = new Point(Integer.MIN_VALUE, 0);
  117. for(MarkerGraph.Node node : nodes) {
  118. if(node.getPoint().getX() > lr.getX()) {
  119. lr = node.getPoint();
  120. }
  121. }
  122. return lr;
  123. }
  124. }