【问题标题】:Google Charts -- Chart not changing on selection谷歌图表——图表在选择时不改变
【发布时间】:2018-07-11 12:43:50
【问题描述】:

在一个 PHP 页面中,我正在使用 tablesorter 创建一个表格,显示和隐藏一个子行。单击表格中的一行时,子行将显示来自谷歌图表的图表。

项目工作:

  • 表格行点击显示子行

  • 显示第一个图表的表格

  • 图表中显示该值的正确数据

项目不工作:

  • Google Chart 更改显示在被选中的下一行上的数据,当单击该行时,应显示表格的空白区域。

我在其中创建表并用数据填充它的 PHP 代码(来自 sql db):

if( isset($db_graph_query)){
    while($row = mysqli_fetch_array($db_graph_query)) {
        $rowcount2++;
        // removed the hard coded column set and made it driven off of the array below
        // could have pulled it from the $cols array above, but there might be columns that we don't wish to show
        echo "                                <tr>";
        $colindex = 0;
        foreach( $cols as $column_name ) {
            $style = "";
            $val = $row[$column_name];
            if ( isset($column_callback)) {
                $style=$column_callback($colindex, $val);
            }
            if($colindex == 0){ //make the first cell our toggle for child row
                echo "<td $style><a href='#' class='toggle' onClick='drawChart(\"$val\");'>$val</a></td>";
            } else {
                echo "<td $style>$val</td>";
            }
            $colindex++;
        }
        echo "</tr>";
        echo "<tr class='tablesorter-childRow'>";
            echo "<td colspan='4'>";
                echo "<div id='chart_div' style='width: 1000px; height: 600px'></div>";
            echo "</td>";
        echo "</tr>";
    }
}

JavaScript 来创建我的图表:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
    google.charts.load('current', {'packages':['corechart']});
    function drawChart(name) {
        var data = new google.visualization.DataTable();
            data.addColumn('string', 'Name');
            data.addColumn('string', 'Date');
            data.addColumn('number', 'Runs');
            data.addColumn('number', 'Fail');
            data.addRows([
                <?php
                    $dbName = "my_db";
                    $config = parse_ini_file("db_info.ini",true);
                    $dbUser = $config["DB"]["db_user"];
                    $dbServer = $config["DB"]["db_ip"];
                    $dbPassword = $config["DB"]["db_pass"];

                    $con = mysql_connect($dbServer, $dbUser, $dbPassword);

                    if (!$con) {
                        die('Could not connect: ' . mysql_error());
                    }

                    mysql_select_db($dbName, $con);
                    $sql = mysql_query("SELECT * FROM mytable");

                    $output = array();

                    while($row = mysql_fetch_array($sql)) {
                        // create a temp array to hold the data
                        $temp = array();

                        // add the data
                        $temp[] = '"' . $row['Name'] . '"';
                        $temp[] = '"' . $row['Date'] . '"';
                        $temp[] = (int) $row['Runs'];
                        $temp[] = (int) $row['Fails'];
                        // implode the temp array into a comma-separated list and add to the output array
                        $output[] = '[' . implode(', ', $temp) . ']';
                    }
                    // implode the output into a comma-newline separated list and echo
                    echo implode(",\n", $output);

                    mysql_close($con);
                ?>
        ]);
        var view = new google.visualization.DataView(data);
        view.setRows(data.getFilteredRows([
            {column: 0, value: name} //I want this to be the value changed on row selection
        ]));
        view.setColumns([1,2,3]);

            var options = {
                hAxis: { 'title': 'Date' },
                width: 1000,
                height: 600,
                curveType: 'function',
                crosshair: { trigger: 'both'}
            };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(view, options);
    }

</script>

输出(名称、日期、运行、失败是列,我在中心的文本由于某种原因重新排列):

【问题讨论】:

    标签: javascript php google-visualization


    【解决方案1】:

    我意识到,图表在第一个 chart_div 内相互堆叠,因为它们没有每个容器的唯一标识符。所以从那里我只是修改了我的子行的 php,看起来像这样:

    原文:

    echo "<tr class='tablesorter-childRow'>";
            echo "<td colspan='4'>";
                echo "<div id='chart_div' style='width: 1000px; height: 600px'></div>";
            echo "</td>";
        echo "</tr>";
    

    新的和改进的:

    echo "<tr class='tablesorter-childRow'>";
            echo "<td colspan='4'>";
                echo "<div id='$val' style='width: 1000px; height: 600px'></div>";
            echo "</td>";
        echo "</tr>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多