Fixing Floating Rectangles in ASP.NET StackedBar Chart

Posted by: Steven Smith, on 09 Feb 2012 | View original | Bookmarked: 0 time(s)

In working on a clients reporting system, I ran across some StackedBar charts that were using the System.Web.DataVisualization.Chart control that were having some problems.  The biggest one was that the rendered bars had gaps and floating rectangles in it.  This turned out to take about an hour to solve, using my best Google-fu, so Im posting my solution and notes here, in case I run into it again and to save some other poor soul an hours time.

The Problem

Heres what the chart looked like, originally. 

image

Ill spare you the original code, as it was a total rats nest, but I was able to dramatically simplify it before proceeding to fix the actual issue.  A bit of searching led me to believe that the problem stemmed from not having the same number of datapoints for each X value.  You can actually dump the values of the chart data into an XML format pretty easily, using this command:

image

The resulting XML looked something like this:

image

Clearly, there are two problems here.  The first one is that each series has a different number of data points.  The second is that the points are not in order by year.  You need to fix both of these problems in order to get the correct chart behavior.

Adding Empty Data Points

My first stab at adding empty data points was to use a method that seemed well-suited to the task:

image

Unfortunately, this didnt fix the chart, and upon looking at my chart.xml data, it also didnt add in any empty points.  I tried a few variations but couldnt get it to work with my data.  Eventually I found a newsgroup posting with some code to do this by hand, which I ended up adapting to my needs:

//// now fix empty data points
var xVals = new List<double>();
foreach (var series in Chart1.Series)
{
    for (int i = 0; i < series.Points.Count; i++)
    {
        double currentXValue = series.Points[i].XValue;
        if (!xVals.Contains(currentXValue))
        {
            xVals.Add(currentXValue);
        }
    }
}
 
foreach (var xVal in xVals)
{
    foreach (var series in Chart1.Series)
    {
        try
        {
            if (series.Points.FindByValue(xVal, "X") == null)
            {
                series.Points.AddXY(xVal, 0);
            }
        }
        catch (Exception)
        {
            series.Points.AddXY(xVal, 0);
        }
    }
}
 
foreach (var series in Chart1.Series)
{
    foreach (var point in series.Points)
    {
        if (point.YValues[0] == 0)
        {
            point.IsEmpty = true;
        }
    }
}


Ill probably clean up this code further and move it into its own method (perhaps an extension method on Chart) in the future.  Its not pretty, but it does the job.  With this in place, the resulting XML code looks like this:

image

The chart, however, is still broken.

Sorting the Points

The last step is to sort the points.  This is actually quite easy to do with the following bit of code:

foreach (var series in Chart1.Series)
{
    Chart1.DataManipulator.Sort(PointSortOrder.Ascending, "X", series);
}


With this in place, the resulting XML code looks like this:

image

and the chart looks like this:

image

Summary

The charting controls available in the DataVisualization namespace are very powerful.  I hope this will help some folks who are trying to get their StackedColumn chart to display propertly.  Unfortunately, the documentation available from Microsoft directly on these controls is rather sparse.  Hopefully this will help others who are trying to use these charts (which, by the way, can certainly be used outside of ASP.NET in my case they are being used to generate image files that are then merged into Word and PDF reports, not for display on web pages).


Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: XML | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 219 | Hits: 4

Similar Posts

  • Making a Step chart in RadChart for Silverlight and WPF more
  • WPF / Silverlight: RadChart with ChartLegend as a ToolTip more
  • WPF Release History : Q1 2009 SP2 (version 2009.1.526) more
  • Silverlight Release History : Q1 2009 SP2 (version 2009.1.526) more
  • Steema teeChart for .NET 2009 released more
  • CTP before the official release of Telerik's charts, gauges and scheduler for WPF more
  • ChartDirector for .NET 4.0.5 Released more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD