我想繪製屬性「值」和「日期」的數據點。問題是,格式是數據:[[...], [...]]
其中第一個括號是'value'和第二個'date'。我想知道如何分解兩個向量,即從每個向量中取一個值並將其放入該格式以創建數據點。也許還有其他方法可以做到這一點?Highchart來自兩個向量的情節:分裂向量?
「價值」被存儲爲$data[] = $row['value'];
「日期」存儲爲$data2[] = $row['date'];
完全正常的代碼只有一個工作載體「價值」樓下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My first column chart by Highcharts</title>
<!-- 1. Add JQuery and Highcharts in the head of your page -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<!-- 2. You can add print and export feature by adding this line -->
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<!-- 3. Add the JavaScript with the Highchart options to initialize the chart -->
<script type="text/javascript">
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<?php
$con=mysql_connect('localhost','root','');
mysql_select_db("sensordb", $con);
$result=mysql_query('select value, date from data');
while($row = mysql_fetch_array($result)) {
$data[] = $row['value'];
$data2[] = $row['date'];
}
?>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<script type="text/javascript">
$(function() {
$('#container').highcharts({
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},
xAxis: {
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '°C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: 'Tokyo',
data: [<?php echo join($data, ',') ?>]
}]
});
});
</script>
</head>
<body>
<!-- 3. Add the container -->
<div id="container" style="width: 600px; height: 400px; margin: 0 auto"></div>
</body>
</html>
我對實際問題不清楚。爲什麼你要創建兩個數組,如果你的目標是用一個日期/值對創建一個數組? – jlbriggs
因爲我不知道該怎麼做。我可以把它放到一個數組中,但是如何從數組中分割數據。它將被渲染爲相同類型的幾個點,即'日期'和'值'將被繪製在X軸上,就好像是相同的類型。 – Lennart
你想讓這個圖表看起來像什麼?你想要日期作爲一個系列,並作爲另一個值?這很不尋常。但是,如果這是您的目標,那麼您當前的設置不起作用?它看起來像你正在創建兩個數組,你的日期之一,你的值之一 – jlbriggs