Working with NetSpell and NPlot - Plots and drawable objects
(Page 4 of 5 )
Once you’ve created a PlotSurface2D, you’re ready to start charting some data. To do this, you first need to create an instance of one of NPlot’s classes that implements the IDrawable or IPlot interface. You then point this object to your data, optionally set a few display properties, and add it to your PlotSurface2D.
Plot classes wrap your data and provide functionality for drawing it against a pair of axes. The different plot classes display your data in different ways (for example, as a series of points or as a line). Plot classes can also draw representations of themselves in a plot surface legend if one is present and can suggest the axes against which they should optimally be drawn.
Several lightweight classes, includingTextItemandArrowItem, implement just theIDrawableinterface. These classes can’t draw representations of themselves in the legend or influence thePlotSurface2D’s selection of axes.
The most commonly used classes implementingIDrawableare:
ArrowItem
Draws arrows pointing to a particular world
coordinate
FilledRegion
Creates a filled area between two line plots
Grid
Adds gridlines that automatically align to axis tick
positions
TextItem
Places text at a specific world coordinate
The following are descriptions of some of NPlot’s available plot types:
LinePlot
Use a line plot (Figure 4-16) when it makes sense to connect successive data points. For example, you would use a line plot to graph measurements of the ambient temperature of a room at various times throughout a day. You can control the line in the plot by passing a configuredSystem.Drawing.Penclass to theLinePlot.
PointPlot
Use a point plot (scatter chart) when it does not make sense to connect successive data points. For example, if you wanted to visualize the heights and weights of a group of people on a chart, you could plot a point for each person with the x-position determined by the person’s weight and the y-position determined by the person’s height. Fifteen predefined marker styles, including the one shown in Figure 4-17, are available.

Figure 4-16. LinePlot graph

Figure 4-17. PointPlot graph
StepPlot
Step plots like the one in Figure 4-18 are useful for displaying sample-based data (such as PCM audio), where each value can be thought of as representing the value of the measured quantity over a specific time period. You can choose whether the horizontal sections of the step plot are centered on the abscissa values or drawn between successive abscissa values.

Figure 4-18. StepPlot graph
BarPlot
A bar plot (or histogram) is usually used to chart the number of data values belonging to one or more categories. The height of the bar represents the number of values in the given category. For example, if you had a collection of dogs and data on the breed of each, you could create a chart of the number of each type of breed.
You will often want to make the x-axis aLabelAxis(the names of the dog breeds, for instance). You can define fill patterns for the bars using HorizontalRectangleBrushand similar classes. Bar charts can also be stacked on top of each other, as shown in Figure 4-19. Horizontal bar plots are currently not supported by NPlot.

Figure 4-19. BarPlot graph
ImagePlot
Image plots (Figure 4-20) are often used to display the variation of a value over a spatial area. Each value in the region is mapped to a color. You can specify the color-to-value mapping using an object of any class that implementsIGradient, such asLinearGradient.

Figure 4-20. ImagePlot graph
If the built-inIPlotandIDrawableclasses don’t provide the functionality you require, creating your own class that implements one of these interfaces is straightforward. This is perhaps the most common way of extending NPlot.
You can add as many plots to aPlotSurface2Dobject as you like. The order in which they are drawn is configured with the z-order parameter of theAdd()method. Also, thePlotSurface2D classes define two independent x-axes and two independent y-axes. When you add an item, you can choose the x-and
y-axes you would like it to be associated with.
Specifying data
The IPlot interface does not enforce how data associated with the specific plot classes should be represented. However, where it makes sense, these classes provide a consistent interface for this purpose. Data can be provided in one of two forms:
- In an object of typeDataSet,DataTable, orDataViewfrom theSystem.Datanamespace
- In any collection that implements theIEnumerableinterface where it is valid to cast each of the elements to typedouble
Examples of such collections are:
- Double[]
- System.Collections.ArrayList
- System.Collections.Generic.List <System.Int16>
If you are working with very large data sets and efficiency is a concern, it is best to pass your data to NPlot via the built-in array typedouble[].
The following four properties are used to specify data:
DataSource
TheDataSet,DataTable, orDataViewobject you are using.
DataMember
A string containing the name of the sourceDataTablein aDataSet.
AbscissaData
The x-coordinates of the data to plot. This should be a string containing the name of the column to take the data from if the source is aDataTableorDataView. Otherwise, it can be set to any container that implements theIEnumerableinterface. This property is optional. If it is not specified (or is set tonull), the abscissa data will be assumed to be 0, 1, 2....
OrdinateData
The y-coordinates of the data to plot. This should be a string containing the name of the column to take the data from if data is being read from aDataTableorDataView. Otherwise, it can be set to any container that implements theIEnumerableinterface.
If these properties are not suitable for a particular plot type, the interface is as close to this as possible. For example,CandlePlotprovidesOpenData,LowData,HighData, andCloseDataproperties instead ofOrdinateData.
Axes. APlotSurface2Dobject automatically determines axes suitable for displaying the plot objects that you add to it. However, these are highly customizable. Some common things that you might wish to add or adjust are:
- A label for the axis (using theLabelproperty)
- Tick text/label fonts (using theTickTextFontandLabelFontproperties)
- The angle of the text next to the ticks (using theTicksAngleproperty)
- The pen used to draw the axis (using theAxisPenproperty)
- World minimum and maximum values (using theWorldMinandWorldMaxproperties)
You can also replace the default axes with a completely different axis type. NPlot provides a number of axis types with individually configurable characteristics, includingLinearAxis,LogAxis,LabelAxis, andDateTimeAxis.
Next: Producing graphs from data sources >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.
|
|