当查询给出的结果为空或无行时,折线图会显示错误“给定轴上的所有序列必须具有相同的数据类型”。
如果仔细看,有7个查询(每个人都在同一张图表上画一条线),如果有任何查询返回空结果(无行),我会得到主题标题中的错误。当所有查询都不为空时,图表可以正常工作。
有办法避免这种错误吗?
<?php
$curyear = date('Y');
$con = mysqli_connect('xxx','xxx','xxx','xxx');
?>
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi">
</script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
var data1 = google.visualization.arrayToDataTable([
['Date', 'LaMotta'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as LaMotta, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ01' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['LaMotta']."],";
}
?>
]);
var data2 = google.visualization.arrayToDataTable([
['Date', 'Spadone'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Spadone, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ02' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Spadone']."],";
}
?>
]);
var data3 = google.visualization.arrayToDataTable([
['Date', 'Ivo'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Ivo, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ11' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Ivo']."],";
}
?>
]);
var data4 = google.visualization.arrayToDataTable([
['Date', 'Montagliani'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Montagliani, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ12' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Montagliani']."],";
}
?>
]);
var data5 = google.visualization.arrayToDataTable([
['Date', 'Galtieri'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Galtieri, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ21' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Galtieri']."],";
}
?>
]);
var data6 = google.visualization.arrayToDataTable([
['Date', 'Giacometti'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Giacometti, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ22' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Giacometti']."],";
}
?>
]);
var data7 = google.visualization.arrayToDataTable([
['Date', 'Mantovani'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Mantovani, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ23' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Mantovani']."],";
}
?>
]);
var joinedData = google.visualization.data.join(data1, data2, 'full', [[0, 0]], [1], [1]);
var joinedData2 = google.visualization.data.join(joinedData, data3, 'full', [[0, 0]], [1,2], [1]);
var joinedData3 = google.visualization.data.join(joinedData2, data4, 'full', [[0, 0, 0]], [1,2,3], [1]);
var joinedData4 = google.visualization.data.join(joinedData3, data5, 'full', [[0, 0, 0, 0]], [1,2,3,4], [1]);
var joinedData5 = google.visualization.data.join(joinedData4, data6, 'full', [[0, 0, 0, 0, 0]], [1,2,3,4,5], [1]);
var joinedData6 = google.visualization.data.join(joinedData5, data7, 'full', [[0, 0, 0, 0, 0, 0]], [1,2,3,4,5,6], [1]);
// sort by start and end dates
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(joinedData6, {
height: 500,
width: 1400,
interpolateNulls: true,
});
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
</script>
</head>
<body><br /><br />
<div style="width:1500px;" align="center">
<h1 align="center" style="font-size:22px">Produzione SMARTEASY: MYENNOVA (Ultimi 10 Mesi)</h1>
<br />
<div align="center" id="piechart2" style="float:left; width: 700px; height: 390px;"></div>
<div align="center" id="piechart1" style="float:left; width: 700px; height: 390px;"></div>
</div>
<!--Divs that will hold the charts-->
<div id="chart_div"></div>
</body>
</html>
答案 0 :(得分:0)
您可以使用数据视图绘制图表,
并且仅包括来自联接数据的具有值的列。
使用数据表方法getColumnRange
,
我们可以确定列中是否有数据。
没有数据的列,
getColumnRange
将同时返回null
和min
的{{1}}
max
有关示例,请参见以下工作片段...
// build view columns
var viewColumns = [];
for (var i = 0; i < joinedData6.getNumberOfColumns(); i++) {
var range = joinedData6.getColumnRange(i);
// determine if column has values
if (!((range.min === null) && (range.min === null))) {
viewColumns.push(i);
}
}
// build data view
var view = new google.visualization.DataView(joinedData6);
view.setColumns(viewColumns);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(view, { // <-- use view to draw chart
height: 500,
width: 1400,
interpolateNulls: true,
});
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data1 = google.visualization.arrayToDataTable([
['Date', 'LaMotta'],
['test1', 5],
['test1', 6],
['test1', 7],
]);
var data2 = google.visualization.arrayToDataTable([
['Date', 'Spadone'],
]);
var data3 = google.visualization.arrayToDataTable([
['Date', 'Ivo'],
['test3', 8],
['test3', 9],
['test3', 0],
]);
var data4 = google.visualization.arrayToDataTable([
['Date', 'Montagliani'],
]);
var data5 = google.visualization.arrayToDataTable([
['Date', 'Galtieri'],
['test5', 1],
['test5', 2],
['test5', 3],
]);
var data6 = google.visualization.arrayToDataTable([
['Date', 'Giacometti'],
]);
var data7 = google.visualization.arrayToDataTable([
['Date', 'Mantovani'],
['test7', 4],
['test7', 5],
['test7', 6],
]);
var joinedData = google.visualization.data.join(data1, data2, 'full', [[0, 0]], [1], [1]);
var joinedData2 = google.visualization.data.join(joinedData, data3, 'full', [[0, 0]], [1,2], [1]);
var joinedData3 = google.visualization.data.join(joinedData2, data4, 'full', [[0, 0, 0]], [1,2,3], [1]);
var joinedData4 = google.visualization.data.join(joinedData3, data5, 'full', [[0, 0, 0, 0]], [1,2,3,4], [1]);
var joinedData5 = google.visualization.data.join(joinedData4, data6, 'full', [[0, 0, 0, 0, 0]], [1,2,3,4,5], [1]);
var joinedData6 = google.visualization.data.join(joinedData5, data7, 'full', [[0, 0, 0, 0, 0, 0]], [1,2,3,4,5,6], [1]);
// build view columns
var viewColumns = [];
for (var i = 0; i < joinedData6.getNumberOfColumns(); i++) {
var range = joinedData6.getColumnRange(i);
// determine if column has values
if ((range.min !== null) || (range.max !== null)) {
viewColumns.push(i);
}
}
// build data view
var view = new google.visualization.DataView(joinedData6);
view.setColumns(viewColumns);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(view, { // <-- use view to draw chart
height: 500,
width: 1400,
interpolateNulls: true,
});
});
答案 1 :(得分:0)
<?php
$curyear = date('Y');
$con = mysqli_connect('xxx','xxx','xxx','xxx');
?>
<!--Load the AJAX API-->
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data1 = google.visualization.arrayToDataTable([
['Date', 'LaMotta'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as LaMotta, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ01' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['LaMotta']."],";
}
?>
]);
var data2 = google.visualization.arrayToDataTable([
['Date', 'Spadone'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Spadone, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ02' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Spadone']."],";
}
?>
]);
var data3 = google.visualization.arrayToDataTable([
['Date', 'Ivo'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Ivo, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ11' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Ivo']."],";
}
?>
]);
var data4 = google.visualization.arrayToDataTable([
['Date', 'Montagliani'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Montagliani, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ12' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Montagliani']."],";
}
?>
]);
var data5 = google.visualization.arrayToDataTable([
['Date', 'Galtieri'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Galtieri, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ21' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Galtieri']."],";
}
?>
]);
var data6 = google.visualization.arrayToDataTable([
['Date', 'Giacometti'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Giacometti, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ22' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Giacometti']."],";
}
?>
]);
var data7 = google.visualization.arrayToDataTable([
['Date', 'Mantovani'],
<?php
$query = "SELECT responsabile, sum(val_fattu) as Mantovani, data_dichiarato FROM dichiarati_myennova WHERE responsabile = 'ADMRZ23' and dichiarati_myennova.DATA_DICHIARATO > (NOW() - INTERVAL 270 DAY) GROUP BY MONTH(data_dichiarato), responsabile ORDER BY data_dichiarato";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".date("Y/m", strtotime($row['data_dichiarato']))."',".$row['Mantovani']."],";
}
?>
]);
var joinedData = google.visualization.data.join(data1, data2, 'full', [[0, 0]], [1], [1]);
var joinedData2 = google.visualization.data.join(joinedData, data3, 'full', [[0, 0]], [1,2], [1]);
var joinedData3 = google.visualization.data.join(joinedData2, data4, 'full', [[0, 0, 0]], [1,2,3], [1]);
var joinedData4 = google.visualization.data.join(joinedData3, data5, 'full', [[0, 0, 0, 0]], [1,2,3,4], [1]);
var joinedData5 = google.visualization.data.join(joinedData4, data6, 'full', [[0, 0, 0, 0, 0]], [1,2,3,4,5], [1]);
var joinedData6 = google.visualization.data.join(joinedData5, data7, 'full', [[0, 0, 0, 0, 0, 0]], [1,2,3,4,5,6], [1]);
// sort by start and end dates
// build view columns
var viewColumns = [];
for (var i = 0; i < joinedData6.getNumberOfColumns(); i++) {
var range = joinedData6.getColumnRange(i);
// determine if column has values
if ((range.min !== null) || (range.max !== null)) {
viewColumns.push(i);
}
}
// build data view
var view = new google.visualization.DataView(joinedData6);
view.setColumns(viewColumns);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(view, { // <-- use view to draw chart
height: 500,
width: 1400,
interpolateNulls: true,
});
});