Chart in PHP

Support_38
Support_38 used Ask the Experts™
on
Good Morning.

I need to create a PHP page where the result of a select is brought in the form of html chart.

The select is simple, it will be two columns:

Column 1 - Analyst
Column 2 - Number

The Select returns the number of incidents assigned to each analyst.

I would like the help of experts for the development of this code.

Attached an example that I generate in excel

The select will run on SQL Server.

Thank you.
Capturar.PNG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
I'm pretty sure you can do this in pure php.  However, charting will be much faster if you can do it on the client side of things.  There are a number of libraries for this.

http://www.chartjs.org/
http://www.highcharts.com/
https://d3js.org/
http://morrisjs.github.io/morris.js/
.... and many more

You will see for both samples, you just need to output json data
http://www.chartjs.org/docs/#bar-chart-introduction
var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.5)",
            strokeColor: "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        },
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.5)",
            strokeColor: "rgba(151,187,205,0.8)",
            highlightFill: "rgba(151,187,205,0.75)",
            highlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
You might want to consider JpGraph.

If you need some hands-on help, you might want to consider using E-E Gigs, where you can pay an expert to help you build the app.  Here's how to do the basics, using simulated data that might be found in the query results set.
http://iconoun.com/demo/temp_alex.php
<?php // demo/temp_alex.php
/**
 * http://www.experts-exchange.com/questions/28932886/Chart-in-PHP.html
 *
 * http://jpgraph.net/
 */
error_reporting(E_ALL);
ob_start();

// A SIMULATED RESULTS SET FROM A SQL QUERY
$results_set = array
( [ 'user' =>  'user1', 'qtde' => 60 ]
, [ 'user' =>  'user2', 'qtde' => 33 ]
, [ 'user' =>  'user3', 'qtde' => 26 ]
, [ 'user' =>  'user4', 'qtde' => 23 ]
, [ 'user' =>  'user5', 'qtde' => 21 ]
, [ 'user' =>  'user6', 'qtde' => 15 ]
, [ 'user' =>  'user7', 'qtde' => 11 ]

, [ 'user' => 'user14', 'qtde' =>  3 ]
, [ 'user' => 'user27', 'qtde' =>  1 ]

)
;

// COLLECT THE DATA SET FROM THE SQL RESULTS SET
$datas = $users = [];
foreach ($results_set as $row)
{
    $users[] = $row['user'];
    $datas[] = $row['qtde'];
}


// THE GRAPH TOOL
require_once ('jpgraph-3.5.0b1/src/jpgraph.php');
require_once ('jpgraph-3.5.0b1/src/jpgraph_bar.php');

// THE GRAPH SIZE AND STRUCTURE
$graph = new Graph(400,400);
$graph->SetScale("textlin");
$graph->SetTheme(new UniversalTheme);

$graph->yaxis->SetTickPositions(array(0,20,40,60,80,100), array(10,30,50,70,90));
$graph->SetBox(false);

$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels($users);
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

// ADD THE DATA TO THE GRAPH
$barplot = new BarPlot($datas);
$graph->Add($barplot);
$barplot->SetColor("white");
$barplot->SetFillColor("red");
$graph->title->Set("Example Bar Plot");

// DISPLAY THE GRAPH
$graph->Stroke();

Open in new window

Author

Commented:
Good Morning,

Has helped a lot, I'll do here, then I say if it worked.

Thank you.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

greetings  Alex, , The way that a PHP and javascript CHART is made depends on the Chart you need to build. I looked at the image in your   Capturar.PNG  that you have here, and at the very Top of that Image , you have columns labeled A to M , can you tell us what code you used to make that chart, and what the columns labeled A to M, are used for?
Also if you tell us about what the things to need to chart, like "Prices", "Car Sales", "Voting Results" or "Sports Scores", then we can tell you what we might use to chart that type of value in a vertical Chart.

The more you can say about your chart, the more we can tell you about the ways to do it.

Author

Commented:
Hello,

It is a portal where you will be told the number of alarms assigned to each analyst.

The query is quite simple.

The select is attached.

Thank you.
select.txt
Most Valuable Expert 2011
Top Expert 2016
Commented:
Hi, Alex.  That will work fine with JpGraph.  Try the code here:
http://www.experts-exchange.com/questions/28932886/Chart-in-PHP.html#a41506141

About the only thing that must change would be to insert the query and fetch the results set.  You would do this in place of the SIMULATED RESULTS SET that is used in the demonstration script.  The other settings can be adjusted to suit your exact display needs.

Author

Commented:
Thank you.

It helped.

Author

Commented:
Thank you for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial