使用谷歌折线图。遇到一个问题,即两个值(T_Temperatur)和(T_Badende_per_Time)同时插入数据库并提交一个。但不是T_Lufttemperatur。这就是图表的问题,也显示了空值。所以这是我的sql:
这是谷歌图表:
代码:
<?php
$con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysql_select_db("nih_bw", $con);
$sth = mysql_query("
SELECT routines.date, routines.time,
SUM( IF( measurements.title = 'T_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), 0 ) ) AS Temperatur,
SUM( IF( measurements.title = 'T_Badende_per_Time', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), 0 ) ) AS Badende,
SUM( IF( measurements.title = 'T_Luft_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), 0 ) ) AS Luft
FROM routines
INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
INNER JOIN measurements ON measure_routine.measure_id = measurements.id
GROUP BY routines.date, routines.time
ORDER BY routines.date, routines.time;
;");
$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(
array('label' => 'routines.date' & 'routines.time', 'type' => 'datetime'),
array('label' => 'Temperatur', 'type' => 'number'),
array('label' => 'Badende', 'type' => 'number'),
array('label' => 'Luft', 'type' => 'number'),
);
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$temp = array();
// assumes dates are in the format "yyyy-MM-dd"
$dateString = $r['date'];
$dateArray = explode('-', $dateString);
$year = $dateArray[0];
$month = $dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months
$day = $dateArray[2];
// assumes time is in the format "hh:mm:ss"
$timeString = $r['time'];
$timeArray = explode(':', $timeString);
$hours = $timeArray[0];
$minutes = $timeArray[1];
$seconds = $timeArray[2];
$temp = array();
$temp[] = array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)");
$temp[] = array('v' => (string) $r['Temperatur']);
$temp[] = array('v' => (string) $r['Badende']);
$temp[] = array('v' => (string) $r['Luft']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
// echo $jsonTable;
?>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
/*width: 900, height: 900, */
title: 'Visualization',
curveType: 'function',
legend: { position: 'bottom' },
pointSize: 5,
vAxis: {title: "Values", titleTextStyle: {italic: false}},
hAxis: {title: "Time", titleTextStyle: {italic: false}},
explorer: {
actions: ['dragToZoom', 'rightClickToReset'],
axis: 'vertical'}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
jsontable:
{"cols":[{"label":"routines.dade","type":"datetime"},{"label":"Temperatur","type":"number"},
{"label":"Badende","type":"number"},{"label":"Luft","type":"number"}],"rows":[{"c":
[{"v":"Date(2014, 3, 02, 09, 04, 12)"},{"v":"29.23"},{"v":"55.00"},{"v":""}]},{"c":
[{"v":"Date(2014, 3, 02, 10, 04, 01)"},{"v":"23.34"},{"v":"34.00"},{"v":""}]},{"c":
[{"v":"Date(2014, 3, 02, 10, 04, 39)"},{"v":"43.54"},{"v":"39.00"},{"v":""}]},{"c":
[{"v":"Date(2014, 3, 02, 10, 04, 53)"},{"v":""},{"v":""},{"v":"23.23"}]}]}
如果您需要更多信息,请随时询问。
答案 0 :(得分:3)
尝试使用nulls替换查询中的0:
SELECT routines.date, routines.time,
SUM( IF( measurements.title = 'T_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), null ) ) AS Temperatur,
SUM( IF( measurements.title = 'T_Badende_per_Time', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), null ) ) AS Badende,
SUM( IF( measurements.title = 'T_Luft_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), null ) ) AS Luft
FROM routines
INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
INNER JOIN measurements ON measure_routine.measure_id = measurements.id
GROUP BY routines.date, routines.time
ORDER BY routines.date, routines.time;
[编辑 - 添加PHP代码以避免将数字转换为字符串]
输入数据时,请勿将其转换为字符串:
$temp[] = array('v' => $r['Temperatur']);
$temp[] = array('v' => $r['Badende']);
$temp[] = array('v' => $r['Luft']);
并使用JSON_NUMERIC_CHECK
调用中的json_encode
选项:
$jsonTable = json_encode($table, JSON_NUMERIC_CHECK);