dyGraphs Second Y Axis not being displayed when using a variable as 'y2'

南笙酒味 提交于 2019-12-02 14:10:25

问题


I have the following code to display a graph with two axes. The data is of the form: Date, Value1, Value2

var sg = new Dygraph(document.getElemantById("div"),
lGraphData,
{
    labels: ['Date', string1, string2],
    legend: 'always',
    series: {
        string2 : {
            axis: 'y2'
        }
    },
    ylabel: string1,
    y2label: string2
});

Instead of displaying the y2axis, both series appear on the graph and only y axis showing. If I replace string2 (a variable string) with Y2 in the above code, both axes appear.

What am I doing wrong?


回答1:


This is a basic JavaScript issue. When string2 appears as a key in an object literal, it means the string "string2", not the value of the string2 variable. You need to create an options object and fill it out in pieces, like so:

var opts = {
    labels: ['Date', string1, string2],
    legend: 'always',
    series: {},
    ylabel: string1,
    y2label: string2
};
opts.series[string2] = { axis: 'y2' };

var sg = new Dygraph(document.getElemantById("div"), lGraphData, opts);


来源:https://stackoverflow.com/questions/16455062/dygraphs-second-y-axis-not-being-displayed-when-using-a-variable-as-y2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!