Hello, Everyone.
I'm happy to know this amazing forum.
I'm stuck in showing node-red dashboard chart from my mysql database;
Database changed
mysql> show tables;
+------------------------+
| Tables_in_water_sensor |
+------------------------+
| sensor_data |
+------------------------+
1 row in set (0.00 sec)
mysql> desc sensor_data;
+-------------+-------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| water_level | float | YES | | NULL | |
| date | date | YES | | NULL | |
+-------------+-------+------+-----+---------+----------------+
3 rows in set (0.03 sec)
mysql> select * from sensor_data;
+----+-------------+------------+
| id | water_level | date |
+----+-------------+------------+
| 1 | 12.5 | 2023-06-01 |
| 2 | 13.2 | 2023-06-02 |
| 3 | 11.8 | 2023-06-03 |
| 4 | 10.3 | 2023-06-04 |
| 5 | 12.1 | 2023-06-05 |
function node code
let labels = [];
let data = [];
msg.payload.forEach(row => {
labels.push(row.date);
// console.log(`row.date ${row.date.toString()}`);
data.push(row.water_level);
});
msg.payload = [
{
series: ['Water Level'],
data: [{
x: labels,
y: data.slice(0, labels.length)
}],
labels: labels
}
];
return msg;
flow json
[
{
"id": "bad3255e28febdae",
"type": "tab",
"label": "db",
"disabled": false,
"info": "",
"env": []
},
{
"id": "9d6acaa2d2ed70c7",
"type": "mysql",
"z": "bad3255e28febdae",
"mydb": "5909f0fde201f313",
"name": "",
"x": 300,
"y": 180,
"wires": [
[
"e07ba751b59c0f58"
]
]
},
{
"id": "7264023da74a4a8c",
"type": "debug",
"z": "bad3255e28febdae",
"name": "debug 46",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 780,
"y": 200,
"wires": []
},
{
"id": "fd3e944368c16007",
"type": "inject",
"z": "bad3255e28febdae",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "SELECT * FROM sensor_data",
"payload": "",
"payloadType": "date",
"x": 120,
"y": 180,
"wires": [
[
"9d6acaa2d2ed70c7"
]
]
},
{
"id": "e07ba751b59c0f58",
"type": "function",
"z": "bad3255e28febdae",
"name": "function 19",
"func": "let labels = [];\nlet data = [];\n\nmsg.payload.forEach(row => {\n labels.push(row.date);\n // console.log(`row.date ${row.date.toString()}`);\n data.push(row.water_level);\n});\n\nmsg.payload = [\n {\n series: ['Water Level'],\n // data: [{\n // x: labels,\n // y: data.slice(0, labels.length)\n // }],\n labels: labels\n }\n];\n\n// console.log(`labels.length : ${labels.length}`);\n// console.log(`data.length : ${data.length}`);\n// for (var i = 0; i < data.length; i++)\n// {\n// console.log(data[i]);\n// node.send({ payload: data[i] });\n// }\n\n// console.dir(msg);\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 550,
"y": 280,
"wires": [
[
"7264023da74a4a8c",
"a4cf520cba4c62af"
]
]
},
{
"id": "a4cf520cba4c62af",
"type": "ui_chart",
"z": "bad3255e28febdae",
"name": "",
"group": "aa8eec0d5014b069",
"order": 1,
"width": 0,
"height": 0,
"label": "chart",
"chartType": "line",
"legend": "false",
"xformat": "HH:mm:ss",
"interpolate": "linear",
"nodata": "",
"dot": false,
"ymin": "0",
"ymax": "50",
"removeOlder": 1,
"removeOlderPoints": "",
"removeOlderUnit": "3600",
"cutout": 0,
"useOneColor": false,
"useUTC": false,
"colors": [
"#1f77b4",
"#aec7e8",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"outputs": 1,
"useDifferentColor": false,
"className": "",
"x": 790,
"y": 320,
"wires": [
[]
]
},
{
"id": "5909f0fde201f313",
"type": "MySQLdatabase",
"name": "",
"host": "127.0.0.1",
"port": "3306",
"db": "water_sensor",
"tz": "",
"charset": "UTF8"
},
{
"id": "aa8eec0d5014b069",
"type": "ui_group",
"name": "second",
"tab": "4f903fa3c7ed98d8",
"order": 2,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "4f903fa3c7ed98d8",
"type": "ui_tab",
"name": "Dashboard2",
"icon": "dashboard",
"order": 1,
"disabled": false,
"hidden": false
}
]
Thank you.