Home > Article > Backend Development > Create data visualization charts using PHP and JpGraph
In today's data-driven era, data analysis is an essential part of various industries. The core part of data analysis is data visualization, because data visualization can help us understand the data more clearly. Through visualization, we can identify problems caused by data that may be missing, incorrect, or for other reasons. However, in the process of processing data and data analysis, sometimes we need to create some professional charts.
One of the important data visualization tools is charts, because charts can represent data interpretively. In this article, we will create data visualization charts using PHP and JpGraph.
PHP is a popular web programming language that is commonly used for the development of web applications. PHP has strong database integration and can easily handle various types of databases, making it one of the best choices for web applications. JpGraph, on the other hand, is a charting library designed for PHP that can help us create various types of dynamic and static charts.
In order to start creating charts, we need to install PHP and JpGraph.
Install PHP and JpGraph
We can use the installation package to install PHP and JpGraph. Before doing this, we need to make sure that Apache and MySQL are installed on our system.
Before installing JpGraph, we need to ensure that the GD library has been installed and enabled. You can confirm whether the GD library has been enabled by using the phpinfo() function. If the GD library is not enabled, please set it in the php.ini file and restart Apache.
Next, we can download JpGraph and unzip it. Copy the unzipped folder to the directory of our web server.
(Note: The installation steps vary depending on the environment, please check the relevant information to choose the installation method that suits you)
Create charts
Before we can start creating charts, we need Create some data. In this example, we will use a simple array to represent sales data as follows:
$month = array("Jan","Feb","Mar","Apr","May ","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
$sales = array(50,150,200,180,250,300,350,400,480,500,550,650);
Now, We can start creating charts using JpGraph. We will use the BarPlot class to create a bar chart. The following is the code to create a bar chart:
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
// Create a chart And subgraph
$graph = new Graph(600,400);
$graph->SetScale("textlin");
$graph->SetShadow();
$graph->SetMarginColor ('white');
$graph->title->Set("Monthly Sales");
$graph->xaxis->title->Set("Month" );
$graph->yaxis->title->Set("Sales");
$graph->xaxis->SetTickLabels($month);
// Create a bar chart
$barplot = new BarPlot($sales);
$barplot->SetFillColor("#B0C4DE");
$barplot->value->Show( );
$graph->Add($barplot);
// Display chart
$graph->Stroke();
In the above code, we first Import the JpGraph library and create a Graph object. Next, we set the size and scaling of the chart (including the x- and y-axis titles). Then, we set the labels for the x-axis scale, here we use the month data we just created. Next, we create the BarPlot object and create the bar chart by passing the sales data to it. We also set the fill color of the histogram and how the values are displayed. Finally, we use the Add() method to add the histogram to the chart, and then use the Stroke() method to display the chart.
In addition, we can also create other types of charts, such as line charts, pie charts, scatter charts, etc. These charts can adjust their size, color, fonts, labels, etc. according to our needs. The following is the code to create a line chart and a pie chart:
Create a line chart:
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php' );
// Create graphs and subgraphs
$graph = new Graph(600,400);
$graph->SetScale("textlin");
$graph->SetShadow ();
$graph->SetMarginColor('white');
$graph->title->Set("Monthly Sales");
$graph->xaxis ->title->Set("Month");
$graph->yaxis->title->Set("Sales");
$graph->xaxis- >SetTickLabels($month);
// Create a line chart
$lineplot = new LinePlot($sales);
$lineplot->SetColor("blue");
$graph->Add($lineplot);
// Display chart
$graph->Stroke();
Create a pie chart:
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_pie.php');
// Create data
$data = array(20,30,50);
// Create charts and subgraphs
$graph = new PieGraph(600,400);
$graph->SetShadow();
// Set title
$graph ->title->Set("Monthly Sales Distribution");
// Create a pie chart
$p1 = new PiePlot($data);
$p1->SetSliceColors(array('#2794F4','#C9DE3C','#FF9933'));
$p1->value->SetFont(FF_ARIAL,FS_BOLD,12);
$p1->value->SetColor('black');
$p1->SetLabelType( PIE_VALUE_PER);
$p1->SetCenter(0.5,0.4);
// Add pie chart
$graph->Add($p1);
/ / Display chart
$graph->Stroke();
In the above code, we created an array named $data to represent sales data. Then, we create a PieGraph object and set its shadow. Next, we set the title of the pie chart. Then, we created a pie chart and set the pie chart's properties such as color, font, label and position. Finally, we add the pie chart to the chart and display the chart.
Summary
In this article, we explored how to create data visualization charts using PHP and JpGraph. We learned how to install PHP and JpGraph, and how to create chart types such as bar charts, line charts, and pie charts. We can also use other libraries of JpGraph to create more different chart types. Most importantly, we can customize the size, color, font, labels, etc. of the chart according to our needs. Data visualization is an extremely important task that helps us understand our data more deeply, while also helping us work more efficiently.
The above is the detailed content of Create data visualization charts using PHP and JpGraph. For more information, please follow other related articles on the PHP Chinese website!