Introduction

Today, in this blog let's play around with one of the interesting and most useful concept in .NET.

Question: What are charts?

In simple terms "It enables to draw out different graphical charts and choose the appropriate one as per the requirement".

Step 1: Create a new WebForm project

Image1.jpg

Step 2: The complete code WebForm1.aspx looks like this

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ChartApp.WebForm1" %>

<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Chart ID="Chart1" runat="server">

<series><asp:Series Name="Series1" IsValueShownAsLabel="true" XValueMember="FirstName" YValueMembers="Age"></asp:Series></series>

<chartareas><asp:ChartArea Name="ChartArea1"></asp:ChartArea></chartareas>

</asp:Chart></div>

</form>

</body>

</html>

Step 3: The complete code WebForm1.aspx.cs looks like this

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

namespace ChartApp

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

getData();

}

}

private void getData()

{

SqlConnection con = new SqlConnection(@"Data Source=WIN-KV3BO1RQQF7;Initial Catalog=Company;Integrated Security=True");

SqlDataAdapter da = new SqlDataAdapter("Select FirstName, Age from Employee", con);

DataSet ds = new DataSet(); da.Fill(ds);

Chart1.DataSource = ds;

Chart1.DataBind();

}

}

}

Step 4: The output of application looks like this

Image2.jpg