Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Improve ASP.NET Chart with jQuery

, 13 Jul 2009
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)

About the Author

Malphas1981
Software Developer
Italy Italy
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 13 Jul 2009
Article Copyright 2009 by Malphas1981
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid