Using the ASP.NET Chart Control
page 3 of 5
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 48530/ 41

Chart Example

Now its time to see an example of this.  Take a look at the following chart.

<asp:Chart id="Chart1" runat="server" ImageType="Jpeg"
      Width="600px" Height="400px" Palette="Chocolate">
      <Titles>
            <asp:Title Name="DefaultTitle" Font="Trebuchet MS, 10pt, style=Bold"
                  Text = "My Title" Enabled="True" />
      </Titles>
      <Legends>
            <asp:Legend Name="DefaultLegend" Enabled="True" Docking="Top" />
      </Legends>
</asp:Chart>

So far, within this chart, we have the core Chart control that defines the width and height of the image.  I'd highly recommend specifying these parameters, to ensure the chart image renders decently.  In addition, there are four image types and this chart renders in Jpeg format, along with using the Chocolate palette (one of many color schemes that changes the color that the individual bars, lines, points, or pie slices use within the chart).

Chart Areas

Next, let's look at the setup of the charting area.

<asp:Chart ..>
      .
      .
      <ChartAreas>
            <asp:ChartArea Name="MainChart" BorderWidth="0">
                  <InnerPlotPosition X="5" Y="5" Height="90" Width="90" />
                  <AxisY LineColor="64,64,64,64">
                        <LabelStyle Font="Trebuchet MS, 10pt, style=Bold" />
                        <MajorGrid Enabled="false" />
                        <MajorTickMark Enabled="false" />
                  </AxisY>
                  <AxisX LineColor="64,64,64,64">
                        <LabelStyle Font="Trebuchet MS, 10pt, style=Bold" />
                        <MajorGrid Enabled="false" />
                        <MajorTickMark Enabled="false" />
                  </AxisX>
            </asp:ChartArea>
      </ChartAreas>
</asp:Chart>

The charting area is setup by the ChartArea object.  This chart contains the name of the charting region (important because there can be more than one charting areas and Series objects have to interrelate to the ChartArea).  The chart area defines information about the charting area itself.  First, the InnerPlotPosition specifies the region of the charting area to render itself in.  This varies by type of chart, as pie charts render differently than bar charts, and each chart functions differently depending on its settings.

The LabelStyle object specifies settings to use for labels that appear within the chart.  Font is just one of those properties that are customizable.  The next two properties affect the rendering of the chart axis.  The MajorGrid property specifies whether to draw a line for the major X or Y values.  So for each item rendered in the list, the chart control specifies the details about the line, such as line color, width, style, etc.  Currently its disabled, so no X/Y lines appear, but can be easily enabled by setting the value to true, and by at least specifying a line width.  This is the same for the tick marks that appear by enabling MajorTickMark.

Major grid lines are represented by the major values in the grid.  For instance, if the X axis shows years, it would most likely show one major grid line per year.  With the Y axis being net sales, the chart control may create a major grid line every $20,000.  The chart control automatically creates major values for you; however, you can override this feature to include whatever ranges you like.

Series and Data Points

Charting data itself is made up of series and data points; these series and data points represent the actual ranges of data.  As I mentioned before in my previous example, a series represents and object of measurement (a product), while the data points contain the measurement value data (years and net sales) used to establish a trend.

This would appear in the chart like the following.

<asp:Chart ..>
      <Series>
            <asp:Series Name="Bicycles" Label="Bicycle Sales"
                ChartArea="MainChart" ChartType="Bar">
                  <Points>
                        <asp:DataPoint Xvalue="2009" Yvalues="5,350.16" />
                        <asp:DataPoint Xvalue="2008" Yvalues="23,551.97" />
                        <asp:DataPoint Xvalue="2007" Yvalues="29,337.62" />
                        <asp:DataPoint Xvalue="2006" Yvalues="16,098.52" />
                        <asp:DataPoint Xvalue="2005" Yvalues="15,929.23" />
                  </Points>
            </asp:Series>
      </Series>
</asp:Chart>

This creates a typical bar chart with four bar lines, one for each of the years that shows the trend of net sales dollars.


View Entire Article

User Comments

Title: o   
Name: o
Date: 2012-12-03 3:54:11 AM
Comment:
o
Title: complaint   
Name: salman
Date: 2012-10-05 1:32:49 AM
Comment:
not goog
Title: Test   
Name: Test
Date: 2012-09-07 6:11:24 AM
Comment:
Test
Title: Nothing   
Name: Sean
Date: 2011-11-13 10:30:55 PM
Comment:
There's no article to read......
Title: it doesn't work   
Name: EnriqueAguilar
Date: 2010-12-23 1:35:26 PM
Comment:
Copy the complete example and does not work, shipping the example copied and error
Error executing child request for ChartImg.axd.
You can say what'S wrong?
code: can not send
Title: Good One   
Name: B.Raj
Date: 2010-11-17 2:19:44 AM
Comment:
Good article
Title: brainy   
Name: gayathri
Date: 2010-09-23 2:25:18 AM
Comment:
great article
thank you so much
Title: Great article   
Name: Kedar Periyapatna
Date: 2009-10-09 9:36:46 PM
Comment:
Hey ... i really appreciate your work .. very simple and clear explanation of asp.net Chart ... I do have one question ... as in WPF charts .. can I highlight every point of intersection b/w X and Y point by a thick dot .... thank u again
Title: MS CHART IS VERY GOOD   
Name: MOHAMMAD JAVED
Date: 2009-08-29 12:29:22 AM
Comment:
I USED MS CHART CONTROL ON AN APPLICATION IT HAS ALL FEATURES BUT SOME FEATURES ARE NOT MENTIONED.
BUT ITS IMPLEMENTATION AND USE ARE VERY EASY.
Title: Loving the MS Chart Control   
Name: Duncan
Date: 2009-03-30 10:28:22 PM
Comment:
I used the MSCC to build an application to track my mass(kg), body-fat(%) and body-water(%). It was a little intimidating at first, but by the end of a week I was comfortable with the control. It can be hard to set up on an ISP as it expects installation (not just upload of .dll to your /bin directory) and/or seems to require write permissions (even when set to persist the image to memory), but YMMV -- I'm happy with it. I really appreciated all the examples the author provided, which is also why I appreciate this article! Thanks!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 11:54:39 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search