- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在寻找解决我的问题的方法,但到目前为止我发现的一切还不足以让我解决我的问题。
我已经创建了与 MQTT 代理的连接,并且能够订阅主题以及读取和显示数据。我的问题是在收到主题和消息时将数据绘制到图表中。
我创建了一个数组来存储传入的主题。
如果我能得到一些关于我可能做错的事情或者我使用错误的功能的反馈,我将不胜感激!当我加载 HTML 文件时,我收到此错误消息:
Failed to create chart: can't acquire context from the given item
每当我收到来自代理的消息时,我就会失去连接并显示以下消息:
*AMQJS0005E Internal error. Error Message: Cannot read property 'x-axis-0' of undefined, Stack trace: TypeError: Cannot read property 'x-axis-0' of undefined error messages
var broker = 'm23.cloudmqtt.com'; // mqtt broker
var port = 35779; // websocket port
var topic = 'lubcos/#'; // topic to subscribe to, # for wildcard.
var myChart;
var topicArray = new Array();
// create new Paho MQTT client, connect with broker, port and client id
var client = new Paho.MQTT.Client(broker, port, "client_ID");
client.onMessageArrived = onMessageArrived;
client.onConnectionLost = onConnectionLost;
// ************************************************************************ //
// options for connecting to broker
var connectionOptions = {
timeout: 3,
useSSL: true,
userName: "",
password: "",
onSuccess: onConnect,
onFailure: doFail
};
// ************************************************************************ //
// on connection, print which broker it connectede to and which topic it is subscribing to
function onConnect() {
console.log("Successfully connected to: " + broker);
client.subscribe(topic, {qos: 0});
console.log("Subscribed to: " + topic);
}
// ************************************************************************ //
// if connection failes, print error message
function doFail(message) {
console.log("Connection failed: " + message.errorMessage);
}
// ************************************************************************ //
// when connection to the broker is lost print error message
// if connection is lost, try to reconnect
function onConnectionLost(responseObject) {
console.log("connection lost: " + responseObject.errorMessage);
//window.setTimeout(location.reload(),5000);
};
// ************************************************************************ //
// when message arrives it should print out topic and message to console
// if the index to the topic is < 0, it should push the topic to the array called
// mqttTopics.
function onMessageArrived(message) {
// print out topic and data to console
console.log("Topic: " + message.destinationName, ' | ', "Data: " + message.payloadString);
// check if it is a new topic, if not, add to array
if (topicArray.indexOf(message.destinationName) < 0){
// add new topic to array
topicArray.push(message.destinationName);
// get the index number
var y = topicArray.indexOf(message.destinationName);
console.log("Topic Array: " + topicArray + " | " + "Index number: " + y);
// create new dadta series for the chart
var newdata = {
id: y,
name: message.destinationName,
data: []
};
// add data to chart
myChart.update(newdata);
}
};
// ************************************************************************ //
// checks if the number is really a number
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
// ************************************************************************ //
// connect to client by using the information from the option variable
function init() {
client.connect(connectionOptions);
};
// ************************************************************************ //
function plot(point, chartno) {
console.log(point);
var series = myChart.newData[0],
shift = newData.data.length > 200;
myChart.update[chartno].addPoint(point, true, shift);
};
// ************************************************************************ //
var graphOptions = {
responsive: true,
title: {
display: true,
position: "top",
text: "LubCos H20plus II",
fontSize: 18,
fontColor: "#111"
},
legend: {
display: true,
position: "bottom",
labels: {
fontColor: "#333",
fontSize: 16
}
},
scales: {
xAxis: [{
type: 'realtime', // x axis will scroll from right to left
text: 'Time',
margin: 30
}],
yAxis: [{
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: 'Temp °C / Humidity %',
margin: 80
}
}]
}
};
// ************************************************************************ //
var chartData = {
labels: ["topic"],
datasets: [{
label: "Topic",
data: ["data"],
fill: false,
lineTension: 0,
radius: 2
}]
}
// ************************************************************************ //
$(document).ready(function() {
var ctx = $("#line-chartcanvas");
myChart = new Chart(ctx, {
type: 'line',
data: chartData,
options: graphOptions
});
});
index.html
<!DOCTYPE html>
<html>
<head>
<title>Sensor Values MQTT</title>
<link href="css/style.css" rel="stylesheet">
<!-- Include JavaScript classes after which order they are used in -->
<script src="js/jquery.min.js"></script>
<script src="js/chart.min.js"></script>
<!-- Eclipse Paho library for MQTT -->
<script src="js/mqttws31.js"></script>
<script src="js/browserMqtt.js"></script>
<script src="js/mqttGraph.js"></script>
</head>
<body>
<!-- Start connection to the MQTT broker by running init function -->
<body onload="init();">
<!-- Include the chart container and insert the line graph -->
<div class="chart-container">
<canvas>Chart goes here</canvas>
</div>
</body>
</html>
最佳答案
您正在第 7 行定义 var Chart;
。
但是您在函数中使用变量myChart
$(document).ready(function () {
var myChart = new Chart(...)
}
因此使用函数 chart.addSeries(...)
永远无法工作。
编辑:(根据哈迪尔布的评论)
var myChart;
function onMessageArrived(message){
...
myChart.addSeries(newseries);
}
$(document).ready(function() {
...
//leave the var
myChart = new Chart(ctx, {...});
});
<小时/>
编辑2:
第一个错误可能与 var ctx = $("#line-chartcanvas");
部分有关。
在 HTML 中,您需要为 Canvas 指定一个与 JavaScript 代码中同名的 ID:
<!-- Include the chart container and insert the line graph -->
<div class="chart-container">
<canvas id=line-chartcanvas>Chart goes here</canvas>
</div>
您的第二个错误似乎来自这部分:
function onMessageArrived(message) {
// print out topic and data to console
console.log("Topic: " + message.destinationName, ' | ', "Data: " + message.payloadString);
// check if it is a new topic, if not, add to array
if (topicArray.indexOf(message.destinationName) < 0){
// add new topic to array
topicArray.push(message.destinationName);
// get the index number
var y = topicArray.indexOf(message.destinationName);
console.log("Topic Array: " + topicArray + " | " + "Index number: " + y);
// create new dadta series for the chart
var newdata = {
id: y,
name: message.destinationName,
data: []
};
// add data to chart
myChart.update(newdata);
}
};
您正在尝试使用 myChart.update(newdata);
将数据添加到图表中,但它似乎不像 Charts.js 库所期望的参数。此外,您始终传递一个空数据数组。
您应该检查 Charts.js 文档以了解如何正确使用更新功能: https://www.chartjs.org/docs/latest/developers/updates.html
关于javascript - 将MQTT主题和数据添加到chartjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50018543/
有没有办法在 vue-chartjs 中制作带圆角的条形图?我在谷歌上搜索了一下,发现我们可以使用此 url 中给出的渲染条的实现来扩展条形图 - How to put rounded corners
我想通过提供两个数组来创建多条垂直线,第一个称为marketing,其中包含诸如“2017-09-21”等日期和一个数组称为 amount,仅包含数字。 我使用 ChartJS 创建了折线图。最终结果
(对不起我的英语)我将 chartjs-plugin-streaming 用于实时 chartjs,我想使用 chartjs-plugin-zoom。 但是缩放不起作用。 当我测试缩放时,出现缩放区域
我使用 angualr2-chartjs 来显示图表。我的问题是当我动态更改选项时图表更新只有一次。在控制台中,我看到一切都很好,但在 View 中我看不到变化。我使用了 chartjs-plugin
我正在尝试创建类似于 www.chartjs.org 顶部的内容其中有一个随机移动的背景条形图。我发现了另一个类似的question ,但是它似乎不起作用。当我在下面添加时,它不会添加除 400x40
感谢任何提示和/或帮助!我在这里碰壁试图让图表呈现。我已经恢复到测试一个非常简单的方法(下面的代码),但我仍然收到以下错误: 类型错误:无法读取未定义的属性(读取“ map ”) 我可以记录从 use
chartjs-plugin-zoom是 Chart.js 的缩放和平移插件 您可以调用 chart.resetZoom() 以编程方式将缩放重置为默认状态。参见 this example on js
我正在尝试将 Chart.js 与 Vue.js 一起使用,这就是我得到的编译结果,但我没有在 GUI 上看到任何显示。 这是我的文件 DonutChart.vue: // NOT SURE IF
好的,所以我正在尝试通过 VueJS 创建一个 ChartJS 实例,以便我可以通过组件内的 AJAX 请求轻松更新其数据。 基本上它可以工作,创建了 ChartJS 实例,但它是空的,高度和宽度为
我想从 chartjs-chart-treemap 添加树状图到我现有的使用 react-chartjs-2 的项目中. 我到目前为止尝试过: import ChartComponent from '
是否可以定义具有多级别/类别轴的条形图? 例如,我想显示地区/省类别,如下 Excel 图表所示: 我找到了this使用多个 xAxes 的示例。 xAxes:[ { id:'xA
我正在使用 chart.js (V2) 尝试构建一个条形图,用户无需将鼠标悬停在任何地方或点击任何地方即可获得更多信息。我提供了两个示例,说明我希望如何编辑我的图表。 Two edited versi
我想使用 Chartjs(chartjs.org) 作为我的图表工具以及使用 TypeScript 的 AngularJS。我已经从 GitHub 安装了 DefinitelyTyped for Ch
我正在使用 chartjs-plugin-datalabels,并且在大图表中显示较小的数据集时值重叠 这是 chartjs-data-plugin 配置 options: { plu
var yLabels = { 1 : 'New', 2 : 'Learning Phase', 3
我正在创建图表以使用图表 js 呈现一些数据。 const data = { labels: ["1 Dec", "8 Dec", "16 Dec", "31 Dec"], dat
我正在尝试将其他数据插入圆环图中。 Controller 将这样的数组传递给 View : [ 0 => array:3 [ "profit" => 20 "sex" => arr
将百分比值添加到图例标签的最简单方法是什么? 我相信它会使用 generateLabels: function (chart) {},谁能提供一个干净的例子? 最佳答案 似乎没有任何本地方式来显示百分
我已经多次看到这个问题,但我找不到适合我的解决方案。 我将一个 Django 变量传递给 Chartjs 进行绘图,所有的个位数都是正确的,但它将两位数变成了一个。像 11 是 1,1...23 是
我正在尝试使用 chartjs 数据标签插件获取每个条上的值。 所以在条形“a”上方 a 想看到数字 30 及以上或在条形“b”内我想看到数字 50。 但它根本没有显示任何值。 任何人都可以帮助并告诉
我是一名优秀的程序员,十分优秀!