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

http://charts4j.googlecode.com/ · Java · 124 lines · 82 code · 11 blank · 31 comment · 0 complexity · 872f043dcdd2227238a9b641e4cb01ff 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.example;
  24. import static com.googlecode.charts4j.Color.*;
  25. import static com.googlecode.charts4j.USAState.Code.*;
  26. import static com.googlecode.charts4j.UrlUtil.normalize;
  27. import static org.junit.Assert.assertEquals;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import org.junit.BeforeClass;
  31. import org.junit.Test;
  32. import com.googlecode.charts4j.Fills;
  33. import com.googlecode.charts4j.GCharts;
  34. import com.googlecode.charts4j.GeographicalArea;
  35. import com.googlecode.charts4j.MapChart;
  36. import com.googlecode.charts4j.PoliticalBoundary;
  37. import com.googlecode.charts4j.USAState;
  38. /**
  39. *
  40. * @author Julien Chastang (julien.c.chastang at gmail dot com)
  41. */
  42. public class MapChartExample {
  43. @BeforeClass
  44. public static void setUpBeforeClass() throws Exception {
  45. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.ALL);
  46. }
  47. @Test
  48. public void example1() {
  49. // EXAMPLE CODE START
  50. MapChart chart = GCharts.newMapChart(GeographicalArea.USA);
  51. PoliticalBoundary al = new USAState( AL, 10);
  52. PoliticalBoundary ak = new USAState( AK, 10);
  53. PoliticalBoundary az = new USAState( AZ, 10);
  54. PoliticalBoundary ar = new USAState( AR, 10);
  55. PoliticalBoundary ca = new USAState( CA, 90);
  56. PoliticalBoundary co = new USAState( CO, 50);
  57. PoliticalBoundary ct = new USAState( CT, 90);
  58. PoliticalBoundary de = new USAState( DE, 90);
  59. PoliticalBoundary fl = new USAState( FL, 50);
  60. PoliticalBoundary ga = new USAState( GA, 10);
  61. PoliticalBoundary hi = new USAState( HI, 90);
  62. PoliticalBoundary id = new USAState( ID, 10);
  63. PoliticalBoundary il = new USAState( IL, 90);
  64. PoliticalBoundary in = new USAState( IN, 50);
  65. PoliticalBoundary ia = new USAState( IA, 50);
  66. PoliticalBoundary ks = new USAState( KS, 10);
  67. PoliticalBoundary ky = new USAState( KY, 10);
  68. PoliticalBoundary la = new USAState( LA, 10);
  69. PoliticalBoundary me = new USAState( ME, 50);
  70. PoliticalBoundary md = new USAState( MD, 90);
  71. PoliticalBoundary ma = new USAState( MA, 90);
  72. PoliticalBoundary mi = new USAState( MI, 90);
  73. PoliticalBoundary mn = new USAState( MN, 90);
  74. PoliticalBoundary ms = new USAState( MS, 10);
  75. PoliticalBoundary mo = new USAState( MO, 10);
  76. PoliticalBoundary mt = new USAState( MT, 90);
  77. PoliticalBoundary ne = new USAState( NE, 10);
  78. PoliticalBoundary nv = new USAState( NV, 90);
  79. PoliticalBoundary nh = new USAState( NH, 90);
  80. PoliticalBoundary nj = new USAState( NJ, 90);
  81. PoliticalBoundary nm = new USAState( NM, 50);
  82. PoliticalBoundary ny = new USAState( NY, 90);
  83. PoliticalBoundary nc = new USAState( NC, 50);
  84. PoliticalBoundary nd = new USAState( ND, 90);
  85. PoliticalBoundary oh = new USAState( OH, 90);
  86. PoliticalBoundary ok = new USAState( OK, 10);
  87. PoliticalBoundary or = new USAState( OR, 90);
  88. PoliticalBoundary pa = new USAState( PA, 50);
  89. PoliticalBoundary ri = new USAState( RI, 90);
  90. PoliticalBoundary sc = new USAState( SC, 50);
  91. PoliticalBoundary sd = new USAState( SD, 10);
  92. PoliticalBoundary tn = new USAState( TN, 10);
  93. PoliticalBoundary tx = new USAState( TX, 10);
  94. PoliticalBoundary ut = new USAState( UT, 10);
  95. PoliticalBoundary vt = new USAState( VT, 90);
  96. PoliticalBoundary va = new USAState( VA, 50);
  97. PoliticalBoundary wa = new USAState( WA, 90);
  98. PoliticalBoundary wv = new USAState( WV, 10);
  99. PoliticalBoundary wi = new USAState( WI, 90);
  100. PoliticalBoundary wy = new USAState( WY, 10);
  101. chart.addPoliticalBoundaries(al,ak,az,ar,ca,co,ct,de,fl,ga,hi,id,il,in,ia,ks,ky,la,me,md,ma,mi,mn,ms,mo,mt,ne,nv,nh,nj,nm,ny,nc,nd,oh,ok,or,pa,ri,sc,sd,tn,tx,ut,vt,va,wa,wv,wi,wy);
  102. chart.setColorGradient(WHITE, RED, BLUE);
  103. chart.setBackgroundFill(Fills.newSolidFill(ALICEBLUE));
  104. String url = chart.toURLString();
  105. // EXAMPLE CODE END. Use this url string in your web or
  106. // Internet application.
  107. Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(url);
  108. String expectedString = "http://chart.apis.google.com/chart?chf=bg,s,F0F8FF&chs=440x220&chd=e:GaGaGaGa5mgA5m5mgAGa5mGa5mgAgAGaGaGagA5m5m5m5mGaGa5mGa5m5m5mgA5mgA5m5mGa5mgA5mgAGaGaGaGa5mgA5mGa5mGa&chtm=usa&chco=FFFFFF,FF0000,0000FF&chld=ALAKAZARCACOCTDEFLGAHIIDILINIAKSKYLAMEMDMAMIMNMSMOMTNENVNHNJNMNYNCNDOHOKORPARISCSDTNTXUTVTVAWAWVWIWY&cht=t";
  109. assertEquals("Junit error", normalize(expectedString), normalize(url));
  110. }
  111. }