Example code for generating bar chart (Bar Chart) effects using html5, html5 bar chart

Source: Internet
Author: User

Example code for generating bar chart (Bar Chart) effects using html5, html5 bar chart
Copy XML/HTML Code to clipboard

  1. <Html>
  2. <Canvas id = "a_canvas" width = "1000" height = "700"> </canvas>
  3. <Script>
  4. (Function (){
  5. Window. addEventListener ("load", function (){
  6. Var data = [,];
  7. Var xinforma = ['August 11', 'August 11', 'August 11', 'August 11', 'August 11', 'August 11', 'August 30', 'August 30 ', 'August 11', 'August 11', 'August 11'];
  8. // Obtain the context
  9. Var a_canvas = document. getElementById ('A _ canvas ');
  10. Var context = a_canvas.getContext ("2d ");
  11. // Draw the background
  12. Var gradient = context. createLinearGradient (0,300 );
  13. // Gradient. addColorStop (0, "# e0e0e0 ");
  14. // Gradient. addColorStop (1, "# ffffff ");
  15. Context. fillStyle = gradient;
  16. Context. fillRect (0, 0, a_canvas.width, a_canvas.height );
  17. Var realheight = a_canvas.height-15;
  18. Var realwidth = a_canvas.width-40;
  19. // Draw the border
  20. Var grid_cols = data. length + 1;
  21. Var grid_rows = 4;
  22. Var cell_height = realheight/grid_rows;
  23. Var cell_width = realwidth/grid_cols;
  24. Context. lineWidth = 1;
  25. Context. strokeStyle = "# a0a0a0 ";
  26. // End Border description
  27. Context. beginPath ();
  28. // Prepare to draw a horizontal line
  29. /* For (var row = 1; row <= grid_rows; row ++ ){
  30. Var y = row * cell_height;
  31. Context. moveTo (0, y );
  32. Context. lineTo (a_canvas.width, y );
  33. }*/
  34. // Draw a line
  35. Context. moveTo (0, realheight );
  36. Context. lineTo (realwidth, realheight );
  37. // Draw a vertical line
  38. Context. moveTo (0, 20 );
  39. Context. lineTo (0, realheight );
  40. Context. lineWidth = 1;
  41. Context. strokeStyle = "black ";
  42. Context. stroke ();
  43. Var max_v = 0;
  44. For (var I = 0; I <data. length; I ++ ){
  45. If (data [I]> max_v) {max_v = data [I]};
  46. }
  47. Max_vmax_v = max_v * 1.1;
  48. // Convert the data into coordinates
  49. Var points = [];
  50. For (var I = 0; I <data. length; I ++ ){
  51. Var v = data [I];
  52. Var px = cell_width * (I + 1 );
  53. Var py = realheight-realheight * (v/max_v );
  54. // Alert (py );
  55. Points. push ({"x": px, "y": py });
  56. }
  57. // Draw the coordinate chart
  58. For (var I in points ){
  59. Var p = points [I];
  60. Context. beginPath ();
  61. Context. fillStyle = "green ";
  62. Context. fillRect (p. x, p. y, 15, realheight-p.y );
  63. Context. fill ();
  64. }
  65. // Add text
  66. For (var I in points)
  67. {Var p = points [I];
  68. Context. beginPath ();
  69. Context. fillStyle = "black ";
  70. Context. fillText (data [I], p. x + 1, p. y-15 );
  71. Context. fillText (xinforma [I], p. x + 1, realheight + 12 );
  72. Context. fillText ('month', realwidth, realheight + 12 );
  73. Context. fillText ('amount of funds ', 0, 10 );
  74. }
  75. }, False );
  76. })();
  77. </Script>
  78. </Html>

Code for generating a bar chart (Bar Chart) using html5

Running result:

The example code for generating bar chart (Bar Chart) Effects in html5 is all the content shared by Alibaba Cloud. I hope you can provide a reference and support for the customer's house.

Address: http://www.cnblogs.com/shuniuniu/p/5318666.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.