Dynamic Charting
This document assumes that you are already familiar with MapServer application development and especially setting up .map files. Readers can also check out the 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 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 | |
|---|---|
| AGG | GD |
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: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.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
For those living on the bleeding edge, the diameter of a pie chart can be bound to an attribute starting from trunk version of 2007-10-20. details here
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.LAYERAt 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 ;) ).
...
CLASS
NAME "value 1" #include a NAME keyword if you want this class to be included in the legend
STYLE
SIZE [attribute] #specify which value from the data source will be used as the value for the graph
....
END
END
CLASS
...
END
...
END
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:STYLE
SIZE [attribute]
COLOR r g b #specify the fill color
OUTLINECOLOR r g b #if present will draw an outline around the corresponding bar or slice
WIDTH w #specify the width of the outline if OUTLINECOLOR is present (defaults to 1)
OFFSET a b #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 usefull for emphasising a specific
#value in each chart. 'b' is required by the mapfile parser but is ignored.
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:
LAYERIn 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.
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
Bar Graphs
Bar graph drawing is set with a PROCESSING keyword in the layer attributes: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:
PROCESSING "CHART_BAR_MINVAL=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).
PROCESSING "CHART_BAR_MAXVAL=val"
About This Document
Copyright Information
Copyright (c) 2007, Thomas Bonfort.
This documentation is covered by the same Open Source license as the MapServer
software itself. See MapServer's License and Credits page for the complete
text.
Disclamer
No liability for the contents of this document can be accepted. Use the concepts, examples and other content at your own risk. As this is a new edition of this document, there may be errors and inaccuracies that may be damaging to your system. Although this is highly unlikely, the author(s) do not take any responsibility for that: proceed with caution.
This How-to applies to: MapServer 5.0
Dynamic charting using dbf files
I'm trying to set up a dynamic chart that will take data from a ESRI shape file and from a standalone dbf file using a join. However, i keep encountering an error saying JOIN not recognized..sometimes it doesn't recognize the fields of the join..
Is there any example out there that can show me how to accomplish what i'm trying to do?
thanks in advance,
ravi nair
re: Dynamic charting using dbf files
thanks
Gap between bars in Bar chart
Thanking you
Raju,Hyderabad,India