/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

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