PageRenderTime 27ms CodeModel.GetById 9ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/src/test/java/com/googlecode/charts4j/example/GeneralExample.java

http://charts4j.googlecode.com/
Java | 115 lines | 83 code | 18 blank | 14 comment | 0 complexity | c2902559ac6fb27349f308fb58ec9a4c MD5 | raw file
  1package com.googlecode.charts4j.example;
  2
  3import static com.googlecode.charts4j.Color.*;
  4import static com.googlecode.charts4j.UrlUtil.normalize;
  5import static org.junit.Assert.assertEquals;
  6
  7import java.util.Arrays;
  8import java.util.logging.Level;
  9import java.util.logging.Logger;
 10
 11import org.junit.BeforeClass;
 12import org.junit.Test;
 13
 14import com.googlecode.charts4j.AxisLabelsFactory;
 15import com.googlecode.charts4j.BarChart;
 16import com.googlecode.charts4j.Data;
 17import com.googlecode.charts4j.GCharts;
 18import com.googlecode.charts4j.LineChart;
 19import com.googlecode.charts4j.Plot;
 20import com.googlecode.charts4j.Plots;
 21import com.googlecode.charts4j.RadarChart;
 22import com.googlecode.charts4j.ScatterPlot;
 23import com.googlecode.charts4j.Shape;
 24import com.googlecode.charts4j.XYLineChart;
 25
 26public class GeneralExample {
 27
 28    @BeforeClass
 29    public static void setUpBeforeClass() throws Exception {
 30        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.ALL);
 31    }
 32
 33    @Test
 34    //Defining a very simple chart.
 35    public void example1() {
 36        // EXAMPLE CODE START
 37        Plot plot = Plots.newPlot(Data.newData(0, 66.6, 33.3, 100));
 38        LineChart chart = GCharts.newLineChart(plot);
 39        String url = chart.toURLString();
 40        // EXAMPLE CODE END. Use this url string in your web or
 41        // Internet application.
 42        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(url);
 43        String expectedString = "http://chart.apis.google.com/chart?chd=e:AAqnVU..&chs=200x125&cht=lc";
 44        assertEquals("Junit error", normalize(expectedString), normalize(url));
 45    }
 46
 47    @Test
 48    //Note that data, grids, axis labels, range markers all snap together nicely.
 49    public void example2() {
 50        // EXAMPLE CODE START
 51        Plot plot = Plots.newPlot(Data.newData(0, 66.6, 33.3, 100));
 52        LineChart chart = GCharts.newLineChart(plot);
 53        chart.addHorizontalRangeMarker(33.3, 66.6, LIGHTBLUE);
 54        chart.setGrid(33.3, 33.3, 3, 3);
 55        chart.addXAxisLabels(AxisLabelsFactory.newAxisLabels(Arrays.asList("Peak","Valley"), Arrays.asList(33.3,66.6)));
 56        chart.addYAxisLabels(AxisLabelsFactory.newNumericAxisLabels(0,33.3,66.6,100));
 57        String url = chart.toURLString();
 58        // EXAMPLE CODE END. Use this url string in your web or
 59        // Internet application.
 60        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(url);
 61        String expectedString = "http://chart.apis.google.com/chart?chs=200x125&chd=e:AAqnVU..&chg=33.3,33.3,3,3&chxt=y,x&chxp=0,0.0,33.3,66.6,100.0|1,33.3,66.6&chxr=0,0.0,100.0|1,0.0,100.0&chxl=1:|Peak|Valley&chm=r,ADD8E6,0,0.33,0.67&cht=lc";
 62        assertEquals("Junit error", normalize(expectedString), normalize(url));
 63    }
 64
 65    @Test
 66    // Polymorphic chart example. A plot can be expressed in a
 67    // line chart, bar chart, radar chart, scatter plot.
 68    public void example3() {
 69        // EXAMPLE CODE START
 70        Plot plot = Plots.newPlot(Data.newData(0, 10, 20, 30, 40, 50, 60, 70, 80, 90));
 71        plot.addShapeMarkers(Shape.DIAMOND, BLUE, 12);
 72
 73        //Same data expressed in different charts.
 74        LineChart lineChart = GCharts.newLineChart(plot);
 75        lineChart.setSize(400, 200);
 76        BarChart barChart = GCharts.newBarChart(plot);
 77        barChart.setSize(400, 200);
 78        RadarChart radarChart = GCharts.newRadarChart(plot);
 79        radarChart.setSize(400, 200);
 80        ScatterPlot  scatterChart = GCharts.newScatterPlot(plot);
 81        scatterChart.setSize(400, 200);
 82        XYLineChart  xyLineChart = GCharts.newXYLineChart(plot);
 83        xyLineChart.setSize(400, 200);
 84
 85
 86        String lineChartUrl = lineChart.toURLString();
 87        String barChartUrl = barChart.toURLString();
 88        String radarChartUrl = radarChart.toURLString();
 89        String scatterChartUrl = scatterChart.toURLString();
 90        String xyLineChartUrl = xyLineChart.toURLString();
 91
 92        // EXAMPLE CODE END. Use these URLs string in your web or
 93        // Internet application.
 94
 95        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(lineChartUrl);
 96        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(barChartUrl);
 97        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(radarChartUrl);
 98        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(scatterChartUrl);
 99        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(xyLineChartUrl);
100
101
102        String expectedLineChartUrl    = "http://chart.apis.google.com/chart?chs=400x200&chd=e:AAGaMzTNZmgAmZszzM5m&chm=d,0000FF,0,-1,12,0&cht=lc";
103        String expectedBarChartUrl     = "http://chart.apis.google.com/chart?chs=400x200&chd=e:AAGaMzTNZmgAmZszzM5m&chm=d,0000FF,0,-1,12,0&chbh=23,4,8&cht=bvg";
104        String expectedRadarChartUrl   = "http://chart.apis.google.com/chart?chs=400x200&chd=e:AAGaMzTNZmgAmZszzM5m&chm=d,0000FF,0,-1,12,0&cht=r";
105        String expectedScatterChartUrl = "http://chart.apis.google.com/chart?chd=e:AAHHOOVVccjjqqxx44..,AAGaMzTNZmgAmZszzM5m&chm=d,0000FF,0,-1,12,0&chs=400x200&cht=s";
106        String expectedXYLineChartUrl  = "http://chart.apis.google.com/chart?chd=e:AAHHOOVVccjjqqxx44..,AAGaMzTNZmgAmZszzM5m&chm=d,0000FF,0,-1,12,0&chs=400x200&cht=lxy";
107
108
109        assertEquals("Junit error", normalize(expectedLineChartUrl), normalize(lineChartUrl));
110        assertEquals("Junit error", normalize(expectedBarChartUrl), normalize(barChartUrl));
111        assertEquals("Junit error", normalize(expectedRadarChartUrl), normalize(radarChartUrl));
112        assertEquals("Junit error", normalize(expectedScatterChartUrl), normalize(scatterChartUrl));
113        assertEquals("Junit error", normalize(expectedXYLineChartUrl), normalize(xyLineChartUrl));
114    }
115}