Highcharts chart with column and line using json - php

I am new to Highcharts and i already like it.
I have made some column charts using json, php en mysql (with given examples).
Now i want to make a chart with columns and a line using json.
Can anybody tell how i schould do that??
$(function () {
var chart;
$(document).ready(function() {
$.getJSON("DataKwhDagCum.php", function(json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'column',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'In (Enexis) vs Out (Zonnestroom)',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20','21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']
},
yAxis: {
title: {
text: 'Amount'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: json
});
});
});
});

You can use two ways
use php and script in the same file: http://docs.highcharts.com/#preprocessing-data-from-a-database
return array as json by using json_encode() in php, and then use it in Highcharts.

Related

Updating Jquery data series using php

How can i update the below Jquery data series of a chart using data avaliable from Mysql using php.
var data = {
labels: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'],
series: [{
name: 'series-1',
data: [5, 9, 7, 8, 5, 3, 5, 4, 5, 9, 7, 8]
}, {
name: 'series-2',
data: [11,14,11,19,15,12,14,18,11,10,13,15]
}]
};
Below is the entire code of the chart js. I tried creating a javascript variable and assigning a php variable to it.but it didn't work.
jQuery(document).ready(function() {
// Chartist
var options = {
low: 0,
high: 20,
showArea: true,
showPoint: false,
fullWidth: true,
axisY: {
labelInterpolationFnc: function(value) {
return '$'+value+'k';
},
scaleMinSpace: 20
},
series: {
'series-2': {
showArea: true,
showPoint: false,
fullWidth: true,
},
'series-1': {
fullWidth: true,
}
}
};
var data = {
labels: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'],
series: [{
name: 'series-1',
data: [5, 9, 7, 8, 5, 3, 5, 4, 5, 9, 7, 8]
}, {
name: 'series-2',
data: [11,14,11,19,15,12,14,18,11,10,13,15]
}]
};
new Chartist.Line("#fullChart", data, options);
var data = {
series: [1, 5]
};
var sum = function(a, b) { return a + b };
new Chartist.Pie('#chartistPie', data, {
labelInterpolationFnc: function(value) {
return '';
}
});
new Chartist.Line('#areaChart', {
labels: [1, 2, 3, 4, 5, 6, 7],
series: [
[5, 9, 7, 8, 5, 3, 5]
]
}, {
low: 0,
showArea: true,
fullWidth: true,
fullWidth: true,
showPoint: false,
colors:["#f44336"],
axisY: {
showGrid: false,
showLabel: false,
offset: 0
},
axisX:{
showGrid: false,
showLabel: false,
offset: 0
},
lineSmooth: true,
});
// Extrabar
$("#layout-static .static-content-wrapper").append("<div class='extrabar-underlay'></div>");
// Calendar
$('#calendar').datepicker({todayHighlight: true});
// Easypie chart
try{
$('.easypiechart#chart1').easyPieChart({
barColor: "#00bcd4",
trackColor: 'rgba(255,255,255,0.1)',
scaleColor: 'transparent',
scaleLength: 8,
lineCap: 'round',
lineWidth: 4,
size: 144,
onStep: function(from, to, percent) {
$(this.el).find('.percent').text(Math.round(percent));
}
});
}
catch(e){}
$('.progress-pie-chart').each(function(index, obj) {
new Chartist.Pie(obj, {
series: [$(obj).attr('data-percent'), 15]
}, {
labelInterpolationFnc: function(value) {
return '';
},
width: '42px',
height: '42px',
});
})
// Sparklines
var sparker = function() {
var barSpacing = ($('#dailysales2').width() - 13*6)/13;
$("#dailysales, #dailysales2").sparkline([5,6,7,2,0,4,2,4,6,8,1,4,6,4], {
type: 'bar',
height: '144px',
width: '100%',
barWidth: 4,
barSpacing: Math.floor(barSpacing),
barColor: 'rgba(255,255,255,0.3)'});
$("#biglines").sparkline([11,5,8,13,10,12,5,9,11], {
type: 'line',
width: '100%',
height: '106px',
lineWidth: 0.01,
lineColor: '#fff',
fillColor: '#e0e0e0',
highlightSpotColor: '#b0bec5',
highlightLineColor: '#b0bec5',
chartRangeMin: 0,chartRangeMax: 20,
spotRadius: 0
});
$("#biglines").sparkline([9,5,10,8,12,5,12,7,10], {
type: 'line',
width: '100%',
height: '106px',
lineWidth: 0.01,
lineColor: '#fff',
fillColor: '#3f51b5',
highlightSpotColor: '#546e7a',
highlightLineColor: '#546e7a',
chartRangeMin: 0,
chartRangeMax: 20,
composite: true,
spotRadius: 0
});
$('#dashboard-sparkline-indigo').sparkline([5,2,4,9,3,4,7,2,6,4], { type: 'bar', barColor: 'rgba(255,255,255,0.5)', height: '48px',width: '100%', barWidth: 2, barSpacing: 4, spotRadius: 4, chartRangeMin: 1});
$('#dashboard-sparkline-gray').sparkline([5,3,1,4,3,4,7,8,2,3], { type: 'bar', barColor: 'rgba(255,255,255,0.5)', height: '48px',width: '100%', barWidth: 2, barSpacing: 4, spotRadius: 4, chartRangeMin: 1});
$('#dashboard-sparkline-primary').sparkline([1,3,2,9,1,6,5,2,6,9], { type: 'bar', barColor: 'rgba(255,255,255,0.5)', height: '48px',width: '100%', barWidth: 2, barSpacing: 4, spotRadius: 4, chartRangeMin: 1});
$('#dashboard-sparkline-success').sparkline([2,5,4,9,6,3,7,1,5,1], { type: 'bar', barColor: 'rgba(255,255,255,0.5)', height: '48px',width: '100%', barWidth: 2, barSpacing: 4, spotRadius: 4, chartRangeMin: 1});
}
var sparkResize;
$(window).resize(function(e) {
clearTimeout(sparkResize);
sparkResize = setTimeout(sparker, 500);
});
sparker();
});
Something like
$data = array(
'labels' => array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'),
'series' => array(array(
'name' => 'series-1',
'data' => array(5, 9, 7, 8, 5, 3, 5, 4, 5, 9, 7, 8)
), array(
'name' => 'series-2',
'data' => array(11,14,11,19,15,12,14,18,11,10,13,15)
))
);
return json_encode($data);
Simple JS array [] - become simple PHP array(), JS object {key: value} become associative PHP array ('key' => 'value');

Highcharts with multiple dataLabels

I am using Highcharts to display transaction status for different months. The chart is displaying fine. To show the number of transactions for each month, I have placed a datalabel on top of each column. The code is given below:
$(function () {
$('#hc').highcharts({
chart: {
renderTo: 'container',
type: <?php echo $type ; ?>,
marginRight: 130,
marginBottom: 35
},
title: {
text: 'Transaction Status',
x: -20
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: <?php echo $monnth_series ?>
},
yAxis: {
title: {
text: 'Transactions'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
color: '#fff',
style: {fontWeight: 'bold'},
formatter: function() {return this.y},
y: -5,
rotation: 360
},
column: {
pointPadding: .2,
groupPadding: 0,
borderWidth: 1
}
}
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +': '+ this.y; +'</b>'
}
},
credits: { enabled: false },
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -6,
y: 90,
borderWidth: 0
},
series: <?php echo $series_data ?>
});
});
Now I want to place another datalabel on the body of the column which will show the type of the transaction.
If I try
formatter: function() {return this.series.name},
inside: true,
then the datalabel shows correctly but the datalabel on top of the column does not show up.
Any idea on how to do this ?

MSSQL/PHP to HighChart via JSON

Im trying to get data from my SQL server via php, and output it as JSON, so i can use it for HighCharts. So far i can get it to post my results as different series if that makes any sense.
SQL/PHP:
$params = array(&$_POST['query']);
$tsql1 = "SELECT TOP 10 Company_Names, Revenue_Value1, Revenue_Value2
FROM Database.test report
GROUP BY Company_Name
ORDER BY Revenue_Value1
$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$getProducts = sqlsrv_query($conn, $tsql1, $params, $options);
if ($getProducts === false)
die( FormatErrors( sqlsrv_errors() ) );
while( $row = sqlsrv_fetch_array( $getProducts, SQLSRV_FETCH_ASSOC) )
{
$rows[] = $row;
}
echo json_encode($rows, JSON_NUMERIC_CHECK);
sqlsrv_free_stmt( $getProducts );?>
the sql prints out something like this
http://i.imgur.com/ww0kasV.png
And prints this chart:
http://i.imgur.com/rh8ICVH.png
Obviously, this is not how i want it to output..
I want it to output xAxis as Quarters, yAxis as Revenue_Value1 and Series-name as Company_Name.
i am currently using this to get my php data.
<script type="text/javascript">
$(function () {
var chart;
$(document).ready(function() {
$.getJSON("data.php", function(json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Revenue',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
title: { text: 'Months'}
},
yAxis: {
title: {
text: 'Revenue'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: json
});
});
});
});
</script>
data.php echoes:
{"Company_Names":"Company_Name","Revenue_Value1":45443,"Revenue_Value2":4654}
Of course i had to change some values because of some rules.. i hope that you can still see what i am trying to achieve.
EDIT:
$arr = array();
while( $row = sqlsrv_fetch_array( $getProducts, SQLSRV_FETCH_ASSOC) )
{
$name = $row['Distributor'];
$data = array($row['Value']);
$arr[] = array('name' => $name, 'data' => $data);
}
echo json_encode($arr, JSON_NUMERIC_CHECK);
This put my data out a way that HighCharts could read it
Your series should be in an array try something var j =[]; j.push(json); then do series: j

Highchart shows only 12 categories

Hi guys I'm having a trouble working with Highchart. I think my error is when the category is more than 12. here's my code:
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},
xAxis: {
categories: ['ARC', 'CLM', 'COLREP', 'CUI', 'FB', 'IOH', 'IST', 'LCV', 'LCWSLC', 'LOGDFR', 'LOGDRT', 'LOGGLE',
'LOGGT', 'LOGSR', 'LOGVL', 'LST', 'OARD', 'OARP', 'OARPPP', 'POR', 'PRIN', 'SLRD', 'SLRS', 'SLRSUP', 'SVTCBR',
'SVTSP', 'SVTST', 'TKD', 'TR', 'USO']
},
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: [400.6, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}, {
name: 'New York',
data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
}, {
name: 'Berlin',
data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
}, {
name: 'London',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 100.9]
}]
});
});
});​
I already try to put the categories in array variable but same result show.
x-axis only show 12 categories. the example on there site is good for 12 categories. Is anyone know how to fix this problem or configure anything in the plugin?
here's there demo page http://www.highcharts.com/demo/line-basic
thanks in advance!
The problem is that you have series with only 12 Values within each array ,so if there's only 12 Values it will pick up first 12 Categories .
High chart's wont show Categories without data related to them using Series ,just put 18 next 0 Values at Series Array and it will show those Categories .

Chart to be created using database details

I have chart code as below:
var chart;
$(document).ready(function() {
chart = new Mychart.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
series: [{
name: 'Tokyo',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}, {
name: 'New York',
data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
}, {
name: 'Berlin',
data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
}, {
name: 'London',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
}]
});
});
I want to pass countries name from my database table country i.e. array of country names and similarly data also from database to be passed to series.
Can anybody help me how this is to be passed in this jquery using php.
Thanks in advance
You create a file and retrieve data from DB, like:
<? $result = mysql_query($query);
echo "series: [";
while ($row = mysql_fetch_assoc($result)) {
echo "{";
echo "name:'".$row['cityname']."',";
echo "data: [".$row['data']."]";
echo "}";
}
echo "];";
?>
If your data is another array you can loop that too, just remember to append strings correctly.
I think, the best practice to pass data you need is json-object, e.g.:
<?php
$countries = array(
'france' => array(
'capital' => 'Paris',
),
'spain' => array(
'capital' => 'Madrid',
)
);
?>
<script type="text/javascript">
var countries = <?php echo json_encode($countries); ?>;
alert(countries.spain.capital) # ==> returns Madrid
</script>

Categories