This is an older version of Design Intellection. Access the new one: http://designintellection.com/.

Design Intellection is a small web design company committed to making the web a better place.

Contact

November 11, 2009

Combining PHP with Processing.js

Creating living infographics by combining PHP
with Processing.js

For the last tutorial in this series we are going to look at an example of integrating PHP with Processing.js to create a dynamic infographic. We will keep it very basic, but we'll create something that could be used in the “real world.”

A SPECIAL NOTE TO RSS READERS:

Due to the complex nature of this tutorial (mainly the examples) you have to go to the post to see the rest.

Note: As mentioned in the introduction of this series (though included as an addendum), IE support is currently not included with these tutorials.

Post Time

For this example I'm going to make a small infographic that shows the most common times at which posts are published. So first I need to get my raw data, the publish time for each post I've made on this site. In Figure 5.1 – using Habari's templating system – I create an array that contains all of the publish times.

Note: There is most certainly a better way to construct all of the PHP code in the examples that follow. I am not a programmer – I just use PHP to do what I want to do (for lack of a better way to say it).

  1. public function add_template_vars()
  2. {
  3. $this->assign('di_archives', Posts::get(array('content_type' => 'entry', 'status' => Post::status('published'), 'limit'=>1000)));
  4. parent::add_template_vars();
  5. }
Figure 5.1

Setting the PHP variable from which to build the array; this code must go in theme.php. (Note: This is particular method is specific to Habari).

Download Source processing-f5.1.txt

Separating and Categorizing

For the sake of our infographic we need to know the number of posts that occur in each hour and the average number for each hour. In Figure 2 we take the collection of post times in Figure 1 and sort them quantitatively by hour. Then at the end we also calculate an average.

  1. $di_post_hour = array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  2. foreach ($di_archives as $di_archive)
  3. {
  4. $di_post_time = $di_archive->pubdate_timedata;
  5. $di_post_time_hour = substr($di_post_time, 0, 2);
  6. $di_post_time_min = substr($di_post_time, 2, 2);
  7. if ($di_post_time_hour == "00") $di_post_hour[0]++;
  8. if ($di_post_time_hour == "01") $di_post_hour[1]++;
  9. if ($di_post_time_hour == "02") $di_post_hour[2]++;
  10. if ($di_post_time_hour == "03") $di_post_hour[3]++;
  11. if ($di_post_time_hour == "04") $di_post_hour[4]++;
  12. if ($di_post_time_hour == "05") $di_post_hour[5]++;
  13. if ($di_post_time_hour == "06") $di_post_hour[6]++;
  14. if ($di_post_time_hour == "07") $di_post_hour[7]++;
  15. if ($di_post_time_hour == "08") $di_post_hour[8]++;
  16. if ($di_post_time_hour == "09") $di_post_hour[9]++;
  17. if ($di_post_time_hour == "10") $di_post_hour[10]++;
  18. if ($di_post_time_hour == "11") $di_post_hour[11]++;
  19. if ($di_post_time_hour == "12") $di_post_hour[12]++;
  20. if ($di_post_time_hour == "13") $di_post_hour[13]++;
  21. if ($di_post_time_hour == "14") $di_post_hour[14]++;
  22. if ($di_post_time_hour == "15") $di_post_hour[15]++;
  23. if ($di_post_time_hour == "16") $di_post_hour[16]++;
  24. if ($di_post_time_hour == "17") $di_post_hour[17]++;
  25. if ($di_post_time_hour == "18") $di_post_hour[18]++;
  26. if ($di_post_time_hour == "19") $di_post_hour[19]++;
  27. if ($di_post_time_hour == "20") $di_post_hour[20]++;
  28. if ($di_post_time_hour == "21") $di_post_hour[21]++;
  29. if ($di_post_time_hour == "22") $di_post_hour[22]++;
  30. if ($di_post_time_hour == "23") $di_post_hour[23]++;
  31. }
  32. $di_post_sumtotal = array_sum($di_post_hour);
  33. $di_post_average_hour = round($di_post_sumtotal / 24, 0);
  34. foreach ($di_post_hour as $i=>$value)
  35. {
  36. $di_post_hour[$i] = round($di_post_hour[$i] / $di_post_sumtotal, 2) * 100;
  37. }
  38. $di_post_hours = implode(",", $di_post_hour);
Figure 5.2

Building the PHP array (Note: This is particular method is specific to Habari).

Download Source processing-f5.2.txt

Visualize It

Now we have a working array that we can pass to Processing.js to create the visualization (Figure 5.3).

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Processing.js Examples</title>
  5. <script type="text/javascript" href="/js/processing.js"></script>
  6. <!--[if IE]>
  7. <script type="text/javascript" href="/path/to/excanvas.compiled.js">
  8. <![endif]-->
  9. <script type="text/javascript" src="/js/init.js"></script>
  10. </head>
  11. <body>
  12. <div class="example">
  13. <script type="application/processing">
  14. // Setup canvas
  15. void setup()
  16. {
  17. size(564,564);
  18. background(255);
  19. }
  20. // Draw elements
  21. void draw()
  22. {
  23. int diameter = 84;
  24. int pie_scale = 16;
  25. // Draw the gray background (average time) pie slices
  26. fill(#cdcdce);
  27. stroke(255);
  28. strokeWeight(1);
  29. int bg_pie_slice_length = <?php echo $di_post_average_hour; ?>;
  30. float last_angle = radians(0);
  31. for (int i=0; i<24; i++)
  32. {
  33. arc(282, 282, pie_scale*bg_pie_slice_length, pie_scale*bg_pie_slice_length, last_angle, last_angle+radians(14))
  34. last_angle += radians(15);
  35. }
  36. fill(#4dbceb);
  37. stroke(255);
  38. strokeWeight(1);
  39. int[] the_post_hour = {<?php echo $di_post_hours; ?>};
  40. float last_angle = radians(180);
  41. for (int i=0; i<24; i++)
  42. {
  43. arc(282, 282, diameter+(pie_scale*the_post_hour[i]), diameter+(pie_scale*the_post_hour[i]), last_angle, last_angle+radians(14))
  44. last_angle += radians(15);
  45. }
  46. fill(255);
  47. ellipse(282, 282, 84, 84);
  48. exit();
  49. }
  50. </script>
  51. <canvas width="564" height="564"></canvas>
  52. </div>
  53. </body>
  54. </html>
Figure 5.3

Inserting PHP output into Processing.js.

Download Source processing-f5.3.txt

To perhaps state the obvious, the beauty of mixing PHP with Processing.js is that this infographic will change everytime I publish a post. It's a “living” infographic that does not need continual maintenance.

The (Premature) End

This tutorial brings us to a close for the working examples on Processing.js. In the final installment I'll provide links to more resources for further learning.

Processing.js A series of concise tutorials.
Part I – Introduction
An introduction to Processing.js
Part II – Bar Charts
Start with a single bar chart and progress to aesthetically pleasing multiple bar charts.
Part III – Pie Charts
Easily create pie charts.
Part IV – Integrate jQuery
Learn how to easily implement Processing.js with native JavaScript.
Part V – Integrate PHP
Create dynamic info-graphics by combining PHP with Processing.js
Part VI – Conclusion
A summary of the series with links to more resources.
Appendix
An example page with all of the working examples and the appropriate source code.

Send a Message Have something to say? Use the form below to email your comment directly to me.
If warranted, I’ll do my best to respond in a timely matter.

Legend * = Required

(Hint: It’s David)

Content © David Yeiser, 2007–2009 | Published with Habari.