gpt4 book ai didi

javascript - Highcharts 动态图表与 MySQL 数据不会重新加载

转载 作者:行者123 更新时间:2023-11-30 00:31:22 26 4
gpt4 key购买 nike

大家晚上好

此时我尝试使用 HighChart/HighStock,几周后我能够在图表中显示我的 MySQL 数据。但是:我总是想要更多。

好吧,我尝试制作一个令人耳目一新的动态图表,就像您可能从网站上了解到的示例一样。 http://jsfiddle.net/sdorzak/HsWF2/

我使用示例代码作为指导。它不起作用,但认为问题是缺少 y 轴,因为 y 轴数据来 self 的 MySQL 数据库,而 x 轴应该是当前时间。

  <!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>POS RESULT</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

</head>

<body>

<?php
include "config.php";

$SQL1 = "SELECT * FROM Prices WHERE ticker ='FB'";

$result1 = mysql_query($SQL1);
$data1 = array();
while ($row = mysql_fetch_array($result1)) {
$data1[] = $row['Close'];
}


?>

<script type="text/javascript">
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25,
events: {
load: function()
{

var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime();
series.addPoint([x], true);
}, 1000);
},
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150

//categories: [<?php echo join(',', $data2); ?>],
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y +'°C';
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: [{
name: 'Tokyo',
data: [<?php echo join(',', $data1); ?>]
}]
});
});

});
</script>

<div id="container" style="min-width: 500px; height: 400px; margin: 0 auto"></div>

</body>
</html>

我正在谈论并在示例中显示的段落是

    chart: {
renderTo: 'container',
type: 'spline',
marginRight: 10,
events: {
load: function() {

// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}

但是,正如我已经写过的,我不需要随机 y 轴,只需要 x 轴。

也许你可以帮忙。感谢您的帮助。

编辑:live-server-data.php

<?php
// Set the JSON header
header("Content-type: text/json");


include "config.php";

$SQL1 = "SELECT * FROM Prices WHERE Ticker ='TSLA'";

$result1 = mysql_query($SQL1);

while ($row = mysql_fetch_array($result1)) {
$data1[] = $row['Close'];
}

// The x value is the current JavaScript time, which is the Unix time multiplied
// by 1000.
$x = time() * 1000;
// The y value is a random number
$y = $data1;

// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>

“收盘价”值是十进制 24,4。

最佳答案

查看该解决方案:http://www.highcharts.com/studies/live-server.htm

    var chart; // global

/**
* Request data from the server, add it to the graph and set a timeout to request again
*/
function requestData() {
$.ajax({
url: 'live-server-data.php',
success: function(point) {
var series = chart.series[0],
shift = series.data.length > 20; // shift if the series is longer than 20

// add the point
chart.series[0].addPoint(eval(point), true, shift);

// call it again after one second
setTimeout(requestData, 1000);
},
cache: false
});
}

$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'spline',
events: {
load: requestData
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150,
maxZoom: 20 * 1000
},
yAxis: {
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: 'Value',
margin: 80
}
},
series: [{
name: 'Random data',
data: []
}]
});
});

关于javascript - Highcharts 动态图表与 MySQL 数据不会重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22493295/

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