123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Highcharts Example</title>
- <style type="text/css">
- .chart {
- min-width: 320px;
- max-width: 800px;
- height: 220px;
- margin: 0 auto;
- }
- </style>
- <!-- http://doc.jsfiddle.net/use/hacks.html#css-panel-hack -->
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <style>
- </style>
- </head>
- <body>
- <script src="../../code/highcharts.js"></script>
- <script src="../../code/modules/data.js"></script>
- <div id="container"></div>
- <script type="text/javascript">
- /*
- The purpose of this demo is to demonstrate how multiple charts on the same page
- can be linked through DOM and Highcharts events and API methods. It takes a
- standard Highcharts config with a small variation for each data set, and a
- mouse/touch event handler to bind the charts together.
- */
- /**
- * In order to synchronize tooltips and crosshairs, override the
- * built-in events with handlers defined on the parent element.
- */
- ['mousemove', 'touchmove', 'touchstart'].forEach(function (eventType) {
- document.getElementById('container').addEventListener(
- eventType,
- function (e) {
- var chart,
- point,
- i,
- event;
- for (i = 0; i < Highcharts.charts.length; i = i + 1) {
- chart = Highcharts.charts[i];
- // Find coordinates within the chart
- event = chart.pointer.normalize(e);
- // Get the hovered point
- point = chart.series[0].searchPoint(event, true);
- if (point) {
- point.highlight(e);
- }
- }
- }
- );
- });
- /**
- * Override the reset function, we don't need to hide the tooltips and
- * crosshairs.
- */
- Highcharts.Pointer.prototype.reset = function () {
- return undefined;
- };
- /**
- * Highlight a point by showing tooltip, setting hover state and draw crosshair
- */
- Highcharts.Point.prototype.highlight = function (event) {
- event = this.series.chart.pointer.normalize(event);
- this.onMouseOver(); // Show the hover marker
- this.series.chart.tooltip.refresh(this); // Show the tooltip
- this.series.chart.xAxis[0].drawCrosshair(event, this); // Show the crosshair
- };
- /**
- * Synchronize zooming through the setExtremes event handler.
- */
- function syncExtremes(e) {
- var thisChart = this.chart;
- if (e.trigger !== 'syncExtremes') { // Prevent feedback loop
- Highcharts.each(Highcharts.charts, function (chart) {
- if (chart !== thisChart) {
- if (chart.xAxis[0].setExtremes) { // It is null while updating
- chart.xAxis[0].setExtremes(
- e.min,
- e.max,
- undefined,
- false,
- { trigger: 'syncExtremes' }
- );
- }
- }
- });
- }
- }
- // Get the data. The contents of the data file can be viewed at
- Highcharts.ajax({
- url: 'https://cdn.rawgit.com/highcharts/highcharts/057b672172ccc6c08fe7dbb27fc17ebca3f5b770/samples/data/activity.json',
- dataType: 'text',
- success: function (activity) {
- activity = JSON.parse(activity);
- activity.datasets.forEach(function (dataset, i) {
- // Add X values
- dataset.data = Highcharts.map(dataset.data, function (val, j) {
- return [activity.xData[j], val];
- });
- var chartDiv = document.createElement('div');
- chartDiv.className = 'chart';
- document.getElementById('container').appendChild(chartDiv);
- Highcharts.chart(chartDiv, {
- chart: {
- marginLeft: 40, // Keep all charts left aligned
- spacingTop: 20,
- spacingBottom: 20
- },
- title: {
- text: dataset.name,
- align: 'left',
- margin: 0,
- x: 30
- },
- credits: {
- enabled: false
- },
- legend: {
- enabled: false
- },
- xAxis: {
- crosshair: true,
- events: {
- setExtremes: syncExtremes
- },
- labels: {
- format: '{value} km'
- }
- },
- yAxis: {
- title: {
- text: null
- }
- },
- tooltip: {
- positioner: function () {
- return {
- // right aligned
- x: this.chart.chartWidth - this.label.width,
- y: 10 // align to title
- };
- },
- borderWidth: 0,
- backgroundColor: 'none',
- pointFormat: '{point.y}',
- headerFormat: '',
- shadow: false,
- style: {
- fontSize: '18px'
- },
- valueDecimals: dataset.valueDecimals
- },
- series: [{
- data: dataset.data,
- name: dataset.name,
- type: dataset.type,
- color: Highcharts.getOptions().colors[i],
- fillOpacity: 0.3,
- tooltip: {
- valueSuffix: ' ' + dataset.unit
- }
- }]
- });
- });
- }
- });
- </script>
- </body>
- </html>
|