Click here to Skip to main content
15,867,330 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.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebSamples._Default" %>

<!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 runat="server">
    <script type="text/javascript" src="Script/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="Script/jquery.json-1.3.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <script type="text/javascript">
       //retrive the URL of the WebService
       var WebServiceURL = "<%=Request.Url.GetLeftPart(UriPartial.Authority)+Request.ApplicationPath.ToString() %>/SampleWebService.asmx";

       //when DOM is ready...
       $(function() {
          
          //intercept the onchange event fire by element with "graphType" ID (SELECT)
          $("#graphType").change(function() {
             //get the attribute "value" of the OPTION element selected and pass it as parameter
             getChartImage($("option:selected", this).attr("value"));
          });
       });

       function getChartImage(type) {
          if (type < 0)
             return;

          //create the object for passing data to Web Service
          var dataPassed = new Object();
          dataPassed.iType = type;

          //call a Web service with jQuery
          $.ajax({
             type: "POST",
             contentType: "application/json; charset=utf-8",
             url: WebServiceURL + "/DrawChart",
             data: $.toJSON(dataPassed),
             success:
                  function(msg) {
                     var data = $.evalJSON(msg).d;
                     //set the "src" attribute of image
                     $("#ChartArea").attr("src", data);
                  },
             error:
                  function(XMLHttpRequest, textStatus, errorThrown) {
                     alert(XMLHttpRequest.responseText);
                  }
          });
       }
    </script>
    <div>
      <table>
      <tr>
      <td>
          <img id="ChartArea" />
       </td>
       <td>
          <select id="graphType">
             <option value="-1">Select type...</option>
             <option value="1">Sin (X)</option>
             <option value="2">Cos (X)</option>
             <option value="3">Tan (X)</option>
          </select>
       </td>
       </tr>
       </table>
       </div>
    </form>
</body>
</html>

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