PageRenderTime 41ms CodeModel.GetById 1ms app.highlight 35ms RepoModel.GetById 1ms app.codeStats 0ms

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