Better Dashboards

Working with Microsoft enterprise chart, dashboard and reporting technologies.

Align Multiple Chart Areas

Posted by milang on February 11, 2009

Last week, I started talking about a scenario where you could not use scale breaks and instead you would have to use multiple chart areas.  I’m going to expand on that post today to talk about how to create and align multiple chart areas.

First, let’s define what a chart area is.  The chart is the top-level container that includes the outer border, the chart title, and the legend.  The chart always contains one default chart area, although it is not visible on the chart itself.

I have highlighted the chart area in red below:

highlightedchartareas

Think of the chart area as a “container” that includes only the axis labels, the axis title, and the plotting area of one or more series.  Each series is connected to one chart area.  By default, the chart adds all series to the default chart area.  When using area, column, line, and scatter charts, any combination of these series can be displayed on the same chart area.

In the graph above, it is beneficial to separate the chart into multiple chart areas.  You can do that by creating a new chart area (thus splitting the chart into two chart areas) and then assigning one of the two series to the new chart area:


using System.Web.UI.DataVisualization.Charting
...

// Create a new chart area
// This will split the chart into two separate chart areas
Chart1.ChartAreas.Add("Chart Area 2");

// Set new chart area properties
// NOTE: I cheated a bit and manually set chart area properties to make the newly created
// chart area look like the first one.  I still have not figured out how to clone chart areas
// and I suspect this is not possible with the MS chart
Chart2.ChartAreas[1].AxisX.LabelStyle.Font = new Font("Trebuchet MS", 8, FontStyle.Bold);
Chart2.ChartAreas[1].AxisY.LabelStyle.Font = new Font("Trebuchet MS", 8, FontStyle.Bold);
Chart2.ChartAreas[1].AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
Chart2.ChartAreas[1].AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);

// Assign the first series to the chart area
Chart2.Series[0].ChartArea = "Chart Area 2";

alignmultiplechartareas1

Okay so the image above improves the readability of the series but notice that the chart areas need to be aligned so that we can make a proper comparison.  To do this, we can simply call AlignWithChartArea:

// Align the new chart area with the old one
 Chart2.ChartAreas[0].AlignWithChartArea = "Chart Area 2";

alignmultiplechartareas2

Lastly, when we start the y-axis labels at a value other than zero by setting IsStartedFromZero to false, the final result provides fodder for an excellent comparison between series:

alignmultiplechartareas4

Align Multiple Chart Areas Horizontally

In the chart sample framework, there is an example of how to align multiple chart areas horizontally, but I would never use it in a live application — it is a major liability because it presumes that your numbers are always fixed.  Aligning multiple chart areas horizontally is not particularly useful anyway because comparisons between different sets of numbers are always made top to bottom, not left to right.

Still, the concept of multiple chart areas allows you to do some pretty cool things.  Here is an example of how to layout multiple pie charts:

alignmultiplechartareas3


using System.Web.UI.DataVisualization.Charting;
using System.Drawing;

public partial class MultiPieChartLayout: System.Web.UI.Page
{
    Random rand;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Clear all series and chart areas so we can re-add them
        Chart2.Series.Clear();
        Chart2.ChartAreas.Clear();

        // Create new random variable for data
        rand = new Random();

        for (int i = 0; i < 4; i++)         {             // Create four new series and four new chart areas             Chart2.Series.Add("Series" + i.ToString());             Chart2.ChartAreas.Add("ChartArea" + i.ToString());             // Assign each series to a separate chart area             Chart2.Series[i].ChartArea = "ChartArea" + i.ToString();             // Set all series to be pie             Chart2.Series[i].ChartType = SeriesChartType.Pie;             // Add 7 random points to each pie chart             for (int j = 0; j < 7; j++)                 Chart2.Series[i].Points.AddXY(i, rand.Next(23, 42));         }         // Set the chart area position for the first chart area.         Chart2.ChartAreas["ChartArea0"].Position.X = 4;         Chart2.ChartAreas["ChartArea0"].Position.Y = 8;         Chart2.ChartAreas["ChartArea0"].Position.Width = 45;         Chart2.ChartAreas["ChartArea0"].Position.Height = 40;         // Set the chart area position for the second chart area.         Chart2.ChartAreas["ChartArea1"].Position.X = 4;         Chart2.ChartAreas["ChartArea1"].Position.Y = 50;         Chart2.ChartAreas["ChartArea1"].Position.Width = 45;         Chart2.ChartAreas["ChartArea1"].Position.Height = 40;         // Set the chart area position for the third chart area.         Chart2.ChartAreas["ChartArea2"].Position.X = 50;         Chart2.ChartAreas["ChartArea2"].Position.Y = 50;         Chart2.ChartAreas["ChartArea2"].Position.Width = 45;         Chart2.ChartAreas["ChartArea2"].Position.Height = 40;         // Set the chart area position for the fourth chart area.         Chart2.ChartAreas["ChartArea3"].Position.X = 50;         Chart2.ChartAreas["ChartArea3"].Position.Y = 8;         Chart2.ChartAreas["ChartArea3"].Position.Width = 45;         Chart2.ChartAreas["ChartArea3"].Position.Height = 40;     } } [/sourcecode] In SSRS, there is a slightly different workflow for creating multiple chart areas but the ideas presented above are still valid.  SSRS users can check the MSDN post on How to Specify a Chart Area for a Series for a step-by-step guide.

Advertisements

2 Responses to “Align Multiple Chart Areas”

  1. Mariya said

    I want to display multiple charts after i clicked a item in a list… I am using afreechart jar file to generate the charts.. In any way i want to display more than one charts…. so please guide me…

    Thanks in advance..

  2. aimbot black ops…

    […]Align Multiple Chart Areas « Better Dashboards[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: