PageRenderTime 43ms CodeModel.GetById 14ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/googlecode/charts4j/MarkableChartTest.java

http://charts4j.googlecode.com/
Java | 190 lines | 123 code | 18 blank | 49 comment | 0 complexity | 5947e27f736cb5150b89b3f582f44b3b MD5 | raw file
  1/**
  2 *
  3 * The MIT License
  4 *
  5 * Copyright (c) 2011 the original author or authors.
  6 *
  7 * Permission is hereby granted, free of charge, to any person obtaining a copy
  8 * of this software and associated documentation files (the "Software"), to deal
  9 * in the Software without restriction, including without limitation the rights
 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11 * copies of the Software, and to permit persons to whom the Software is
 12 * furnished to do so, subject to the following conditions:
 13
 14 * The above copyright notice and this permission notice shall be included in
 15 * all copies or substantial portions of the Software.
 16
 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23 * THE SOFTWARE.
 24 */
 25
 26package com.googlecode.charts4j;
 27
 28import static com.googlecode.charts4j.Color.*;
 29import static com.googlecode.charts4j.UrlUtil.normalize;
 30import static org.junit.Assert.assertEquals;
 31
 32import java.util.logging.Level;
 33import java.util.logging.Logger;
 34
 35import org.junit.After;
 36import org.junit.AfterClass;
 37import org.junit.Before;
 38import org.junit.BeforeClass;
 39import org.junit.Test;
 40
 41/**
 42 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 43 *
 44 */
 45public class MarkableChartTest {
 46
 47    @BeforeClass
 48    public static void setUpBeforeClass() throws Exception {
 49        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.ALL);
 50    }
 51
 52    /**
 53     * @throws java.lang.Exception
 54     */
 55    @AfterClass
 56    public static void tearDownAfterClass() throws Exception {
 57    }
 58
 59    /**
 60     * @throws java.lang.Exception
 61     */
 62    @Before
 63    public void setUp() throws Exception {
 64    }
 65
 66    /**
 67     * @throws java.lang.Exception
 68     */
 69    @After
 70    public void tearDown() throws Exception {
 71    }
 72
 73    @Test
 74    public void test0() {
 75        final Line line = Plots.newLine(Data.newData(10, 20, 30));
 76        // Defining Line
 77        final Line line2 = Plots.newLine(Data.newData(0, 45, 35, 75, 100));
 78        line2.setColor(RED);
 79        line2.addShapeMarker(Shape.DIAMOND, BLUE, 20, 1);
 80        line2.addShapeMarker(Shape.CIRCLE, RED, 20, 2);
 81        line2.addTextMarker("Hello There", BLACK, 20, 3);
 82        // Defining chart.
 83        final LineChart chart = GCharts.newLineChart(line, line2);
 84        chart.setSize(400, 300);
 85        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
 86        String expectedString = "http://chart.apis.google.com/chart?chs=400x300&chd=e:GaMzTN,AAczWZv...&chco=000000,FF0000&chm=d,0000FF,1,1,20,0|o,FF0000,1,2,20,0|tHello+There,000000,1,3,20,0&cht=lc";
 87        assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
 88    }
 89
 90    @Test
 91    public void test1() {
 92        final Data xData = Data.newData(10, 20, 30);
 93        final Data yData = Data.newData(10, 20, 30);
 94        final ScatterPlotData data = Plots.newScatterPlotData(xData, yData);
 95        data.addShapeMarker(Shape.DIAMOND, RED, 20, 0);
 96        final ScatterPlot chart = GCharts.newScatterPlot(data);
 97        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
 98        String expectedString = "http://chart.apis.google.com/chart?chd=e:GaMzTN,GaMzTN&chm=o,000000,0,0,10,0|d,FF0000,0,0,20,0&chs=200x125&cht=s";
 99        assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
100    }
101
102    // This test exposes a known bug in the google chart api. In particular, you
103    // can't mix and match shape marker in scatter plots.
104    @Test
105    public void test2() {
106        final Data xData = Data.newData(10, 20, 30);
107        final Data yData = Data.newData(10, 20, 30);
108        final ScatterPlotData data = Plots.newScatterPlotData(xData, yData);
109        data.addShapeMarker(Shape.ARROW, GREEN, 5, 0);
110        data.addShapeMarker(Shape.CIRCLE, BLUE, 20, 1);
111        data.addShapeMarker(Shape.DIAMOND, RED, 10, 2);
112        final ScatterPlot chart = GCharts.newScatterPlot(data);
113        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
114        String expectedString = "http://chart.apis.google.com/chart?chd=e:GaMzTN,GaMzTN&chm=o,000000,0,0,10,0|a,008000,0,0,5,0|o,0000FF,0,1,20,0|d,FF0000,0,2,10,0&chs=200x125&cht=s";
115        assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
116    }
117
118    @Test
119    public void test3() {
120        final Line line = Plots.newLine(Data.newData(10, 20, 30));
121        final LineChart chart = GCharts.newLineChart(line);
122        chart.addHorizontalRangeMarker(20, 80, RED);
123        chart.addVerticalRangeMarker(20, 80, BLUE);
124        // Defining background and chart fills.
125        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
126        String expectedString = "http://chart.apis.google.com/chart?chd=e:GaMzTN&chm=r,FF0000,0,0.20,0.80|R,0000FF,0,0.20,0.80&chs=200x125&cht=lc";
127        assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
128    }
129
130    @Test
131    public void test4() {
132        final Line line1 = Plots.newLine(Data.newData(70, 80, 90));
133        final Line line2 = Plots.newLine(Data.newData(40, 50, 60));
134        final Line line3 = Plots.newLine(Data.newData(10, 20, 30));
135        line1.setFillAreaColor(GREEN);
136        line2.setFillAreaColor(BLUE);
137        line3.setFillAreaColor(RED);
138        final LineChart chart = GCharts.newLineChart(line1, line2, line3);
139        // Defining background and chart fills.
140        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
141        String expectedString = "http://chart.apis.google.com/chart?cht=lc&chs=200x125&chm=B,008000,0,0,0|B,0000FF,1,0,0|B,FF0000,2,0,0&chd=e:szzM5m,ZmgAmZ,GaMzTN";
142        assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
143    }
144
145    @Test
146    public void testPriority() {
147        // Defining Line
148        final Line line = Plots.newLine(Data.newData(0, 45, 35, 75, 100));
149        line.setColor(GREEN);
150        Marker diamond = Markers.newShapeMarker(Shape.DIAMOND, BLUE, 20, Priority.HIGH);
151        Marker circle = Markers.newShapeMarker(Shape.CIRCLE, RED, 20, Priority.LOW);
152        line.addMarker(diamond, 1);
153        line.addMarker(circle, 2);
154        // Defining chart.
155        final LineChart chart = GCharts.newLineChart(line);
156        chart.setSize(400, 300);
157        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
158        String expectedString = "http://chart.apis.google.com/chart?cht=lc&chs=400x300&chco=008000&chm=d,0000FF,0,1,20,1|o,FF0000,0,2,20,-1&chd=e:AAczWZv...";
159        assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
160    }
161
162    @Test
163    public void testShapeMarkedPoints() {
164        // Defining Line
165        final Line line = Plots.newLine(Data.newData(0, 45, 35, 75, 100, 10, 20, 30));
166        Marker diamond = Markers.newShapeMarker(Shape.DIAMOND, BLUE, 20, Priority.HIGH);
167        line.addMarkers(diamond, 1);
168        // Defining chart.
169        final LineChart chart = GCharts.newLineChart(line);
170        chart.setSize(400, 300);
171        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
172        String expectedString = "http://chart.apis.google.com/chart?chd=e:AAczWZv...GaMzTN&chm=d,0000FF,0,0:7:1,20,1&chs=400x300&cht=lc";
173        assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
174    }
175
176    @Test
177    public void testTextMarkedPoints() {
178        // Defining Line
179        final Line line = Plots.newLine(Data.newData(0, 45, 35, 75, 100, 10, 20, 30));
180        Marker diamond = Markers.newTextMarker("Z", BLUE, 12);
181        line.addMarkers(diamond, 2, 7, 2);
182        // Defining chart.
183        final LineChart chart = GCharts.newLineChart(line);
184        chart.setSize(400, 300);
185        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
186        String expectedString = "http://chart.apis.google.com/chart?chd=e:AAczWZv...GaMzTN&chm=tZ,0000FF,0,2:6:2,12,0&chs=400x300&cht=lc";
187        assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
188    }
189
190}