.. _dynamic_charting: ***************************************************************************** Dynamic Charting ***************************************************************************** :Author: Thomas Bonfort :Contact: thomas.bonfort at gmail.com :Revision: $Revision$ :Date: $Date$ :Last Updated: 2009/01/17 .. contents:: Table of Contents :depth: 2 :backlinks: top Starting with version 5.0, MapServer included the ability to automatically draw pie or bar graphs whose values are taken and adjusted from attributes of a datasource. This document assumes that you are already familiar with MapServer application development and especially setting up :ref:`mapfile` s. You can also check out the :ref:`Vector Data Access Guide `, which has lots of examples of how to access specific data sources. Setup ------------------------------------------------------------------------------- Supported Renderers ............................................................................... Dynamic charts are supported solely with the GD and :ref:`AGG ` renderers. Attempting to add a chart layer with any other renderer (e.g. PDF or SWF) will result in an error. Rendering quality with the GD renderer is less than optimal, especially with small graphs, due to the lack of subpixel rendering functions. Output from AGG and GD Renderers ............................................................................... MapServer AGG Rendering .. image:: ../images/renderer-agg.png MapServer GD Rendering .. image:: ../images/gd-renderer.png Adding a Chart Layer to a Mapfile ------------------------------------------------------------------------------- Layer Type ............................................................................... A new type of layer has been added to the mapfile syntax. To specify a chart layer, use .. code-block:: mapfile LAYER ... TYPE CHART ... END No other specific keywords have been added in order to keep the number of different keywords to a minimum in the mapfile syntax, therefore all the chart specific configuration is determined by PROCESSING directives. Specifying the Size of each Chart ............................................................................... The size of each chart is specified by the CHART_SIZE directive. If two values are given for this parameter, this will specify the width and height of each chart (this only applies for bar graphs). By default, the charts are 20x20 pixels. .. code-block:: mapfile LAYER TYPE CHART PROCESSING "CHART_SIZE=21" # specify size of the chart for pie or bar graphs #PROCESSING "CHART_SIZE=20 10" # specify width and height for bar graphs ... END From version 5.2 and onwards, the diameter of a pie chart can be bound to an attribute,using the CHART_SIZE_RANGE PROCESSING attribute: .. code-block:: mapfile PROCESSING "CHART_SIZE_RANGE = itemname minsize maxsize minval maxval" where: - itemname is the name of the attribute that drives the chart size (e.g. total_sales) - minsize and maxsize are the minimum and maximum chart size values in pixels (e.g. "10 100") - minval and maxval are the minimum values of the attribute that correspond to chart sizes of minsize and maxsize (e.g. 10000 1000000). If the attribute value is smaller than 'minval' then the chart size will be minsize pixels, and if the attribute value is larger than maxval, the chart size will be maxsize pixels. Specifying the Values to be Plotted ............................................................................... Each value to be plotted (i.e. a slice in a pie chart, or a bar in a par graph) is specified in a CLASS of the chart layer. The value to be plotted is taken from the SIZE keyword from the first STYLE block of the class. This is semantically a bit awkward, but keeps the number of different keywords to a minimum in the mapfile syntax. The value given to the SIZE keyword could of course be given a static value, but dynamic charting really only makes sense with attribute binding. .. code-block:: mapfile LAYER ... CLASS # include a NAME keyword if you want this class to be included # in the legend NAME "value 1" STYLE # specify which value from the data source will be used as the # value for the graph SIZE [attribute] ... END END CLASS ... END ... END At least 2 CLASS blocks must be specified before charting can occur (but you already knew this if you want your charts to convey at least *some* information ;) ). Specifying Style ............................................................................... The styling of each value in the charts is specified by the usual MapServer syntax. Only one style per class is supported, any other STYLE block will be silently ignored. Only a subset of the styling keywords are supported: .. code-block:: mapfile STYLE SIZE [attribute] # specify the fill color COLOR r g b # if present will draw an outline around the corresponding bar or slice OUTLINECOLOR r g b #specify the width of the outline if OUTLINECOLOR is present (defaults to 1) WIDTH w # only for pie charts. 'a' is the number of pixels the corresponding # slice will be offset relative to the center of the pie. This is useful # for emphasizing a specific value in each chart. 'b' is required by the # mapfile parser but is ignored. OFFSET a b END Pie Charts ------------------------------------------------------------------------------- This is the default type of chart that is rendered. This can also be specifically set with a PROCESSING keyword in the layer attributes: :: PROCESSING "CHART_TYPE=PIE" For each shape in the layer's datasource, the STYLE SIZE is used to set the relative size (value) of each pie slice, with the angles of the slices that are automatically computed so as to form a full pie. For example: .. code-block:: mapfile :linenos: LAYER NAME "Ages" TYPE CHART CONNECTIONTYPE postgis CONNECTION "blabla" DATA "the_geom from demo" PROCESSING "CHART_TYPE=pie" PROCESSING "CHART_SIZE=30" STATUS ON CLASS NAME "Population Age 0-19" STYLE SIZE [v1006] COLOR 255 244 237 END END CLASS NAME "Population Age 20-39" STYLE SIZE [v1007] COLOR 255 217 191 END END CLASS NAME "Population Age 40-59" STYLE SIZE [v1008] COLOR 255 186 140 END END END In the example above, if for a given shape we have v1006=1000, v1007=600 and v1008=400 then the actual pie slices for each class will be respectively 50%, 30% and 20% of the total pie size. Bar Graphs ------------------------------------------------------------------------------- Bar graph drawing is set with a PROCESSING keyword in the layer attributes: .. code-block:: mapfile PROCESSING "CHART_TYPE=BAR" For each shape in the layer's datasource, the STYLE SIZE is used to set the relative size (value) of each bar in the graph. By default, the vertical axis of each bar graph is scaled for the values of the corresponding shape, and will always include the origin (=0). For example * a shape whose STYLE SIZEs contains values {5,8,10,3} will be plotted on a graph whose vertical axis spans 0 to 10. * a shape whose STYLE SIZEs contains values {-5,-8,-10,-3} will be plotted on a graph whose vertical axis spans -10 to 0. * a shape whose STYLE SIZEs contains values {-5,-8,10,3} will be plotted on a graph whose vertical axis spans -8 to 10. Additional PROCESSING directives are used to optionally specify the bounds of vertical axes so that the graphs for all the shapes can be plotted with the same scale: .. code-block:: mapfile PROCESSING "CHART_BAR_MINVAL=val" PROCESSING "CHART_BAR_MAXVAL=val" Values in the datasource that are above CHART_BAR_MAXVAL or below CHART_BAR_MINVAL will be clipped respectively to these values. If only one of these directives is included, the other will be automatically adjusted for each shape to include at least the origin, i.e. the graphs for all the shapes will be in the same scale *only if* all the values are of the same sign (positive or negative). Stacked bar Graphs ............................................................................... Stacked bar graphs can be drawn using: .. code-block:: mapfile PROCESSING "CHART_TYPE=VBAR"