创建多个y轴组合图

时间:2015-04-13 10:43:26

标签: javascript charts

我正在考虑进一步改进我在这里创建的组合图表,首先是双y轴组合图表,然后是多轴组合图表。

如所见here我已经想出如何使用本教程创建一个基本的组合图表。

google.load("visualization", "1", {
    packages: [ "corechart", "bar" ]
});

google.setOnLoadCallback(drawVisualization);

function drawVisualization() {
    // Some raw data (not necessarily accurate)
    var data = google.visualization.arrayToDataTable([
        [ 'Month', 'Col1', 'Col2', 'Col3', 'Col4', 'Col5', 'Col6' ],
        [ 'Set 1', 165, 938, 522, 998, 450, 614.6 ],
        [ 'Set 2', 135, 1120, 599, 1268, 288, 682 ],
        [ 'Set 3', 157, 1167, 587, 807, 397, 623 ],
        [ 'Set 4', 139, 1110, 615, 968, 215, 609.4 ],
        [ 'Set 5', 136, 691, 629, 1026, 366, 569.6 ]
    ]);

    var options = {
        title: 'Chart title',
        width: 1001,
        height: 500,
        vAxis: {
            title: "VAxis title"
        },
        hAxis: {
            title: "HAxis title"
        },
        seriesType: "bars",
        series: {
            5: {
                type: "line"
            }
        }
    };

    var chart = new google.visualization.ComboChart(document.getElementById('number_format_chart'));
    chart.draw(data, options);
}
.chartwrapper {
    margin: 20px 0 12px 0;
}
#number_format_chart {
    width: 100%
}
<script src="https://www.google.com/jsapi"></script>
<div class="chartwrapper">
    <!--Div that will hold the bar charts -->
    <div id="number_format_chart"></div>
</div>

现在继续将简单的组合图表更改为双y版本,这正是我想要实现的目标。

enter image description here

我认为添加下面的代码会有所帮助,但没有运气,因为没有显示第二个y轴

series: {
    0: { axis: 'Col1' }, // Bind series 0 to an axis named 'distance'.
    1: { axis: 'Col2' } // Bind series 1 to an axis named 'brightness'.
},
axes: {
    y: {
        Col1: {
            label: 'leftyaxis'
        }, // Left y-axis.
        Col2: {
            side: 'right',
            label: 'rightyaxis'
        } // Right y-axis.
    }
}

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

你关闭了。我完成它的方式使用索引,因此轴部分看起来像这样

    vAxes: {
            0: {
                title: 'leftyaxis'
            },
            1: {
                title: 'rightyaxis'
            }
        }  

您可以在系列中添加targetAxisIndex: 0之类的内容。

Fiddle

picture