This post demonstrates how to re-template a Visiblox Chart to render data with mathematical-style "on-chart" axes.
Mathematical charts are usually drawn around the zero point, with the X and Y axes crossing at 0,0, and the plot drawn on top of the axes. In other fields, quantities which are never negative are plotted, and this means the zero point (or origin) may be the bottom left of the chart. Otherwise 0 may not be plotted, or horizontal axis may be time or some discrete scale. In each case the axes are fixed at the outside of the chart.
Visiblox Charts supports this latter type of chart out of the box. What about the mathematical axes-crossing style? At the moment that's not supported, but here I'll show you how simple it is to achieve this effect by re-templating the chart.
Templating a Visiblox Chart
An Visiblox chart is an instance of the Chart control. As with any WPF/Silverlight custom control, this can be retemplated to alter its visual appearence. Of course as the control has all the rendering logic to draw complex charts, there is a limit to what you can achive, but it's surprising how much can be done to alter the layout of the chart. (Other examples might include embedding some custom data display within the chart.)
Firstly, consult the API documentation to see the default template of the chart (the Visiblox API documentation reproduces default or sample templates for most templatable controls, but in general you can find this out for yourself using a tool such as StyleSnooper or Reflector). You'll see that there is some complexity to support axes on all four sides of the chart, and other nice default behaviour - but here we'll cut back to basics.
We'll use a Grid with 2 rows, the first being the title, the 2nd containing the chart. Notice that the default template has a more complex grid. Important parts of this are the plot area (containing all the elements rendered "on chart" in their own individual containers), and the axis containers.
At this point I'm going to digress a little regarding axes. A Visiblox axis is responsible for 2 things: calculating based on data values where points should be rendered in that dimension, and actually rendering the axis itself. Note that in the default template the Y axis container is in a grid cell adjacent to the plot area, it thus has the same height as the plot area, and thus the ticks on the axis correspond to render positions within the plot area. Similarly the X axis is the same width as the plot area.
In order to render our axes within the chart, we'll simply move them into the same grid cell as the plot area. The Y axis will have the same height as it did before, and the X axis will have the same width, so everything will render correctly using the default Visiblox axis implementations.
All that remains to be done is to shift the axes to the correct 0 position in order to achive the desired visual effect. The following code repositions the axes. The Y axis is positioned by asking the X axis where it plots 0, and vice versa.
The axes will have to be repositioned when either the layout changes (eg the chart changes in size or the Y axis becomes wider/narrower), or the position to plot 0 changes (eg because the chart range has changed). Putting it together, the following chart plots a sine curve, and adds a panning behaviour to demonstrate that the usual interactivity of a Visiblox chart is available.
In this article I showed how to re-template a Visiblox chart to render its axes within the main plot area. I didn't consider many features Visiblox provides, such as a legend or multiple axes, but for those that need it, a custom template can provide flexibility to meet your particular situation.
You can download source for this example in this project.