This post dig a little more into this blog post code about working with and customizing Dynamic Data Display charting. Since the default bar chart implementation does not work, it’s easy to create our own by simply deriving one chart from PointGraphBase.
public class BarChart : PointsGraphBase
{…
Then we define the classical bounch of dependencies properties which code is not so fancy to see: Thickness, Stroke and fill brush, and so on. The core function we must implement is OnRenderCore. Let’s see how:
protected override void OnRenderCore(System.Windows.Media.DrawingContext dc, RenderState state)
{
if (DataSource == null) return;
var transform = Plotter2D.Viewport.Transform;
DataRect bounds = DataRect.Empty;
using (IPointEnumerator enumerator = DataSource.GetEnumerator(GetContext()))
{
Point point = new Point();
while (enumerator.MoveNext())
{
enumerator.GetCurrent(ref point);
enumerator.ApplyMappings(this);
Point zero = new Point(point.X, 0);
Point screenPoint = point.DataToScreen(transform);
Point screenZero = zero.DataToScreen(transform);
double height = screenPoint.Y - screenZero.Y;
if (height >= 0)
{
dc.DrawRectangle(Fill, new Pen(Stroke, StrokeThickness)
, new Rect(screenPoint.X - BarWidth / 2, screenZero.Y, BarWidth, height));
}
else
{
dc.DrawRectangle(Fill, new Pen(Stroke, StrokeThickness), new Rect(screenPoint.X - BarWidth / 2, screenPoint.Y, BarWidth, -height));
}
bounds = DataRect.Union(bounds, point);
}
}
Viewport2D.SetContentBounds(this, bounds);
}
So nothing really special, just some key point:
-
ApplyMapping is the function who map the model coordinates into the graphical X-Y coordinates.
-
DataToScreen/ScreenToData are the (extension) method who maps from world coordinates to screen coordinates.
The other code is just drawing the rectangles for each point at a certain bar width, and we obtain this:
As you can probably see, line are anti-aliased even if we specified SnapToDevicePixels. This is due to the fact that SnapToDevicePixels does not work when we manually draw onto a DrawingContext. If we want to remove this we need to follow these instructions and create some GudelineSet to force WPF align to physical bytes.