Related
// Add series
series: [
{
name: 'Internet Explorer',
type: 'bar',
stack: 'Total',
data: [320, 332, 301, 334, 390, 330, 320] // set quantity of top 10 product of seven days
},
{
name: 'Opera',
type: 'bar',
stack: 'Total',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Safari',
type: 'bar',
stack: 'Total',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Firefox',
type: 'bar',
stack: 'Total',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'Chrome',
type: 'bar',
stack: 'Total',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
};
in data i have to set quantity dynamically to get top 10 product of seven days
Of some reason I can't seem to get this to work with highstock range selector. The navigator and range selector is not displaying correct data groups. The default data grouping doesn't work. The data displays correct in highcharts:
As you can see, the data grouping doesn't work when clicking on the navigator zoom panel. Also the columns are not increasing
http://marialaustsen.com/htdocs/index.html
What am I missing?
Many thanks
PHP script PDO call
//Checking if a request has been made
if( isset( $_POST ) ) {
//Establishing connection and retrieving the data
try {
$con = new PDO( 'mysql:dbname=output;host=localhost', 'root', 'root' );
$results = $con->query( 'SELECT sent_ecards, date FROM details ORDER BY details.date' );
} catch ( PDOException $e ) {
echo 'Connection failed: ' . $e->getMessage();
}
if( isset( $results ) ) {
$data = [];
$categories = [];
$combined = [];
foreach( $results as $row ) {
array_push( $categories, $row['date'] );
array_push( $data, (int) $row['sent_ecards'] );
}
array_push( $combined, $data );
array_push( $combined, $categories );
header( 'Content-Type: application/json' );
echo json_encode( $combined );
}
}
Index file
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Send eCards</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
</head>
<body>
<div id="container" style="width: 100%; height: 800px;"></div>
<button data-action="retrieve_data">Retrieve New Data</button>
<script>
$(document).ready(function() {
//Sending request to the server on button click
$( '[data-action="retrieve_data"]' ).on( 'click', function( e ) {
e.preventDefault();
$.ajax({
url: 'retrieve-data.php',
type: 'POST',
//async: true,
//dataType: "json",
success: function( data ) {
console.log( data );
var axis = $( '#container' ).highcharts().get( 'datetime' ),
series = $( '#container' ).highcharts().get( 'rewards' );
axis.setCategories( data[1], false );
series.setData( data[0], false );
$( '#container' ).highcharts().redraw();
},
})
//Initializing basic chart configuration object
window.chart = new Highcharts.StockChart({
chart: {
renderTo: 'container',
type: 'column',
alignTicks: false
},
title: {
text: 'Send ecards'
},
rangeSelector: {
selected: 1
},
xAxis: {
type: 'datetime',
tickInterval:24 * 3600 * 1000 * 365,
labels: {
formatter: function () {
return Highcharts.dateFormat('%b \'%y', this.value);
}
},
title: {
text: 'Date'
},
id: 'datetime'
},
yAxis: {
title: {
text: 'Amount of ecards'
}
},
tooltip: {
valueSuffix: ' K',
useHTML: true,
crosshairs: true,
shared: true,
pointFormat: '<span style="color:{point.color}">\u25CF </span> <b>{point.series.name}: <b>{point.y}</b> ({point.percentage:.1f}%)<br/>',
formatter: function () {
return Highcharts.dateFormat('%A, %b %e, %Y', this.x);
},
},
series: [{
name: 'ecards',
id: 'rewards',
data: [],
pointInterval: 24*3600*1000,
pointStart: Date.UTC(2010,0,1)
}],
});
//$( '#container' ).(chartConfigOptions);
});
});
</script>
</body>
</html>
The data arrays from consol:
(2) [Array(1999), Array(1999)]
0
:
(1999) [843, 2, 544, 81, 45, 12, 454, 81, 45, 466, 3557, 578, 5, 3434, 65, 843, 3435, 544, 65, 843, 4324, 454, 323, 45, 3435, 544, 323, 45, 2, 546, 323, 45, 739, 3557, 342, 235, 2, 546, 342, 235, 565, 342, 235, 2432, 56, 3456, 2432, 56, 2432, 4543, 5, 3456, 2311, 5, 2839, 565, 5, 2839, 65, 4927, 2, 565, 7, 2, 544, 863, 29, 4324, 454, 5, 346, 466, 565, 5, 346, 2311, 454, 264, 68, 343, 546, 29, 4324, 454, 546, 29, 235, 565, 34, 346, 4324, 454, 34, 346, 739, 3557, 34, 346, 98, 454, …]
1
:
(1999) ["01/01/2010", "01/01/2011", "01/01/2012", "01/01/2013", "01/01/2014", "01/01/2015", "01/02/2010", "01/02/2011", "01/02/2012", "01/02/2013", "01/02/2014", "01/02/2015", "01/03/2010", "01/03/2011", "01/03/2012", "01/03/2013", "01/03/2014", "01/03/2015", "01/04/2010", "01/04/2011", "01/04/2012", "01/04/2013", "01/04/2014", "01/04/2015", "01/05/2010", "01/05/2011", "01/05/2012", "01/05/2013", "01/05/2014", "01/05/2015", "01/06/2010", "01/06/2011", "01/06/2012", "01/06/2013", "01/06/2014", "01/06/2015", "01/07/2010", "01/07/2011", "01/07/2012", "01/07/2013", "01/07/2014", "01/08/2010", "01/08/2011", "01/08/2012", "01/08/2013", "01/08/2014", "01/09/2010", "01/09/2011", "01/09/2012", "01/09/2013", "01/09/2014", "01/10/2010", "01/10/2011", "01/10/2012", "01/10/2013", "01/10/2014", "01/11/2010", "01/11/2011", "01/11/2012", "01/11/2013", "01/11/2014", "01/12/2010", "01/12/2011", "01/12/2012", "01/12/2013", "01/12/2014", "02/01/2010", "02/01/2011", "02/01/2012", "02/01/2013", "02/01/2014", "02/01/2015", "02/02/2010", "02/02/2011", "02/02/2012", "02/02/2013", "02/02/2014", "02/02/2015", "02/03/2010", "02/03/2011", "02/03/2012", "02/03/2013", "02/03/2014", "02/03/2015", "02/04/2010", "02/04/2011", "02/04/2012", "02/04/2013", "02/04/2014", "02/04/2015", "02/05/2010", "02/05/2011", "02/05/2012", "02/05/2013", "02/05/2014", "02/05/2015", "02/06/2010", "02/06/2011", "02/06/2012", "02/06/2013", …]
Here i have a Pie Graph, which is drawn with chart.js. And its working fine. But the problem is the, it renders same color in multiple data sets, which is looking so awkward and confusing.
Here is a snap for that pie chart
Here is my AJAX code
$.ajax({
url: "<?php echo base_url('Analytics/AdminAnalyticsCat');?>",
method: "GET",
success: function (data) {
alert(data);
console.log(data);
var data = JSON.parse(data);
var month = [];
var customers = [];
for (var i in data) {
month.push("Customers in " + data[i].cat_name);
customers.push(data[i].counter);
}
var ctx = document.getElementById("canvasDoughnut").getContext('2d');
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: month,
datasets: [{
data: customers,
lineTension: 0,
fill: false
}]
},
options: {
legend: {
display: false
}
}
});
}
});
I tries so many times for setup their different colors, but its not working.
So Please guide me, where i am going wrong? Thanks..
I am not seeing anywhere in the code you posted, the mention of specifying color scheme...
Per the official documentation found # http://www.chartjs.org/docs/latest/, you need to do the following --- tweak it as needed
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
put the above before options: {
You can call this function which generates random colors for each bars:
var randomColorGenerator = function () {
return '#' + (Math.random().toString(16) + '0000000').slice(2, 8);
};
var barChartData = {
labels: ["Your label sets"],
datasets: [
{
label: "My First dataset",
fillColor: randomColorGenerator(),
strokeColor: randomColorGenerator(),
highlightFill: randomColorGenerator(),
highlightStroke: randomColorGenerator(),
data: [Your datasets]
}
]
};
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');
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