Telerik blogs

So a customer emailed into our office yesterday asking about whether RadChart for Silverlight supported a Step chart type.  Well, while we do have a boatload of chart types already, Step is currently not one of them.  But a Step chart is really just a Line chart with a bit of extra work, right? 

With the help of our chart wizard Manol, we were able to show that the LineSeriesDefinition could, in fact, replicate a Step chart.  But that was with setting all points manually and took a bit of thinking to make sure all the steps looked right.  So we went ahead and took the next logical step - make a method!

Here is the code that we came up with after a little trial and error:

public List<DataPoint> MakeStepChart(List<double> chartNums)
{
    // our list to return
    List<DataPoint> myPoints = new List<DataPoint>();
    // we keep count to ensure steps stay aligned
    int xcount = 1;
    foreach (double anInt in chartNums)
    {
        // make a point for the value
        DataPoint aPoint = new DataPoint();
        aPoint.XValue = xcount;
        aPoint.YValue = anInt;
        myPoints.Add(aPoint);
        // make a point for the step up to the next value (if there is one)
        if (xcount < chartNums.Count)
        {
            myPoints.Add(new DataPoint(xcount + 1, chartNums[xcount - 1]));
        }
        xcount++;
    }
    // make one final point to complete the last step
    myPoints.Add(new DataPoint(chartNums.Count + 1, chartNums[chartNums.Count - 1]));
    return myPoints;
}

In a nutshell, the method takes your list of points and adds all the extra points which are necessary to get a step chart result.  See the results for yourself here:

Line vs Step Chart

That uses the same list of doubles, however the line chart is using just that list and the step chart is using the result of the MakeStepChart method. 

As an added bonus, since we share a common codebase and API between RadControls for Silverlight and RadControls for WPF, I was able to add a new project to the solution, literally copied and pasted the code from the SL app to the WPF one, and the results are exactly the same.  Here's a link so you can download it and check it out for yourself.

Happy coding!


About the Author

Evan Hutnick

works as a Developer Evangelist for Telerik specializing in Silverlight and WPF in addition to being a Microsoft MVP for Silverlight. After years as a development enthusiast in .Net technologies, he has been able to excel in XAML development helping to provide samples and expertise in these cutting edge technologies. You can find him on Twitter @EvanHutnick.

Related Posts

Comments

Comments are disabled in preview mode.