MSDN Magazine - March 2009 - (Page 111) K. SCOTT ALLEN EXTREME ASP.NET Charting with ASP .NET and LINQ Microsoft recently released a new charting control for ASP.NET . SP. The chart control is easy to use and allows you to add attractive data visualizations to your ASP.NET Web applications. The control supports all of the standard chart types, like line charts and pie charts, as well as advanced visualizations like funnel and pyramid charts. In this column, I’ll explore the chart control and generate some data using queries written for LINQ to objects. The full source code for this column Figure 1 Visual Studio is available from the MSDN Code Gallery. Tools Studio also adds a new HTTP handler entry into the IIS . section and a similar entry into the section (for use with IIS . and the Visual Studio Web development server): Getting Started The first step is to download the chart control from the Microsoft Download Center (go.microsoft.com/fwlink/?LinkId=139617). This download installs the key runtime components you’ll need for charting, including the placement of the System.Web.DataVisualization assembly into the global assembly cache. You’ll also want to download the Visual Studio Tool Support (go.microsoft.com/fwlink/?LinkId=139618) and the charting sample Web site (go.microsoft.com/fwlink/?LinkId=139619). The tool support will give you toolbox integration and IntelliSense support at design time, while the sample Web site will give you hundreds of examples to examine for inspiration into creating the type of chart you need. Note that you will need Service Pack installed for both the Microsoft .NET Framework . runtime and Visual Studio . After the installations are all complete, you should be able to create a new ASP.NET project in Visual Studio and find the Chart control in the Toolbox window (see Figure 1). You can drag the chart into an ASPX file using the design view (which will make some necessary configuration changes to your web.config file) or work with the control directly in the Source view of an ASPX file (in which case you’ll need to manually make the configuration changes I will soon describe). When you place a chart into the Web forms designer, Visual Studio will place a new entry into the section of your web.config file, like so: This HTTP handler is responsible for processing requests that arrive for ChartImg.axd, which is the default endpoint the chart control will use for serving charts. I’ll go into more detail about the HTTP handler later. The code in Figure 2 shows the basic elements of a chart. Every chart includes at least one Series object populated with data. The ChartType property of each series will determine the type of chart used to plot the series (the default type is a column chart). Each chart can also contain one or more ChartArea objects where plotting will occur. You can customize nearly every visual element of the ASP.NET chart control, including backgrounds, axes, titles, legends, and labels. The chart control is so highly customizable that you should have a plan in place to keep the charts in your application looking consistent. In this column, I will use a builder strategy to apply consistent fonts and colors to all charts. This builder class will Figure 2 A Basic Chart Send your questions and comments to xtrmasp@microsoft.com. This entry allows you to use the chart control with the familiar asp tag prefix that other built-in ASP.NET controls utilize. Visual Code download available at code.msdn.microsoft.com/mag200903Extreme. March 2009 111 http://go.microsoft.com/fwlink/?LinkId=139617 http://go.microsoft.com/fwlink/?LinkId=139618 http://go.microsoft.com/fwlink/?LinkId=139619 http://code.msdn.microsoft.com/mag200903Extreme
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.