[英]How to transfer Javascript code correctly?
I have two codes, both generate a line chart. 我有两个代码,都生成一个折线图。 However, the first one doesn't use mysql datasource, it uses random math generated datapoints.
但是,第一个不使用mysql数据源,它使用随机数学生成的数据点。 But it uses a refresh interval and thus is live.
但是它使用刷新间隔,因此是实时的。
The second code does in fact use a mysql datasource and displays the data in my database in the line-chart. 第二个代码实际上确实使用了mysql数据源,并在线图中显示了数据库中的数据。 However it is not live, because it does not it has not refresh-interval function.
但是,它不是实时的,因为它不具有刷新间隔功能。
I was trying to transfer the refresh-Interval / chart-update code parts of the first code to my second code that is not live but uses a real data source. 我试图将第一个代码的refresh-interval / chart-update代码部分转移到我的第二个代码,该代码不是实时的,而是使用真实的数据源。
Here is my live code, with random datapoints: 这是我的实时代码,带有随机数据点:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js">`
<script type="text/javascript">
window.onload = function () {
var dps = []; // dataPoints
var chart = new CanvasJS.Chart("chartContainer2",{
title :{
text: "Patient #01"
},
data: [{
type: "line",
dataPoints: dps
}]
});
var xVal = 0;
var yVal = 100;
var updateInterval = 20;
var dataLength = 500; // number of dataPoints visible at any point
var updateChart = function (count) {
count = count || 1;
// count is number of times loop runs to generate random dataPoints.
for (var j = 0; j < count; j++) {
yVal = yVal + Math.round(5 + Math.random() *(-5-5));
dps.push({
x: xVal,
y: yVal
});
xVal++;
};
if (dps.length > dataLength)
{
dps.shift();
}
chart.render();
};
// generates first set of dataPoints
updateChart(dataLength);
// update chart after specified time.
setInterval(function(){updateChart()}, updateInterval);
}
</script>
This is my code of the static line chart (not live) but uses real data source: 这是我的静态折线图代码(不是实时代码),但是使用了真实的数据源:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js">`
</script>
<script type="text/javascript">
$().ready(function () {
$.getJSON("arduino_data.php", function (result) {
var dataPoints = [];
for (var i = 0; i <= result.length - 1; i++) {
dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
}
var chart = new CanvasJS.Chart("chartContainer",{
title :{
text: "Patient #01"
},
data: [{
type: "line",
dataPoints: dataPoints
}]
});
chart.render();
});
});
</script>
<script type="text/javascript" src="canvasjs.min.js"></script>
This is what I have tried so far: 到目前为止,这是我尝试过的:
<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"> </script>
<script type="text/javascript">
$().ready(function () {
$.getJSON("arduino_data.php", function (result) {
var chart = new CanvasJS.Chart("chartContainer",{
title :{
text: "Patient #01"
},
data: [{
type: "line",
dataPoints: dataPoints
}]
});
var dataPoints = [];
var updateInterval = 20;
var dataLength = 500; // number of dataPoints visible at any point
var updateChart = function (count) {
count = count || 1;
for (var i = 0; i <= result.length - 1; i++) {
dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
};
}
if (dataPoints.length > dataLength)
{
dataPoints.shift();
}
chart.render();
)};
// generates first set of dataPoints
updateChart(dataLength);
// update chart after specified time.
setInterval(function(){updateChart()}, updateInterval);
}
</script>
<script type="text/javascript" src="canvasjs.min.js"></script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width:100%;">
</div>
</body>
</html>
But it keeps saying 但它一直在说
Unexpected token ')' at line 42
第42行出现意外的令牌')'
chart.render();
)};
I am pretty embarrassed but I can't find the solution due to all the bracelets/parenthesizes. 我很尴尬,但由于所有手镯/括号都找不到解决方案。 I have tried everything.
我已经尝试了一切。 With ) and without } but nothing seems to deliver.
有)和无},但似乎无济于事。
If this is solved, will the chronological positions of the code be alright? 如果解决了这个问题,代码的时间顺序会正确吗?
EDIT: FIRST PROBLEM SOLVED, NEW PROBLEM: JS POSITIONING 编辑:解决了第一个问题,新问题:JS定位
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"> </script>
<script type="text/javascript">
$().ready(function () {
$.getJSON("arduino_data.php", function (result) {
var chart = new CanvasJS.Chart("chartContainer",{
title :{
text: "Patient #01"
},
data: [{
type: "line",
dataPoints: dataPoints
}]
});
var dataPoints = [];
var updateInterval = 20;
var dataLength = 500; // number of dataPoints visible at any point
var updateChart = function (count) {
count = count || 1;
for (var i = 0; i <= result.length - 1; i++) {
dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
};
}
if (dataPoints.length > dataLength)
{
dataPoints.shift();
}
chart.render();
});
// generates first set of dataPoints
updateChart(dataLength);
// update chart after specified time.
setInterval(updateChart, updateInterval);
});
</script>
<script type="text/javascript" src="canvasjs.min.js"></script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width:100%;">
</div>
</body>
</html>
output: 输出:
Can't find variable: updateChart
找不到变量:updateChart
You used )};
您使用了
)};
instead of });
代替
});
also at the end of your JS you used only }
instead of });
同样在JS的末尾,您只使用了
}
而不是});
also call your chart like 也叫你的图表像
setInterval(updateChart, updateInterval);
and make sure your updateInterval
is in the right function scope. 并确保您的
updateInterval
在正确的函数范围内。
Here's how it should approximately look like: 大致如下所示:
jQuery(function ($) {
function updateChart( result ) { // move it here!!!
$.getJSON("arduino_data.php", function( result ){
var dataPoints = [];
var dataLength = 500; // number of dataPoints visible at any point
var updateInterval = 1000;
var chart = new CanvasJS.Chart("chartContainer",{ // new chart Object
title :{
text: "Patient #01"
},
data: [{
type: "line",
dataPoints: dataPoints
}]
});
for (var i = 0; i <= result.length - 1; i++) {
dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
}
if (dataPoints.length > dataLength){
dataPoints.shift();
}
chart.render();
});
}
// First read - Start
updateChart();
// Update chart after specified time.
setInterval(updateChart, updateInterval);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.