小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JS組件系列——開(kāi)源免費(fèi)圖表組件:Chart.js

 WindySky 2016-05-17

前言:最近被開(kāi)源免費(fèi)得有點(diǎn)上火了,各種組件首先想到的就是是開(kāi)源否、是否免費(fèi)、是否和bootstrap風(fēng)格一致。想著以后做報(bào)表肯定要用到圖表組件的,于是在Bootstrap中文網(wǎng)上面找到了Chart.js,總的來(lái)說(shuō),這個(gè)組件不能說(shuō)最好,但是對(duì)于一般不太復(fù)雜的報(bào)表是夠用了。今天就來(lái)看看它如何使用吧。

一、組件比較以及選用

其實(shí)說(shuō)起報(bào)表組件,網(wǎng)上一搜一大把,各種讓人眼花繚亂的組件,但貌似比較出名一點(diǎn)的都是收費(fèi)的。綜合來(lái)看:

JsChart組件功能強(qiáng)大,能適應(yīng)各種復(fù)雜的需求和業(yè)務(wù);Chart.js免費(fèi)。

FunsionChart界面優(yōu)美,效果炫,用戶體驗(yàn)好;Chart.js免費(fèi)。

HighChart使用方便,調(diào)用簡(jiǎn)單,開(kāi)發(fā)效率高;Chart.js免費(fèi)。

如果你說(shuō):咱們公司不缺錢(qián),當(dāng)然是哪個(gè)最好用哪個(gè)嘍。那博主只能說(shuō):有錢(qián),任性。至少博主所在的公司是把免費(fèi)放在選用組件的第一原則的。

chart.js源碼:https://github.com/nnnick/Chart.js

chart.js 英文文檔:http://www./docs/

chart.js 中文文檔:http://www./p/chart.js/docs/

二、組件效果展示

上面扯了一大堆沒(méi)用的,先來(lái)一組效果圖看看吧。

1、柱狀圖

原始的柱狀圖

加上圖表說(shuō)明和tooltip的柱狀圖

2、餅狀圖

3、曲線圖

4、環(huán)狀圖

5、極地區(qū)域圖

6、雷達(dá)圖

三、代碼示例

關(guān)于chart.js的使用代碼其實(shí)不用多說(shuō),文檔里面很容易看懂。這里就簡(jiǎn)單展示一個(gè):

chart.js的原理是使用html5的canvas標(biāo)簽,所以首先它需要一個(gè)canvas的標(biāo)簽放在cshtml頁(yè)面

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Content/Chart.js-master/Chart.js"></script>
<canvas id="myChart" width="800" height="600"></canvas>

然后js里面(我們先以柱狀圖為例來(lái)說(shuō)明)

復(fù)制代碼
var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            fillColor: "#CCCCFF",
            strokeColor: "rgba(220,220,220,1)",
            label: "2010年",
            data: [65, 59, 90, 81, 56, 55, 40]
        },
        {
            fillColor: "#CCFFCC",
            strokeColor: "#CCFFCC",
            label:"2011年",
            data: [28, 48, 40, 19, 96, 27, 100]
        },
        {
            fillColor: "#FFFFCC",
            strokeColor: "#FFFFCC",
            label: "2012年",
            data: [13, 55, 40, 19, 23, 27, 64]
        },
        {
            fillColor: "#99FFFF",
            strokeColor: "#99FFFF",
            label: "2013年",
            data: [98, 11, 52, 19, 65, 20, 77]
        }
    ]
}

$(function () {
    var ctx = $("#myChart").get(0).getContext("2d");
    var myNewChart = new Chart(ctx);
    myNewChart.Bar(data);
});
復(fù)制代碼

如果是做業(yè)務(wù)需求,一般來(lái)說(shuō),data的數(shù)據(jù)是從后臺(tái)構(gòu)造成指定格式的json對(duì)象然后傳遞到前端。前端調(diào)用的時(shí)候只需要簡(jiǎn)單的兩句:

var ctx = $("#myChart").get(0).getContext("2d");
new Chart(ctx).Bar(data);

如果是需要修改它的默認(rèn)顯示參數(shù),則可以指定options

復(fù)制代碼
var options = {
    //Boolean - If we show the scale above the chart data      
    //是否顯示柱狀圖上面的數(shù)據(jù)
    scaleOverlay: true,

    //Boolean - If we want to override with a hard coded scale
    scaleOverride: false,

    //** Required if scaleOverride is true **
    //Number - The number of steps in a hard coded scale
    scaleSteps: null,
    //Number - The value jump in the hard coded scale
    scaleStepWidth: 50,
    //Number - The scale starting value
    scaleStartValue: null,

    //String - Colour of the scale line    
    //x/y軸坐標(biāo)線的顏色
    scaleLineColor: "rgba(0,0,0,.1)",

    //Number - Pixel width of the scale line  
    //坐標(biāo)線的寬度
    scaleLineWidth: null,

    //Boolean - Whether to show labels on the scale    
    //是否顯示label值
    scaleShowLabels: true,

    //Interpolated JS string - can access value
    scaleLabel: "<%=value%>",

    //String - Scale label font declaration for the scale label
    //字體Family
    scaleFontFamily: "'Arial'",

    //Number - Scale label font size in pixels
    //字體大小
    scaleFontSize: 12,

    //String - Scale label font weight style  
    //字體樣式
    scaleFontStyle: "normal",

    //String - Scale label font colour   
    //字體顏色
    scaleFontColor: "#666",

    ///Boolean - Whether grid lines are shown across the chart
    scaleShowGridLines: false,

    //String - Colour of the grid lines
    //網(wǎng)格線顏色
    scaleGridLineColor: "rgba(0,0,0,.05)",

    //Number - Width of the grid lines
    scaleGridLineWidth: 1,

    //Boolean - If there is a stroke on each bar    
    barShowStroke: true,

    //Number - Pixel width of the bar stroke    
    barStrokeWidth: 2,

    //Number - Spacing between each of the X value sets
    // 柱狀塊與x值所形成的線(如:x=20這條線)之間的距離
    barValueSpacing: 5,

    //Number - Spacing between data sets within X values
    // 在同一x值內(nèi)的柱狀塊之間的間距
    barDatasetSpacing: 1,

    //Boolean - Whether to animate the chart
    animation: true,

    //Number - Number of animation steps
    animationSteps: 60,

    //String - Animation easing effect
    animationEasing: "easeOutQuart",

    //Function - Fires when the animation is complete
    onAnimationComplete: function () {
        var strHtml = "";
        for (var i = 0; i < this.datasets.length; i++) {
            strHtml += "<li><div><span style='background-color:" + this.datasets[i].fillColor + ";'></span><label>" + this.datasets[i].label + "</label></div><div style='clear:both;'></div></li>";
        }
        $("#ul_type").html(strHtml);
    }
}
復(fù)制代碼

然后在調(diào)用的時(shí)候稍作修改:

$(function () {
    var ctx = $("#myChart").get(0).getContext("2d");
    var myNewChart = new Chart(ctx);
    myNewChart.Bar(data, options);
});

 這里需要說(shuō)明的一個(gè)地方是:由于使用的是chart.js 1.0.2版本,所以下圖右下角的那個(gè)說(shuō)明的塊是博主自己在onAnimationComplete這個(gè)事件里面通過(guò)js加上去的

好像新版的chart.js是自帶的這個(gè)功能。等待發(fā)布!

四、總結(jié)

至此,柱狀圖的使用就說(shuō)完了。其他圖表的用法和這個(gè)相似度達(dá)到90%,博主就不一一介紹了,待會(huì)直接上源碼。總的來(lái)說(shuō),這個(gè)組件開(kāi)源、免費(fèi),然后它非常輕量級(jí),不依賴(lài)任何的js組件(如果上面的代碼中不用jQuery,可以直接用DOM的方法去取),整個(gè)js壓縮后僅僅4.5K大小。然而由于它的原理是基于html5的,所以對(duì)瀏覽器有一定的要求,并且它對(duì)IE7、IE8有降級(jí)處理方案,詳見(jiàn)Chart.js中文文檔。附上源碼,有需要看看。

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多