index.htm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #container {
  9. height: 500px;
  10. min-width: 310px;
  11. max-width: 600px;
  12. margin: 0 auto;
  13. }
  14. .loading {
  15. margin-top: 10em;
  16. text-align: center;
  17. color: gray;
  18. }
  19. </style>
  20. <script type="text/javascript">
  21. $(function () {
  22. // Load the data from a Google Spreadsheet
  23. // https://docs.google.com/a/highsoft.com/spreadsheet/pub?hl=en_GB&hl=en_GB&key=0AoIaUO7wH1HwdDFXSlpjN2J4aGg5MkVHWVhsYmtyVWc&output=html
  24. Highcharts.data({
  25. googleSpreadsheetKey: '0AoIaUO7wH1HwdDFXSlpjN2J4aGg5MkVHWVhsYmtyVWc',
  26. // custom handler for columns
  27. parsed: function (columns) {
  28. /**
  29. * Event handler for clicking points. Use jQuery UI to pop up
  30. * a pie chart showing the details for each state.
  31. */
  32. function pointClick() {
  33. var row = this.options.row,
  34. $div = $('<div></div>')
  35. .dialog({
  36. title: this.name,
  37. width: 400,
  38. height: 300
  39. });
  40. window.chart = new Highcharts.Chart({
  41. chart: {
  42. renderTo: $div[0],
  43. type: 'pie',
  44. width: 370,
  45. height: 240
  46. },
  47. title: {
  48. text: null
  49. },
  50. series: [{
  51. name: 'Votes',
  52. data: [{
  53. name: 'Obama',
  54. color: '#0200D0',
  55. y: parseInt(columns[3][row], 10)
  56. }, {
  57. name: 'Romney',
  58. color: '#C40401',
  59. y: parseInt(columns[4][row], 10)
  60. }],
  61. dataLabels: {
  62. format: '<b>{point.name}</b> {point.percentage:.1f}%'
  63. }
  64. }]
  65. });
  66. }
  67. // Make the columns easier to read
  68. var keys = columns[0],
  69. names = columns[1],
  70. percent = columns[7],
  71. // Build the chart options
  72. options = {
  73. chart: {
  74. renderTo: 'container',
  75. type: 'map',
  76. borderWidth: 1
  77. },
  78. title: {
  79. text: 'US presidential election 2012 results'
  80. },
  81. subtitle: {
  82. text: 'Source: <a href="http://en.wikipedia.org/wiki/United_States_presidential_election,' +
  83. '_2012">Wikipedia</a>'
  84. },
  85. legend: {
  86. align: 'right',
  87. verticalAlign: 'top',
  88. x: -100,
  89. y: 70,
  90. floating: true,
  91. layout: 'vertical',
  92. valueDecimals: 0,
  93. backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || 'rgba(255, 255, 255, 0.85)'
  94. },
  95. mapNavigation: {
  96. enabled: true,
  97. enableButtons: false
  98. },
  99. colorAxis: {
  100. dataClasses: [{
  101. from: -100,
  102. to: 0,
  103. color: '#C40401',
  104. name: 'Romney'
  105. }, {
  106. from: 0,
  107. to: 100,
  108. color: '#0200D0',
  109. name: 'Obama'
  110. }]
  111. },
  112. series: [{
  113. data: [],
  114. mapData: Highcharts.maps['countries/us/us-all'],
  115. joinBy: 'postal-code',
  116. dataLabels: {
  117. enabled: true,
  118. color: '#FFFFFF',
  119. format: '{point.postal-code}',
  120. style: {
  121. textTransform: 'uppercase'
  122. }
  123. },
  124. name: 'Democrats margin',
  125. point: {
  126. events: {
  127. click: pointClick
  128. }
  129. },
  130. tooltip: {
  131. ySuffix: ' %'
  132. },
  133. cursor: 'pointer'
  134. }, {
  135. name: 'Separators',
  136. type: 'mapline',
  137. data: Highcharts.geojson(Highcharts.maps['countries/us/us-all'], 'mapline'),
  138. color: 'silver',
  139. showInLegend: false,
  140. enableMouseTracking: false
  141. }]
  142. };
  143. keys = keys.map(function (key) {
  144. return key.toUpperCase();
  145. });
  146. Highcharts.each(options.series[0].mapData.features, function (mapPoint) {
  147. if (mapPoint.properties['postal-code']) {
  148. var postalCode = mapPoint.properties['postal-code'],
  149. i = $.inArray(postalCode, keys);
  150. options.series[0].data.push(Highcharts.extend({
  151. value: parseFloat(percent[i]),
  152. name: names[i],
  153. 'postal-code': postalCode,
  154. row: i
  155. }, mapPoint));
  156. }
  157. });
  158. // Initiate the chart
  159. window.chart = new Highcharts.Map(options);
  160. },
  161. error: function () {
  162. $('#container').html('<div class="loading">' +
  163. '<i class="icon-frown icon-large"></i> ' +
  164. '<p>Error loading data from Google Spreadsheets</p>' +
  165. '</div>');
  166. }
  167. });
  168. });
  169. </script>
  170. </head>
  171. <body>
  172. <script src="../../js/highcharts.js"></script>
  173. <script src="../../js/modules/map.js"></script>
  174. <script src="../../js/modules/data.js"></script>
  175. <script src="http://code.highcharts.com/mapdata/countries/us/us-all.js"></script>
  176. <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
  177. <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
  178. <link href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet" type="text/css" />
  179. <div id="container">
  180. <div class="loading">
  181. <i class="icon-spinner icon-spin icon-large"></i>
  182. Loading data from Google Spreadsheets...
  183. </div>
  184. </div>
  185. </body>
  186. </html>