Better Dashboards

Working with Microsoft enterprise chart, dashboard and reporting technologies.

Archive for the ‘.NET Chart’ Category

Make Your ASP.NET Chart Look Like The Excel 2007 Chart

Posted by milang on September 18, 2010

The Office Excel chart remains the most popular data visualization tool in the Microsoft stack.  This post describes how you can mimic the look and feel of the Excel chart using the ASP.NET chart that ships with Visual Studio 2010.

It is worth pointing out that the default ASP.NET chart already has a built-in Excel palette, but it is based on the Excel 2003 color palette:

In Office 2007, the Microsoft Excel product team changed the colors of their default chart palette and for whatever reason, the corresponding change did not make it into the ASP.NET chart.  However, you can still create the effect of an Excel 2007 chart by using a custom palette to override the built-in palettes, and adding the colors that are present on the default Excel chart.

Here is an example of the source code for this operation:

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

public partial class PieChartPercentageLabels : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// In order to use a custom palette, you must set the default palette to None.
chart1.Palette = ChartColorPalette.None;

// This will insert Excel colors for up to 6 series on your ASP.NET chart.
chart1.PaletteCustomColors = new Color[] {
Color.FromArgb(79, 129, 189),
Color.FromArgb(192, 80, 77),
Color.FromArgb(155, 187, 89),
Color.FromArgb(128, 100, 162),
Color.FromArgb(75, 172, 198),
Color.FromArgb(247, 150, 70)};
 }
}

You can also use this technique to mimic the fonts and background colors used in the Excel chart, although I’m having a hard time trying to figure out what the default font is for the Excel 2007 chart.  Any ideas?

Advertisements

Posted in .NET Chart | Tagged: , | 2 Comments »

How to Create a Sparkline Chart in ASP.NET

Posted by milang on February 21, 2010

In this post, I’ll show how to create a sparkline chart in ASP.NET using the Microsoft chart control.  Sparklines aren’t supported natively, but they can be implemented easily by stripping the chart down to its basic series.

Try dropping a new chart onto the Visual Studio design surface and use the code below as a starting point to generate a sparkline similar to the one shown above.


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

protected void Page_Load(object sender, EventArgs e)
 {
 // Generate random data
 Random rand = new Random();
 double high = rand.NextDouble() * 40;

 // Assign the random number to the chart to create 35 points
 for (int x = 0; x < 35; x++)
 Chart2.Series[0].Points.AddXY(DateTime.Now.AddDays(x), rand.Next(100, 200));

 // Start hiding both sets of axes, labels, gridlines and tick marks
 Chart2.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
 Chart2.ChartAreas[0].AxisY.LabelStyle.Enabled = false;
 Chart2.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
 Chart2.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
 Chart2.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;
 Chart2.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
 Chart2.ChartAreas[0].AxisX.LineWidth = 0;
 Chart2.ChartAreas[0].AxisY.LineWidth = 0;

 // Sparklines use the 'Spline' chart type to show a smoother trend with a line chart
 Chart2.Series[0].ChartType = SeriesChartType.Spline;

 // Since the line is the only thing you see on the chart, you might want to
 // increase its width.  Interestingly, you need to set the BorderWidth property
 // in order to accomplish that.
 Chart2.Series[0].BorderWidth = 2;

 // Re-adjust the size of the chart to reduce unnecessary white space
 Chart2.Width = 400;
 Chart2.Height = 100;
 }

I see why sparklines are popular but frankly, I’m not a big fan of them.  Charts should have context to understand what the trend line really means.  The “fast food” approach to data visualization will only lead to more confusion in the long run.  Finding easier ways to understand data is cool, but sometimes I feel that we’re just getting lazy as dashboard writers, compromising the difficulties of scientifically analyzing data in favor of quickly finding a pattern.  I can appreciate what Tufte was trying to get at with sparklines, but keep in mind that his use case will not work in all cases.

Anyway, rant over – enjoy your sparklines.  🙂  Feel free to post questions if this is unclear.

Posted in .NET Chart, SQL Reporting Services | Tagged: , | 10 Comments »

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.

Posted in .NET Chart, SQL Reporting Services | Tagged: | 2 Comments »