javascript Highcharts:动态更改/添加轴属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15578754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:15:02  来源:igfitidea点击:

Highcharts: Dynamically change / add axis attributes

javascripthighcharts

提问by thefreeline

I am creating a number of highcharts which all contain the same default framework. To update the dynamic elements of the charts, I have been creating new objects and referencing them within a function call which draws the chart.

我正在创建许多 highcharts,它们都包含相同的默认框架。为了更新图表的动态元素,我一直在创建新对象并在绘制图表的函数调用中引用它们。

This has worked fine until I have started trying to make the axis labels dynamic (some charts require dual axis). I have included a jsfiddle which shows the dynamic structure I am working with and an example of the same chart hard coded. Can someone tell me why this might not be working?

这一直很好,直到我开始尝试使轴标签动态化(某些图表需要双轴)。我已经包含了一个 jsfiddle,它显示了我正在使用的动态结构和硬编码的相同图表的示例。有人能告诉我为什么这可能不起作用吗?

https://jsfiddle.net/jeo3y1rx/

https://jsfiddle.net/jeo3y1rx/

Example of Object:

对象示例:

yAxis = {
    'dollars': function () { 
        var yAxis = { 
            title: { 
                text: "Burdened Dollars" 
            }
        }
        return yAxis;
    }
};

Example of how this is referenced in the highcharts object:

在 highcharts 对象中如何引用 this 的示例:

    var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        type: 'column'
    },
    xAxis: {
        categories: ['FY Total']
    },
    yAxis: yAxis.dollars,
    series: [{
        name: 'YTD Plan',
        data: [1000]
    }, {
        type: 'column',
        name: 'YTD Actual',
        data: [900]
    }]
    });

Any help is appreciated.

任何帮助表示赞赏。

采纳答案by Rahul Gupta

Here is the working fiddle: DEMO.

这是工作小提琴:DEMO

As you are declaring "dollars" as a function which return the values, you will have to call it like yAxis: yAxis.dollars(),

当您将“美元”声明为返回值的函数时,您必须像这样调用它 yAxis: yAxis.dollars(),

JS code:

JS代码:

var yAxis = {
    'dollars': function () { 
        var yAxis = { 
            title: { 
                text: "Burdened Dollars" 
            }
        };
        return yAxis;
    }
};

$(function () {
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'column'
        },
        xAxis: {
            categories: ['FY Total']
        },
        yAxis: yAxis.dollars(),//call it like this
        series: [{
            name: 'YTD Plan2',
            data: [1000]
        }, {
            type: 'column',
            name: 'YTD Actual',
            data: [900]
        }]
    });
});

回答by SteveP

You can't use a function as an attribute that way in highcharts. However, you can use the highcharts API after the chart is loaded:

您不能在 highcharts 中以这种方式使用函数作为属性。但是,您可以在图表加载后使用 highcharts API:

chart.yAxis[0].setTitle({
        text: "Burdened Dollars"
});

http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/axis-settitle/

http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/axis-settitle/

回答by Sebastian Bochan

Highcharts 3.0 release allows to change axis dynamically by update() function

Highcharts 3.0 版本允许通过 update() 函数动态更改轴

http://api.highcharts.com/highcharts#Axis.update()

http://api.highcharts.com/highcharts#Axis.update()