ARTICLE

Charts for Asp.Net Application

Posted by Krishna Garad Articles | ASP.NET Programming February 28, 2011
In this article we will learn how to create a chart for our ASP.NET application.
Reader Level:
Download Files:
 


Introduction:

In this article we will learn how to create a chart for our ASP.NET application.

Background:

Here we will see how to draw a chart for our data in ASP.Net. We are using a third party control to draw our chart. This is a .dll file I found somewhere using Google search. It's not my .dll. This .dll file provides various types of charts such as LineChart, PieChart, CircleChart etc...

To prepare for a chart using this .dll file follow the steps given below.

Step 1:

Start a new web-site; give the name as ChartInAsp.Net. It will give you one Default.aspx and .cs file.

Step 2:

Download source code and copy the Webchart.dll file from the bin directory to some location. Now add a reference to this WebChart.dll file.

Step 3:

Now Register this dll in your Page directory such as shown bellow. And get the Chart control from this dll.

<%@ Register TagPrefix="Web" Namespace="WebChart" Assembly="WebChart" %>
<Web:ChartControl ID="ChartControl1" runat="Server" Height="366px"
                        Width="500px" />

Step 4:

Now move to Design view and see that the Chart control is created as shown below. Whenever we are creating this control the default chart is LineChart; you can change it to another type by setting it's type property in property window or in the .cs file. We will see how to change it again in the .cs file.

charts.gif

Step 5:

Now move to .cs file and Import the WebChart as shown below.

using WebChart;

Step 6:

Now get your Data from database or prepare only Dataset or DataTable.

// Preparing Data Source For Chart Control
        DataTable dt = new DataTable("Chart");
        DataColumn dc = new DataColumn("Year", typeof(int));
        DataColumn dc1 = new DataColumn("Aveg", typeof(double));
        dt.Columns.Add(dc);
        dt.Columns.Add(dc1);
        DataRow dr = dt.NewRow();
        dr[0] = 1995;
        dr[1] = 10.5;
        dt.Rows.Add(dr);
        DataRow dr1 = dt.NewRow();
        dr1[0] = 2000;
        dr1[1] = 11.5;
        dt.Rows.Add(dr1);

Step 7:

Now bind this data to the chart control. Here you can choose either LineChart, CircleChart or PieChart etc… Here we will bind the data to our LineChart and color this chart as follows.

//Chart type you can change chart type here ex. pie chart,circle chart

        LineChart chart = new LineChart();//Class instance for LineChart
        chart.Fill.Color = Color.FromArgb(50, Color.SteelBlue);
        chart.Line.Color = Color.SteelBlue;
        chart.Line.Width = 2;

        chart.Legend = "X Axis: Year.\nY Axis: Average";
        //looping through datatable and adding to chart control
        foreach (DataRow dr2 in dt.Rows)
        {

            chart.Data.Add(new ChartPoint(dr2["Year"].ToString(), (float)System.Convert.ToSingle(dr2["Aveg"])));
        }
        ConfigureColors();
        ChartControl1.Charts.Add(chart);
        ChartControl1.RedrawChart();

Step 8:

The ConfigureColors Method to color our chart.

private void ConfigureColors()
{
    ChartControl1.Background.Color = Color.FromArgb(75, Color.SteelBlue);
    ChartControl1.Background.Type = InteriorType.LinearGradient;
    ChartControl1.Background.ForeColor = Color.SteelBlue;
    ChartControl1.Background.EndPoint = new Point(500, 350);
    ChartControl1.Legend.Position = LegendPosition.Bottom;
    ChartControl1.Legend.Width = 40;

    ChartControl1.YAxisFont.ForeColor = Color.SteelBlue;
    ChartControl1.XAxisFont.ForeColor = Color.SteelBlue;

    ChartControl1.ChartTitle.Text = "GRAPH EXAMPLE IN ASP.NET";
    ChartControl1.ChartTitle.ForeColor = Color.White;

    ChartControl1.Border.Color = Color.SteelBlue;
    ChartControl1.BorderStyle = BorderStyle.Ridge;
}

Step 9:

Now run your application; it will show a nice chart Control to you. This dll will create a WebCharts folder in your application Directory for storing chart Images.

Conclusion:

In this way you can prepare various types of charts for you ASP.Net application.

Login to add your contents and source code to this article
post comment
     

+ ProjetcData.Rows[i][PPM_DESCRIPTION] + Environment.NewLine +"Strategic Value\t\t: " + ProjetcData.Rows[i][PPM_STRATEGICVALUE] + " %" + Environment.NewLine +"NPV\t\t\t: " + ProjetcData.Rows[i][PPM_BUBBLESIZE] + Environment.NewLine +"Budget\t\t\t: " + (ProjetcData.Rows[i][PPM_BUDGET].ToString()) + Environment.NewLine +"Risk Rating\t\t: " + ProjetcData.Rows[i][PPM_RISKTYPE] + Environment.NewLine +"Requestor(s)\t\t: " + ProjetcData.Rows[i][PPM_REQUESTOR] + Environment.NewLine +"Approver(s)\t\t: " + ProjetcData.Rows[i][PPM_APPROVER] + Environment.NewLine +"Manager\t\t: " + ProjetcData.Rows[i][PPM_SPONSOR] + Environment.NewLine +"Sponsor(s)\t\t: " + ProjetcData.Rows[i][PPM_PLANSTART] + Environment.NewLine +"Planned Start\t\t: " + ProjetcData.Rows[i][PPM_PLANEND] + Environment.NewLine +"Planned End\t\t: " + ProjetcData.Rows[i][PPM_PLANEFFORT] + Environment.NewLine +"Planned Effort\t\t: " +

Posted by chetan koriya Jul 02, 2012

hi i have one problem with asp.net chart control tootip that is if less number of coloumns are there it showing correctely but if more number of coloumns are there like 15 it will not show correctely, means if data is more than 30 characters to any coloumn it will cutting last coloumn please help its very urgent for me string lToolTipTxt = "Project\t\t\t: " + ProjetcData.Rows[i][PPM_PROJECT] + Environment.NewLine +"Description\t\t: " + ProjetcData.Rows[i][PPM_DESCRIPTION] + Environment.NewLine +"Strategic Value\t\t: " + ProjetcData.Rows[i][PPM_STRATEGICVALUE] + " %" + Environment.NewLine +"NPV\t\t\t: " + ProjetcData.Rows[i][PPM_BUBBLESIZE] + Environment.NewLine +"Budget\t\t\t: " + (ProjetcData.Rows[i][PPM_BUDGET].ToString()) + Environment.NewLine +"Risk Rating\t\t: " + ProjetcData.Rows[i][PPM_RISKTYPE] + Environment.NewLine +"Requestor(s)\t\t: " + ProjetcData.Rows[i][PPM_REQUESTOR] + Environment.NewLine +"Approver(s)\t\t: " + ProjetcData.Rows[i][PPM_APPROVER] + Environment.NewLine +"Manager\t\t: " + ProjetcData.Rows[i][PPM_SPONSOR] + Environment.NewLine +"Sponsor(s)\t\t: " + ProjetcData.Rows[i][PPM_PLANSTART] + Environment.NewLine +"Planned Start\t\t: " + ProjetcData.Rows[i][PPM_PLANEND] + Environment.NewLine +"Planned End\t\t: " + ProjetcData.Rows[i][PPM_PLANEFFORT] + Environment.NewLine +"Planned Effort\t\t: " + ProjetcData.Rows[i][PPM_PROJECTCATEGORY] + Environment.NewLine +"Category\t\t: " + ProjetcData.Rows[i][PPM_PROJECTTYPE] + Environment.NewLine +"Type\t\t\t: " + ProjetcData.Rows[i][PPM_PROJECTCATEGORY] + Environment.NewLine +"Requesting Organization\t: " + ProjetcData.Rows[i][PPM_REQORG] + Environment.NewLine +"Sponsoring Organizaion\t: " + ProjetcData.Rows[i][PPM_SPONORG] + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine; datapoint.tootip=lToolTipTxt; if i give like this it will show above issue

Posted by janaki janaki Dec 22, 2011

Yes we have System.DataVisualization.dll Here I'm just showing one other way to draw the charts.

Posted by Krishna Garad Feb 28, 2011

Dont we have System.DataVisualization.dll to draw much better chart ? :)

Posted by Dhananjay Kumar Feb 28, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter