点击监听器第二次后谷歌图表冻结

时间:2016-11-25 07:23:36

标签: javascript asp.net vb.net google-visualization

  

我有一个问题,当我点击任何一个栏一旦它工作正常,然后我关闭子窗口并再次点击相同的栏图表冻结。但是,如果一开始我点击一个条,然后点击另一个不同的栏,它不会冻结,直到我再次点击同一个栏

<script type="text/javascript">
    google.charts.load('current', { packages: ['corechart', 'bar'] });
    google.charts.setOnLoadCallback(drawBasic);

    function drawBasic() {

        var data = google.visualization.arrayToDataTable([
            ['Year', 'Remaining Items Per LOT', { role: 'style' }],

<%
    Dim intValue As Integer = CInt(Rnd() * 32768)
    Dim myRandom as New Random
    Dim graphXSplitted As String() = graphX.Split(New String() {"|"}, StringSplitOptions.None)
    Dim graphYSplitted As String() = graphY.Split(New String() {"|"}, StringSplitOptions.None)
    Dim plot As String = ""
    For index As Integer = 0 To graphXSplitted.Length - 2
        plot += "[""" & graphXSplitted(index) & """, " & graphYSplitted(index) & ", 'stroke-color: #000000; stroke-width: 2; fill-color: #" & Hex$(myRandom.Next(1118481,8388607)) & "'],"
    Next
    Response.Write(plot.Remove(plot.Length - 1))
%>
        ]);

        var options = {
            title: 'Wakugai Inventory Items',
            height: 300,
            hAxis: {
                title: 'Part Names',
                direction: -1,
                slantedText: true,
                slantedTextAngle: 45,
                textStyle: { fontSize: 12 }
            },
            vAxis: {
                title: 'Remaining (LOT)'
            },
            chartArea: { left: 130, top: 10, height: '30%' }
        };

        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));

        chart.draw(data, options);

        google.visualization.events.addListener(chart, 'select', function () {
            var category = data.getValue(chart.getSelection()[0].row, 0);
            window.open("graph.aspx?x=" + category + "", "Popup", "height=400,width=1000");
        });

    }
</script>

1 个答案:

答案 0 :(得分:0)

当选择了一个栏并且未选中时,'select'事件被触发

第一次点击,“选择”栏

再次点击同一个栏,“取消选择”栏

因此,需要在访问数组内容之前检查选择的length

因为在未选择任何内容时chart.getSelection()[0]将被取消定义

google.visualization.events.addListener(chart, 'select', function () {
  var selection = chart.getSelection();
  if (selection.length > 0) {
    var category = data.getValue(selection[0].row, 0);
    window.open("graph.aspx?x=" + category + "", "Popup", "height=400,width=1000");
  }
});