JS Chart Plugin Chart.js

Source: Internet
Author: User

Chart is a pure JS plug-in, it is powerful and small to use is very simple.

The first step is to introduce chart.js.

<script type= "Text/javascript" src= "<%=path%>/js/mapping/chart.js" ></script>

The second draw control style can be defined by itself but the ID cannot be changed

<canvas id= "Canvas" height= "width=" ></canvas>

The third step configuration parameter//If it is multiple data comparison, copy multiple datasets separated by commas but the value of datasets data to pass a different

var linechartdata = {
Labels:monthyear,//labels is the x-axis parameter, such as 201601,201602, is a JS array. Remember that it must be a JS string array
Datasets: [
{
Label: "My first DataSet",
FillColor: "Rgba (220,220,220,0.2)",
Strokecolor: "Rgba (220,220,220,1)",
Pointcolor: "Rgba (220,220,220,1)",
Pointstrokecolor: "#fff",
Pointhighlightfill: "#fff",
Pointhighlightstroke: "Rgba (220,220,220,1)",
data:usercnt2//This is the y-axis parameter is the exact value, say 201602 months. Production value is 30,000 pieces this is a JS array of values
}
]
};

Fourth Step Call function

Graph
function Diagram () {
var ctx = document.getElementById ("Canvas"). GetContext ("2d");
Window.myline = new Chart (CTX). Line (Linechartdata, {
Responsive:true
});
};

Bar chart
function Barchart () {
var ctx = document.getElementById ("Canvas"). GetContext ("2d");
Window.myline = new Chart (CTX). Bar (Linechartdata, {
Responsive:true
});
};

Which chart do you need to call directly

Bar chart

Barchart ();//This will do it.

In addition to the Java list assignment of a JS array method, I loop list the fields I need to stitch my string comma separated and then uploaded to the page

var month= ' ${month} ';//month
var monthyear=month.split (",");//Date string array
var cnt= ' ${cnt} ';
var usercnt=cnt.split (",");//Number of strings array
var usercnt2=new Array (); ;//Declare a blank array
for (var i = 0; i< usercnt.length; i++) {
Usercnt2[i]=parseint (Usercnt[i]);//assignment to an array of values
}

JS Chart Plugin chart.js

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.