Click here to Skip to main content
12,401,832 members (54,431 online)
Click here to Skip to main content

Stats

57K views
2.4K downloads
68 bookmarked
Posted

Improve ASP.NET Chart with jQuery

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

Share

About the Author

Malphas1981
Software Developer
Italy Italy
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 13 Jul 2009
Article Copyright 2009 by Malphas1981
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid