HTML5 generate bar chart (bar chart) effect instance code

Time:2021-7-20
XML/HTML CodeCopy content to clipboard
  1. <html>    
  2. <canvas id=“a_canvas” width=“1000” height=“700”></canvas>    
  3. <script>    
  4.     (function (){    
  5.      
  6.         window.addEventListener(“load”, function(){    
  7.      
  8.           var data = [1000,1300,2000,3000,2000,2000,1000,1500,2000,5000,1000,1000];    
  9.           var xinforma=  [‘ January ‘,’ February ‘,’ March ‘,’ April ‘,’ may ‘,’ June ‘,’ July ‘,’ August ‘,’ September ‘,’ October ‘,’ November ‘,’ December ‘];
  10.      
  11. //   Get context
  12.           var a_canvas = document.getElementById(‘a_canvas’);    
  13.           var context = a_canvas.getContext(“2d”);    
  14.      
  15.      
  16. //   Draw background
  17.           var gradient = context.createLinearGradient(0,0,0,300);    
  18.      
  19.      
  20.          // gradient.addColorStop(0,”#e0e0e0″);    
  21.           //gradient.addColorStop(1,”#ffffff”);    
  22.      
  23.      
  24.           context.fillStyle = gradient;    
  25.      
  26.           context.fillRect(0,0,a_canvas.width,a_canvas.height);    
  27.      
  28.           var realheight = a_canvas.height-15;    
  29.           var realwidth = a_canvas.width-40;    
  30. //   Draw border
  31.           var grid_cols = data.length + 1;    
  32.           var grid_rows = 4;    
  33.           var cell_height = realheight / grid_rows;    
  34.           var cell_width = realwidth / grid_cols;    
  35.           context.lineWidth = 1;    
  36.           context.strokeStyle = “#a0a0a0”;    
  37.      
  38. //   End border drawing
  39.           context.beginPath();    
  40. //   Ready to draw the line
  41.           /*for(var row = 1; row <= grid_rows; row++){    
  42.             var y = row * cell_height;    
  43.             context.moveTo(0,y);    
  44.             context.lineTo(a_canvas.width, y);    
  45.           }*/    
  46.               
  47. //Draw a line
  48.             context.moveTo(0,realheight);    
  49.             context.lineTo(realwidth,realheight);    
  50.                     
  51.                  
  52. //Draw a vertical line
  53.           context.moveTo(0,20);    
  54.            context.lineTo(0,realheight);    
  55.           context.lineWidth = 1;    
  56.           context.strokeStyle = “black”;    
  57.           context.stroke();    
  58.                   
  59.      
  60.           var max_v =0;    
  61.               
  62.           for(var i = 0; i<data.length; i++){    
  63.             if (data[i] > max_v) { max_v =data[i]};    
  64.           }    
  65.           max_vmax_v = max_v * 1.1;    
  66. //   Convert data to coordinates
  67.           var points = [];    
  68.           for( var i=0; i < data.length; i++){    
  69.             var vdata[i];    
  70.             var px = cell_width * (i +1);    
  71.             var py = realheight – realheight*(v / max_v);    
  72.             //alert(py);    
  73.             points.push({“x”:px,”y”:py});    
  74.           }    
  75.      
  76. //Drawing coordinate graphics
  77.           for(var i in points){    
  78.             var p = points[i];    
  79.             context.beginPath();    
  80.             context.fillStyle=“green”;    
  81.             context.fillRect(p.x,p.y,15,realheight-p.y);    
  82.                  
  83.             context.fill();    
  84.           }    
  85. //Add text
  86.           for(var i in points)    
  87.           {  var p = points[i];    
  88.             context.beginPath();    
  89.             context.fillStyle=“black”;    
  90.             context.fillText(data[i], p.x + 1, p.y – 15);    
  91.              context.fillText(xinforma[i],p.x + 1,realheight+12);    
  92. Context. Filltext (‘month ‘, realwidth, realheight + 12’);
  93. Context. Filltext (‘amount of funds’, 0,10);
  94.               }    
  95.         },false);    
  96.       })();    
  97.            
  98. </script>    
  99. </html>  

HTML5 generate bar chart (bar chart) detailed code

Results of operation:

The above HTML5 generated bar chart (bar chart) effect example code is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developer more.

Original address:http://www.cnblogs.com/shuniuniu/p/5318666.html

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]