Chart to be created using database details - php

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>

Related

Chartjs.org Displaying different data on Radar Chart tooltip

For example, on the tooltip, I want to show the data for option1 not 2 but 200. Because we show adapted data in the graph. But it will be better to show the real data on the tooltip.
<script>
new Chart(document.getElementById('exampleChartjsRadar').getContext('2d'), {
type: 'radar',
data: {
labels: ["Option1", "Option2", "Option3"],
pointLabelFontSize: 14,
datasets: [{
label: 'Sporcu',
data: [2, 4, 5],
fill: true,
backgroundColor: 'rgba(4, 197, 249, 0.2)',
borderColor: 'rgb(4, 197, 249)',
pointBackgroundColor: 'rgb(4, 197, 249)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgb(4, 197, 249)'
}]
},
options: {
plugins: {
},
elements: {
line: {
borderWidth: 2
}
}
},
});
</script>
enter image description here
You can use the tooltip callback for this:
const options = {
type: 'radar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
}]
},
options: {
plugins: {
tooltip: {
callbacks: {
label: (ctx) => (`${ctx.dataset.label}: ${ctx.raw *100}`)
}
}
}
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script>
</body>
Thanks for the answer, how can I use it if I want to write different values instead of multiplying by 100.
label: (ctx) => (`${ctx.dataset.label}: ${ctx.raw *100}`)

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 - dual-axes column chart not rendering the way it was a week ago

I have created a duel-axes column chart, columns side by side, and it was working great until yesterday.
Yesterday, it started rendering a stacked column graph instead of the columns showing side by side. After checking the highcharts examples on their website, I realized that theirs are also rendering this way now.
Any thoughts on how to get the columns to be side by side again?
Here is an image of what I see in FF and Chrome:
Here is the js from highcharts.com (http://jsfiddle.net/d4GfS/):
$('#container').highcharts({
chart: {
zoomType: 'xy'
},
title: {
text: 'Average Monthly Temperature and Rainfall in Tokyo'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: [{
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
}],
yAxis: [{ // Primary yAxis
labels: {
format: '{value}°C',
style: {
color: '#89A54E'
}
},
title: {
text: 'Temperature',
style: {
color: '#89A54E'
}
}
}, { // Secondary yAxis
title: {
text: 'Rainfall',
style: {
color: '#4572A7'
}
},
labels: {
format: '{value} mm',
style: {
color: '#4572A7'
}
},
opposite: true
}],
tooltip: {
shared: true
},
legend: {
layout: 'vertical',
align: 'left',
x: 120,
verticalAlign: 'top',
y: 100,
floating: true,
backgroundColor: '#FFFFFF'
},
series: [{
name: 'Rainfall',
color: '#4572A7',
type: 'column',
yAxis: 1,
data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
tooltip: {
valueSuffix: ' mm'
}
}, {
name: 'Temperature',
color: '#89A54E',
type: 'column',
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],
tooltip: {
valueSuffix: '°C'
}
}]
});
});

Highcharts line graph, filling xAxis from JSON

I'm trying to get a line graph from Highcharts. My JSON looks like
[{"Monat":"April","d07geb":1.5},{"Monat":"June","d07geb":0.5},{"Monat":"March","d07geb":0.5},{"Monat":"May","d07geb":0.2}]
Monat means month, the value i'm trying to get on the xAxis. d07geb is the amount of money for that specific month.
The line draws perfect and the highchart seems to be correct. But instead of month, the xAxis has numeric values. Starting with 1. Jan and the next entry is 00:00:00.001.
I'm very new at this. I've managed to use pie and column charts. But this one drives me nuts
$(function () {
var chart;
var myJson =
[{"Monat":"April","d07geb":1.5},{"Monat":"June","d07geb":0.5},{"Monat":"March","d07geb":0.5},{"Monat":"May","d07geb":0.2}]
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Money per Month'
},
xAxis: {
type: 'datetime'},
tooltip: {
formatter: function() {
return '<b>'+ this.point.name;
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>'+ this.point.name;
}
}
}
},
series: []
};
var seriesNew = {
type: 'line',
name: 'value',
data: []
};
jQuery.each(myJson, function (itemNo, item) {
seriesNew.data.push([
item.Monat,
item.d07geb
])
});
options.series.push(seriesNew);
chart = new Highcharts.Chart(options);
});
});
If your xAxis is set to datetime, then your JSON needs JS timestamps (Unixtime*1000). I'd suggest you use categories as xAxis:
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
Then you also have to change your JSON to deliver 12 values sorted from January to December.
Could you not separate your x-axis labels and your data values?
If that's an option you could do this:
http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-basic/
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
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]
}]

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 .

Categories