【问题标题】:Highcharts undefined valueHighcharts 未定义值
【发布时间】:2018-03-04 11:52:36
【问题描述】:
$.getJSON("<?php echo $chartURL; ?>", function (data) {

      var seriesData = [];

      // split the data set into crypto and volume
    for (i=0; i<data.length; i++) {
      seriesData.push({
          x: data[i].time,
          y: data[i].open,
          high: data[i].high
      });
    }

    console.log(seriesData);
    // Create the chart
    Highcharts.stockChart('container', {

        rangeSelector: {
            selected: 0
        },

        title: {
            text: 'AAPL Stock Price'
        },

        tooltip: {
            formatter: function () {
                return Highcharts.dateFormat('%d %b %Y', new Date(this.x)) + '<br/>' +
                    'Open: <b>' + this.y + '</b><br/>' +
                    'High: <b>' + this.point.high + '</b>';
            }
        },

        series: [{
              type: 'line',
              name: 'USD',
              data: seriesData,
              turboThreshold: 0
              }]
    });
});

在 for 循环中,我定义了值 high,并在 tooltip 中传递了变量,但脚本返回错误:

无法读取未定义的属性“高”

当我将tooltip 更改为:

    tooltip: {
        formatter: function() {
            var s = '<b>'+ Highcharts.dateFormat('%d %b %Y', new Date(this.x)) +'</b>';

            $.each(this.points, function(i, point) {
                s += '<br/>High: ' + seriesData[i].high + '<br/>Price: ' + point.y;
            });

            return s;
        },
        shared: true
    },

脚本没有返回任何错误但是high的值还是一样的,没有变化。

我尝试使用: this.point.high, point.high, this.data.high, this.seriesData[i].high ...我仍然有错误undefined

所有值均取自 JSON:

[{"time":1347321600000,"open":11.17,"high":11.35,"low":10.88,"close":11.33,"volumeto":721708.44},{"time":1347408000000,"open":11.33,"high":11.39,"low":10.78,"close":11.36,"volumeto":657004.1},{"time":1347494400000,"open":11.36,"high":11.4,"low":11.22,"close":11.4,"volumeto":233401.71},{"time":1347580800000,"open":11.4,"high":11.8,"low":11.32,"close":11.67,"volumeto":500768.38},{"time":1347667200000,"open":11.67,"high":11.79,"low":11.6,"close":11.75,"volumeto":190511.93},{"time":1347753600000,"open":11.75,"high":11.99,"low":11.72,"close":11.87,"volumeto":359833.88}...]

【问题讨论】:

  • point 来自哪里? -> this.high
  • @Andreas 我不知道这是否是你的意思,但所有值都取自 JSON,在变量 $chartURL 中,我添加到我的帖子 JSON

标签: jquery highcharts tooltip


【解决方案1】:

high 键位于 this.points[0].point 对象内,因此您的工具提示代码将是:

    tooltip: {
        formatter: function() {
        console.log(this.points);
            return Highcharts.dateFormat('%d %b %Y', new Date(this.x)) + '<br/>' +
                'Open: <b>' + this.y + '</b><br/>' +
                'High: <b>' + this.points[0].point.high + '</b>';
        }
    },

查看JSFIDDLE

【讨论】:

  • 谢谢你的回答,但我有第二个问题,我只能从 JSON 中获取 560 条记录,并且脚本可以正常工作,但是当我得到 561 条记录时,high 的值是未定义的。我不知道为什么...?
  • 我没看懂这个问题,当 JSON 记录为 561 时会出现错误吗?或者当记录的数量是 560 并且当你点击 561 时high 是未定义的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多