Click here to Skip to main content
15,891,424 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.6K   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.Web;
using System.Web.Services;
using System.Web.UI.DataVisualization.Charting;
using System.IO;

namespace WebSamples
{
   /// <summary>
   /// Summary description for SampleWebService
   /// </summary>
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
   [System.Web.Script.Services.ScriptService]
   public class SampleWebService : System.Web.Services.WebService
   {

      [WebMethod]
      public string HelloWorld()
      {
         return "Hello World";
      }


      /// <summary>
      /// Create a chart image and return image file name
      /// </summary>
      /// 
      ///<param name="iType"> 
      ///Type of the chart to draw: 1=Sin(x) 2=Cos(x) 3=Tan(x)
      ///</param>
      [WebMethod]
      public String DrawChart(Int32 iType){

         //class that creates the Chart object
         RuntimeChart runChart = new RuntimeChart();
         Chart m_chart = runChart.makeChart(iType);

         String tempFileName = String.Format("TempChartImage/Chart_{0}.png", System.Guid.NewGuid().ToString());

         tempFileName = Context.Server.MapPath(tempFileName);

         m_chart.SaveImage(tempFileName);
         String strImageSrc = @"TempChartImage/" + Path.GetFileName(tempFileName);

         // set callback when item was removed from cache
         ChartImageDestructor cid = new ChartImageDestructor(tempFileName);
         System.Web.Caching.CacheItemRemovedCallback onRemove = new System.Web.Caching.CacheItemRemovedCallback(cid.RemovedCallback);


         //insert filename into cache
         HttpContext.Current.Cache.Add(tempFileName, cid, null,
               DateTime.Now.AddMinutes(10),
               System.Web.Caching.Cache.NoSlidingExpiration,
               System.Web.Caching.CacheItemPriority.NotRemovable,
               onRemove);

         return strImageSrc;
      }
   }
}

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