Linear Area Chart tutorial Based on HTML5 Canvas, html5canvas
We have seen many web charts implemented with jQuery before, some of which are more practical. Today, we will introduce a linear area chart application based on HTML5 Canvas. This chart application allows you to display multiple groups of data at the same time and display the data results in a linear chart, the areas formed between data are represented in different colors. For details, refer to the DEMO below.
You can also view online demos here
Next we will briefly introduce the process and source code for implementing this HTML5 chart.
HTML code:
<canvas id="cvs" width="600" height="250">[No canvas support]</canvas>
A simple canvas label, our chart is drawn on the canvas.
Since this chart is based on RGraph, we also need to reference the relevant js scripts of RGraph and the jquery Class Library:
<script src="../libraries/RGraph.common.core.js" ></script><script src="../libraries/RGraph.common.effects.js" ></script><script src="../libraries/RGraph.common.dynamic.js" ></script><script src="../libraries/RGraph.common.tooltips.js" ></script><script src="../libraries/RGraph.drawing.poly.js" ></script><script src="../libraries/RGraph.line.js" ></script><script src="../libraries/jquery.min.js" ></script>
Finally, Javascript code:
var d1 = []; var d2 = []; var val = 47; // Create the data for (var i=0; i<100; i+=1) d1[i] = RGraph.random(45,55); for (var i=0; i<100; i+=1) d2[i] = RGraph.random(25,35); var line = new RGraph.Line('cvs', d1, d2) .Set('background.grid.autofit.numhlines', 10) .Set('hmargin', 10) .Set('filled', true) .Set('fillstyle', 'red') .Set('filled.range', true) .Set('filled.range.threshold',40) .Set('filled.range.threshold.colors', ['rgba(255,0,0,0.5)', 'rgba(0,0,255,0.5)']) .Set('labels',['Q1','Q2','Q3','Q4']) .Set('colors', ['gray', 'gray']) .Set('numxticks', 8) .Set('linewidth', 1) .Set('ymax', 60) RGraph.Effects.Line.jQuery.Trace(line); var coords = [] for (var i=0; i<(line.coords.length/2); i+=1) { coords.push(line.coords[i]) } for (var i=(line.coords.length - 1); i>=(line.coords.length/2); i-=1) { coords.push(line.coords[i]) } var poly = new RGraph.Drawing.Poly('cvs', coords) .Set('fillstyle', 'rgba(0,0,0,0)') .Set('strokestyle', 'rgba(0,0,0,0)') .Set('tooltips', ['The range chart']) .Set('highlight.fill', 'rgba(255,255,255,0.3)') .Draw();
It mainly initializes some data. RGraph is a very good HTML5 chart framework. We will continue to explain more RGraph examples in the future. Source code download>
Hello, I want to teach you an html5 canvas problem: I drew multiple images in the canvas, including images and straight lines.
Canvas can be implemented
First, canvas must respond to mouse events (such as onmousedown)
Then, all images must create corresponding objects to record their locations and sizes, and zOrder (stacked positions, which determine who is on top when two objects overlap ), put the corresponding objects in an array and sort by zOrder
After the canvas mouse click event is triggered, the zOrder sequence is used to check that the mouse coordinates are not in the Area of an object. If the mouse coordinates are in the Area of an object, the corresponding function is executed.
How to Use html5 canvas to create a small graph example
<! Doctype html>