Skip to main content

Microsoft Chart Controls - ASP.NET


Downloads:
  1. Click here to download Microsoft chart control add on.
  2.  Install the add on.
Steps :
  1. Add Chart control in your VS tool box.





2. Create a New Web Application.
3. Drag and drop the chart control to design page.
4. The 3 major properties of the Chart is  Title,Series and Chart Area.
    4.1. Title property is used to set the title of the chart.
    Eg: 
   <Titles>
      <asp:Title ShadowColor="32, 0, 0, 0"
                 Font="Trebuchet MS, 14.25pt, style=Bold"  
                 ShadowOffset="3" Text="Sample 1"  
                 Alignment="MiddleCenter" ForeColor="White">
      </asp:Title>
   </Titles>

 4.2. Series is collection of values to show in the chart.
  Eg: 
   <Series>
      <asp:Series Palette="EarthTones"  
              ChartArea="MainChartArea" 
              IsValueShownAsLabel="True"
              YValuesPerPoint="2">
      </asp:Series>
   </Series>

   4.3. Chart Area is used to design the chart bars chart type and 3D Positions.
    Eg: 
<ChartAreas>
      <asp:ChartArea Name="MainChartArea" BorderColor="64, 64, 64, 64" 
           BackSecondaryColor="Transparent" BackColor="64, 165, 191, 228" 
       ShadowColor="Transparent"  BackGradientStyle="TopBottom">
      <Area3DStyle Enable3D="True" />
      <AxisY LineColor="64, 64, 64, 64" IsLabelAutoFit="False">
         <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
         <MajorGrid LineColor="64, 64, 64, 64" />
      </AxisY>
      <AxisX LineColor="64, 64, 64, 64" IsLabelAutoFit="False"
                    Interval="1">
         <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
         <MajorGrid LineColor="64, 64, 64, 64" />
      </AxisX>
    </asp:ChartArea>
</ChartAreas>
5. In the example i put 3 chart controls and changed the chart type and chart area .

  Eg:


6. Add script manager and timer control in the page for random chart values.
7. Add the following code in .cs file.

Namespaces:
Winforms:
using System.Windows.Forms.DataVisualization.Charting;

Webforms:
using System.Web.UI.DataVisualization.Charting;

Code:

protected void Page_Load(object sender, EventArgs e)
    {
      Series series1 = Chart1.Series[0];
      Series series2 = Chart2.Series[0];
      Series series3 = Chart3.Series[0];

      // Set series tool tips
      series1.ToolTip = "X value \t= #VALX \nY value \t= #VALY ";
      series2.ToolTip = "X value \t= #VALX \nY value \t= #VALY ";
      series3.ToolTip = "X value \t= #VALX \nY value \t= #VALY ";

      Random ra = new Random();
      string[] Months = { "Jan", "Feb", "Mar", "Apr", "May",
            "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

      foreach (string dRow in Months)
      {
         int ran = ra.Next(500) + 10;
         Chart1.Series[0].Points.AddXY(dRow, ran);

         ran = ra.Next(1000) + 10;
         Chart2.Series[0].Points.AddXY(dRow, ran);

         ran = ra.Next(1500) + 10;
         Chart3.Series[0].Points.AddXY(dRow, ran);
      }
  }
   
protected void Timer1_Tick(object sender, EventArgs e)
    {
        Random rand = new Random();
        string[] Months = { "Jan", "Feb", "Mar", "Apr", "May",
             "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

        // Add several random point into each series
        foreach (Series series in this.Chart1.Series)
        {
            double lastYValue = 0;
            if (series.Points.Count > 0)
            {
                lastYValue = series.Points[series.Points.Count].YValues[0];
            }
            for (int pointIndex = 0; pointIndex < 11; pointIndex++)
            {
                lastYValue += rand.Next(-3, 4);
                if (lastYValue >= 100.0)
                {
                    lastYValue -= 25.0;
                }
                else if (lastYValue <= 10.0)
                {
                    lastYValue += 25.0;
                }
                series.Points.AddXY(Months[pointIndex].ToString(), lastYValue);
            }
        }

        foreach (Series series in this.Chart2.Series)
        {
            double lastYValue = 0;
            if (series.Points.Count > 0)
            {
                lastYValue = series.Points[series.Points.Count].YValues[0];
            }
            for (int pointIndex = 0; pointIndex < 11; pointIndex++)
            {
                lastYValue += rand.Next(-3, 4);
                if (lastYValue >= 100.0)
                {
                    lastYValue -= 25.0;
                }
                else if (lastYValue <= 10.0)
                {
                    lastYValue += 25.0;
                }
                series.Points.AddXY(Months[pointIndex].ToString(), lastYValue);
            }
        }

        foreach (Series series in this.Chart3.Series)
        {
            double lastYValue = 0;
            if (series.Points.Count > 0)
            {
                lastYValue = series.Points[series.Points.Count].YValues[0];
            }
            for (int pointIndex = 0; pointIndex < 11; pointIndex++)
            {
                lastYValue += rand.Next(-3, 4);
                if (lastYValue >= 100.0)
                {
                    lastYValue -= 25.0;
                }
                else if (lastYValue <= 10.0)
                {
                    lastYValue += 25.0;
                }
                series.Points.AddXY(Months[pointIndex].ToString(), lastYValue);
            }
        }
    }

Output:

 











Comments

Popular posts from this blog

SQL - Reporting Server - Part III

Display Reports in Web Application Open Visual studio and create a new Web Application. Add Microsoft Report Viewer control. Specify the ReportServerURL and ReportPath in report properties. Run the application and see the output in browser.

File System Watcher

The Following is the example for File System Watcher. Create Console Application in Visual Studio. File --> New --> Project In the Template Select the Console Application. Eg: Namespaces Used : using System.Security.Permissions; using System.IO; These two namespaces used for file operation and file permissions. Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Permissions; using System.IO; namespace Watcher {     public class Watcher     {          public static void Main()         {             Run();         }         [ PermissionSet ( SecurityAction .Demand, Name = "FullTrust" )]         public static void Run()         {             // Create a new FileSystemWatcher and set its properties.             FileSystemWatcher watcher = new FileSystemWatcher ();             watcher.Path = @"D:\P