/    Sign up×
Community /Pin to ProfileBookmark

Chartjs from MariaDB

Greetings!

I have the following issue, I’ll try to describe it as precisely as possible while not overcomplicating things. My Arduino is currently measuring the Temperature and the Relative Humidity via a DHT-11 sensor, then forwards that data to my Raspberry Pi through bluetooth. A script on the Pi then reads the data and writes it into a MariaDB database. The layout is the following:
_TempPressure_ is the database name, _SensorData_ is the table, the columns are _ID_, _temperature_, _humidity_ and date
The following code retrieves the last 20 measured data from the database:

[code]<?php
header(‘Content-Type: application/json’);

$conn = new mysqli(“”, “”, “”, “”);

$sqlQuery = “SELECT * FROM(SELECT * FROM SensorData order by ID desc limit 20) as tmpList order by ID asc”;

$result = mysqli_query($conn,$sqlQuery);

$data = array();
foreach ($result as $row) {
$data[] = $row;
}

mysqli_close($conn);

echo json_encode($data);

?>[/code]

This works fine as you can see on this picture:
http://puu.sh/GACVj/29cc5e6c72.png![http://puu.sh/GACVj/29cc5e6c72.png](src)

The front end would look like a line chart with the 2 measured results where x is the date and y is temperature and humidity both with a separate line. I’ve done most of my work with the help of chartjs.org on my own.

[code]<!DOCTYPE html>
<html>
<head>
<title>Temperature and Relative Humidity Sensor Data</title>
<style>
canvas{
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>

<script type=”text/javascript” src=”js/jquery.min.js”></script>
<script type=”text/javascript” src=”js/Chart.min.js”></script>

</head>
<body>
<div style=”width:75%”>
<canvas id=”graphCanvas”></canvas>
</div>

<script>
$(document).ready(function () {
showGraph();
});

function showGraph()
{
{
$.post(“temp_data.php”,
function (data)
{
console.log(data);
var date = [];
var temperature = [];
var humidity = [];

for (var i in data) {
date.push(data[i].date);
temperature.push(data[i].temperature);
humidity.push(data[i].humidity);
}

var graphTarget = $(“#graphCanvas”);
var lineGraph = new Chart(graphTarget, {
type: ‘line’,
data: {
labels: date,
datasets[{
label:’Temperature’,
backgroundColor: ‘#49e2ff’,
borderColor: ‘#46d5f1’,
hoverBackgroundColor: ‘#CCCCCC’,
hoverBorderColor: ‘#666666’,
data: temperature,
},
{
label:’Humidity’,
backgroundColor: ‘#49e2ee’,
borderColor: ‘#36f5f1’,
hoverBackgroundColor: ‘#CCCCCC’,
hoverBorderColor: ‘#333333’,
data: humidity,
}]
},

options: {
scales: {
yAxes: [{
ticks: {
min:0,
max:45,
beginAtZero: true
}
}]
}
}
}
});

});
}
}
</script>
</body>
</html>[/code]

After a few days of trying I’ve decided to turn to some help. So here I am. If you need further clarification please do tell and I’ll provide the required information.

~Finger

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 07.2020 — Unfortunately backtics didn't format your code as required. Use code tags instead: `your code here`. I edited your posting accordingly.

You better post the JSON as text in code tags so that one can easily set up a test file.

I used some mockup JSON created by mockaroo and made your code runnable.

There were several errors in the bracketing.

When using $.post you need to parse the JSON or specify the datatype for jQuery. The manual says: "dataType... Default: Intelligent Guess (xml, json, script, text, html)." but this didn't work. Using $.getJSON would be another option.

for-in loop should not be used for arrays:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Array_iteration_and_for...in

&lt;div style="width:75%"&gt;
&lt;canvas id="graphCanvas"&gt;&lt;/canvas&gt;
&lt;/div&gt;

<i> </i>&lt;script&gt;
<i> </i> showGraph();
<i> </i> function showGraph() {
<i> </i> $.post("thread455.php",
<i> </i> function (data) {
<i> </i> console.log(data);
<i> </i> var date = [];
<i> </i> var temperature = [];
<i> </i> var humidity = [];

<i> </i> // for (var i in data) {
<i> </i> // date.push(data[i].date);
<i> </i> // temperature.push(data[i].temperature);
<i> </i> // humidity.push(data[i].humidity);
<i> </i> // }
<i> </i> data.forEach(item =&gt; {
<i> </i> date.push(item.date);
<i> </i> temperature.push(item.temperature);
<i> </i> humidity.push(item.humidity);
<i> </i> });

<i> </i> var graphTarget = $("#graphCanvas");
<i> </i> var lineGraph = new Chart(graphTarget, {
<i> </i> type: 'line',
<i> </i> data: {
<i> </i> labels: date,
<i> </i> datasets: [{
<i> </i> label: 'Temperature',
<i> </i> backgroundColor: '#49e2ff',
<i> </i> borderColor: '#46d5f1',
<i> </i> hoverBackgroundColor: '#CCCCCC',
<i> </i> hoverBorderColor: '#666666',
<i> </i> data: temperature,
<i> </i> },
<i> </i> {
<i> </i> label: 'Humidity',
<i> </i> backgroundColor: '#49e2ee',
<i> </i> borderColor: '#36f5f1',
<i> </i> hoverBackgroundColor: '#CCCCCC',
<i> </i> hoverBorderColor: '#333333',
<i> </i> data: humidity,
<i> </i> }]
<i> </i> },

<i> </i> options: {
<i> </i> scales: {
<i> </i> yAxes: [{
<i> </i> ticks: {
<i> </i> min: 0,
<i> </i> max: 100,
<i> </i> beginAtZero: true
<i> </i> }
<i> </i> }]
<i> </i> }
<i> </i> }
<i> </i> });
<i> </i> },
<i> </i> 'json'
<i> </i> );
<i> </i> }
<i> </i>&lt;/script&gt;
Copy linkTweet thisAlerts:
@FingerwimpauthorOct 07.2020 — @Sempervivum#1624046

I was trying to explain how it all worked but forgot to actually ask the question. Yes the chart is not working unfortunately. Sorry about that. Cannot seem to figure out what the issue is, I can get it work with 1 dataset but for what I'm trying to do 2 is essential.

I've tested it earlier with this version of the code and it displays it properly, adjusted to how it is on chartjs and it unfortunately stops working.

var chartdata = {
labels: date,
datasets: [
{
label: 'Temperature',
backgroundColor: '#49e2ff',
borderColor: '#46d5f1',
hoverBackgroundColor: '#CCCCCC',
hoverBorderColor: '#666666',
data: temperature
}
]
<br/>
<i> </i> };
<i> </i> var graphTarget = $("#graphCanvas");

<i> </i> var lineGraph = new Chart(graphTarget, {
<i> </i> type: 'line',
<i> </i> data: chartdata,
<i> </i> options: {
<i> </i> scales: {
<i> </i> yAxes: [{
<i> </i> ticks: {
<i> </i> min: 0,
<i> </i> max: 45
<i> </i> }
<i> </i> }]
<i> </i> }
<i> </i> }
<i> </i> });
<i> </i>

Sorry for the formatting error as well.
Copy linkTweet thisAlerts:
@SempervivumOct 07.2020 — I edited my posting, unfortunately this and your second posting crossed. Please reload this page and read my first posting again.
Copy linkTweet thisAlerts:
@FingerwimpauthorOct 07.2020 — @Sempervivum#1624048 I feel silly for just saying thank you for your help because you have no idea how much I tried to fix it and seems like I've been looking at the wrong place the entire time. Unfortunately all I can say is that I really appreciate it. Now that the layout looks like on chartjs too regarding the datasets, the additional alteration also seems to be working which is just the cherry on the top.

I'll be more careful with my post formatting in the future, thanks again for your quick responses and accurate help.
Copy linkTweet thisAlerts:
@SempervivumOct 07.2020 — You're welcome, feel free to ask more questions.
×

Success!

Help @Fingerwimp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...