Google Charts - 点击图例键时隐藏行 [英] Google Charts - Hide line when clicking legend key

查看:87
本文介绍了Google Charts - 点击图例键时隐藏行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在点击图例中的相关按键时显示/隐藏线图上的线条,这可能吗?

I'd like to be able to show/hide the lines on my line graph when clicking the relevant key in the legend, is this possible?

推荐答案

如果您不需要包含缩放和动画,那么一个选项就是使用lineWidth和areaOpacity值隐藏数据;

If you don't need to include scaling and animation then one option is just hide data using lineWidth and areaOpacity values;

 <head>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>

    <script>
        function updateTable() {

            // quick data - cleaned up for this example real data sources
            data = new Array();
            data[0] = new Array();
            data[0][0] = "Day";
            data[0][1] = "Metric 1";
            data[0][2] = "Metric 2";
            data[0][3] = "Metric 3";
            data[1] = new Array();
            data[1][0] = 1;
            data[1][1] = 200;
            data[1][2] = 50;
            data[1][3] = 400;
            data[2] = new Array();
            data[2][0] = 2;
            data[2][1] = 440;
            data[2][2] = 140;
            data[2][3] = 40;
            data[3] = new Array();
            data[3][0] = 3;
            data[3][1] = 300;
            data[3][2] = 500;
            data[3][3] = 600;

            var gdata = google.visualization.arrayToDataTable(data);

            var options = {
              // title: 'kala',
              hAxis: {title: 'Days',  titleTextStyle: {color: '#333'}}
              ,vAxis: {minValue: 0}
              ,height: 300
              ,width: 600
              ,chartArea: {left: 60}
              ,lineWidth: 2
              ,series: {0:{color: 'black', areaOpacity: 0.3, lineWidth: 2}
              ,1:{color: 'red', areaOpacity: 0.3, lineWidth: 2}
              ,2:{color: 'purple', areaOpacity: 0.3, lineWidth: 2}}
            };

            var chart = new google.visualization.AreaChart(document.getElementById('my_chart'));
            chart.draw(gdata, options);

            google.visualization.events.addListener(chart, 
                'select', 
                (function (x) { return function () { AreaSelectHander(chart, gdata, options)}})(1));

    }

    function AreaSelectHander(chart, gdata, options) {
        // when ever clicked we enter here 
        // more code needed to inspect what actually was clicked, now assuming people
        // play nicely and click only lables...
        var selection = chart.getSelection();       
        var view = new google.visualization.DataView(gdata);
        console.log(options);

        // click and data index are one off
        i = selection[0].column - 1;

        // just simple reverse
        if (options.series[i].lineWidth == 0) {
            options.series[i].lineWidth = 2;
            options.series[i].areaOpacity = 0.3;
        }
        else {
            options.series[i].lineWidth = 0;
            options.series[i].areaOpacity = 0.0;            
        }

        chart.draw(gdata, options);
    }
    </script>

    <script type='text/javascript'>
        google.load('visualization', '1', {packages:['table', 'corechart']});
        google.setOnLoadCallback(updateTable);
    </script>

</head>

<body>
    <div id='my_chart'></div>
</body>

这篇关于Google Charts - 点击图例键时隐藏行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆