You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,8 +6,8 @@ heading: Create Interactive Charts using PHP and MySQL Database
6
6
7
7
There are two ways of obtaining data for the chart:
8
8
9
-
1.__Statically__ - by defining the data during the creation of the chart
10
-
2.__Dynamically__ - by retrieving data from another source
9
+
1.**Statically** - by defining the data during the creation of the chart
10
+
2.**Dynamically** - by retrieving data from another source
11
11
12
12
`Static binding` of data is not much useful and it does not accommodate updates to the underlying data whereas in case of `dynamic binding`, the data shown by the chart is always fresh.
13
13
@@ -17,10 +17,10 @@ But how do we go about dynamic binding of data? Before we explain that, let us u
17
17
18
18
In this article we are going to show you how to write server side code in PHP to retrieve the data from MySQL database and bind the data obtained to the chart. We are going to divide this article in two four parts:
19
19
20
-
* Installing prerequisite software
21
-
* Creating database and initializing with seed data in MySQL
22
-
* Implementing PHP based server program
23
-
* Integrating chart with the data from server
20
+
- Installing prerequisite software
21
+
- Creating database and initializing with seed data in MySQL
22
+
- Implementing PHP based server program
23
+
- Integrating chart with the data from server
24
24
25
25
To demonstrate this let us consider data of [top 10 wicket takers](http://stats.espncricinfo.com/ci/engine/records/bowling/most_wickets_career.html?class=2;id=2015;type=year) in One Day International (ODI) cricket in the year 2015.
26
26
@@ -34,8 +34,7 @@ To get the code in this article working we would need to install the following:
34
34
- Apache web server
35
35
- PHP
36
36
37
-
This stack is popularly called as *AMP(Apache MySQL PHP) stack and when you include the platform on which this stack is running then it becomes either WAMP (Windows Apache MySql PHP) or LAMP (Linux Apache MySQL PHP). So there are lots of vendors who bundle this stack and make it available for download. For this article we are going to make use of the WAMP bundle provided by Bitnami and can be downloaded from [here](https://bitnami.com/stack/wamp).
38
-
37
+
This stack is popularly called as \*AMP(Apache MySQL PHP) stack and when you include the platform on which this stack is running then it becomes either WAMP (Windows Apache MySql PHP) or LAMP (Linux Apache MySQL PHP). So there are lots of vendors who bundle this stack and make it available for download. For this article we are going to make use of the WAMP bundle provided by Bitnami and can be downloaded from [here](https://bitnami.com/stack/wamp).
39
38
40
39
At the end of the installation of the WAMP bundle you will be prompted to launch the app as shown below:
41
40
@@ -79,7 +78,6 @@ Now let us seed this table with some initial data taken from [here](http://stats
79
78
INSERT INTO top_odi_wicket_takers(player, wickets) VALUES('UT Yadav', 22);
80
79
```
81
80
82
-
83
81
We can verify whether the data is inserted by running the SELECT SQL command as shown below:
84
82
85
83
```sql
@@ -109,14 +107,16 @@ PHP is one of the most widely used server side programming languages. (other suc
109
107
110
108
Let us first list out the steps involved in the program before we proceed with its implementation.
111
109
112
-
* Establish the connection with MySQL Database using the hostname, username, password and database name.
113
-
* Execute the query to get the list of players from DB and store them in a variable.
114
-
* Iterate over the result set to build an array of maps where each map consists of two keys namely: **label** and **value**. The label stores the name of the player and value contains the number of wickets
115
-
* Encode the array of maps into JSON and then output the encoded JSON. (If JSON is something new for you, then we would recommend you to read about it [here](https://en.wikipedia.org/wiki/JSON).)
110
+
- Establish the connection with MySQL Database using the hostname, username, password and database name.
111
+
- Execute the query to get the list of players from DB and store them in a variable.
112
+
- Iterate over the result set to build an array of maps where each map consists of two keys namely: **label** and **value**. The label stores the name of the player and value contains the number of wickets
113
+
- Encode the array of maps into JSON and then output the encoded JSON. (If JSON is something new for you, then we would recommend you to read about it [here](https://en.wikipedia.org/wiki/JSON).)
116
114
117
115
The implementation of above steps is as follows:
118
116
119
-
* The default username to connect to MySQL instance running on your machine is **root** and **password** is the one you specified during the installation of your WAMP bundle. The interaction with the database from PHP is achieved using the mysqli extension. Below is the implementation to establish the connection:
117
+
- The default username to connect to MySQL instance running on your machine is **root** and **password** is the one you specified during the installation of your WAMP bundle. The interaction with the database from PHP is achieved using the mysqli extension. Below is the default implementation to establish the connection:
118
+
119
+
> You can set custom `username` and `password` to connect to the database.
120
120
121
121
```php
122
122
<?php
@@ -136,10 +136,10 @@ The implementation of above steps is as follows:
* Once we have the connection established, we use connection object $conn to execute any SQL commands. We can now execute our SQL query as shown below:
142
+
- Once we have the connection established, we use connection object \$conn to execute any SQL commands. We can now execute our SQL query as shown below:
143
143
144
144
```php
145
145
<?php
@@ -150,7 +150,7 @@ The implementation of above steps is as follows:
150
150
?>
151
151
```
152
152
153
-
* We have to now process the $result obtained in the above step in the form which is understood by FusionCharts. FusionCharts uses the chart data in the form of a list of label-value pairs. If there are rows/data returned by the query, we convert that data into an associative array. An associative array is just like any other array but in place of indices it uses keys to store the value of the array element. Below is the implementation to process the data in $result object:
153
+
- We have to now process the $result obtained in the above step in the form which is understood by FusionCharts. FusionCharts uses the chart data in the form of a list of label-value pairs. If there are rows/data returned by the query, we convert that data into an associative array. An associative array is just like any other array but in place of indices it uses keys to store the value of the array element. Below is the implementation to process the data in $result object:
154
154
155
155
```php
156
156
<?php
@@ -170,17 +170,17 @@ The implementation of above steps is as follows:
170
170
?>
171
171
```
172
172
173
-
* Let us now encode the data processed in above step into JSON data as shown below:
173
+
- Let us now encode the data processed in above step into JSON data as shown below:
174
174
175
175
```php
176
176
<?php
177
177
//Closing the connection to DB
178
178
$conn->close();
179
179
//set the response content type as JSON
180
180
header('Content-type: application/json');
181
-
//output the return value of json encode using the echo function.
181
+
//output the return value of json encode using the echo function.
182
182
echo json_encode($jsonArray);
183
-
?>
183
+
?>
184
184
```
185
185
186
186
Let us integrate the code we have from all the steps above into a file named: chart_data.php and place this file at **BITNAMI_INSTALL_DIR\apache2\htdocs**, where BITNAMI_INSTALL_DIR is the path where your Bitnami WAMP package is installed.
@@ -228,51 +228,50 @@ The contents of the chart_data.php is as shown below:
228
228
$conn->close();
229
229
//set the response content type as JSON
230
230
header('Content-type: application/json');
231
-
//output the return value of json encode using the echo function.
231
+
//output the return value of json encode using the echo function.
232
232
echo json_encode($jsonArray);
233
-
?>
233
+
?>
234
234
```
235
235
236
-
237
236
```javascript
238
-
var apiChart =newFusionCharts({
239
-
type:'column2d',
240
-
renderAt:'api-chart-container',
241
-
width:'550',
242
-
height:'350',
243
-
dataFormat:'json',
244
-
dataSource: {
245
-
"chart": chartProperties,
246
-
"data": chartData
247
-
}
248
-
});
249
-
$(function(){
250
-
$("#background-btn").click(function(){
251
-
modifyBackground();
252
-
});
253
-
254
-
$("#canvas-btn").click(function(){
255
-
modifyCanvas();
256
-
});
257
-
258
-
$("#dataplot-btn").click(function(){
259
-
modifyDataplot();
260
-
});
261
-
262
-
apiChart.render();
263
-
});
264
-
265
-
functionmodifyBackground(){
266
-
//to be implemented
267
-
}
268
-
269
-
functionmodifyCanvas(){
270
-
//to be implemented
271
-
}
272
-
273
-
functionmodifyDataplot(){
274
-
//to be implemented
275
-
}
237
+
var apiChart =newFusionCharts({
238
+
type:"column2d",
239
+
renderAt:"api-chart-container",
240
+
width:"550",
241
+
height:"350",
242
+
dataFormat:"json",
243
+
dataSource: {
244
+
chart: chartProperties,
245
+
data: chartData
246
+
}
247
+
});
248
+
$(function(){
249
+
$("#background-btn").click(function(){
250
+
modifyBackground();
251
+
});
252
+
253
+
$("#canvas-btn").click(function(){
254
+
modifyCanvas();
255
+
});
256
+
257
+
$("#dataplot-btn").click(function(){
258
+
modifyDataplot();
259
+
});
260
+
261
+
apiChart.render();
262
+
});
263
+
264
+
functionmodifyBackground(){
265
+
//to be implemented
266
+
}
267
+
268
+
functionmodifyCanvas(){
269
+
//to be implemented
270
+
}
271
+
272
+
functionmodifyDataplot(){
273
+
//to be implemented
274
+
}
276
275
```
277
276
278
277
To run chart_data.php, open http://localhost/chart_data.php in your browser. You would see the below JSON output:
@@ -304,37 +303,37 @@ First let us download the required JavaScript libraries:
304
303
Now let us create the HTML page chart_sample.html required to render the chart:
305
304
306
305
```html
307
-
<!DOCTYPE html>
308
-
<html>
309
-
<head>
310
-
<title>FusionCharts Column 2D Sample</title>
311
-
</head>
312
-
<body>
313
-
<divid="chart-container">FusionCharts will render here</div>
The code which interacts with the PHP server implemented above and renders the chart using our Javascript library would be implemented in app.js. The javascript implementation is as shown below:
322
+
The code which interacts with the PHP server implemented above. This code renders the chart using a Javascript library implemented in the `app.js` file. Place the `app.js` file in the `js` folder. The javascript implementation is shown below:
324
323
325
-
1. Fetch the required data from the server via an Ajax call using jQuery:
324
+
1. Fetch the required data from the server via an Ajax call using jQuery:
326
325
327
326
```javascript
328
-
$.ajax({
329
-
url:'http://localhost/chart_data.php',
330
-
type:'GET',
331
-
success:function(data) {
332
-
chartData = data;
333
-
}
334
-
});
327
+
$.ajax({
328
+
url:"http://localhost/chart_data.php",
329
+
type:"GET",
330
+
success:function(data) {
331
+
chartData = data;
332
+
}
333
+
});
335
334
```
336
335
337
-
2. After retrieving the data from the server, initialise the FusionCharts object with the chart properties and chart data as shown below:
336
+
2. After retrieving the data from the server, initialise the FusionCharts object with the chart properties and chart data as shown below:
338
337
339
338
```javascript
340
339
var chartProperties = {
@@ -357,40 +356,40 @@ The code which interacts with the PHP server implemented above and renders the c
357
356
});
358
357
apiChart.render();
359
358
}
360
-
```
359
+
```
361
360
362
361
### Final app.js will look as below:
363
362
364
363
```javascript
365
-
$(function(){
366
-
$.ajax({
367
-
url:'http://localhost/chart_data.php',
368
-
type:'GET',
369
-
success:function(data) {
370
-
chartData = data;
371
-
var chartProperties = {
372
-
"caption":"Top 10 wicket takes ODI Cricket in 2015",
373
-
"xAxisName":"Player",
374
-
"yAxisName":"Wickets Taken",
375
-
"rotatevalues":"1",
376
-
"theme":"zune"
377
-
};
378
-
apiChart =newFusionCharts({
379
-
type:'column2d',
380
-
renderAt:'chart-container',
381
-
width:'550',
382
-
height:'350',
383
-
dataFormat:'json',
384
-
dataSource: {
385
-
"chart": chartProperties,
386
-
"data": chartData
387
-
}
388
-
});
389
-
apiChart.render();
390
-
}
391
-
});
392
-
});
393
-
```
364
+
$(function(){
365
+
$.ajax({
366
+
url:"http://localhost/chart_data.php",
367
+
type:"GET",
368
+
success:function(data) {
369
+
chartData = data;
370
+
var chartProperties = {
371
+
caption:"Top 10 wicket takes ODI Cricket in 2015",
372
+
xAxisName:"Player",
373
+
yAxisName:"Wickets Taken",
374
+
rotatevalues:"1",
375
+
theme:"zune"
376
+
};
377
+
apiChart =newFusionCharts({
378
+
type:"column2d",
379
+
renderAt:"chart-container",
380
+
width:"550",
381
+
height:"350",
382
+
dataFormat:"json",
383
+
dataSource: {
384
+
chart: chartProperties,
385
+
data: chartData
386
+
}
387
+
});
388
+
apiChart.render();
389
+
}
390
+
});
391
+
});
392
+
```
394
393
395
394
Now load the HTML in the browser using the URL: `http://localhost/chart_sample.html` and you will get the below chart:
0 commit comments