index.htm 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. </style>
  9. </head>
  10. <body>
  11. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  12. <script src="../../code/highcharts.js"></script>
  13. <script src="../../code/modules/exporting.js"></script>
  14. <script src="../../code/modules/export-data.js"></script>
  15. <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
  16. <script type="text/javascript">
  17. $.getJSON(
  18. 'https://cdn.rawgit.com/highcharts/highcharts/057b672172ccc6c08fe7dbb27fc17ebca3f5b770/samples/data/usdeur.json',
  19. function (data) {
  20. var detailChart;
  21. // create the detail chart
  22. function createDetail(masterChart) {
  23. // prepare the detail chart
  24. var detailData = [],
  25. detailStart = data[0][0];
  26. $.each(masterChart.series[0].data, function () {
  27. if (this.x >= detailStart) {
  28. detailData.push(this.y);
  29. }
  30. });
  31. // create a detail chart referenced by a global variable
  32. detailChart = Highcharts.chart('detail-container', {
  33. chart: {
  34. marginBottom: 120,
  35. reflow: false,
  36. marginLeft: 50,
  37. marginRight: 20,
  38. style: {
  39. position: 'absolute'
  40. }
  41. },
  42. credits: {
  43. enabled: false
  44. },
  45. title: {
  46. text: 'Historical USD to EUR Exchange Rate'
  47. },
  48. subtitle: {
  49. text: 'Select an area by dragging across the lower chart'
  50. },
  51. xAxis: {
  52. type: 'datetime'
  53. },
  54. yAxis: {
  55. title: {
  56. text: null
  57. },
  58. maxZoom: 0.1
  59. },
  60. tooltip: {
  61. formatter: function () {
  62. var point = this.points[0];
  63. return '<b>' + point.series.name + '</b><br/>' + Highcharts.dateFormat('%A %B %e %Y', this.x) + ':<br/>' +
  64. '1 USD = ' + Highcharts.numberFormat(point.y, 2) + ' EUR';
  65. },
  66. shared: true
  67. },
  68. legend: {
  69. enabled: false
  70. },
  71. plotOptions: {
  72. series: {
  73. marker: {
  74. enabled: false,
  75. states: {
  76. hover: {
  77. enabled: true,
  78. radius: 3
  79. }
  80. }
  81. }
  82. }
  83. },
  84. series: [{
  85. name: 'USD to EUR',
  86. pointStart: detailStart,
  87. pointInterval: 24 * 3600 * 1000,
  88. data: detailData
  89. }],
  90. exporting: {
  91. enabled: false
  92. }
  93. }); // return chart
  94. }
  95. // create the master chart
  96. function createMaster() {
  97. Highcharts.chart('master-container', {
  98. chart: {
  99. reflow: false,
  100. borderWidth: 0,
  101. backgroundColor: null,
  102. marginLeft: 50,
  103. marginRight: 20,
  104. zoomType: 'x',
  105. events: {
  106. // listen to the selection event on the master chart to update the
  107. // extremes of the detail chart
  108. selection: function (event) {
  109. var extremesObject = event.xAxis[0],
  110. min = extremesObject.min,
  111. max = extremesObject.max,
  112. detailData = [],
  113. xAxis = this.xAxis[0];
  114. // reverse engineer the last part of the data
  115. $.each(this.series[0].data, function () {
  116. if (this.x > min && this.x < max) {
  117. detailData.push([this.x, this.y]);
  118. }
  119. });
  120. // move the plot bands to reflect the new detail span
  121. xAxis.removePlotBand('mask-before');
  122. xAxis.addPlotBand({
  123. id: 'mask-before',
  124. from: data[0][0],
  125. to: min,
  126. color: 'rgba(0, 0, 0, 0.2)'
  127. });
  128. xAxis.removePlotBand('mask-after');
  129. xAxis.addPlotBand({
  130. id: 'mask-after',
  131. from: max,
  132. to: data[data.length - 1][0],
  133. color: 'rgba(0, 0, 0, 0.2)'
  134. });
  135. detailChart.series[0].setData(detailData);
  136. return false;
  137. }
  138. }
  139. },
  140. title: {
  141. text: null
  142. },
  143. xAxis: {
  144. type: 'datetime',
  145. showLastTickLabel: true,
  146. maxZoom: 14 * 24 * 3600000, // fourteen days
  147. plotBands: [{
  148. id: 'mask-before',
  149. from: data[0][0],
  150. to: data[data.length - 1][0],
  151. color: 'rgba(0, 0, 0, 0.2)'
  152. }],
  153. title: {
  154. text: null
  155. }
  156. },
  157. yAxis: {
  158. gridLineWidth: 0,
  159. labels: {
  160. enabled: false
  161. },
  162. title: {
  163. text: null
  164. },
  165. min: 0.6,
  166. showFirstLabel: false
  167. },
  168. tooltip: {
  169. formatter: function () {
  170. return false;
  171. }
  172. },
  173. legend: {
  174. enabled: false
  175. },
  176. credits: {
  177. enabled: false
  178. },
  179. plotOptions: {
  180. series: {
  181. fillColor: {
  182. linearGradient: [0, 0, 0, 70],
  183. stops: [
  184. [0, Highcharts.getOptions().colors[0]],
  185. [1, 'rgba(255,255,255,0)']
  186. ]
  187. },
  188. lineWidth: 1,
  189. marker: {
  190. enabled: false
  191. },
  192. shadow: false,
  193. states: {
  194. hover: {
  195. lineWidth: 1
  196. }
  197. },
  198. enableMouseTracking: false
  199. }
  200. },
  201. series: [{
  202. type: 'area',
  203. name: 'USD to EUR',
  204. pointInterval: 24 * 3600 * 1000,
  205. pointStart: data[0][0],
  206. data: data
  207. }],
  208. exporting: {
  209. enabled: false
  210. }
  211. }, function (masterChart) {
  212. createDetail(masterChart);
  213. }); // return chart instance
  214. }
  215. // make the container smaller and add a second container for the master chart
  216. var $container = $('#container')
  217. .css('position', 'relative');
  218. $('<div id="detail-container">')
  219. .appendTo($container);
  220. $('<div id="master-container">')
  221. .css({
  222. position: 'absolute',
  223. top: 300,
  224. height: 100,
  225. width: '100%'
  226. })
  227. .appendTo($container);
  228. // create master and in its callback, create the detail chart
  229. createMaster();
  230. }
  231. );
  232. </script>
  233. </body>
  234. </html>