Surface Chart in WPF with Data Biding and Colour Map

This is a step-by-step guide on how to build a surface chart using MindFusion WPF Surface Chart control. The data is set with data binding. After we build the chart we’ll adjust some properties and preview how they affect the chart appearance.

Step-by-step guide on how to build a 3D surface  or scatter chart

Step-by-step guide on how to build a 3D surface or scatter chart

I. General Settings

We start with a new WPF project and add the libraries needed for the WPF surface chart: MindFusion.Charting.Wpf and MindFusion.Licensing. We create a mapping to the charting namespace like that:

xmlns:chart="http://mindfusion.eu/charting/wpf"

If you have installed the control or MindFusion WPF Pack with toolbox integration you’ll just need to drag and drop the surface chart control.

Drag and drop the surface chart control

Drag and drop the surface chart control

Next step is to declare the Surface Chart. We do this in XAML:

<chart:surfacechart x:name="surfaceChart" effect3d="ShaderEffect" scatterfacesize="1" showsurface="True" showwireframe="True" showscatters="False" title="ScatterChart3D" sidewallthickness="0.5" interpolationtype="Bezier" gridtype="Horizontal" scale="200" groundlevel="0">
</chart:surfacechart>

We use a ShaderEffect to make the chart more beautiful. The ShowSurface property means the control shall draw a surface but will not draw a wireframe or scatters – we’ll change that later. The SideWallThickness sets how the walls will look – we make them modestly thick. If we don’t set a GridType it will be more difficult to read the chart, so we set a horizontal grid. Scale determines how the chart will be zoomed and GroundLevel sets the location of the bottom or ground wall.

II. Data Binding

We’ll use a single series in the chart and we initialize it in XAML as well:

<chart:surfaceseries x:name="series1">
</chart:surfaceseries>

For the purpose of this sample we create just a List of Point3D objects, which will be the data source. We do this in code.

List Data = new List();
Point3D point3D;

for (int i = 0; i < 360; i+=10)
{
double angle_rad = i / 180.0 * Math.PI;

for (int r = 0; r <= 10; r++)
{
double radius = r * 0.2;
double x = radius * Math.Cos(angle_rad);
double z = radius * Math.Sin(angle_rad);

point3D = new Point3D(x, radius, z);
Data.Add(point3D);
 }
}

We generate the points using the algorithm for a cone. Now let’s bind the data list to the chart’s properties.

surfaceChart.DataSource = Data;
series1.XDataPath = "X";
series1.YDataPath = "Y";
series1.ZDataPath = "Z";

The DataPath properties specify the names of each data bound property.

Now that we know our data we must set the minimum and maximum value of all three chart axes to make sure data is rendered correctly:

surfaceChart.XAxisSettings.MinValue = -2;
surfaceChart.XAxisSettings.Interval = 0.4;
surfaceChart.XAxisSettings.MaxValue = 2;
surfaceChart.YAxisSettings.MinValue = 0;
surfaceChart.YAxisSettings.MaxValue = 2;
surfaceChart.YAxisSettings.Interval = 0.4;
surfaceChart.ZAxisSettings.MinValue = -2;
surfaceChart.ZAxisSettings.MaxValue = 2;
surfaceChart.ZAxisSettings.Interval = 0.4;

Each axis exposes MinValue, MaxValue and Interval properties, which are used to define the scale.

III. Colours

The colours for a surface chart are set with the Fills property. In a surface chart, if you use a gradient brush, each point will read its color mapped to the matching position on the gradient. If you want more details, you can use the TextureType and ColorMapList properties to specify the colour map. In our sample we use XAML and add a gradient brush:

<chart:surfaceseries.fills>
 	<lineargradientbrush startpoint="0,0" endpoint="0,1">
<gradientstop color="#D2D9A3" offset="0">
<gradientstop color="#023E73" offset="0.33">
<gradientstop color="#8A188C" offset="0.66">
<gradientstop color="#F24B6A" offset="1.0">
</gradientstop></gradientstop></gradientstop></gradientstop></lineargradientbrush>
</chart:surfaceseries.fills>

IV. Customizations

We can show a wire frame by setting ShowWireFrame to “true”. If we want to show scatters, we’d better turn off ShowSurface and set ShowScatters to true. A wire frame can be show no matter the surface type. You can combine all of them – scatters, wire frame and surface.

InterpolationType lets you choose among various types of interpolation. Choosing None means that the control shall simply connect the 3D points.

An important property is PointMergeThreshold – it indicates the smallest difference at which a point that comes too close to another point won’t be drawn. If we set it to 0 or a negative number all points will be drawn.

Here is a selection of images of charts with different combination of surface properties turned on: wire frame, surface, scatters.

By turning a single property you can completely change the appearance of a 3D surface chart

By turning a single property you can completely change the appearance of a 3D surface chart

You can download the sample directly from this link:

Download MindFusion Surface Chart Tutorial

Learn more about the WPF Chart & Gauge control here.

Charting for WPF, V2.0

The list below describes recent changes and additions to MindFusion.Charting for WPF:

Support for Multiple Axes

The chart can now render multiple axes at each side of the plot area. To enable that, add a new Axis instance to the XAxes collection properties for bottom axes, to YAxes collection for left-hand axes, and X2Axes / Y2Axes for respectively top and right sides. The old AxisSettings class and XAxisSettings / YAxisSettings objects have been removed, and now their properties are set independently for each Axis instance in the collections.

Multiple axes in the WPF chart control.

Multiple axes in the WPF chart control.

Improved Zooming

Selected area with width smaller than MinZoomSpan does not evoke any action in the control. In addition, the new ZoomChanged event fires whenever zoom occurs and provides useful data for the zoom action with its ZoomChangedArgs.

Cross Hair Improvements

The cross hair control has been improved with several new properties, a method and an event. The properties are:

The new CrossHairPosition method returns the current location of the cross hair. For more precise handling of cross hair movements a new event is available – CrossHairPositionChanged.

Cross hair is drawn in a WPF column chart.

Cross hair is drawn in a WPF column chart.

Greatly Improved 3D Charts

3D charts have received plenty of improvements, new properties and performance optimizations: PointMergeThreshold – The property sets the radius of an area around a given point where no other points are drawn. The result is better performance especially in charts with numerous points, which happen to be close to one another. Of points with similar coordinates only a single one is rendered, the rest are omitted.

InterpolationType.None – A new InterpolationType has been added to the InterpolationType enum, which does not interpolate but adds data directly and connects the points with triangulation.

The SurfaceType enum has been replaced with three bool properties, which makes the API easier to understand and use.

ScatterFaceSize – the property regulates the size of the polygons that build a 3D scatter. Bigger values lead to bigger polygons, which results in faster performance and rougher scatter mesh. Effect3D.ShaderEffect – the property can be applied to all 3D chart elements, including scatters and performs much faster.

3D surface chart with color map and wire frame.

3D surface chart with color map and wire frame.

Exporting Images

Two new methods have been added for exporting the chart as an image – CreateImage and ExportImage.

Custom Formatting of Labels in Real-time Charts

A new property has been added to the RealTimeChart library – Axis.LabelFormatProvider. Use it to specify custom formatting of numeric labels. If required, you can specify format arguments for your format classes with Axis.LabelFormat.

Direct download of the trial version is available from here:

Download MindFusion.Charting for WPF 2.0 Trial Version

You are welcome to contact us with any questions, problems or inquiries about the Charting for Wpf control or any other of our products. MindFusion has always put special emphasis on providing excellent customer support and we usually answer your inquiries in a few hours of receiving them.

About MindFusion.Charting for Wpf: A programming component that combines powerful charting capabilities with an elegant API and easy use. Among the features of the control are fully customizable grid, positive and negative values on all chart axes, 3D charts, gauges and many more – read a detailed list here.

The control provides detailed documentation and various samples that demonstrate how to customize every type of chart. It supports a wide range of 2D and 3D charts including bar, line, radar, bubble pie etc. You can add tooltips, define themes, perform hit testing, zoom and more.

Charting for WPF, V1.9

MindFusion is pleased to announce a new release of Charting for WPF programming component. Here is an overview of the new features:

Real-time Chart
The RealTimeChart control is optimized to render animated line chart graphics of large amounts of data. That’s implemented by moving already rendered elements from the WPF visual tree to a cache bitmap, relying that new data is added incrementally rather than changing coordinates of old data points. The library also offers various customization options for the chart – scatters, multiple legends, multiple Y and Y2 axes, custom labels, tooltips, grid.

The real time chart.

The real time chart.

3D Charts

  • All 3D charts now support unlimited number of series.
  • New property Lights holds a collection of Light objects instead of the LightType property.
  • Detailed hit testing, which returns a ChartHitTestResult3D that contains the clicked 3D point, the normalized 3D point, a collection of chart elements (if found), and the nearest peak (in surface charts).
  • AllowMeshSelection property, which when enabled allows selection of mesh objects with a mouse click.
  • The MeshSelectionBrush sets the brush for the selection.
  • Behavior3D property, which lets you choose how the chart responds to mouse interactions.
  • Improved performance for all chart types.

Surface Charts

  • SurfaceType property, which sets whether scatters or surface is drawn.
  • SurfaceSeries.GroundLevelMargin property, which sets the GroundLevel-offset.
  • Each Series3D can be clipped in a given range with the ClipMinimum and ClipMaximum properties.
  • TextureType property sets the texture of the chart – Brush or ColorMap.
  • New ColorMapList property, which sets a Color map with arbitrary colors and offsets.
  • CloseModelFigure property, which closes the sides of the model if enabled.

A surface chart

A surface chart

Miscellaneous

  • ShowCrossHair renders a cross hair that will trace the mouse position over the plot area.
  • Enable the LineSeries.Curve property to draws lines as canonical splines.

You can read further details about the release and the other new features at the news section in the Charting forum. A trial version is available for download from here:

Download MindFusion.Charting for WPF 1.9 Trial Version

You are welcome to contact us with any questions, problems or inquiries about the Charting for Wpf control or any other of our products. MindFusion has always put special emphasis on providing excellent customer support and we usually answer your inquiries in a few hours of receiving them.

About MindFusion.Charting for Wpf: A programming component that combines powerful charting capabilities with an elegant API and easy use. Among the features of the control are fully customizable grid, positive and negative values on all chart axes, 3D charts, gauges and many more – read a detailed list here.

The control provides detailed documentation and various samples that demonstrate how to customize every type of chart. It supports a wide range of 2D and 3D charts including bar, line, radar, bubble pie etc. You can add tooltips, define themes, perform hit testing, zoom and more.