Highcharts highstock plot with data from php - php

Trying to recreate a simple example of html page with a Highstock plot with no success. Seem to do the same thing as in multiple examples I found online, but the plot is still empty.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>This is title</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'spline',
marginRight: 130,
marginBottom: 25,
},
title: {
text: 'test plot'
},
xAxis: {
type: 'datetime',
categories: []
},
yAxis: {
text: 'value'
},
series: [{name:'myline',data:'<?php $data = array(2,3,1,4); echo json_encode($data) ?>'}]
}
chart = new Highcharts.Chart(options);
});
</script>
</head>
<body>
<div id="container" style="width: 100%; height: 400px; margin: 0 auto"></div>
</body>
</html>
If I change the series line to
series: [{name:'myline',data:[2,3,1,4]}]
it works. So, the problem must be on this line and with the php script.
However, I can't make it to work with php. Tried printing a string with properly formatted numbers, json_encrode and some other things, but can't seem to make it work. All I see in such case is chart with no line on it. Printing out result from php execution script doesn't seem to produce the output of the script as well, only an empty line... What am I doing wrong?

It turned out to be a stupid mistake due to the fact that I'm a noob in this. I had to set up a web server on my computer (I used LAMP) and put my php file into /var/www/ directory. Apter that this data:<?php $data = array(2,3,1,4); echo json_encode($data); ?> works as it should.

Related

Graph is not showing up in the webpage using php and html

I'm trying to get a Graph from mySQL database, but somehow it doesn't show a graph on my webpage.
This is the graph.html file, which should be showing the graph on the webpage.
<h1>Graphs</h1>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<title>
Google Visualization API Sample
</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart']});
</script>
<script type="text/javascript">
function drawVisualization() {
var jsonData = null;
var json = $.ajax({
url: "data.php", // make this url point to the data file
dataType: "json",
async: false,
success: (
function(data) {
jsonData = data;
})
}).responseText;
// Create and populate the data table.
var data = new google.visualization.DataTable(jsonData);
// Create and draw the visualization.
var chart= new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, {curveType: "function",
width: 500, height: 400,
vAxis: {maxValue: 10}}
);
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 500px; height: 400px;"></div>
</body>
</html>
In the browser I get an error saying:
Uncaught (in promise) TypeError: $.ajax is not a function and
Error: Could not establish connection. receiving end does not exist.
Any idea how to fix this?
Thanks.
Edit:
In the head I've changed: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> to <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"></script>, I tought i used a slim version of jquery, so i've chand the head of the html file, but it still doesn't work.
I tried downloading jquery using npm install jquery, but that doesn't do anything for either.
Most of my code for the html page is from https://developers.google.com/chart/interactive/docs/php_example.
So i'm kind of lost where to look for errors.

jqGrid not loading json data

I am not sure what's missing in my code. I have a jqGrid loading json data. I am using jqGrid version 5.2.0.
Below is my html and script.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Data Grids</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
<!-- jqGrid jQuery css -->
<!-- A link to a jQuery UI ThemeRoller theme, more than 22 built-in and many more custom -->
<link rel="stylesheet" href="../static/plugins/jqGrid/css/jquery-ui.css" />
<!-- The link to the CSS that the grid needs -->
<link rel="stylesheet" href="../static/plugins/jqGrid/css/ui.jqgrid.css" />
<!-- jqGrid jQuery js -->
<script type="text/javascript" src="../static/plugins/jqGrid/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="../static/plugins/jqGrid/js/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="../static/plugins/jqGrid/js/i18n/grid.locale-en.js"></script>
</head>
<body>
<table id="myGrid"></table>
<div id="myGridPager"></div>
<script type="text/javascript">
$(document).ready(function () {
$("#myGrid").jqGrid({
url: "/pages/php/getGridData.php",
datatype: "json",
mtype: "GET",
colNames: ["Asset Number", "Cost", "Source", "Project ID", "Task ID", "PO Number"],
colModel: [
{ name: "ASSET_ID", width: 80 },
{ name: "FIXED_ASSETS_COST", width: 90, align: "right" },
{ name: "FEEDER_SYSTEM_NAME", width: 80 },
{ name: "PROJECT_ID", width: 80 },
{ name: "TASK_ID", width: 80 },
{ name: "PO", width: 80, sortable: false }
],
jsonReader : {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: true,
id: "id",
cell: "cell"
},
width: 700,
height: 'auto',
pager: "#myGridPager",
rowNum: 10,
rowList: [10, 20, 30],
sortname: "ASSET_ID",
sortorder: "desc",
viewrecords: true,
gridview: true,
autoencode: true,
caption: "Source Lines (FAR)"
});
$("#myGrid").jqGrid('navGrid','#myGridPager',{edit:false,add:false,del:false});
});
</script>
</body>
</html>
The json data is as follows:
{"page":1,"total":1,"records":10,"rows":[{"id":2432447,"cell":[2432447,1450410,57.23,"ORACLE PROJECTS",835711,3219040,"86652"]},{"id":2432451,"cell":[2432451,868512,0,"ORACLE PROJECTS",741610,3126807,"84605"]},{"id":2432453,"cell":[2432453,868512,5403.6,"ORACLE PROJECTS",741610,3126807,"84605"]},{"id":2432455,"cell":[2432455,1814091,0,"ORACLE PROJECTS",840842,3210792,"87986"]},{"id":2432456,"cell":[2432456,1814091,600,"ORACLE PROJECTS",840842,3210792,"87986"]},{"id":2432460,"cell":[2432460,841706,0,"ORACLE PROJECTS",767616,3226001,"85666"]},{"id":2432465,"cell":[2432465,1814095,0,"ORACLE PROJECTS",840842,3210293,"87986"]},{"id":2432466,"cell":[2432466,1814095,600,"ORACLE PROJECTS",840842,3210293,"87986"]},{"id":2432901,"cell":[2432901,1231634,0,"ORACLE PROJECTS",741610,3190901,"84605"]},{"id":2432915,"cell":[2432915,1231634,4651.72,"ORACLE PROJECTS",741610,3190901,"84605"]}]}
The page loads but with no data. Is there anything I am doing wrong?
From the code that you have posted it seems like href attribute is not set to a valid URL.
For example :
<link rel="stylesheet" href="../static/plugins/jqGrid/css/jquery-ui.css" />
(Similarly for other JS and CSS links added in the link and script tags)
Changing the links to a valid url will most probably solve your issue.
The problem was my php code (getGridData.php) which even though was giving the correct json output, but was also outputting some warnings and notices.
I had to disable E_WARNING and E_NOTICE errors in my php.
error_reporting(E_ERROR | E_PARSE);
I still have to take care of my php code not just to ignore the errors. But in the meantime, this worked.
Thanks Pratyay for your response.

Flot graph not showing

I am pulling data from a mySql database using php to plot a graph using flot. The query works fine but no graph is being displayed when I run the code. A blank space is coming up where the graph should be. Here is what I have:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Flot Graph</title>
<link href="layout.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript" src="../jquery.js"></script>
<script language="javascript" type="text/javascript" src="../jquery.flot.js"></script>
</head>
<body>
<h1>Flot Graph</h1>
<div id="placeholder" style="width:600px;height:300px;"></div>
<?php
$mysqli=new mysqli('localhost','root',"",'simpledb');
$sql="select name, age from `people` where age=23";
$results = $mysqli->query($sql);
if ($results)
{
while ($row=$results->fetch_assoc())
{
$dataset1[] = array($row['name'],$row['age']);
}
}
else
{
echo "Error";
}
?>
<script type="text/javascript">
var dataset1 = <?php echo json_encode($dataset1); ?>;
$(function ()
{
$.plot($("#placeholder"), [
{
data: dataset1,
bars: {show: true}
}
]);
});
</script>
</body>
</html>
It looks like your x values are strings, yet you haven't included the categories plugin.
Thanks everyone. I got it to work. I'm not sure what the problem was but it worked after I changed the javascript part to :
$(function ()
{
$.plot("#placeholder", [ dataset1 ], {
series: {
bars: {
show:true,
barWidth: 0.6,
align: "center"
}
},
xaxis: {
mode: "categories",
tickLength:0
}
});
});

Getting data in a text file after updating it on highcharts but initially retrieving from csv file

I am actually trying to do exactly what has been done in this example "http://jsfiddle.net/BrLp7/" but unfortunately not able to perform this task when retrieving data from csv file. Below is my code which doesn't output anything and when we click on a point in a given example the resulting graph has to be stored in some text file in this form 5,10,13 if last point was clicked.
<html>
<head>
<title>A BASIC HTML FORM</title>
</head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION = "BasicForm.php">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<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://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="../js/highcharts.js"></script>
<!--[if IE]>
<script type="text/javascript" src="../js/excanvas.compiled.js"></script>
<![endif]-->
<!-- 2. Add the JavaScript to initialize the chart on document ready -->
<script type="text/javascript">
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'line'
},
title: {
text: 'Input'
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Units'
}
},
plotOptions: {
series: {
cursor: 'pointer',
point: {
events: {
click: function() {
$.get('testFile.csv', function(data) {
// Split the lines
var lines = data.split('\n');
$.each(lines, function(lineNo, line) {
var x=this.series.processedXData.indexOf(this.x);
var items = line.split(',');
seriesData =[];
$.each(items, function(itemNo, item) {
if (itemNo < x) {
seriesData.push(parseFloat(item));
} else if (itemNo == x){
seriesData.push(0);
}
});
}
this.series.setData(seriesData);
}
}
}
},
series: []
};
$.get('testFile.csv', function(data) {
// Split the lines
var lines = data.split('\n');
$.each(lines, function(lineNo, line) {
var items = line.split(',');
var series = {
data: []
};
$.each(items, function(itemNo, item) {
series.data.push(parseFloat(item)); });
options.series.push(series);
});
var chart = new Highcharts.Chart(options);
});
});
</script>
</head>
<body>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/data.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<!-- 3. Add the container -->
<div id="container" style="width: 1400px; height: 400px; margin: 0 auto"></div>
</body>
</html>
</FORM>
</body>
</html>

Highchart and php

Hi guys i m trying to pass value from my php to highchart dataset but its not working help needed.
<?php $count2=$decode[nextPage][totalResults];
echo("<input type=hidden name=aaqib value=".$count.">");
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var chart;
var j= $("input[name=aaqib]").val();
var k=100;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
title: {
text: 'Logarithmic axis demo'
},
xAxis: {
tickInterval: 1
},
yAxis: {
type: 'logarithmic',
minorTickInterval: 0.1
},
tooltip: {
headerFormat: '<b>{series.name}</b><br />',
pointFormat: 'x = {point.x}, y = {point.y}'
},
series: [{
data: [j,k,
pointStart: 1
}]
});
});
});
</script>
</head>
<body>
<script src="../../js/highcharts.js"></script>
<script src="../../js/modules/exporting.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>
Hi guys i m trying to pass value from my php to highchart dataset but its not working help needed.
i need to use value of J and K in data so that they are shown in highcharts
Store your php vars into a js vars and then pass to highcharts.
Fix the following too.
echo('<input type="hidden" name="aaqib" value="{$count}"/>');
Update according to this comment.
series: [{
data: [[j,k]]
}]
Tthis demo should help you.
why are you echoing the input above your doctype declaration? I think that belongs inside your body. And you should also '-signs around the attribute values in you echo. something like this:
<?php $count2=$decode[nextPage][totalResults];
echo("<input type='hidden' name='aaqib' value='$count'>");
?>
Some browsers might threat this correctly, but it's actualy invalid html

Categories