Вопрос

I have been assigned a task of displaying 2 charts in a graph, therefore I need two y-axis to represent the charts. One axis will be on the left, the other on the right.

My problem is that only one y axis is showing. As I am using amCharts, I have noticed one issue where the visible y Axis has an array called allLabels and it has elements in it, but the second y Axis does not, however I don't know what fills up that array property.

Code:

function addconsumerUsageGraph(consumerUsageGraph) {
//You can ignore this
    if (consumerUsageGraph.graphs.length == 2)
    {
        var gr1 = consumerUsageGraph.graphs[0];
        var gr2 = consumerUsageGraph.graphs[1];
        consumerUsageGraph.removeGraph(gr1);
        consumerUsageGraph.removeGraph(gr2);
    }

    if (consumerUsageGraph.graphs.length == 1) {
        var gr1 = consumerUsageGraph.graphs[0];
        consumerUsageGraph.removeGraph(gr1);
    }

  //I add the two yAxis here:
    var yAxis1 = new AmCharts.ValueAxis();
    yAxis1.position = "left";

    var yAxis2 = new AmCharts.ValueAxis();
    yAxis2.position = "right"; // set this to "right" if you want it on the right

    //Adding the value axis to the whole graph
    consumerUsageGraph.addValueAxis(yAxis1);
    consumerUsageGraph.addValueAxis(yAxis2);

    var graph = new AmCharts.AmGraph();
    graph.valueField = "value";
    graph.type = "column";
    graph.title = "Usage";
    graph.lineAlpha = 1;
    graph.fillAlphas = 0.75;

    consumerUsageGraph.addGraph(graph);

    var graph2 = new AmCharts.AmGraph();
    graph2.valueField = "cost";
    graph2.type = "line";
    graph2.lineAlpha = 1;
    graph2.title = "Cost";
    graph2.lineColor = graphColour;
    graph2.bullet = "round";
    graph2.bulletAlpha = 0.5;
    graph2.bulletBorderAlpha = 0.8;

    //Assigning the second yAxis to the second graph
    graph2.valueAxes = yAxis2;


    consumerUsageGraph.addGraph(graph2);

    //consumerUsageGraph.write("chartdiv");

    var legend = new AmCharts.AmLegend();
    legend.marginBottom = -10;
    legend.useGraphSettings = true;
    consumerUsageGraph.addLegend(legend);
    }

How the chart looks now:

enter image description here

Это было полезно?

Решение

graph doesn't have property valueAxes, use valueAxis instead:

graph2.valueAxis = yAxis2;

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top