我正在尝试对图表使用jquery选项卡,但在按时间间隔自动更新图表时遇到问题。我观察到,图表只有在我从一个选项卡切换到另一个选项卡并切换回来时才会更新。$dataPoints1
和$dataPoints2
是从一个文件中收集的,并且工作得很好。
查看我的选项卡设置:
我之前的代码(数据在$dataPoints1
&$dataPoints2
中):
<script>
window.onload = function() {
var updateInterval = 1500;
var chart1 = {
animationEnabled: true,
zoomEnabled: true,
theme: "light2",
title: {
text: "Room temperature",
fontSize: 22
},
axisX:{
title: "chart updates every " + updateInterval / 1000 + " secs"
},
axisY:{
suffix: " C"
},
toolTip: {
shared: true
},
legend: {
cursor:"pointer",
verticalAlign: "top",
fontSize: 18,
fontColor: "dimGrey",
itemclick : toggleDataSeries1
},
data: [{
type: "line",
yValueFormatString: "##.##",
xValueFormatString: "hh:mm:ss TT",
showInLegend: true,
legendText: "{name} " + yValue1 + " C",
toolTipContent: "{y} C",
dataPoints: $dataPoints1
}]
}
var chart2 = {
animationEnabled: true,
zoomEnabled: true,
theme: "light2",
title: {
text: "Barometric pressure",
fontSize: 22
},
axisX:{
title: "chart updates every " + updateInterval / 1000 + " secs"
},
axisY:{
suffix: " hPA"
},
toolTip: {
shared: true
},
legend: {
cursor:"pointer",
verticalAlign: "top",
fontSize: 18,
fontColor: "dimGrey",
itemclick : toggleDataSeries2
},
data: [{
type: "line",
yValueFormatString: "##.##",
xValueFormatString: "hh:mm:ss TT",
showInLegend: true,
legendText: "{name} " + yValue2 + " hPA",
toolTipContent: "{y} hPA",
dataPoints: $dataPoints2
}]
}
setInterval(function () { updateChart() }, updateInterval);
var xValue1 = $dataPoints1.length;
var yValue1 = $dataPoints1[$dataPoints1.length - 1].y;
var xValue2 = $dataPoints2.length;
var yValue2 = $dataPoints2[$dataPoints2.length - 1].y;
function toggleDataSeries1(e) {
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else {
e.dataSeries.visible = true;
}
chart1.render();
}
function toggleDataSeries2(e) {
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else {
e.dataSeries.visible = true;
}
chart2.render();
}
function updateChart() {
yValue1 += (Math.random() - 0.5) * 0.1;
yValue2 += (Math.random() - 0.5) * 0.1;
$dataPoints1.push({ x: xValue1, y: yValue1 });
$dataPoints2.push({ x: xValue2, y: yValue2 });
xValue1++;
xValue2++;
chart1.options.data[0].legendText = "Temperature " + yValue1.toFixed(2) + " C";
chart2.options.data[0].legendText = "Barometric " + yValue2.toFixed(2) + " hPA";
chart1.render();
chart2.render();
};
$("#tabs").tabs(
{
create: function (event, ui) {
//Render Charts after tabs have been created.
$("#chartContainer11").CanvasJSChart(chart1);
$("#chartContainer12").CanvasJSChart(chart2);
},
activate: function (event, ui) {
//Updates the chart to its container size if it has changed.
ui.newPanel.children().first().CanvasJSChart().render();
}
}
);
};
</scritp>
<body>
<div id="tabs" style="height: 360px">
<ul>
<li ><a href="#tabs-1" style="font-size: 12px">Client -1-</a></li>
<li ><a href="#tabs-2" style="font-size: 12px">Client -2-</a></li>
</ul>
<div id="tabs-1" style="height: 300px">
<div id="chartContainer11" style="width: 48%; height: 350px;display: inline-block;"></div>
<div id="chartContainer12" style="width: 48%; height: 350px;display: inline-block;"></div><br/>
<div id="chartContainer13" style="width: 48%; height: 350px;display: inline-block;"></div>
<div id="chartContainer14" style="width: 48%; height: 350px;display: inline-block;"></div>
</div>
<div id="tabs-2" style="height: 300px">
<div id="chartContainer21" style="width: 48%; height: 350px;display: inline-block;"></div>
<div id="chartContainer22" style="width: 48%; height: 350px;display: inline-block;"></div><br/>
<div id="chartContainer23" style="width: 48%; height: 350px;display: inline-block;"></div>
<div id="chartContainer24" style="width: 48%; height: 350px;display: inline-block;"></div>
</div>
</div>
<script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
<script src="https://canvasjs.com/assets/script/jquery-ui.1.11.2.min.js"></script>
<script src="https://canvasjs.com/assets/script/jquery.canvasjs.min.js"></script>
</body>
我的错误在哪里?
更新:我接受了@VishwasR的建议,现在有了一个可行的解决方案,但我还有一个问题。如果Client2没有显示任何内容,我怎么能不执行activate: function (event,ui)部分?现在,当我切换到Client2时,我得到了下面的错误,因为没有创建图表:
Uncaught TypeError: ui.newPanel.children().first().CanvasJSChart() is undefined
发布于 2021-04-14 06:23:58
您似乎混淆了chart & chart-options。您将选项存储在chart1和chart2变量中,但试图通过将其调用为chart1.render() & chart2.render()来呈现图表。在创建后立即存储chart1和chart2并每1.5秒更新一次似乎工作得很好。下面是工作代码(JSFiddle)。
var updateInterval = 1500;
var dataPoints1 = [], dataPoints2 = [], chart1, chart2;
var options1 = {
animationEnabled: true,
zoomEnabled: true,
theme: "light2",
title: {
text: "Room temperature",
fontSize: 22
},
axisX:{
title: "chart updates every " + updateInterval / 1000 + " secs"
},
axisY:{
suffix: " C"
},
toolTip: {
shared: true
},
legend: {
cursor:"pointer",
verticalAlign: "top",
fontSize: 18,
fontColor: "dimGrey",
itemclick : toggleDataSeries1
},
data: [{
type: "line",
yValueFormatString: "##.##",
xValueFormatString: "hh:mm:ss TT",
showInLegend: true,
legendText: "{name} " + yValue1 + " C",
toolTipContent: "{y} C",
dataPoints: dataPoints1
}]
}
var options2 = {
animationEnabled: true,
zoomEnabled: true,
theme: "light2",
title: {
text: "Barometric pressure",
fontSize: 22
},
axisX:{
title: "chart updates every " + updateInterval / 1000 + " secs"
},
axisY:{
suffix: " hPA"
},
toolTip: {
shared: true
},
legend: {
cursor:"pointer",
verticalAlign: "top",
fontSize: 18,
fontColor: "dimGrey",
itemclick : toggleDataSeries2
},
data: [{
type: "line",
yValueFormatString: "##.##",
xValueFormatString: "hh:mm:ss TT",
showInLegend: true,
legendText: "{name} " + yValue2 + " hPA",
toolTipContent: "{y} hPA",
dataPoints: dataPoints2
}]
}
function toggleDataSeries1(e) {
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else {
e.dataSeries.visible = true;
}
chart1.render();
}
function toggleDataSeries2(e) {
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else {
e.dataSeries.visible = true;
}
chart2.render();
}
var xValue1 = 0, yValue1 = 100, xValue2 = 0, yValue2 = 100;
function updateChart() {
yValue1 += (Math.random() - 0.5) * 0.1;
yValue2 += (Math.random() - 0.5) * 0.1;
dataPoints1.push({ x: xValue1, y: yValue1 });
dataPoints2.push({ x: xValue2, y: yValue2 });
xValue1++;
xValue2++;
chart1.options.data[0].legendText = "Temperature " + yValue1.toFixed(2) + " C";
chart2.options.data[0].legendText = "Barometric " + yValue2.toFixed(2) + " hPA";
chart1.render();
chart2.render();
};
$("#tabs").tabs(
{
create: function (event, ui) {
//Render Charts after tabs have been created.
$("#chartContainer11").CanvasJSChart(options1);
$("#chartContainer12").CanvasJSChart(options2);
chart1 = $("#chartContainer11").CanvasJSChart();
chart2 = $("#chartContainer12").CanvasJSChart();
setInterval(function () { updateChart() }, updateInterval);
},
activate: function (event, ui) {
//Updates the chart to its container size if it has changed.
ui.newPanel.children().first().CanvasJSChart().render();
}
}
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://canvasjs.com/assets/script/jquery-ui.1.11.2.min.js"></script>
<script src="https://canvasjs.com/assets/script/jquery.canvasjs.min.js"></script>
<link href="https://canvasjs.com/assets/css/jquery-ui.1.11.2.min.css" rel="stylesheet"/>
<div id="tabs" style="height: 360px">
<ul>
<li ><a href="#tabs-1" style="font-size: 12px">Client -1-</a></li>
<li ><a href="#tabs-2" style="font-size: 12px">Client -2-</a></li>
</ul>
<div id="tabs-1" style="height: 300px">
<div id="chartContainer11" style="width: 100%; height: 300px;display: inline-block;"></div>
</div>
<div id="tabs-2" style="height: 300px">
<div id="chartContainer12" style="width: 100%; height: 300px;display: inline-block;"></div>
</div>
</div>
https://stackoverflow.com/questions/67058069
复制相似问题