Chart.js

Chart.js

创建Chart.js

//一个demo
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图表第一种配置方式</title>
    <script src="Chart.bundle.js"></script>
</head>
<body>
<canvas id="myChart"></canvas>
<script>
    var ctx = document.getElementById("myChart").getContext("2d");

    // 设置数据内容
    var data = {
        // 设置图表中水平轴显示的内容
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        // 设置图表中的数据
        datasets: [
            {
                // 当前图表数据的标题内容
                label: "My First dataset",
                // 是否填充折线与水平轴中间的区域
                fill: true,
                // 设置折线数据点的贝塞尔曲线值(值为0时为折线)
                lineTension: 0.1,
                // 设置背景颜色
                backgroundColor: "rgba(75,192,192,0.4)",
                // 设置边框颜色
                borderColor: "rgba(75,192,192,1)",
                // 设置折线端点的样式
                borderCapStyle: 'butt',
                // 破折号的长度和间距
                borderDash: [],
                // 破折号的偏移量
                borderDashOffset: 0.0,
                // 设置折线交点的样式
                borderJoinStyle: 'miter',
                // 设置折线数据点的边框颜色
                pointBorderColor: "rgba(75,192,192,1)",
                // 设置折线数据点的背景颜色
                pointBackgroundColor: "#fff",
                // 设置折线数据点的边框宽度
                pointBorderWidth: 1,
                // 设置当鼠标悬停折线数据点时的半径
                pointHoverRadius: 5,
                // 设置当鼠标悬停折线数据点时的背景颜色
                pointHoverBackgroundColor: "rgba(75,192,192,1)",
                // 设置当鼠标悬停折线数据点时的边框颜色
                pointHoverBorderColor: "rgba(220,220,220,1)",
                // 设置当鼠标悬停折线数据点时的边框宽度
                pointHoverBorderWidth: 2,
                // 设置折线数据点的半径
                pointRadius: 1,
                //
                pointHitRadius: 10,
                // 设置折线中所有的数据
                data: [65, 59, 80, 81, 56, 55, 40],
                // 如果设置为true,将在没有数据或空数据的点之间绘制直线
                spanGaps: false,
            }
        ]
    };

    var options = {
        title: {
            display: true,
            text: 'Custom Chart Title'
        }
    }

    /*
        创建图表对象
        1. 第一种方式 - 新版本提供
         var myLineChart = new Chart(ctx, {
             type: 'line',
             data: data,
             options: options
         });
        2. 第二种方式 - 旧版本支持
         var myLineChart = Chart.Line(ctx, {
             data: data,
             options: options
         });
     */
    var myLineChart = Chart.Line(ctx, {
        data: data,
        options: options
    });
</script>
</body>
</html>

创建折线图

new Chart(document.getElementById("canvas"),{
    "type":"line",    //图表类型
    "data":{
        "labels":["January","February","March","April","May","June","July"],    //X轴
        "datasets":[
            {"label":"My First Dataset",    //标题
            "data":[65,59,80,81,56,55,40],    //与labels对应
            "fill":false,    //是否填充
            "borderColor":"rgb(75, 192, 192)",
            "lineTension":0.3}    //贝塞尔曲线效果
        ]
    },
    "options":{}
});

创建柱状图

var ctx = document.getElementById('canvas');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});

雷达图

new Chart(document.getElementById("canvas"),{
    "type":"radar",
    "data":{
        "labels":[
            "Eating","Drinking","Sleeping","Designing","Coding","Cycling","Running"
        ],
        "datasets":[
            {
                "label":"My First Dataset","data":[65,59,90,81,56,55,40],
                "fill":true,
                "backgroundColor":"rgba(255, 99, 132, 0.2)",
                "borderColor":"rgb(255, 99, 132)",
                "pointBackgroundColor":"rgb(255, 99, 132)",
                "pointBorderColor":"#fff","pointHoverBackgroundColor":"#fff",
                "pointHoverBorderColor":"rgb(255, 99, 132)"
            },
            {
                "label":"My Second Dataset","data":[28,48,40,19,96,27,100],
                "fill":true,
                "backgroundColor":"rgba(54, 162, 235, 0.2)",
                "borderColor":"rgb(54, 162, 235)",
                "pointBackgroundColor":"rgb(54, 162, 235)",
                "pointBorderColor":"#fff",
                "pointHoverBackgroundColor":"#fff",
                "pointHoverBorderColor":"rgb(54, 162, 235)"}]
    },
    "options":{
        "elements":{
            "line":{"tension":0,"borderWidth":3}
        }
    }
});

极地区域图

var ctx = document.getElementById('canvas');
var data = {
   datasets: [{
       data: [
           11,16,7,3,14
       ],
       backgroundColor: [
           "#FF6384",
           "#4BC0C0",
       ],
       label: 'My dataset' // for legend
   }],
   labels: [
       "Red",
       "Green",
       "Yellow",
       "Grey",
       "Blue"
   ]
};
var options = {};
var myPolarAreaChart = new Chart(ctx, {
   type: 'polarArea',
   data: data,
   options: options
});

饼状图与环形图

var ctx = document.getElementById('canvas');
var data = {
    labels: [
        "Red",
        "Blue",
        "Yellow"
    ],
    datasets: [
        {
            data: [300, 50, 100],
            backgroundColor: [
                "#FF6384",
                "#36A2EB",
                "#FFCE56"
            ],
            hoverBackgroundColor: [
                "#FF6384",
                "#36A2EB",
                "#FFCE56"
            ]
        }]
};
var options = {};
//饼状图
var myPieChart = new Chart(ctx,{
    type: 'pie',
    data: data,
    options: options
});
//环形图
var myDoughnutChart = new Chart(ctx, {
    type: 'doughnut',
    data: data,
    options: options
});

气泡图

var ctx = document.getElementById("myChart").getContext("2d");
var data = {
    datasets: [
        {
            label: 'First Dataset',
            data: [
                {
                    x: 20,
                    y: 30,
                    r: 15
                },
                {
                    x: 40,
                    y: 10,
                    r: 10
                }
            ],
            backgroundColor:"#FF6384",
            hoverBackgroundColor: "#FF6384",
        }]
};
var options = {};
var myBubbleChart = new Chart(ctx,{
    type: 'bubble',
    data: data,
    options: options
});

设置图表的位置和大小

将canvas用div包裹起来,设置宽高,使用absolute来固定位置

配置

全局配置
Chart.defaults.global.title.display = true;
Chart.defaults.global.title.text = 'Custom Chart Title';

混合型图表

var ctx = document.getElementById("myChart").getContext("2d");

var myChart = new Chart(ctx, {
    type: 'bar',      //必须设为bar
    data: {
        labels: ['Item 1', 'Item 2', 'Item 3'],
        datasets: [
            {
                type: 'bar',      //要增加type属性
                label: 'Bar Component',
                data: [10, 20, 30],
            },
            {
                type: 'line',
                label: 'Line Component',
                data: [30, 20, 10],
            }
        ]
    }
});

Chartist.js

HighChart.js

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容