/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. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. package com.googlecode.charts4j;
  24. import static com.googlecode.charts4j.Color.*;
  25. import static com.googlecode.charts4j.UrlUtil.normalize;
  26. import static org.junit.Assert.assertEquals;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import org.junit.After;
  30. import org.junit.AfterClass;
  31. import org.junit.Before;
  32. import org.junit.BeforeClass;
  33. import org.junit.Test;
  34. /**
  35. * @author Julien Chastang (julien.c.chastang at gmail dot com)
  36. *
  37. */
  38. public class MarkableChartTest {
  39. @BeforeClass
  40. public static void setUpBeforeClass() throws Exception {
  41. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.ALL);
  42. }
  43. /**
  44. * @throws java.lang.Exception
  45. */
  46. @AfterClass
  47. public static void tearDownAfterClass() throws Exception {
  48. }
  49. /**
  50. * @throws java.lang.Exception
  51. */
  52. @Before
  53. public void setUp() throws Exception {
  54. }
  55. /**
  56. * @throws java.lang.Exception
  57. */
  58. @After
  59. public void tearDown() throws Exception {
  60. }
  61. @Test
  62. public void test0() {
  63. final Line line = Plots.newLine(Data.newData(10, 20, 30));
  64. // Defining Line
  65. final Line line2 = Plots.newLine(Data.newData(0, 45, 35, 75, 100));
  66. line2.setColor(RED);
  67. line2.addShapeMarker(Shape.DIAMOND, BLUE, 20, 1);
  68. line2.addShapeMarker(Shape.CIRCLE, RED, 20, 2);
  69. line2.addTextMarker("Hello There", BLACK, 20, 3);
  70. // Defining chart.
  71. final LineChart chart = GCharts.newLineChart(line, line2);
  72. chart.setSize(400, 300);
  73. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
  74. 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";
  75. assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
  76. }
  77. @Test
  78. public void test1() {
  79. final Data xData = Data.newData(10, 20, 30);
  80. final Data yData = Data.newData(10, 20, 30);
  81. final ScatterPlotData data = Plots.newScatterPlotData(xData, yData);
  82. data.addShapeMarker(Shape.DIAMOND, RED, 20, 0);
  83. final ScatterPlot chart = GCharts.newScatterPlot(data);
  84. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
  85. 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";
  86. assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
  87. }
  88. // This test exposes a known bug in the google chart api. In particular, you
  89. // can't mix and match shape marker in scatter plots.
  90. @Test
  91. public void test2() {
  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.ARROW, GREEN, 5, 0);
  96. data.addShapeMarker(Shape.CIRCLE, BLUE, 20, 1);
  97. data.addShapeMarker(Shape.DIAMOND, RED, 10, 2);
  98. final ScatterPlot chart = GCharts.newScatterPlot(data);
  99. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
  100. 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";
  101. assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
  102. }
  103. @Test
  104. public void test3() {
  105. final Line line = Plots.newLine(Data.newData(10, 20, 30));
  106. final LineChart chart = GCharts.newLineChart(line);
  107. chart.addHorizontalRangeMarker(20, 80, RED);
  108. chart.addVerticalRangeMarker(20, 80, BLUE);
  109. // Defining background and chart fills.
  110. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
  111. 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";
  112. assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
  113. }
  114. @Test
  115. public void test4() {
  116. final Line line1 = Plots.newLine(Data.newData(70, 80, 90));
  117. final Line line2 = Plots.newLine(Data.newData(40, 50, 60));
  118. final Line line3 = Plots.newLine(Data.newData(10, 20, 30));
  119. line1.setFillAreaColor(GREEN);
  120. line2.setFillAreaColor(BLUE);
  121. line3.setFillAreaColor(RED);
  122. final LineChart chart = GCharts.newLineChart(line1, line2, line3);
  123. // Defining background and chart fills.
  124. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
  125. 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";
  126. assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
  127. }
  128. @Test
  129. public void testPriority() {
  130. // Defining Line
  131. final Line line = Plots.newLine(Data.newData(0, 45, 35, 75, 100));
  132. line.setColor(GREEN);
  133. Marker diamond = Markers.newShapeMarker(Shape.DIAMOND, BLUE, 20, Priority.HIGH);
  134. Marker circle = Markers.newShapeMarker(Shape.CIRCLE, RED, 20, Priority.LOW);
  135. line.addMarker(diamond, 1);
  136. line.addMarker(circle, 2);
  137. // Defining chart.
  138. final LineChart chart = GCharts.newLineChart(line);
  139. chart.setSize(400, 300);
  140. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
  141. 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...";
  142. assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
  143. }
  144. @Test
  145. public void testShapeMarkedPoints() {
  146. // Defining Line
  147. final Line line = Plots.newLine(Data.newData(0, 45, 35, 75, 100, 10, 20, 30));
  148. Marker diamond = Markers.newShapeMarker(Shape.DIAMOND, BLUE, 20, Priority.HIGH);
  149. line.addMarkers(diamond, 1);
  150. // Defining chart.
  151. final LineChart chart = GCharts.newLineChart(line);
  152. chart.setSize(400, 300);
  153. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
  154. 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";
  155. assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
  156. }
  157. @Test
  158. public void testTextMarkedPoints() {
  159. // Defining Line
  160. final Line line = Plots.newLine(Data.newData(0, 45, 35, 75, 100, 10, 20, 30));
  161. Marker diamond = Markers.newTextMarker("Z", BLUE, 12);
  162. line.addMarkers(diamond, 2, 7, 2);
  163. // Defining chart.
  164. final LineChart chart = GCharts.newLineChart(line);
  165. chart.setSize(400, 300);
  166. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(chart.toURLString());
  167. 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";
  168. assertEquals("Junit error", normalize(expectedString), normalize(chart.toURLString()));
  169. }
  170. }