Flot Charts using SQL Server db with multiple y axis - php

This is my first post, apologies if I've asked a question a number of people have already done so. I don't see the answer I need in other posts.
I'm using Flot Charts and have a SQL Server db, I've got a php file that will connect to the db and return all the values within the sql in an array.
<?php
$server = "XXXX";
$database = "XXXX";
$user = "ReportsUser";
$pwd = "ReportsUser";
$cnn = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $pwd);
if(!$cnn)
{
echo "error in connecting";
}
$sql = odbc_exec($cnn, "
SELECT Months
,Referrals
,ProjectedVol
FROM mis.ReferralsBudgetvsActual
WHERE Months <= MONTH(GETDATE())
");
while($result = odbc_fetch_array($sql))
{
$allreferrals[] = array($result['Months'],$result['Referrals'],$result['ProjectedVol']);
}
echo json_encode(($allreferrals), JSON_NUMERIC_CHECK);
exit;
?>
This works well and produces the array as below
[[1,5981,7465],[2,5473,6962],[3,4974,7391],[4,5731,6985],[5,5891,7080],[6,5168,7136],[7,5551,7543],[8,4427,7242],[9,4617,7204],[10,4807,7642]]
Now, when it all comes together in the jquery file, this is where I end up getting stuck. I don't see where it pulls any other columns back apart from the first data column, how can this be done?
// document ready function
$(document).ready(function() {
var chartColours = ['#88bbc8', '#ed7a53', '#9FC569', '#bbdce3', '#9a3b1b', '#5a8022', '#2c7282'];
// function for refreshing shiftstats chart
make_chart();
function make_chart() {
$.ajax({
cache: 'false',
type: 'GET',
dataType: 'json',
url: "test.php",
success: function(data) {
var d1 = data;
var d2 = data;
//define placeholder class
var placeholder = $(".shifts-chart");
//graph options
var options = {
grid: {
show: true,
aboveData: true,
color: "#3f3f3f" ,
labelMargin: 5,
axisMargin: 0,
borderWidth: 0,
borderColor:null,
minBorderMargin: 5 ,
clickable: true,
hoverable: true,
autoHighlight: true,
mouseActiveRadius: 20
},
series: {
grow: {
active: false,
stepMode: "linear",
steps: 50,
stepDelay: true
},
lines: {
show: true,
fill: false,
lineWidth: 4,
steps: false
},
points: {
show:true,
radius: 5,
symbol: "circle",
fill: true,
borderColor: "#fff"
}
},
legend: {
position: "ne",
margin: [0,-25],
noColumns: 0,
labelBoxBorderColor: null,
labelFormatter: function(label, series) {
// just add some space to labes
return label+' ';
}
},
yaxis: { min: 0 },
xaxis: {ticks:11, tickDecimals: 0},
colors: chartColours,
shadowSize:1,
tooltip: true, //activate tooltip
tooltipOpts: {
content: "%s : %y.0",
shifts: {
x: -30,
y: -50
}
}
};
$.plot(placeholder,
[{
label: "Referrals",
data: d1,
lines: {fillColor: "#f2f7f9"},
points: {fillColor: "#88bbc8"}
},
{
label: "ProjectedVol",
data: d2,
lines: {fillColor: "#f2f7f9"},
points: {fillColor: "#88bbc8"}
}
], options);
}
});
}
});
Thanks

You'll need to change your php array creation to properly format the data into two series:
$d1 = array();
$d2 = array();
while($result = odbc_fetch_array($sql))
{
array_push($d1, array($result['Months'], $result['Referrals']));
array_push($d2, array($result['Months'], $result['ProjectedVol']));
}
$allreferrals = array($d1, $d2);
echo json_encode(($allreferrals), JSON_NUMERIC_CHECK);
This should return it as an array of arrays:
[
[[1,5981],[2,5473],[3,4974],[4,5731],[5,5891],[6,5168],[7,5551],[8,4427],[9,4617,7204],[10,4807]],
[[1,7465],[2,6962],[3,7391],[4,6985],[5,7080],[6,7136],[7,7543],[8,7242],[9,7204],[10,7642]]
]
(Hopefully I have the syntax correct, I'm not a fan of php)
Update from comment
If you are returning a slew of series, you might be better returning an associative array from PHP:
$allreferrals = array('ref' => array(), 'projVol'=> array());
while($result = odbc_fetch_array($sql))
{
array_push($allreferrals['ref'], array($result['Months'], $result['Referrals']));
array_push($allreferrals['projVol'], array($result['Months'], $result['ProjectedVol']));
}
echo json_encode(($allreferrals), JSON_NUMERIC_CHECK);
Then back in the javascript:
$.plot(placeholder,
[{
label: "Referrals",
data: data["ref"]
},
{
label: "ProjectedVol",
data: data["projVol"]
}],
options);

Related

Line Chart using Chart js with time data

I am creating a line chart using chartJs by passing date at X-Axis and time (mm:ss) at Y-Axis. I am not sure how to use chartJs with time values.I tried different solutions from stack but none works in my case.
Here is json file
{"label":["08-Aug-2019","11-Aug-2019","22-Aug-2019","25-Aug-2019"],"time":["1:08","1:44","2:27","1:02"],"chart_data":"{\"label\":[\"08-Aug-2019\",\"11-Aug-2019\",\"22-Aug-2019\",\"25-Aug-2019\"],\"time\":[\"1:08\",\"1:44\",\"2:27\",\"1:02\"]}"}
Here is what i am trying to code
<div id="time_chart"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js"></script>
<script>
let sData = JSON.parse('<?php echo $chart_data; ?>');
let time_ctx = $("#time-chart");
//Line Chart
var time_data = {
labels: sData.label,
datasets: [
{
label: sData.label,
data: sData.time
}
]
};
//options line chart
var time_options = {
responsive: true,
title: {
display: false
},
legend: {
display: false
},
scales: {
yAxes: [{
type: 'time',
time: {
parser: 'm:s',
unit: 'minute',
unitStepSize: 2,
min: '0:0',
max: '30:00',
displayFormats: {
'seconds': 'm.s'
}
},
ticks: {
callback: function(value, index, values) {
//Ticks reverse does not work with time axes so we have to revert in this callback
if (values[values.length - index] != undefined) {
return moment(values[values.length - index].value).format('m.s');
}
}
}
}]
}
};
var chart2 = new Chart(time_ctx, {
type: "line",
data: time_data,
options: time_options
});
</script>
This is what I am getting with this code:
Although I didn't manage to use a time axis for both the x- and y-axes, I managed to create a workaround with a timed x-axis and a linear y-axis.
I parse your time and return the time in seconds (integer). I use this value to display your time and change the format back to mm:ss.
I hope this is what you wanted. I'm not sure you want the axes this way (because in your code you use the y-axis as type "time").
PS: My first post, please feel free to tell me what I can improve.
JSFiddle: https://jsfiddle.net/5837nmyo/
JSBin: https://jsbin.com/yadixolica/1/edit?html,js,output
let sData = {}
sData.label = ["08-Aug-2019","11-Aug-2019","22-Aug-2019","25-Aug-2019"]
sData.time = ["1:08","1:44","2:27","1:02"]
let chartData = {}
chartData.label = sData.label
chartData.time = parseTimeToSeconds(sData.time)
function parseTimeToSeconds(times){
let regex = /(\d*):(\d{2})/gm
let parsedTime = []
for (let x = 0; x < times.length; x++) {
let match = regex.exec(times)
parsedTime.push(parseInt(match[1])*60 + parseInt(match[2]))
}
return parsedTime
}
let time_ctx = document.getElementById('time_chart');
let time_data = {
labels: chartData.label,
datasets: [{
label: chartData.label,
data: chartData.time
}]
};
let time_options = {
responsive: true,
title: {
display: false
},
legend: {
display: false
},
tooltips: {
callbacks: {
label: function(tooltipItem, data){
let value = parseInt(tooltipItem.value)
if (value%60 < 10)
return Math.floor(value/60) + ":" + 0 + value%60
else
return Math.floor(value/60) + ":" + value%60
}
}
},
scales: {
xAxes: [{
type: 'time'
}],
yAxes: [{
ticks: {
min: 0,
max: 1800,
stepSize: 120,
callback: function(value, index, values) {
if (value%60 < 10)
return Math.floor(value/60) + ":" + 0 + value%60
else
return Math.floor(value/60) + ":" + value%60
}
}
}]
}
};
let chart2 = new Chart(time_ctx, {
type: "line",
data: time_data,
options: time_options
});

Execute SQL prepared statement in AJAX

I can successfully create a graph through Chart.js. The issue is that I would like to generate a graph based on a specific customer's ID. So for example if I were to visit customer http://localhost/test/view_customer?customer_id=21&operation=edit. I would like use the bdi vs. date of customer ID 21 to generate a graph.
I have tried many different solutions, however I have been unsuccessful. I wanted to know if there was anyway to execute an SQL request through AJAX and use that data to generate a graph.
Here is my SQL code that grabs the ID from the URL and generates a string that could be read by chart.js to create a line graph:
<?php
//Gets customer ID from URL
$cid = htmlentities ($_GET['customer_id']);
//query to get data from the table
$sql = sprintf("SELECT treatment_log.bdi, treatment_log.date FROM treatment_log WHERE treatment_fk = ? ORDER BY created_at");
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "i", $cid);
mysqli_stmt_execute($stmt);
$data = array();
mysqli_stmt_bind_result($stmt, $bdi, $date);
while(mysqli_stmt_fetch($stmt)) {
$data[] = array('bdi' => $bdi, 'date' => $date);
}
//free memory associated with result
$result->close();
//now print the data
print json_encode($data); ?>
Here is the code I use to generate a line graph:
$(document).ready(function(){
$.ajax({
url: "http://localhost/test/data.php",
method: "GET",
success: function(data) {
console.log(data);
var bdi = [];
var date = [];
for(var i in data) {
date.push( data[i].date);
bdi.push(data[i].bdi);
}
var chartdata = {
labels: date,
datasets : [
{
label: 'BDI',
backgroundColor: 'rgba(239, 243, 255, 0.75)',
borderColor: 'rgba(84, 132, 255, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: bdi
}
]
};
var ctx = $("#mycanvas");
var barGraph = new Chart(ctx, {
type: 'line',
data: chartdata,
options: {
responsive: true,
legend: {
position: 'bottom',
},
scales: {
yAxes: [{
ticks: {
fontColor: "rgba(0,0,0,0.5)",
fontStyle: "bold",
beginAtZero: true,
maxTicksLimit: 5,
padding: 20
},
gridLines: {
drawTicks: false,
drawBorder: false,
}
}],
xAxes: [{
gridLines: {
zeroLineColor: "transparent",
display: false
},
ticks: {
padding: 20,
fontColor: "rgba(0,0,0,0.5)",
fontStyle: "bold"
}
}]
},
tooltips: {
backgroundColor: 'rgba(255,255,255)',
titleFontColor: 'rgb(184,189,201)',
bodyFontColor: 'black',
displayColors: false,
borderColor: 'rgb(214,217,225)',
borderWidth: 1,
caretSize: 5,
cornerRadius: 2,
xPadding: 10,
yPadding: 10
}
}
});
},
error: function(data) {
console.log(data);
}
});
});
Currently I am using a URL http://localhost/test/data.php that has a string of data:
[{"bdi":"4","date":"2018-07-11"},{"bdi":"1","date":"2018-07-21"},{"bdi":"5","date":"2018-07-21"},{"bdi":"34","date":"2018-07-21"},{"bdi":"34","date":"2018-07-21"},{"bdi":"3","date":"2018-07-22"},{"bdi":"2","date":"2018-07-23"},{"bdi":"12","date":"2018-07-23"},{"bdi":"3","date":"2018-07-24"},{"bdi":"2","date":"2018-07-25"},{"bdi":"12","date":"2018-07-30"},{"bdi":"3","date":"2018-07-30"},{"bdi":"4","date":"2018-07-30"},{"bdi":"11","date":"2018-07-30"}]
Instead of using a URL to link the data. I want the AJAX to run my SQL code and generate the graph (where the treatment_fk is a variable).
Question:
1. Is it possible to execute an SQL command in AJAX to generate a graph, where the ID is a variable collected from the URL? (How would I do this?)
2. Is there a better way? How would I do that?
Apple one addition parameter to ajax request
$.ajax({
url: "data.php",
data: {
"ChartType": 1/*Or change to drop down selection id*/,
"customer_id":1
},
type: "GET",
success: function(response) {
console.log(response);
},
error: function(xhr) {
}
});
You can see ChartType above, pass its value dynamically for which your want to separate sql query/
Now on server side:
$cid = htmlentities ($_GET['customer_id']);
$cType = htmlentities ($_GET['ChartType']);
if($cType==1)
$sql = sprintf("SELECT treatment_log.bdi, treatment_log.date FROM treatment_log WHERE treatment_fk = ? ORDER BY created_at");
else if($cType==2)
$sql = sprintf("SELECT __ other column name __ WHERE cid=$cid");
else
$sql = sprintf("SELECT __ other column name __ WHERE cid=$cid");
I know this is not complete code, but it will give you idea how to do it.

Pass php variable in bar graph with highcharts

I have a bar graph made with highcharts that display properly, data are from mysql table. I would like to pass value i mean : ($category['data'][] = $r['Assign_To']) in each bar as variable so that when i click on a bar this value contained in variable is taken to a new page in order to use it in that page. The exemple given in highcharts website doesn't fit me because i use data from table.
1- this is the graph
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'bar'
},
title: {
text: 'EOP Postings Issues Chart',
x: -20 //center
},
credits: {
enabled: false
},
xAxis: {
categories: []
},
yAxis: {
min: 0,
title: {
text: 'Requests'
},
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: []
}
$.getJSON("DataEobChart.php", function(json) {
options.xAxis.categories = json[0]['data'];
options.series[0] = json[1];
chart = new Highcharts.Chart(options);
});
});
DataEobChart.php
$dbc = #mysqli_connect('', '', '', '');
$query = mysqli_query($dbc, "select *, count(Assign_To) as count,Assign_To from claims_follow_up.eob_posting where Status='open' group by Assign_To order by count desc");
$category = array();
$category['name'] = 'Month';
$series1 = array();
$series1['name'] = 'Number of issues assigned';
while($r = mysqli_fetch_array($query)) {
$category['data'][] = $r['Assign_To'];
$series1['data'][] = $r['count'];
}
$result = array();
array_push($result,$category);
array_push($result,$series1);
print json_encode($result, JSON_NUMERIC_CHECK);
mysqli_close($dbc);
Add a new section to your HighCharts definition called plotOptions containing this code:
plotOptions: {
series: {
cursor: 'pointer',
point: {
events: {
click: function (event) {
var index = event.point.index;
var Assign_To = options.xAxis.categories[index];
alert(Assign_To);
location.href = "NewPage.php?AssignTo=" + Assign_To;
}
}
}
}
},
This provides a click event handler which will figure out which options.xAxis.categories[] item to use based on which bar was clicked. It will then alert the result and then take the user to a page called NewPage.php with a request string called AssignTo.

HighCharts, Json and Ajax

I have a php code that generates me a result in json format
<?php
header('Content-type: application/json');
include_once 'Conexion.php';
$objeto = new Conexion();
$conexion = $objeto->conectar();
$fecInicio = (isset($_POST['fechaInicio'])) ? $_POST['fechaInicio'] : '';
$fecFin = (isset($_POST['fechaFin'])) ? $_POST['fechaFin'] : '';
$consulta = "SELECT MONTHNAME(fecha), sum(totalVenta) FROM ventas WHERE fecha BETWEEN '$fecInicio' AND '$fecFin' GROUP BY MONTH(fecha)";
$resultado = $conexion->prepare($consulta);
$resultado->execute();
$result = array();
while ($fila = $resultado->fetch(PDO::FETCH_ASSOC)){
array_push($result, array($fila["MONTHNAME(fecha)"], $fila["sum(totalVenta)"])) ;
}
print json_encode($result);
$conexion=null;
With jquery ajax json I get and I want to use in data for a chart with Highcharts. This is the code of my JS
var chart1;
var options;
$(document).ready(function() {
var fechaInicio;
var fechaFin;
$("#generarReporte").click(function(){
fechaInicio = $("#fechaInicio").val();
fechaFin = $("#fechaFin").val();
$.ajax({
url: "../libreria/ORM/reportes.php",
type: "POST",
datatype:"json",
data: {fechaInicio:fechaInicio, fechaFin:fechaFin },
success: function(data) {
//recibo el json desde PHP y lo paso a string
var valores = JSON.stringify(data);
console.log(valores);
options.series[0].data = valores;
}
});
$("#opcion5").click();
});
$("#opcion5").click(function(){
var theModal = $("#modalGraficos").modal({
show: false
});
options = {
chart: {
renderTo: 'container1',
type: "column"
},
title: {
text: "Ventas Mensuales"
},
subtitle: {
text: "PerĂ­odo consultado, desde: <strong>"+fechaInicio+"</strong> hasta: <strong>"+fechaFin+"</strong>"
},
xAxis: {
type: "category",
labels: {
rotation: -45,
style: {
fontSize: "13px",
fontFamily: "Verdana, sans-serif"
}
}
},
yAxis: {
min: 0,
title: {
text: "Pesos AR$"
}
},
//establecemos los colores de las columnas por Mes
colors: [
"#4572A7",
"rgba(248, 44, 91, 0.61)",
"#89A54E",
"#80699B",
"#3D96AE",
"#DB843D",
"#92A8CD",
"#A47D7C",
"#B5CA92"
],
plotOptions: {
column: {
colorByPoint: true
}
},
legend: {
enabled: false
},
tooltip: {
pointFormat: "Total del Mes: <b>$ {point.y:.2f}</b>"
},
series: [{
name: "Ventas por mes",
dataLabels: {
enabled: true,
//rotation: -90,
rotation: 0,
color: "#ffffff",
align: "center",
format: "{point.y:,.2f}",
y: 30, // 10 pixels down from the top
style: {
fontSize: "13px",
fontFamily: "Verdana, sans-serif"
}
},
data:[
]
}]
}; //fin options
var chart1 = new Highcharts.Chart(options);
theModal.on("shown",function(){
// Recreate the chart now and it will be correct
});
theModal.modal("show");
});
});
The result console log is valid json format:
[["March","436400.00"],["April","1302261.50"]]
console log
I can not make the chart. It can help or assist? thank you very much
you need to set the data you get from ajax call to chart options. this link might help Load data into Highcharts with Ajax
A few clues:
Chart should mi inititailised in ajax callback
numbers shoudl not be a strings like you have. It should be [["March",436400.00],["April",1302261.50]]. To do that set the flag "JSON_NUMERIC_CHECK" in json_encode() function.

Get stats and show it in chart - PHP/HighCharts

I have a table called "tracks",it looks like this:
id,
name,
url
hits
I have a pie charts, from highcharts, the code looks like this:
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'chart',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Where users came from, total'
},
tooltip: {
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false
},
showInLegend: true
}
},
credits: {
enabled: false
},
series: [{
type: 'pie',
name: 'Browser share',
data: [
['Firefox.com', 45.0],
['Default.com', 26.8],
['MSN', 12.8],
['Google.com', 8.5],
['Yahoo.com', 6.2],
['Others', 0.7]
]
}]
});
});
My question is, how can I get data from the "tracks" table (hits), and the (name) and output it in the pie chart like:
'name','hits'
And then, at last, convert # of hits, to %.
series: [{
type: 'pie',
name: 'Browser share',
data: [
<?php
$sql = 'select sum(hits) from tracks';
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$totalHits = $row[0];
$sql = 'select name, hits from tracks order by hits desc';
$result = mysql_query($sql);
$i = 0;
$totalOther = 0;
$maxSlices = 5;
$minPercent = 10;
while ($row = mysql_fetch_row($result))
{
$percent = row[1] / $totalHits * 100;
if (++$i <= $maxSlices && $percent >= $minPercent)
{
echo json_encode($row);
}
else
{
$totalOther += $row[1];
}
}
if ( $totalOther > 0 ) echo json_encode(array('Other', $totalOther));
?>
]
}]
if you want to refresh it thourgh ajax...
...
series: [{
type: 'pie',
name: 'Browser share',
data: getData(function(result) {return result;}),
...
and
function getData(setDataCallback)
{
$.getJSON('file.php', args, function(result) { // use of jquery for clarity.
setDataCallback(result);
});
}
in php you have to do a new file ('file.php') with the code shown in the first code block;
Simplest way is to query data from mySql and then sort it same as
[
['Firefox.com', 45.0],
['Default.com', 26.8],
['MSN', 12.8],
['Google.com', 8.5],
['Yahoo.com', 6.2],
['Others', 0.7]
]
Requires if you want to show chart just on page loading.
You can also use JSON.

Categories