d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How About Highcharts?
Add Reply New Topic New Poll
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
Dec 18 2014 12:18pm
hey folks, so I'm working on trying to get a chart up and running that just randomly generates live random values (so I can later put my own values in for an experiment).

Basically, to start off with, I want to take this page:

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

Download the entire thing as an html file, and then open that file on my browser, and see the exact same thing.

Whenever I do that, it shows the page I want, but the chart is empty and isn't streaming anything.

Here's that:

Code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <!-- Why is this like this? What's it getting from here that's important? -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>


<!-- 1. Add these JavaScript inclusions in the head of your page -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script>
<script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>


<!-- 2. Add the JavaScript to initialize the chart on document ready -->
<script>
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: []
}]
});
});
</script>

</head>
<body>

<!-- 3. Add the container -->
<div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>

<div style="width: 800px; margin: 0 auto">
<p>This example shows how to run a live chart with data retrieved from the server
each second. Study the source code for details.</p>
<p>This is the content of the PHP file, <a href="live-server-data.php">
live-server-data.php</a>:</p>
<pre>&lt;?php
// Set the JSON header
header("Content-type: text/json");

// 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 = rand(0, 100);

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

</div>
</body>
</html>


I figured maybe there was more code that I was missing, so I inspected each of the elements of the first link, and copy and pasted eeeverything.

Still didn't work. It's pretty long (like endless scrolling) which gives the impression I did something wrong.

I originally started with this tutorial:

http://www.highcharts.com/docs/working-with-data/preprocessing-live-data

But I just don't know enough about it to understand what I'm doing. Anyone notice any glaring errors?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 18 2014 02:37pm
did you change the url? need to fully qualify if you're not hosting the file as well.

url: 'live-server-data.php',

This post was edited by carteblanche on Dec 18 2014 02:57pm
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
Dec 18 2014 04:29pm
Quote (carteblanche @ Dec 18 2014 02:37pm)
did you change the url? need to fully qualify if you're not hosting the file as well.

url: 'live-server-data.php',


that doesn't refer to the php at the bottom?

hmm I guess not. What do you mean by fully qualify? When you say hosting the file as well, it sounds like there's a separate file that is a php file. Is there a way to put it all in the same source file? If not, how would I reference it (if that makes sense).

I'm also not opposed to reading, so if there's a resource out there that spells all this out specifically, feel free to link it instead of typing it out. googling "fully qualify" with various programming terms didn't yield much to me. :/

This post was edited by Rejection on Dec 18 2014 04:33pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 18 2014 04:50pm
Quote (Rejection @ Dec 18 2014 05:29pm)
that doesn't refer to the php at the bottom?


You cannot see php in the browser. it executes on the server and sends back plain html to the browser. That's why the author posted the source code for you to see, which is the snippet you're referring to on the bottom.

Author provided the full url if you clicked it: http://www.highcharts.com/studies/live-server-data.php

/edit: did you follow step 1 in the tutorial you followed?

Quote
Set up the server. In this case, we have a simple PHP script returning a JavaScript array with the JavaScript time and a random y value. This is the contents of the live-server-data.php file:


This post was edited by carteblanche on Dec 18 2014 04:56pm
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
Dec 18 2014 05:47pm
Quote (carteblanche @ Dec 18 2014 04:50pm)
You cannot see php in the browser. it executes on the server and sends back plain html to the browser. That's why the author posted the source code for you to see, which is the snippet you're referring to on the bottom.

Author provided the full url if you clicked it: http://www.highcharts.com/studies/live-server-data.php

/edit: did you follow step 1 in the tutorial you followed?



Well that's embarrassing. I thought by setting up the server, it meant set up the code that will act as the pretend server that's going to be sending random values to the chart.

It also makes sense why clicking on this link: http://www.highcharts.com/studies/live-server-data.php

doesn't give me the actual source code, but some arbitrary number - it's generating it from their server.

Ok ok cool. So if I'm understanding correctly, I either need my own "server" that actually runs that php code (in which case I need that source code) and sends it to that javascript function, ooorrrrrr I need a random number generator API in javascript to be imported into that file and then referenced in that function. Right?

This post was edited by Rejection on Dec 18 2014 05:53pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 18 2014 08:01pm
Quote (Rejection @ Dec 18 2014 06:47pm)
Well that's embarrassing. I thought by setting up the server, it meant set up the code that will act as the pretend server that's going to be sending random values to the chart.

It also makes sense why clicking on this link: http://www.highcharts.com/studies/live-server-data.php

doesn't give me the actual source code, but some arbitrary number - it's generating it from their server.

Ok ok cool. So if I'm understanding correctly, I either need my own "server" that actually runs that php code (in which case I need that source code) and sends it to that javascript function, ooorrrrrr I need a random number generator API in javascript to be imported into that file and then referenced in that function. Right?


i'm being a little picky with terminology, but the php isn't sending data to the javascript function, but rather your client is pulling data from the php. you're the only one who knows where your chart data is coming from, so it's up to you to set it up appropriately. if you wanna do it all in javascript, then go for it.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll