gpt4 book ai didi

javascript - "Uncaught ReferenceError: chart is not defined"

转载 作者:行者123 更新时间:2023-11-29 15:40:20 27 4
gpt4 key购买 nike

全部,

我正在使用以下 HTML/Javascript 代码:

<!doctype html>
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<!-- google fonts from CDN -->
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600' rel='stylesheet' type='text/css'>
<!-- highcharts -->
<script src="http://code.highcharts.com/highcharts.js"></script>

<style>
html, body {
width: 95%;
margin: auto;
font-family: 'Open Sans',sans-serif;
}
#chart_container {
width:100%;
height:500px;
}
</style>
</head>
<body>
<h1>Live Data</h1>
<div id="chart_container">

</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'chart_container',
defaultSeriesType: 'line',
events: {
load: getBirds
}
},
title: {
text: 'DRHW Live Data Stream'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150,
maxZoom: 20 * 1000
},
yAxis: {
title: {
text: 'Count Observed'
}
},
series: [
],
legend: {
layout: 'horiztonal',
align: 'center'
}
});
function getBirds() {
var now = new Date();
var et = now.getTime() / 1000; //PHP TS
var st = et - 10; //30 seconds prior in PHP
console.log(chart);
if (chart.series.length > 0) {
var series = chart.series[0];
var length = series.length + 1;
var shift = series.data.length > 20;
}
$.getJSON("https://path/to/json&callback=?", function(result) {
var data = result.data;if (data.length == 0) {
return;
} else {
additions = new Array();
for (i=0;i<data.length;i++) {
if (data[i].qstype != "1") {
species = data[i].answers[0]['answer'];
scode = species.substring(0,species.search(" - ")).trim()
count = (data[i].answers[1]['answer'] * 1);
newdata = new Object();
newdata['name'] = species;
newdata['count'] = count;
additions.push(newdata);
}
}
//now, for each addition, you need to loop through the existing data structures, and see if the thing exists. if it does, add the data; if not, add the thing, then add the data.
var existingdata = chart.series;
for (i=0;i<additions.length;i++) {
isnewpoint = true;
for (j=0;j<existingdata.length;j++) {
if (existingdata[j].name == additions[i].name) {
isnewpoint = false
count = additions[i].count;
point = [now.getTime(),count];
chart.series[j].addPoint(point, true, shift);
shift = false; //this way, only one shift occurs - the first time through adding a new point to an existing data set. this will control against future shifting, either from other datapoints having new points added,
}
}
if (isnewpoint) {
newseries = new Object();
count = additions[i].count;
newseries['name'] = additions[i].name;
for (j=0;j<length;j++) {
newseries['data'].push(0);
}
newseries['data'].push(count);
chart.series.push(newseries);
}
}
//we have now looped through and added a new data point to all species where new data was created in this pull. We still need to add a new point to those that were not affected.
existingdata = chart.series;
for (i=0;i<existingdata.length;i++) {
getname = existingdata[i].name;
getlength = existingdata[i].data.length;
if (getlength<length) { //which we established earlier as one MORE than existed previously, prior to the addition
point = [now.getTime(),0]
chart.series[i].addPoint(point, true, shift);
}
}
}
setTimeout(getBirds,10000);
});
}
});
</script>

</html>

我遇到的问题非常简单(但让我抓狂!),并且在 js block 的早期。虽然我将变量“chart”定义为新的 Highcharts 图表,并且我将“getBirds”设置为加载后加载的函数,但 console.log 行告诉我图表未定义,下面的行它会抛出错误(Uncaught TypeError: Cannot read property 'series' of undefined)。

我检查了以下内容:

  1. Highcharts 引用资料 ( http://www.highcharts.com/docs/working-with-data/preprocessing-live-data ),其中建议的设置与我的类似;
  2. 我已经尝试在它自己的行上定义 chart 变量(这当然为我的 console.log 定义了图表,但没有定义所需的 chart.series在下一行);
  3. 我已经研究了 stackoverflow 和其他有关可变范围的文档,但我认为根据我的研究我正在正确处理它。
  4. 我试过颠倒顺序 - 将 getBirds() 函数放在 chart 定义之上。

我很迷茫。非常感谢提供的任何帮助;提前致谢!

最佳答案

原因很可能是您无法在声明阶段引用变量。我猜 load 函数在声明时被调用。幸运的是,您可以在函数声明期间引用该对象。尝试以下代码块。

function getBirds(e) {
var now = new Date(),
et = now.getTime() / 1000, //PHP TS
st = et - 10, //30 seconds prior in PHP
chart = this;
if (chart.series.length > 0) {

...在 var block 中声明 chart 变量。

关于javascript - "Uncaught ReferenceError: chart is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20166159/

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