Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
130
rated 0 times [  137] [ 7]  / answers: 1 / hits: 5109  / 10 Years ago, tue, march 18, 2014, 12:00:00

Good evening everyone,



this moment I try to work with HighChart / HighStock and after several weeks I were able to display my MySQL-data in the charts. But: I always want more.



Well, I try to reach a dynamic chart which is refreshing like the examples you may know from the website. http://jsfiddle.net/sdorzak/HsWF2/



I used the sample code as a guide. It doesn't work, but think the problem is the missing y-axis because y-axis data come from my MySQL database while x-axis should be the current time.



  <!DOCTYPE HTML>
<html>
<head>
<meta http-equiv=Content-Type content=text/html; charset=utf-8>
<title>POS RESULT</title>

<script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js></script>
<script src=http://code.highcharts.com/highcharts.js></script>
<script src=http://code.highcharts.com/modules/exporting.js></script>

</head>

<body>

<?php
include config.php;

$SQL1 = SELECT * FROM Prices WHERE ticker ='FB';

$result1 = mysql_query($SQL1);
$data1 = array();
while ($row = mysql_fetch_array($result1)) {
$data1[] = $row['Close'];
}


?>

<script type=text/javascript>
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25,
events: {
load: function()
{

var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime();
series.addPoint([x], true);
}, 1000);
},
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150

//categories: [<?php echo join(',', $data2); ?>],
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y +'°C';
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: [{
name: 'Tokyo',
data: [<?php echo join(',', $data1); ?>]
}]
});
});

});
</script>

<div id=container style=min-width: 500px; height: 400px; margin: 0 auto></div>

</body>
</html>


The passage I am talking about and which is shown in the samples is



    chart: {
renderTo: 'container',
type: 'spline',
marginRight: 10,
events: {
load: function() {

// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}


But, as I already wrote, I don't need a random y-axis, just x-axis.



Maybe you can help.
Thanks for your help.



Edit:
live-server-data.php



<?php
// Set the JSON header
header(Content-type: text/json);


include config.php;

$SQL1 = SELECT * FROM Prices WHERE Ticker ='TSLA';

$result1 = mysql_query($SQL1);

while ($row = mysql_fetch_array($result1)) {
$data1[] = $row['Close'];
}

// The x value is the current JavaScript time, which is the Unix time multiplied
// by 1000.
$x = time() * 1000;
// The y value is a random number
$y = $data1;

// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>


The value Close is decimal 24,4.


More From » mysql

 Answers
6

See that solution: http://www.highcharts.com/studies/live-server.htm



    var chart; // global

/**
* Request data from the server, add it to the graph and set a timeout to request again
*/
function requestData() {
$.ajax({
url: 'live-server-data.php',
success: function(point) {
var series = chart.series[0],
shift = series.data.length > 20; // shift if the series is longer than 20

// add the point
chart.series[0].addPoint(eval(point), true, shift);

// call it again after one second
setTimeout(requestData, 1000);
},
cache: false
});
}

$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'spline',
events: {
load: requestData
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150,
maxZoom: 20 * 1000
},
yAxis: {
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: 'Value',
margin: 80
}
},
series: [{
name: 'Random data',
data: []
}]
});
});

[#46738] Tuesday, March 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
debra

Total Points: 583
Total Questions: 111
Total Answers: 111

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
;