With the advent of the data era, data visualization has become the focus of many companies and developers. For many developers, the implementation of data visualization is not an easy task. However, with the integration of PHP and Highcharts, visualizing charts will become a piece of cake.
PHP is a popular programming language that is widely used in the field of web development. Highcharts is a chart library written in JavaScript that can easily create various types of charts, such as column charts, line charts, pie charts, etc.
Next, we will introduce how to integrate PHP and Highcharts and realize chart visualization.
Step 1: Download Highcharts
First, you need to download the Highcharts library. The official website provides two versions, one is the open source version and the other is the commercial authorized version. If you need a commercial licensed version, please purchase the corresponding license.
After the download is complete, unzip Highcharts into a folder on your web server and make sure your folder has the appropriate permissions.
Step 2: Prepare your data
Before you start creating a chart, you need to prepare your data. Data can come from any data source, such as databases, CSV files, etc.
For the examples in this article, we will use a PHP array as the data source. Here is the array we will be using:
$data = array(
array('name' => 'Firefox', 'y' => 45.0), array('name' => 'IE', 'y' => 26.8), array('name' => 'Chrome', 'y' => 12.8), array('name' => 'Safari', 'y' => 8.5), array('name' => 'Opera', 'y' => 6.2), array('name' => 'Others', 'y' => 0.7)
);
Step 3: Create the HTML page
Now, we will Create an HTML page and integrate Highcharts library files and the JavaScript code required to create charts. Here is the HTML code for the example:
<title>Highcharts Integration with PHP</title> <script src="highcharts/highcharts.js"></script> <script src="highcharts/modules/exporting.js"></script>
< body>
<div id="container" style="width: 100%; height: 400px;"></div> <script type="text/javascript"> var data = <?php echo json_encode($data); ?>; Highcharts.chart('container', { chart: { plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false, type: 'pie' }, title: { text: 'Browser market share, January, 2018 to May, 2018' }, tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: true, format: '<b>{point.name}</b>: {point.percentage:.1f} %', style: { color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' } } } }, series: [{ name: 'Brands', colorByPoint: true, data: data }] }); </script>