Saturday, 31 January 2015

HTML5 JavaScript Charts

"An easy to use HTML5 JavaScript Charting library built on Canvas element. Charts can render across devices including iPhone, iPad, Android, Windows Phone, Desktops, etc. Graphs include several good looking themes and are 10x faster than conventional Flash / SVG based Libraries – resulting in lightweight, beautiful and responsive dashboards". more...

Here is my little ASP.NET program to draw a dynamic chart:

(These codes explains how to access JavaScripts function from codebehind and call codebehind functions from JavaScript)

Codebehind:
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3.     // Call JavaScript function in aspx
  4.     ScriptManager.RegisterStartupScript(this, GetType(), "draw", "draw();", true);           
  5. }
  6. [WebMethod]
  7. public static string chartType()
  8. {
  9.     return "bar";
  10. }
  11. [WebMethod]
  12. public static string labels()
  13. {
  14.     return "apple,orange,banana,mango,grape";
  15. }
  16. [WebMethod]
  17. public static string values()
  18. {
  19.     return "10,20,30,40,50";
  20. }
ASPX Page:
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3.     <title></title>
  4.     <script type="text/javascript" src="scripts/canvasjs.min.js"></script>
  5.     <script type="text/javascript">
  6.         function draw() {
  7.             var t = '<%=labels()%>'; // get the values from codebhind method
  8.             var l = new Array();
  9.             l = t.split(",");
  10.             var t = '<%=values()%>';
  11.             var v = new Array();
  12.             v = t.split(",");
  13.             var data = [];
  14.             for (var x = 0; x < v.length; x++) {
  15.                 data.push({
  16.                     label: l[x],
  17.                     y: parseInt(v[x]),
  18.                 });
  19.             }
  20.             var chart = new CanvasJS.Chart("chartContainer", {
  21.                 theme: "theme1",//theme1
  22.                 title: {
  23.                     text: "Basic Column Chart - CanvasJS"
  24.                 },
  25.                 animationEnabled: false,   // change to true
  26.                 data: [
  27.                 {
  28.                     // Change type to "bar", "splineArea", "area", "spline", "pie",etc.
  29.                     type: '<%=chartType()%>',
  30.                     dataPoints: data
  31.                 }
  32.                 ]
  33.             });
  34.             chart.render();
  35.         }
  36.     </script>
  37.     <script type="text/javascript" src="/assets/script/canvasjs.min.js"></script>
  38. </head>
  39. <body>
  40.     <form id="form1" runat="server">
  41.     <div>
  42.         <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server" />
  43.         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
  44.     </div>
  45.         <div id="chartContainer" style="height: 300px; width: 100%;"></div>
  46.     </form>
  47. </body>
  48. </html>

No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...