How to create a graph in PHP with quickcharts - php

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).

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

need to create sperated data in combination chart (limitless)

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

Change color according to data in pie chart.js

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]
}
]
};

Highchart error with date values on xaxis

I am trying to build a line chart but my highchart isn't rendering with the following code.
Could someone check what am I doing wrong?
$('#employee_chart').highcharts({
chart: {
type: 'line'
},
title: {
text: 'REPORTS AT A GLANCE (LAST 30 DAYS)'
},
xAxis: {
type: "datetime",
dateTimeLabelFormats: {
day: '%d'
},
tickInterval: 24 * 3600 * 1000,
min: Date.UTC(<?php echo date("Y, m, d",strtotime("-30 days"));?>),
max: Date.UTC(<?php echo date("Y, m, d");?>)
},
yAxis: {
title: {
text: 'Temperature (°C)'
}
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: false
}
},
series: [{
name: 'Unique Logins',
data: [7, 6, 9, 14, 18, 21, 25, 26, 23, 18, 13, 9, 7, 6, 9, 14, 18, 21, 25, 26, 23, 18, 13, 9, 7, 6, 9, 14, 18, 21]
}]
});
Thank you
It seems you are missing pointStart which is cause of the issue.
pointStart :someDate
See Fiddle with similar issue when no pointStart
and Working fiddle when defined pointStart
The problem seems to be coming from your xaxis settings:
As you can see here your chart renders without these settings: http://jsfiddle.net/e4fru078/
tickInterval: 24 * 3600 * 1000,
min: Date.UTC(<?php echo date("Y, m, d",strtotime("-30 days"));?>),
max: Date.UTC(<?php echo date("Y, m, d");?>)
I would recommend to look into categories for your xaxis:
example here: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/chart/reflow-true/

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