index.htm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>Highmaps Example</title>
  6. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  7. <style type="text/css">
  8. * {
  9. font-family: sans-serif;
  10. }
  11. #wrapper {
  12. height: 500px;
  13. width: 1000px;
  14. margin: 0 auto;
  15. padding: 0;
  16. }
  17. #container {
  18. float: left;
  19. height: 500px;
  20. width: 700px;
  21. margin: 0;
  22. }
  23. #info {
  24. float: left;
  25. width: 270px;
  26. padding-left: 20px;
  27. margin: 100px 0 0 0;
  28. border-left: 1px solid silver;
  29. }
  30. #info h2 {
  31. display: inline;
  32. font-size: 13pt;
  33. }
  34. #info .f32 .flag {
  35. vertical-align: bottom !important;
  36. }
  37. #info h4 {
  38. margin: 1em 0 0 0;
  39. }
  40. </style>
  41. <script type="text/javascript">
  42. $(function () {
  43. $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=world-population-history.csv&callback=?', function (csv) {
  44. // Parse the CSV Data
  45. /*Highcharts.data({
  46. csv: data,
  47. switchRowsAndColumns: true,
  48. parsed: function () {
  49. console.log(this.columns);
  50. }
  51. });*/
  52. // Very simple and case-specific CSV string splitting
  53. function CSVtoArray(text) {
  54. return text.replace(/^"/, '')
  55. .replace(/",$/, '')
  56. .split('","');
  57. };
  58. csv = csv.split(/\n/);
  59. var countries = {},
  60. mapChart,
  61. countryChart,
  62. numRegex = /^[0-9\.]+$/,
  63. quoteRegex = /\"/g,
  64. categories = CSVtoArray(csv[1]).slice(4);
  65. // Parse the CSV into arrays, one array each country
  66. $.each(csv.slice(2), function (j, line) {
  67. var row = CSVtoArray(line),
  68. data = row.slice(4);
  69. $.each(data, function (i, val) {
  70. val = val.replace(quoteRegex, '');
  71. if (numRegex.test(val)) {
  72. val = parseInt(val);
  73. } else if (!val) {
  74. val = null;
  75. }
  76. data[i] = val;
  77. });
  78. countries[row[1]] = {
  79. name: row[0],
  80. code3: row[1],
  81. data: data
  82. };
  83. });
  84. // For each country, use the latest value for current population
  85. var data = [];
  86. for (var code3 in countries) {
  87. var value = null,
  88. year,
  89. itemData = countries[code3].data,
  90. i = itemData.length;
  91. while (i--) {
  92. if (typeof itemData[i] === 'number') {
  93. value = itemData[i];
  94. year = categories[i];
  95. break;
  96. }
  97. }
  98. data.push({
  99. name: countries[code3].name,
  100. code3: code3,
  101. value: value,
  102. year: year
  103. });
  104. }
  105. // Add lower case codes to the data set for inclusion in the tooltip.pointFormat
  106. var mapData = Highcharts.geojson(Highcharts.maps['custom/world']);
  107. $.each(mapData, function () {
  108. this.id = this.properties['hc-key']; // for Chart.get()
  109. this.flag = this.id.replace('UK', 'GB').toLowerCase();
  110. });
  111. // Wrap point.select to get to the total selected points
  112. Highcharts.wrap(Highcharts.Point.prototype, 'select', function (proceed) {
  113. proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  114. var points = mapChart.getSelectedPoints();
  115. if (points.length) {
  116. if (points.length === 1) {
  117. $('#info #flag').attr('class', 'flag ' + points[0].flag);
  118. $('#info h2').html(points[0].name);
  119. } else {
  120. $('#info #flag').attr('class', 'flag');
  121. $('#info h2').html('Comparing countries');
  122. }
  123. $('#info .subheader').html('<h4>Historical population</h4><small><em>Shift + Click on map to compare countries</em></small>')
  124. if (!countryChart) {
  125. countryChart = $('#country-chart').highcharts({
  126. chart: {
  127. height: 250,
  128. spacingLeft: 0
  129. },
  130. credits: {
  131. enabled: false
  132. },
  133. title: {
  134. text: null
  135. },
  136. subtitle: {
  137. text: null
  138. },
  139. xAxis: {
  140. tickPixelInterval: 50,
  141. crosshair: true
  142. },
  143. yAxis: {
  144. title: null,
  145. opposite: true
  146. },
  147. tooltip: {
  148. shared: true
  149. },
  150. plotOptions: {
  151. series: {
  152. animation: {
  153. duration: 500
  154. },
  155. marker: {
  156. enabled: false
  157. },
  158. threshold: 0,
  159. pointStart: parseInt(categories[0]),
  160. }
  161. }
  162. }).highcharts();
  163. }
  164. $.each(points, function (i) {
  165. // Update
  166. if (countryChart.series[i]) {
  167. /*$.each(countries[this.code3].data, function (pointI, value) {
  168. countryChart.series[i].points[pointI].update(value, false);
  169. });*/
  170. countryChart.series[i].update({
  171. name: this.name,
  172. data: countries[this.code3].data,
  173. type: points.length > 1 ? 'line' : 'area'
  174. }, false);
  175. } else {
  176. countryChart.addSeries({
  177. name: this.name,
  178. data: countries[this.code3].data,
  179. type: points.length > 1 ? 'line' : 'area'
  180. }, false);
  181. }
  182. });
  183. while (countryChart.series.length > points.length) {
  184. countryChart.series[countryChart.series.length - 1].remove(false);
  185. }
  186. countryChart.redraw();
  187. } else {
  188. $('#info #flag').attr('class', '');
  189. $('#info h2').html('');
  190. $('#info .subheader').html('');
  191. if (countryChart) {
  192. countryChart = countryChart.destroy();
  193. }
  194. }
  195. });
  196. // Initiate the map chart
  197. mapChart = $('#container').highcharts('Map', {
  198. title : {
  199. text : 'Population history by country'
  200. },
  201. subtitle: {
  202. text: 'Source: <a href="http://data.worldbank.org/indicator/SP.POP.TOTL/countries/1W?display=default">The World Bank</a>'
  203. },
  204. mapNavigation: {
  205. enabled: true,
  206. buttonOptions: {
  207. verticalAlign: 'bottom'
  208. }
  209. },
  210. colorAxis: {
  211. type: 'logarithmic',
  212. endOnTick: false,
  213. startOnTick: false,
  214. min: 50000
  215. },
  216. tooltip: {
  217. footerFormat: '<span style="font-size: 10px">(Click for details)</span>'
  218. },
  219. series : [{
  220. data : data,
  221. mapData: mapData,
  222. joinBy: ['iso-a3', 'code3'],
  223. name: 'Current population',
  224. allowPointSelect: true,
  225. cursor: 'pointer',
  226. states: {
  227. select: {
  228. color: '#a4edba',
  229. borderColor: 'black',
  230. dashStyle: 'shortdot'
  231. }
  232. }
  233. }]
  234. }).highcharts();
  235. // Pre-select a country
  236. mapChart.get('us').select();
  237. });
  238. });
  239. </script>
  240. </head>
  241. <body>
  242. <script src="../../js/highcharts.js"></script>
  243. <script src="../../js/modules/map.js"></script>
  244. <script src="http://code.highcharts.com/mapdata/custom/world.js"></script>
  245. <!-- Flag sprites service provided by Martijn Lafeber, https://github.com/lafeber/world-flags-sprite/blob/master/LICENSE -->
  246. <link rel="stylesheet" type="text/css" href="http://cloud.github.com/downloads/lafeber/world-flags-sprite/flags32.css" />
  247. <div id="wrapper">
  248. <div id="container"></div>
  249. <div id="info">
  250. <span class="f32"><span id="flag"></span></span>
  251. <h2></h2>
  252. <div class="subheader">Click countries to view history</div>
  253. <div id="country-chart"></div>
  254. </div>
  255. </div>
  256. </body>
  257. </html>