A Step-by-Step Guide to Building a Donation Manager from Scratch in PHP – Part II

dm reports

In this part of the tutorial, we will continue from where we left off in Build a Donation Manager Part I. You will learn how to use phpChart, an easy-to-use charting and graphing component, that will seamlessly integrates with phpGrid to create professional-looking, interactive reports for your Donation Manager application.

Before we start, we need to install phpChart. To fully benefit from this tutorial, I recommend you obtain the full version of phpChart since the free version – phpChart Lite – supports only the line chart.

Setup phpChart

It is very important that phpGrid and phpChart be kept in its separate folders. Below is the recommended folder hierarchy.

1
2
3
4
5
6
7
www
+-- Donation_Manager
|   |-- phpGrid
|   |   +-- conf.php
|   |-- phpChart
|   |   +-- conf.php
|   +-- ...

That’s it.

Report Design

Our report consists of a bar chart and a pie chart followed by a campaign summary datagrid. The datagrid provides the data used to plot the two charts.

reports mockup

phpGrid phpChart Integration

First of all, include both conf.php files (phpGrid and phpChart) at the beginning of the code.

1
2
require_once("phpGrid/conf.php");
require_once("phpChart/conf.php");

Bar Chart

The first chart we will create is the bar chart. Below is the complete code to create an EMPTY bar chart. We first must create an empty chart is because the campaign data used to plot the chart is not available at the time when the chart is initialized.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$pc = new C_PhpChartX(array(array(null)),'BarChart');
$pc->add_plugins(array('canvasTextRenderer','canvasAxisTickRenderer', 'pointLabels'),true);
$pc->set_animate(true);
$pc->set_series_default(array(
        'renderer'=>'plugin::BarRenderer',
        'rendererOptions' => array('barWidth'=>60),
        'pointLabels'=> array('show'=>true)
        ));
$pc->set_axes(array(
     'xaxis'=>array(
        'label'=>'Campaign',
        'renderer'=>'plugin::CategoryAxisRenderer',
        'rendererOptions'=>array('tickRenderer'=>'plugin::CanvasAxisTickRenderer'),
        'ticks'=>array(null),
        'tickOptions'=>array(
                'angle'=>-35,
                'fontStretch'=>1)),
    'yaxis'=>array('label'=>'Total Donations')
));
$pc->draw(600,400);

Let’s break this baby apart. For the sake of simplicity, we will only cover high level implementation and skip over details where the code is actually self-explanatory.

The first line is the constructor. We pass an empty array, array(null), as the series data because we don’t wish to have any data displayed in the bar chart when it is first rendered. We also give our graph an unique name, BarGraph.

1
$pc = new C_PhpChartX(array(array(null)),'BarGraph');

Next, we use the add_plugins() function to include some additional new elements to our bar chart. For this tutorial, we will include the canvasTextRenderer, canvasAxisTickRenderer, and pointLabels plugins. A list of supported plugins can be found here.

1
$pc->add_plugins(array('canvasTextRenderer','canvasAxisTickRenderer', 'pointLabels'),true);

In the series default function, be sure to set the renderer type to BarRenderer. We can also set the rendererOptions and pointLabels properties here.

1
2
3
4
5
$pc->set_series_default(array(
            'renderer'=>'plugin::BarRenderer',
            'rendererOptions' => array('barWidth'=>60),
            'pointLabels'=> array('show'=>true)
            ));

A bar chart has two sets of data – one set for the x axis and another for the y. In this example, the x axis shows the total amount donated to a given campaign and the y axis displays the name of the campaign. We can set the properties for both x and y in one statement by using the set_axes() function. Notice how label, renderer, and ticks properties are used.

Complete documentation for phpChart methods and properties documentation can be found online.

1
2
3
4
5
6
7
8
9
10
11
$pc->set_axes(array(
     'xaxis'=>array(
        'label'=>'Campaign',
        'renderer'=>'plugin::CategoryAxisRenderer',
        'rendererOptions'=>array('tickRenderer'=>'plugin::CanvasAxisTickRenderer'),
        'ticks'=>array(null),
        'tickOptions'=>array(
                'angle'=>-35,
                'fontStretch'=>1)),
    'yaxis'=>array('label'=>'Total Donations')
));

Finally, the last line reserves space for a chart that is 600px high and 400px wide.

1
$pc->draw(600,400);

Pie Chart

Now let’s move on to the second chart in our report. Below is the entire code to create an EMPTY pie chart.

1
2
3
4
5
6
7
8
9
$pc2 = new C_PhpChartX(array(array(null)),'PieChart');
$pc2->set_series_default(array( 'shadow'=> false,
    'renderer'=> 'plugin::PieRenderer',
    'rendererOptions'=> array(
      'sliceMargin'=> 3,
      'showDataLabels'=> true )
  ));
$pc2->set_legend(array('show'=>true,'location'=> 'w'));
$pc2->draw(600,400);

Let’s do a quick review of the above script.

The first line is the phpChart constructor. Again, the first parameter is an empty array, array(null), as the series data will be loaded later from the Campaign Summary datagrid.

1
$pc2 = new C_PhpChartX(array(array(null)),'PieChart');

We then set the renderer to PieRenderer and set the its properties using the series default function.

1
2
3
4
5
6
$pc2->set_series_default(array( 'shadow'=> false,
    'renderer'=> 'plugin::PieRenderer',
    'rendererOptions'=> array(
      'sliceMargin'=> 3,
      'showDataLabels'=> true )
  ));

We also want to display the legend to the west, or left, of the chart.

1
$pc2->set_legend(array('show'=>true,'location'=> 'w'));

Lastly, we draws the pie chart.

1
$pc2->draw(600,400);

You should now have two blank charts on the page.

blank charts

Campaign Summary Datagrid

The Campaign datagrid used in this report is the same as what was used to create the Campaign page in Part 1. We just need to add one more thing – an event handler.

In phpGrid, we can add an event handler with the add_event() function. add_event() binds an event handler, which is essentially a JavaScript function, for a specific phpGrid event. A list of possible events can be found here.

Since we must wait for the datagrid to finish loading before sending its data needed to plot the charts, we use the event jqGridLoadComplete.

phpGrid 101 – jqGridLoadComplete Event

jqGridLoadComplete is the last event that occurs once the whole datagrid body has finished loading. Note that the grid body will be reloaded if the user changes the sorting of a column or sets a filter.

Send Data Using Javascript

The following is the Javascript event handler for jqGridLoadComplete.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function(status, rowid)
{
    var barData1 = [];
    var barData2 = [];

    d1 = $('#campaigns').jqGrid('getCol', 'TotalDonation', false);
    d2 = $('#campaigns').jqGrid('getCol', 'CampaignName', false);

    npoints = d1.length;
    for(var i=0; i < npoints; i++){
        barData1[i] = [i+1, parseInt(d1[i])];
        barData2[i] = [i+1, d2[i]];
    }
    _BarChart.series[0].data = barData1;
    _BarChart.axes.xaxis.ticks = barData2;
    _BarChart.replot({resetAxes:true});

    var pieData = [];
    for(var j=0; j < barData1.length; j++)
    {
        pieData.push([barData2[j][1],barData1[j][1]]);
    }
    // console.log(pieData);
    _PieChart.series[0].data = pieData;
    _PieChart.replot({resetAxes:true});
   }

The Javascript function is the bread and butter that makes our report come alive. It requires a certain level of knowledge of basic Javascript.

Right off the bat, we declare two empty arrays to store x and y axis data points. Since we are now using JavaScript, the syntax will be different from that used in PHP.

1
2
var barData1 = [];
var barData2 = [];

The following two lines return data from the datagrid column TotalDonation and CampaignName:

1
2
d1 = $('#campaigns').jqGrid('getCol', 'TotalDonation', false);
d2 = $('#campaigns').jqGrid('getCol', 'CampaignName', false);

The for loop iterates each column and then stores the results in the x and y axis arrays.

1
2
3
4
5
npoints = d1.length;
for(var i=0; i < npoints; i++){
    barData1[i] = [i+1, parseInt(d1[i])];
    barData2[i] = [i+1, d2[i]];
}

Next, we assign the barData from the datagrid to the bar chart object. Lastly always call jqPlot replot function to load the chart.

1
2
3
_BarChart.series[0].data = barData1;
_BarChart.axes.xaxis.ticks = barData2;
_BarChart.replot({resetAxes:true});
phpChart 101 – JavaScript Chart Object Name

The JavaScript chart object name is the same as the name used in the phpChart constructor, e.g. BarChart, prefixed with the underscore character “_”.

We have just completed our bar chart.

Next, we move on to the pie chart. The JavaScript code for this is similar to what we used for the bar chart except that a pie chart does not have x and y axes. Instead, it uses a two-dimensional array.

1
pieData.push([barData2[j][1],barData1[j][1]]);

bar chart & pie chart in donation manager

That’s all there is to it!

Conclusion

Hopefully, after finishing this tutorial, you have become a true campaign master yourself – one who is capable of developing a fully functional donation manager system from start to finish.

Run Demo Get this App *

* Included in Ultimate edition.