“;tickPositions未定义”;添加第二个y轴后

"tickPositions is undefined" after adding a second Y-Axis

本文关键字:第二个 轴后 添加 tickPositions 未定义      更新时间:2023-09-26

我有一个Highcharts图表,当有两个y轴时,在addSeries方法中抛出错误。

这是一个主干项目,所以我简化了这个JSFiddle中的代码:http://jsfiddle.net/michalcarson/V84pP/.

代码创建了一个没有数据的图表。它增加了第二个y轴。然后添加第一个系列的数据。错误发生在highcharts.src.js第7315行"TypeError: tickPositions is undefined"

我添加的数据应该与主y轴(chart.yAxis[0])相关联,但无论我是否指定该关联,都会发生错误。查看JSFiddle了解更多细节。

var chart,
options = {
    chart: {
        renderTo: 'chartdiv'
    }
},
real_series = {
    "data": [10.816667, 8.458333, 9.1, 8.794771, 7.91789, 7.281212,
        6.671075, 6.08748, 5.530427, 4.999916, 4.495946, 4.018517, 3.567631]
};
chart = new Highcharts.Chart(options);
chart.addAxis({
    id: "LCUReal",
    title: { text: "LCU Real" },
    opposite: true
}, false);
chart.addSeries(real_series);

我使用jQuery 1.11.1和Highcharts 4.0.3。

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/4.0.3/highcharts.src.js"></script>
<div id="chartdiv"></div>

那么问题是:这段代码有什么问题?如何使第二个y轴正常工作而不出现这个错误?

我没有对tickPositions做任何操作。我以前从来没有这样做过。我很乐意让Highcharts来解决这个问题。

由于您的轴没有范围,您可以通过将您的轴与第一个轴连接来消除它。

的例子:

chart.addAxis({
            linkedTo: 0,
            id: "LCUReal",
            title: {
                text: "LCU Real"
            },
            opposite: true
        }, false);
http://jsfiddle.net/sFfrL/8/

我发现,如果我推迟创建次要y轴,直到需要时,我可以做到这一点。也就是

  • 添加主y轴,
  • 使用主y轴添加数据系列,
  • 添加辅助y轴,
  • 使用辅助y轴添加数据系列。

纠正代码:

var chart,
options = {
    chart: {
        renderTo: 'chartdiv'
    }
},
series1 = {
    "name": "Interest Rate %",
    "data": [10.816667, 8.458333, 9.1, 8.794771, 7.91789, 7.281212,
    6.671075, 6.08748, 5.530427, 4.999916, 4.495946, 4.018517, 3.567631],
    "yAxis": "%"
},
series2 = {
    "name": "Exports",
    "data": [5803.475611, 5820.886038, 5925.661986, 6022.908979, 6091.005915, 6133.429061, 
             6158.918132, 6174.863244, 6186.050828, 6195.144854, 6203.458226, 6211.561078, 6219.661642],
     "yAxis": "LCUReal"
};
chart = new Highcharts.Chart(options);
chart.yAxis[0].update({
    id: "%",
    title: { text: "%" }
});
// SOLUTION: adding series1 here prevents the error
chart.addSeries(series1);
chart.addAxis({
    id: "LCUReal",
    title: {
        text: "LCU Real"
    },
    opposite: true
}, false);
// PROBLEM: adding secondary y-axis before adding data series to the primary y-axis throws 
//          TypeError: tickPositions is undefined.
// PROBLEM: even if the data is added above before the secondary y-axis is created, adding 
//          a series to the primary y-axis when there is no data on the secondary y-axis
//          will throw the same TypeError: tickPositions is undefined.
//chart.addSeries(series1);
chart.addSeries(series2);