Search code examples
c#silverlightchartssilverlight-toolkit

Can't clear Silverlight Charting toolkit axes by code


I am attempting to have a Silverlight chart (windows phone 7) that a user can modify by changing some settings.

Upon clearing the charts axes and readding new axes, i end up with double axes being being reported by chart.ActualAxes.Count

does anyone know how to fully clear and delete all the axes on a silverlight chart and add new ones? Am i meant to call something to update the ActualAxes list after adding?

thanks in advance

code sample (call this twice and your chart will end up with 4 axes instead of two):

chart.Axes.Clear();

chart.Axes.Add(new LinearAxis()
{
    Orientation = AxisOrientation.Y,
    Location = AxisLocation.Left,
    Minimum = 0
});
chart.Axes.Add(new DateTimeAxis()
{
    Orientation = AxisOrientation.X,
    Location = AxisLocation.Bottom,
    IntervalType = DateTimeIntervalType.Days
});

Solution

  • The Chart Axes collection represents the persistentAxes in the chart that will be rendered even if there are no series in the chart. The ActualAxes represents a combination of both the persistent Axes and those in use by the series in the chart.

    When you Clear the collection and test ActualAxes.Count you will find it still says 2 even though Axes is now 0. The ActualAxes (an instance of SeriesHostAxesCollection) will not allow the removal of an axis which is in use be an existing Series. Hence the ActualAxes collection holds on to the originals. You then add 2 others to the persistent Axes collection so those 2 new ones are also added to the ActualAxes, you end up with 4.

    Run your code yet again (a third time) and you should see the ActualAxes count remains 4. Thats because the 2 Axis you added in the second call are not being used by any series so they can be removed from the ActualAxes collection.