简体   繁体   English

如何使用Highcharts在两个x轴上显示当前值?

[英]How to show current value on two x-axis using Highcharts?

I'm trying to make a graph of one of my training sessions, and want to show the heart rate as y-axis, and time and distance as two x-axises. 我试图绘制一张训练图,并希望将心率显示为y轴,将时间和距离显示为两个x轴。

I'm able to create axises for both the time and distance, but i'm not sure how to populate the data. 我可以为时间和距离创建轴,但是我不确定如何填充数据。 Now am I adding the heart rate data for both the distance and the time, and that seams to screw up the presentation, since the data points are doubled up. 现在,我要添加距离和时间的心率数据,并且由于数据点增加了一倍,因此接缝会扭曲显示效果。

What is the best way of keeping the two x-axises independent of each other, and only adding the data for time, distance and heart rate once? 保持两个x轴彼此独立,并且仅一次添加时间,距离和心率数据的最佳方法是什么?

I want the tooltip to display something like this: 我希望工具提示显示如下内容:

Time: 00:19:59
Distance: 5607.2 m
Heart Rate: 190

Demo: http://jsfiddle.net/remisture/4js1nx8z/ 演示: http//jsfiddle.net/remisture/4js1nx8z/

$(function() {
  $(document).ready(function() {
    Highcharts.setOptions({
      global: {
        useUTC: false
      }
    });

    $.getJSON('http://www.json-generator.com/api/json/get/bTSFRnarqW?indent=2&callback=?', function(jsonData) {

      $('#container').highcharts({
        chart: {
          type: 'area',
          marginRight: 10
        },
        title: {
          text: '20:00, 5611 m'
        },
        xAxis: [{
          title: {
            text: 'Time'
          },
          type: 'datetime',
          tickPixelInterval: 50
        }, {
          title: {
            text: 'Distance'
          },
          opposite: true
        }],
        yAxis: {
          title: {
            text: 'Heart rate'
          }
        },
        tooltip: {
          formatter: function() {
            var s = [];
            s.push("<b>Time:</b> " + Highcharts.dateFormat('%H:%M:%S', this.points[0].x));
            $.each(this.points, function(i, point) {

              s.push('<b>' + point.series.name + '</b>: ' + point.y);

            });

            return s.join('<br/>');
          },
          shared: true
        },
        legend: {
          enabled: false
        },
        exporting: {
          enabled: false
        },
        series: [{
          xAxis: 0,
          name: 'BPM',
          data: (function() {
            var data = [],
              time,
              i;

            for (i = 0; i < jsonData.length; i++) {
              time = new Date(1970, 0, 1);
              time.setSeconds(jsonData[i]['Time (seconds)']);
              data.push({
                x: time,
                y: jsonData[i]['Heart Rate']
              });
            }

            return data;
          })()
        }, {
          xAxis: 1,
          name: 'BPM',
          data: (function() {
            var data = [];

            for (i = 0; i < jsonData.length; i++) {
              data.push({
                x: jsonData[i]['Distance (meters)'],
                y: jsonData[i]['Heart Rate']
              });
            }

            return data;
          })()
        }]
      });
    });
  });

});

Thank you. 谢谢。

Ok, after re-reading and looking at the data, I think I see what you want. 好的,在重新阅读并查看数据之后,我想我明白了您想要的内容。

It seems like the relationship between time and distance is strong enough, that for your purposes it's not a problem, but be aware that the value plotted might not always completely match the axis values. 时间和距离之间的关系似乎足够牢固,对您而言这不是问题,但请注意,绘制的值可能并不总是与轴值完全匹配。

I would do it this way 我会这样

1) Get the max distance value ahead of time (I cheated in the example and hard-coded it after looking at the data), and set it as the Distance xAxis max value 1)提前获取最大距离值(我在示例中作了欺骗,并在查看数据后对其进行了硬编码),并将其设置为Distance xAxis max

2) Set a dummy series on that x axis to make it display properly, with an empty data array 2)在该x轴上设置一个虚拟序列以使其正确显示,并带有一个空数据数组

3) Set the distance as a third parameter in your primary data series, and call it out in the tooltip that way 3)将距离设置为主数据系列中的第三个参数,并以这种方式在工具提示中将其标出

So, for the axes: 因此,对于轴:

xAxis: [{
    title: {
      text: 'Seconds'
    },
  }, {
    opposite: true,
    min: 0,
    max: 5607, <-- get this from the data ahead of time
    title: {
      text: 'Distance'
    },
  }]

and then the series: 然后是系列:

series: [{
    name: 'BPM',
    data: (function() {
      var data = [];
      for (i = 0; i < jsonData.length; i++) {
        data.push({
          x: jsonData[i]['Time (seconds)'],
          y: jsonData[i]['Heart Rate'],
                        'Distance': jsonData[i]['Distance (meters)']
        });
      }
      return data;
    })()
  }, {
    xAxis: 1,
    data: []
  }]

And then the tooltip: 然后是工具提示:

tooltip: {
    crosshairs: true,
    formatter: function() {
        return  '<b>Time: </b>'+ this.point.x
                +'<br/><b>BPM: </b>'+ this.point.y
                +'<br/><b>Distance: </b>'+ this.point.Distance;
    }
  }      

Fiddle: 小提琴:

Output: 输出:

屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM