How to create charts in ASP.NET 4.0 using built-in Chart control

By | August 19, 2013

Introduction
In this article I will explain how to create charts in ASP.NET 4.0 using the Chart control introduced in ASP.NET 4.0

Description
ASP.NET 4.0 introduced a new data control “Chart” with Visual Studio 2010. Chart control can be used to create around 35 types of interactive charts. Data in the chart controls can be added declaratively or from the database. Chart data can be manipulated using its built-in methods.

Step 1: Create a new ASP.NET Application

Step 2: Drag a Chart Control on Default.aspx page from the Toolbox under the Data section. Following markups are added in the HTML source of the page for the Chart control

<asp:Chart ID="Chart1" runat="server">
    <Series>
        <asp:Series Name="Series1">
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1">
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

The Chart control contains two collections, Series and ChartAreas. Series collection contains different Series object. A Series object is a collection of DataPoints which represents data in the chart. Different Series can be added for different type of ChartArea.

ChartAreas collection contains different ChartArea objects. A ChartArea object defines appearance of a Chart where data is plotted.

Step 3: After adding the Chart control on the page, some configuration is automatically added to the application to handle the charts. So please ensure that following tags are added in your web.config file

<httpHandlers>
    <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
</httpHandlers>

<pages>
    <controls>
        <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </controls>
</pages>

Step 4: Edit the Chart control in the HTML source as following to add some sample data to show in the chart

<asp:Chart ID="chartProducts" runat="server" Width="800" Height="500">
    <Series>
        <asp:Series ChartArea="ProductChartArea" Name="ProductSeries" ChartType="Column">
            <points>
                <asp:DataPoint AxisLabel="Rhönbräu Klosterbier" YValues="360" />
                <asp:DataPoint AxisLabel="Boston Crab Meat" YValues="220" />
                <asp:DataPoint AxisLabel="Boysenberry Spread" YValues="240" />
                <asp:DataPoint AxisLabel="Pâté chinois" YValues="350" />
                <asp:DataPoint AxisLabel="Röd Kaviar" YValues="150" />
            </points>
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ProductChartArea">
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

Here, first we have set the ChartType of the Series object “ProductSeries” to “Column” to create a Column chart. We also set ChartArea property to the ChartArea object “ProductChartArea”, which specifies that “ProductSeries” series is associated with “ProductChartArea” ChartArea.

Then we added some DataPoint objects inside the “ProductSeries” to plot the data. YValues is the value used to generate the chart and AxisLabel is the label for that value.

Output:

Column Chart

ASP.NET Column Chart

Pie Chart (When ChartType of the Series “ProductSeries” is changed to “Pie”)

ASP.NET Pie Chart

0 thoughts on “How to create charts in ASP.NET 4.0 using built-in Chart control

  1. Pingback: Creating 3D (Three-Dimensional) charts in ASP.NET | DEEPAK SHARMA

  2. Howardrah

    Your comment is awaiting moderation.

    Awesome write ups. Thanks!
    http://talahicc.com/
    cheap canadian drugs north west pharmacy canada online pharmacy canadian drugs
    canadian pharmacy meds
    canadian rx pharmacy online
    canadapharmacy com
    canada online pharmacy
    <a href="http://mirabiliatour.blogspot.com/ask?q=+Seriously+loads+of+fantastic+data%21+http%3A%2F%2Ftalahicc.com%2F+canada+pharmacy+online+orders+canadian+pharmaceuticals+online+best+online+international+pharmacies+%5Burl%3Dhttp%3A%2F%2Fcanadianpharmacymim.com%2F%5Donline+pharmacies+of+canada%5B%2Furl%5D+%0D%0Aoverseas+pharmacies+%0D%0Acanadian+pharmacies+%0D%0Aweb+medical+information+%0D%0Adrug+price+%0D%0Aprescription+online+%0D%0Acanada+pharmacy&submit=ask”>prescription drug assistance
    walmart pharmacy price check

    Reply
  3. QuentinBak

    Your comment is awaiting moderation.

    Cheers! Ample write ups.
    http://canadianpharmacyntx.com/
    walmart pharmacy price check canada drug drugs online mexican pharmacies shipping to usa
    online pharmacy usa
    walmart pharmacy price check
    canadian drug stores online
    canada online pharmacies
    pharmacy canada
    <a href="http://mirabiliatour.blogspot.com/ask?q=+Tips+clearly+used..+%0D%0Ahttp%3A%2F%2Ftalahicc.com%2F+%0D%0Amexican+pharmacy+online+canadian+viagra+most+reliable+canadian+pharmacies+%5Burl%3Dhttp%3A%2F%2Fwww.canadianpharmacymsn.com%2F%5Dcanadian+pharmacies-24h%5B%2Furl%5D+%0D%0Acanadian+cialis+%0D%0Acanadian+pharmacy+viagra+%0D%0Acanadian+rx+pharmacy+online+%0D%0Abest+canadian+prescription+prices+%0D%0Abest+price+prescription+drugs+%0D%0Atrust+pharmacy+canada&submit=ask”>canadian pharmacy reviews

    Reply
  4. QuentinBak

    Your comment is awaiting moderation.

    Superb advice. Regards!
    http://talahicc.com/
    international pharmacies that ship to the usa canadian pharmaceuticals online overseas pharmacies canadianpharmacy
    <a href="http://shket.ua/product/matras-naduvnoy-plyazhnyy-intex-68798/?DIGNITY=You+actually+suggested+that+adequately.+http%3A%2F%2Fcanadianpharmaciesnnm.com%2F+buying+drugs+canada+top+rated+canadian+pharmacies+online+%0D%0Amost+reliable+canadian+pharmacies+%0D%0Acanada+drugs+online&SHORTCOMINGS=You+actually+suggested+that+adequately.+%0D%0Ahttp%3A%2F%2Fcanadianpharmaciesnnm.com%2F+%0D%0Abuying+drugs+canada+%0D%0Atop+rated+canadian+pharmacies+online+%0D%0Amost+reliable+canadian+pharmacies+%0D%0Acanada+drugs+online&COMMENT=You+actually+suggested+that+adequately.+%0D%0Ahttp%3A%2F%2Fcanadianpharmaciesnnm.com%2F+%0D%0Abuying+drugs+canada+%0D%0Atop+rated+canadian+pharmacies+online+%0D%0Amost+reliable+canadian+pharmacies+%0D%0Acanada+drugs+online&NAME=HowardNaw&USED=&RATING=0&PRODUCT_NAME=%D0%9C%D0%B0%D1%82%D1%80%D0%B0%D1%81+%D0%BD%D0%B0%D0%B4%D1%83%D0%B2%D0%BD%D0%BE%D0%B9+%D0%BF%D0%BB%D1%8F%D0%B6%D0%BD%D1%8B%D0%B9+Intex+%2868798%29&PRODUCT_ID=28472″>buy prescription drugs canada
    buy drugs online
    canadian pharmacies shipping to usa
    canada meds
    cialis canadian pharmacy
    international pharmacies that ship to the usa

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *