index.htm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Highcharts Example</title>
  7. <style type="text/css">
  8. .chart {
  9. min-width: 320px;
  10. max-width: 800px;
  11. height: 220px;
  12. margin: 0 auto;
  13. }
  14. </style>
  15. <!-- http://doc.jsfiddle.net/use/hacks.html#css-panel-hack -->
  16. <meta name="viewport" content="width=device-width, initial-scale=1" />
  17. <style>
  18. </style>
  19. </head>
  20. <body>
  21. <script src="../../code/highcharts.js"></script>
  22. <script src="../../code/modules/data.js"></script>
  23. <div id="container"></div>
  24. <script type="text/javascript">
  25. /*
  26. The purpose of this demo is to demonstrate how multiple charts on the same page
  27. can be linked through DOM and Highcharts events and API methods. It takes a
  28. standard Highcharts config with a small variation for each data set, and a
  29. mouse/touch event handler to bind the charts together.
  30. */
  31. /**
  32. * In order to synchronize tooltips and crosshairs, override the
  33. * built-in events with handlers defined on the parent element.
  34. */
  35. ['mousemove', 'touchmove', 'touchstart'].forEach(function (eventType) {
  36. document.getElementById('container').addEventListener(
  37. eventType,
  38. function (e) {
  39. var chart,
  40. point,
  41. i,
  42. event;
  43. for (i = 0; i < Highcharts.charts.length; i = i + 1) {
  44. chart = Highcharts.charts[i];
  45. // Find coordinates within the chart
  46. event = chart.pointer.normalize(e);
  47. // Get the hovered point
  48. point = chart.series[0].searchPoint(event, true);
  49. if (point) {
  50. point.highlight(e);
  51. }
  52. }
  53. }
  54. );
  55. });
  56. /**
  57. * Override the reset function, we don't need to hide the tooltips and
  58. * crosshairs.
  59. */
  60. Highcharts.Pointer.prototype.reset = function () {
  61. return undefined;
  62. };
  63. /**
  64. * Highlight a point by showing tooltip, setting hover state and draw crosshair
  65. */
  66. Highcharts.Point.prototype.highlight = function (event) {
  67. event = this.series.chart.pointer.normalize(event);
  68. this.onMouseOver(); // Show the hover marker
  69. this.series.chart.tooltip.refresh(this); // Show the tooltip
  70. this.series.chart.xAxis[0].drawCrosshair(event, this); // Show the crosshair
  71. };
  72. /**
  73. * Synchronize zooming through the setExtremes event handler.
  74. */
  75. function syncExtremes(e) {
  76. var thisChart = this.chart;
  77. if (e.trigger !== 'syncExtremes') { // Prevent feedback loop
  78. Highcharts.each(Highcharts.charts, function (chart) {
  79. if (chart !== thisChart) {
  80. if (chart.xAxis[0].setExtremes) { // It is null while updating
  81. chart.xAxis[0].setExtremes(
  82. e.min,
  83. e.max,
  84. undefined,
  85. false,
  86. { trigger: 'syncExtremes' }
  87. );
  88. }
  89. }
  90. });
  91. }
  92. }
  93. // Get the data. The contents of the data file can be viewed at
  94. Highcharts.ajax({
  95. url: 'https://cdn.rawgit.com/highcharts/highcharts/057b672172ccc6c08fe7dbb27fc17ebca3f5b770/samples/data/activity.json',
  96. dataType: 'text',
  97. success: function (activity) {
  98. activity = JSON.parse(activity);
  99. activity.datasets.forEach(function (dataset, i) {
  100. // Add X values
  101. dataset.data = Highcharts.map(dataset.data, function (val, j) {
  102. return [activity.xData[j], val];
  103. });
  104. var chartDiv = document.createElement('div');
  105. chartDiv.className = 'chart';
  106. document.getElementById('container').appendChild(chartDiv);
  107. Highcharts.chart(chartDiv, {
  108. chart: {
  109. marginLeft: 40, // Keep all charts left aligned
  110. spacingTop: 20,
  111. spacingBottom: 20
  112. },
  113. title: {
  114. text: dataset.name,
  115. align: 'left',
  116. margin: 0,
  117. x: 30
  118. },
  119. credits: {
  120. enabled: false
  121. },
  122. legend: {
  123. enabled: false
  124. },
  125. xAxis: {
  126. crosshair: true,
  127. events: {
  128. setExtremes: syncExtremes
  129. },
  130. labels: {
  131. format: '{value} km'
  132. }
  133. },
  134. yAxis: {
  135. title: {
  136. text: null
  137. }
  138. },
  139. tooltip: {
  140. positioner: function () {
  141. return {
  142. // right aligned
  143. x: this.chart.chartWidth - this.label.width,
  144. y: 10 // align to title
  145. };
  146. },
  147. borderWidth: 0,
  148. backgroundColor: 'none',
  149. pointFormat: '{point.y}',
  150. headerFormat: '',
  151. shadow: false,
  152. style: {
  153. fontSize: '18px'
  154. },
  155. valueDecimals: dataset.valueDecimals
  156. },
  157. series: [{
  158. data: dataset.data,
  159. name: dataset.name,
  160. type: dataset.type,
  161. color: Highcharts.getOptions().colors[i],
  162. fillOpacity: 0.3,
  163. tooltip: {
  164. valueSuffix: ' ' + dataset.unit
  165. }
  166. }]
  167. });
  168. });
  169. }
  170. });
  171. </script>
  172. </body>
  173. </html>