0👍
✅
This what how i did it.
<?php
//$sth = $db->prepare("SELECT Actual FROM `csv_1data` WHERE Name = '1) Height 130' ORDER BY FK_palle");
// USE VALUE FROM <select><option></option></select> INSTEAD OF 1) Height 130
$sth = $db->prepare("SELECT Actual FROM `Angle` ORDER BY Dato_ur_stillet");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
$result = $sth->fetchAll(PDO::FETCH_COLUMN);
// $result = explode("@", implode(",@", $result));
// print_r for at se resultaterne.
echo'<pre>';
print_r($result);
echo'</pre>';
$std = $db->prepare("SELECT Palle_nr FROM `Angle` ORDER BY `Dato_ur_stillet` ASC");
$std->execute();
/* Fetch all of the remaining rows in the result set */
$palle = $std->fetchAll(PDO::FETCH_COLUMN);
?>
<!----------------------myChart---------------------->
<script src="./assets/charts/dist/Chart.js"></script>
<script>
var canvas = document.getElementById("myChart");
var ctx = canvas.getContext("2d");
var horizonalLinePlugin = {
afterDraw: function(chartInstance) {
var yScale = chartInstance.scales["y-axis-0"];
var canvas = chartInstance.chart;
var ctx = canvas.ctx;
var index;
var line;
var style;
if (chartInstance.options.horizontalLine) {
for (index = 0; index < chartInstance.options.horizontalLine.length; index++) {
line = chartInstance.options.horizontalLine[index];
if (!line.style) {
style = "rgba(169,169,169, .6)";
} else {
style = line.style;
}
if (line.y) {
yValue = yScale.getPixelForValue(line.y);
} else {
yValue = 0;
}
ctx.lineWidth = 3;
if (yValue) {
ctx.beginPath();
ctx.moveTo(0, yValue);
ctx.lineTo(canvas.width, yValue);
ctx.strokeStyle = style;
ctx.stroke();
}
if (line.text) {
ctx.fillStyle = style;
ctx.fillText(line.text, 0, yValue + ctx.lineWidth);
}
}
return;
};
}
};
Chart.pluginService.register(horizonalLinePlugin);
var data = {
labels: [<?php echo join($palle, ',') ?>],
datasets: [{
label: "My First dataset",
fill: false,
lineTension: 0,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(51,150,255,100.2)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 2,
pointHitRadius: 10,
data: [<?php echo join($result, ',') ?>],
}]
};
var myChart = new Chart(ctx, {
type: 'line',
data: data,
options: {
"horizontalLine": [{
"y": 130.75,
"style": "rgba(255, 0, 0, .4)",
}, {
"y": 129.1,
"style": "#00ffff",
}]
}
});
</script>
0👍
- How can i edit my data into data from database.
You should make a separate php
file for getting data from database, graph.php
for instance. After getting data from database, json_encode()
and print()
it.
$query = "SELECT * FROM `csvhoejde1`";
$result = mysqli_query($conn, $query);
$data = array();
foreach($result as $row){
$data[] = $row;
}
print(json_encode($data));
- How can i edit my code so that it updates on time interval.
You can accomplish this with an ajax
call and setInteval()
function.
Now, in chart.js
, do something like this:
function init(){
$.ajax({
type : "get",
url : "graph.php"
success: function(data){
var json = JSON.parse(data);
//remaining of your chart code goes here, add this json to data
}
});
}
setInterval(init, 5000);
where 5000 means 5 sec, change this to whatever time you want.
Source:stackexchange.com