need to create sperated data in combination chart (limitless) - php

// 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

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}`)

How to create a graph in PHP with quickcharts

I have a dataset as follows,
Timestamp Value Importance
5/22/2022 4:19 3245 0.0234
5/22/2022 4:09 3246 0.0214
5/22/2022 4:09 3247 0.1234
5/22/2022 3:59 3248 0.0534
5/22/2022 3:59 3249 0.1234
5/22/2022 3:49 3250 0.0244
5/22/2022 3:49 3251 0.4234
5/22/2022 3:39 3252 0.0534
5/22/2022 3:39 3253 0.0234
5/22/2022 3:29 3254 0.4234
5/22/2022 3:29 3255 0.8234
5/22/2022 3:19 3256 0.1234
5/22/2022 3:19 3257 0.0534
5/22/2022 3:09 3258 0.0334
5/22/2022 3:09 3259 0.0234
5/22/2022 2:59 3260 0.0234
I need to show this in a line graph in my php file but I need to highlight point radius according to their importance value, further I need to neglect highlighting all the points which are less than 0.2. So I have searched and found below configuration class from https://quickchart.io/documentation/ but I could not achieve what I need exactly. This will highlight different datasets but not some points only in same dataset.
{
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: 'dataset - big points',
data: [-15, -80, 79, -11, -5, 33, -57],
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
fill: false,
borderDash: [5, 5],
pointRadius: 15,
pointHoverRadius: 10,
},
{
label: 'dataset - individual point sizes',
data: [-86, 59, -70, -40, 40, 33, 16],
backgroundColor: 'rgb(54, 162, 235)',
borderColor: 'rgb(54, 162, 235)',
fill: false,
borderDash: [5, 5],
pointRadius: [2, 4, 6, 18, 0, 12, 20],
},
{
label: 'dataset - large pointHoverRadius',
data: [59, -65, -33, 0, -79, 95, -53],
backgroundColor: 'rgb(75, 192, 192)',
borderColor: 'rgb(75, 192, 192)',
fill: false,
pointHoverRadius: 30,
},
{
label: 'dataset - large pointHitRadius',
data: [73, 83, -19, 74, 16, -12, 8],
backgroundColor: 'rgb(255, 205, 86)',
borderColor: 'rgb(255, 205, 86)',
fill: false,
pointHitRadius: 20,
},
],
},
options: {
legend: {
position: 'bottom',
},
title: {
display: true,
text: 'Chart.js Line Chart - Different point sizes',
},
},
}
Can someone show me how to use above example with my dataset to achieve what I Need?
With Chart.js, you can use pointRadius as a scriptable option.
For example, you can set pointRadius dynamically based on the value:
pointRadius: function (context) {
const index = context.dataIndex;
const value = context.dataset.data[index];
if (value < 0.2) {
return 0;
}
if (value > 0.5) {
return 20;
}
return 10;
}
Link to example in the QuickChart chart editor
In your case, use scriptable pointRadius to implement logic that returns a point size based on the value (e.g., by looking it up in a table of importance).

Getting data from MySQL DB using PHP highstock

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", …]

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');

When rendering Chart by Ajax Uncaught TypeError: Cannot read property 'getTime' of undefined

When I attempt to render the chart with data passed back form ajax, I receive the error, "Uncaught TypeError: Cannot read property 'getTime' of undefined".
$.ajax({
type : 'POST',
url : 'admin-controllers/chart-contorller.php',
dataType : 'json',
encode : true
})
.done(function(data) {
alert('contorller');
if ( success) {
var dataPoints = data.graphData;
console.log(dataPoints);
var chart = new CanvasJS.Chart("timeToSetChart", {
title:{
text: "User Time to initialy set DNS",
fontSize: 20,
},
axisX:{
labelFontSize: 12
},
axisY: {
title: "Number of users",
titleFontSize: 14,
labelFontSize: 12
},
data: [{
type : 'column',
dataPoints: data.graphData
}]
});
chart.render();
}
my string data.graphData comes back as:
[{x: 10, y: 0, label: "1hours"},{x: 20, y: 0, label: "2hours"},{x: 30, y: 16, label: "6hours"},{x: 40, y: 0, label: "12hours"},{x: 50, y: 0, label: "24hours"},{x: 60, y: 0, label: "2days"},{x: 70, y: 0, label: "3days"},{x: 80, y: 0, label: "4days"},{x: 90, y: 0, label: "5days"},{x: 100, y: 0, label: "6days"},{x: 110, y: 0, label: "7days"},{x: 120, y: 3, label: "8plusdays"},]
Which when I paste it into the dataPoints variable it works fine.

Categories