Click here to Skip to main content
15,867,835 members
Articles / Web Development / ASP.NET

Improve ASP.NET Chart with jQuery

Rate me:
Please Sign up or sign in to vote.
4.76/5 (16 votes)
13 Jul 2009CPOL3 min read 79.3K   2.6K   67  
A simple example of how to use the ASP.NET charting control with jQuery.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Collections;
using System.Data;

namespace WebSamples
{
   public class RuntimeChart{
      private Chart m_chart;

      public RuntimeChart() {
         m_chart = new Chart();
      }

      public Chart makeChart(Int32 iType){
         //Chart setting 
         m_chart.Height = Unit.Pixel(200);
         m_chart.Width = Unit.Pixel(400);
         m_chart.BorderlineColor = Color.Black;
         m_chart.BorderlineDashStyle = ChartDashStyle.Solid;
         m_chart.BorderlineWidth = 2;
         m_chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;


         //Chart Area
         ChartArea mainArea = new ChartArea();
         mainArea.Name = "mainArea";
         mainArea.BackColor = Color.FromArgb(255, 255, 192);
         mainArea.BackGradientStyle = GradientStyle.TopBottom;
         mainArea.BorderDashStyle = ChartDashStyle.Solid;
         m_chart.ChartAreas.Add(mainArea);

         //Legend
         Legend mainLegend = new Legend();
         mainLegend.Name = "mainLegend";
         mainLegend.DockedToChartArea = "mainArea";
         mainLegend.Docking = Docking.Top;
         mainLegend.HeaderSeparator = LegendSeparatorStyle.Line;
         mainLegend.IsDockedInsideChartArea = false;
         m_chart.Legends.Add(mainLegend);

         Series sr = new Series();
         sr.ChartArea = "mainArea";
         sr.Legend = "mainLegend";
         sr.ChartType = SeriesChartType.Spline;
         sr.Name = "Series1";

         //generate some point for the chart
         for (Int32 i = 0; i < 10; i++)
         {
            Double x = (Double)i;

            //value of the y depend on parameter iType
            switch (iType) {
               case 1: {
                  sr.Points.AddXY(x, Math.Sin(x));
               } break;
               case 2:{
                  sr.Points.AddXY(x, Math.Cos(x));
                  } break;
               case 3:{
                  sr.Points.AddXY(x, Math.Tan(x));
                  } break;
            }
            
         }
         m_chart.Series.Add(sr);

         return m_chart;
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions