gpt4 book ai didi

javascript - 谷歌图表——图表在选择时没有改变

转载 作者:行者123 更新时间:2023-11-30 20:22:47 25 4
gpt4 key购买 nike

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

项目工作:

  • 表格行点击显示子行

  • 显示第一个图表的表格

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

项目不工作:

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

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

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>

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

最佳答案

我意识到图表在第一个 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>";

关于javascript - 谷歌图表——图表在选择时没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51285777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com