Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion

Change in passenger numbers from 2005 to 2010 - Bristol area stationsIf you want to draw a diagram or chart on your web page, using data held on your server, chances are that the co-ordinates of the data won't match the pixel numbers that you're using on the screen. A case in point - on this week's PHP Techniques Workshop was a file of data for the years 2005 to 2010 (to be plotted along the X axis of a 750 pixel wide window ... allowing 100 pixels on the left for border and labels, and 50 pixels on the right for border. Then on the Y axis, 400 pixels long from pixel 0 at the top to pixel 399 at the base, we wanted to plot a number in the range 1.0. to 3.0.

In computer graphics for diagrams such as this, you can convert a world co-ordinate to a screen co-ordiante using a multiplication and an addition; the clever bit is working out what the factors are for the multiplication and addition. During the course, I wrote a "Bull at a gate" piece of code and then, as I wrote it, moved constants out to single assignments, and created a "pixelator" class to performa axis transforms. The contsructor is called with two mappings alon an axis - for example
  $xaxis = new pixelator(2005,100,2010,$wid-50);
2005 (world) converts to 100 (pixel) and 2010 (world) converts to 50 less that the graphic width (pixel). I can then call a method to convert world to pixel co-ordinates as I draw my diagaram:
  $xp = $xaxis->w2p($year);
and leave the messier geekier calculation safely encapsulated within the class. The full graphic script is [here].

One of the other issues was how to display numbers in very different y ranges on the same set of axes, and as we were particularly interested in changes from 2005 to 2010, we normalised the data so that it always started at 100% and then went up or down. The graph shows the strong growth, at almost every station in the Bristol area, of train use over the six years, with some stations shooting way off the top of the graph. Actually it's not a huge surprise that the biggest proportional growth was at Severn Beach, where the service is now much better than it was in 2005.

But although a picture paints a lot of words very quickly, it's useful to have access to the real data. Rather than include that within the image, we elected to include it in a table in a web page that enclosed the table, and then call up the image script from within the HTML generating script. That web page script is [here] (and to complete the picture, the data file is [here].

The comments I have made above are about Bristol. Would you like to see what the diagram and figures are for your part of the country? Easy ... I've included a form at the end of the page into which you can type the letter or letter(s) that start your postcode, and you'll get a diagram for your own postcode area. You can do that from the running program which is [here] in our demo directory. It starts with "SN" for Swindon ...

(written 2011-03-24)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H108 - Objects in PHP
  [4073] Learning about Object Orientation in PHP - a new set of examples - (2013-04-28)
  [4057] stdClass in PHP - using an object rather than an associative array - (2013-04-02)
  [3953] Objects in PHP - Revision - (2012-12-16)
  [3843] Caching Design Patterns - (2012-08-20)
  [3841] Copying, duplicating, cloning an object in PHP - (2012-08-18)
  [3840] Autoload in PHP - (2012-08-17)
  [3609] How do classes relate to each other? Associated Classes - (2012-02-12)
  [3608] Design Patterns - what are they? Why use them? - (2012-02-12)
  [3607] Designing your application - using UML techniques - (2012-02-11)
  [3210] Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON - (2011-03-22)
  [3142] Private and Public - and things between - (2011-01-22)
  [2922] Getting the OO design write - with PHP a example - (2010-08-14)
  [2921] Does copying a variable duplicate the contents? - (2010-08-14)
  [2774] PHP - Object Oriented Design in use - (2010-05-21)
  [2741] What is a factory? - (2010-04-26)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2680] Static class members in PHP - a documented example - (2010-03-16)
  [2641] Object Oriented Programming in PHP - (2010-02-19)
  [2632] Shipping a test harness with your class in PHP - (2010-02-12)
  [2435] Serialization - storing and reloading objects - (2009-10-04)
  [2434] Abstract classes, Interfaces, PHP and Java - (2009-10-03)
  [2380] Object Oriented programming - a practical design example - (2009-08-27)
  [2172] PHP4 v PHP5 - Object Model Difference - (2009-05-11)
  [2171] Cleaning up redundant objects - (2009-05-11)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2160] PHP - getclass v instanceof - (2009-05-07)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1820] Sorting objects in PHP - (2008-10-04)
  [1819] Calling base class constructors - (2008-10-03)
  [1682] Accounts in PHP - an OO demo - (2008-06-19)
  [1535] OO PHP demonstration - comparing objects and more - (2008-02-08)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [1153] Object Oriented Model - a summary of changes from PHP4 to PHP5 - (2007-04-18)
  [1027] Cue the music, I'm happy. - (2007-01-09)
  [836] Build on what you already have with OO - (2006-08-17)
  [720] Planning a hotel refurb - an example of a Gant chart in PHP - (2006-05-14)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [485] North, Norther and Northest - PHP 5 Objects - (2005-11-04)
  [421] Don't repeat code - use loops or functions - (2005-08-21)
  [343] Should I use structured or object oriented? - (2005-06-10)
  [205] PHP5 lets you say no - (2005-02-07)
  [124] PHP v Java - (2004-11-20)
  [67] Object Oriented Programming in PHP - (2004-09-29)

H309 - PHP - Maps, Graphics and Geographics
  [4178] Where are you? How to write a geosensitive application - (2013-09-18)
  [3817] Fpdf - generating .pdf documents easily from your PHP program - (2012-07-24)
  [3734] QR codes with marketing logos embedded - (2012-05-16)
  [3584] QR codes - graphics images that provide quick phone links - (2012-01-18)
  [3536] UK Mapping Data - and more to come - under government Open Data measures - (2011-12-03)
  [3447] Needle in a haystack - finding the web server overload - (2011-09-18)
  [3197] Finding and diverting image requests from rogue domains - (2011-03-08)
  [3133] An image from a website that occasionally comes out as hyroglyphics - (2011-01-14)
  [3027] Server logs - drawing a graph of gathered data - (2010-11-03)
  [2939] Protecting your images from use out of context - (2010-08-29)
  [2729] Uploading a document or image to its own URL via a browser - (2010-04-18)
  [2675] Redirecting to your main domain for correct security keys - (2010-03-13)
  [2583] Reducing image size on digital photos - PHP - (2010-01-17)
  [2390] Dynamic / changing images on your web page - (2009-09-01)
  [2361] Geocoding - converting address to latitude / Longitude with PHP via Google - (2009-08-14)
  [2343] World Flags in your PHP pages - (2009-08-10)
  [1956] Images for Christmas - (2008-12-21)
  [1923] Making it all worthwhile - (2008-12-04)
  [1756] Ever had One of THOSE mornings? - (2008-08-16)
  [1752] Dynamic maps / geographics in PHP - (2008-08-13)
  [1734] All around the world? - (2008-08-03)
  [1724] addslashes v mysql_real_escape_string in PHP - (2008-07-27)
  [1628] Gant charts - drawing them with a PHP script - (2008-05-03)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1390] Converting from postal address to latitude / longitude - (2007-10-13)
  [1389] Controlling and labelling Google maps via PHP - (2007-10-13)
  [1194] Drawing hands on a clock face - PHP - (2007-05-19)
  [1104] Drawing dynamic graphs in PHP - (2007-03-09)
  [937] Display an image from a MySQL database in a web page via PHP - (2006-11-22)
  [665] PHP Image viewing application - (2006-04-01)
  [563] Merging pictures using PHP and GD - (2006-01-13)
  [320] Ordnance Survey - using a 'Get a map' - (2005-05-22)

Q913 - Object Orientation and General technical topics - Computer Graphics Principles
  [3262] Some SVG Elements, pixel and percent positioning - (2011-04-24)
  [3261] Scalable Vector Graphics - easy, low bandwidth, high resolution, dynamic. - (2011-04-23)
  [3148] OpenGL / C / C++ - an example to get you started - (2011-01-26)
  [3049] Computer Graphics is fun - even if Java Applets are Old Hat - (2010-11-13)
  [2992] Matplotlib - graphing in Python - teaching examples - (2010-10-10)
  [2758] But what will you DO with all those pictures? - (2010-05-09)
  [2633] Why do I teach niche skills rather than mainstream? - (2010-02-13)
  [2245] Client side (Applet) and Server side (Servlet) Graphics in Java - (2009-06-17)


Back to
Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON
Previous and next
or
Horse's mouth home
Forward to
Well House Manor - a home from home for the business and leisure guest in Melksham
Some other Articles
Solution looking for problem
Melksham Campus - any last minute inputs from Melksham businesses?
Should I use Open Source or Commercial software?
Well House Manor - a home from home for the business and leisure guest in Melksham
Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion
Seeing Electricity Pylons near Melksham
Links for social media, microblogs and business networking
Lambing at Lackham
Wiltshire College / Lackham College - the animals
4318 posts, page by page
Link to page ... 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, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3211_Com ... rsion.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat