Chart.js

前言

1、Chart.js

  • Chart.js 是可以画各种图表的一款前端工具。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [{
    label: '示例',
    data: [12, 19, 3, 5, 2, 3],
    borderColor: 'blue',
    borderWidth: 1,
    fill: false,
    }]
    },
    options: {
    tooltips: {
    intersect: false,
    mode: 'index'
    }
    }
    });
    </script>

2、柱状图

  • 柱状图

  • 垂直

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [{
    label: '示例',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    }]
    }
    });
    </script>
  • 水平

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'horizontalBar',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [{
    label: '示例',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    }]
    }
    });
    </script>
  • 多数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [
    {
    label: '示例1',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    },
    {
    label: '示例2',
    data: [182, 51, 133, 54, 105, 96],
    borderColor: 'red',
    backgroundColor: 'pink',
    borderWidth: 1,
    },

    ]
    }
    });
    </script>
  • 多轴

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [
    {
    label: '示例1',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    yAxisID: 'y-axis-1',
    },
    {
    label: '示例2',
    data: [182, 51, 133, 54, 105, 96],
    borderColor: 'red',
    backgroundColor: 'pink',
    borderWidth: 1,
    yAxisID: 'y-axis-2',
    },

    ]
    },
    options: {
    scales: {
    yAxes: [{
    type: 'linear',
    display: true,
    position: 'left',
    id: 'y-axis-1',
    }, {
    type: 'linear',
    display: true,
    position: 'right',
    id: 'y-axis-2',
    gridLines: {
    drawOnChartArea: false
    }
    }],

    }
    }
    });
    </script>

3、曲线图

  • 曲线图

  • 单曲线

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [{
    label: '示例',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    fill: false,
    }]
    }
    });
    </script>
  • 多曲线

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [
    {
    label: '示例1',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    fill: false,

    },
    {
    label: '示例2',
    data: [182, 51, 133, 54, 105, 96],
    borderColor: 'red',
    backgroundColor: 'pink',
    borderWidth: 1,
    fill: false,
    },

    ]
    }
    });
    </script>
  • 多轴

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [
    {
    label: '示例1',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-1',
    },
    {
    label: '示例2',
    data: [182, 51, 133, 54, 105, 96],
    borderColor: 'red',
    backgroundColor: 'pink',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-2',

    },

    ]
    },
    options: {
    scales: {
    yAxes: [{
    type: 'linear',
    display: true,
    position: 'left',
    id: 'y-axis-1',
    }, {
    type: 'linear',
    display: true,
    position: 'right',
    id: 'y-axis-2',
    gridLines: {
    drawOnChartArea: false
    }
    }],

    }
    }
    });
    </script>
  • 虚线

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [
    {
    label: '示例1',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-1',
    },
    {
    label: '示例2',
    data: [182, 51, 133, 54, 105, 96],
    borderColor: 'red',
    backgroundColor: 'pink',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-2',
    borderDash: [5, 5],
    },

    ]
    },
    options: {
    scales: {
    yAxes: [{
    type: 'linear',
    display: true,
    position: 'left',
    id: 'y-axis-1',
    }, {
    type: 'linear',
    display: true,
    position: 'right',
    id: 'y-axis-2',
    gridLines: {
    drawOnChartArea: false
    }
    }],

    }
    }
    });
    </script>

4、饼状图

  • 饼状图

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'pie',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [{
    label: '示例',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'lightGray',
    backgroundColor: ['pink', 'skyblue', 'LightYellow', 'LawnGreen', 'MediumPurple', 'orange'],
    borderWidth: 1
    }]
    }
    });
    </script>

5、甜甜圈

  • 甜甜圈

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'doughnut',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [{
    label: '示例',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'lightGray',
    backgroundColor: ['pink', 'skyblue', 'LightYellow', 'LawnGreen', 'MediumPurple', 'orange'],
    borderWidth: 1
    }]
    }
    });
    </script>

6、提示信息

  • 提示信息

  • 交叉点,默认就是这样,需要把鼠标放在交叉点上才会显示提示信息。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [
    {
    label: '示例1',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-1',
    },
    {
    label: '示例2',
    data: [182, 51, 133, 54, 105, 96],
    borderColor: 'red',
    backgroundColor: 'pink',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-2',

    },

    ]
    },
    options: {
    scales: {
    yAxes: [{
    type: 'linear',
    display: true,
    position: 'left',
    id: 'y-axis-1',
    }, {
    type: 'linear',
    display: true,
    position: 'right',
    id: 'y-axis-2',
    gridLines: {
    drawOnChartArea: false
    }
    }],

    },
    tooltips: {
    intersect: true,
    },
    }
    });
    </script>
  • 非交叉点,intersect 修改成 false, 就表示鼠标移动到图表就会出现提示信息了。nearest 表示显示最近的一根线的信息。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [
    {
    label: '示例1',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-1',
    },
    {
    label: '示例2',
    data: [182, 51, 133, 54, 105, 96],
    borderColor: 'red',
    backgroundColor: 'pink',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-2',

    },

    ]
    },
    options: {
    scales: {
    yAxes: [{
    type: 'linear',
    display: true,
    position: 'left',
    id: 'y-axis-1',
    }, {
    type: 'linear',
    display: true,
    position: 'right',
    id: 'y-axis-2',
    gridLines: {
    drawOnChartArea: false
    }
    }],

    },
    tooltips: {
    mode: 'nearest',
    intersect: false,
    },
    }
    });
    </script>
  • 出现位置 中间,有时候要显示多根线的信息,并且出现在中间位置,那么就可以使用 index。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [
    {
    label: '示例1',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-1',
    },
    {
    label: '示例2',
    data: [182, 51, 133, 54, 105, 96],
    borderColor: 'red',
    backgroundColor: 'pink',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-2',

    },

    ]
    },
    options: {
    scales: {
    yAxes: [{
    type: 'linear',
    display: true,
    position: 'left',
    id: 'y-axis-1',
    }, {
    type: 'linear',
    display: true,
    position: 'right',
    id: 'y-axis-2',
    gridLines: {
    drawOnChartArea: false
    }
    }],

    },
    tooltips: {
    mode: 'index',
    intersect: false,
    },
    }
    });
    </script>
  • 回调函数,通过 callbacks 函数可以自定义提示文字里的显示信息。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    <script src="https://repo.qianchia.com/chart.js/3.7.1/chart.min.js"></script>

    <div style="width:400px;margin:0px auto">
    <canvas id="myChart"></canvas>
    </div>
    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
    datasets: [
    {
    label: '示例1',
    data: [12, 19, 3, 5, 0, 3],
    borderColor: 'blue',
    backgroundColor: 'skyBlue',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-1',
    },
    {
    label: '示例2',
    data: [182, 51, 133, 54, 105, 96],
    borderColor: 'red',
    backgroundColor: 'pink',
    borderWidth: 1,
    fill: false,
    yAxisID: 'y-axis-2',

    },

    ]
    },
    options: {
    scales: {
    yAxes: [{
    type: 'linear',
    display: true,
    position: 'left',
    id: 'y-axis-1',
    }, {
    type: 'linear',
    display: true,
    position: 'right',
    id: 'y-axis-2',
    gridLines: {
    drawOnChartArea: false
    }
    }],

    },
    tooltips: {
    mode: 'index',
    intersect: false,
    callbacks: {
    label: function (tooltipItem, myData) {
    var label = myData.datasets[tooltipItem.datasetIndex].label || '';
    if (label) {
    label += '的数值是: ';
    }
    label += parseFloat(tooltipItem.value).toFixed(2);
    return label;
    }
    },
    },
    }
    });
    </script>
文章目录
  1. 1. 前言
  2. 2. 1、Chart.js
  3. 3. 2、柱状图
  4. 4. 3、曲线图
  5. 5. 4、饼状图
  6. 6. 5、甜甜圈
  7. 7. 6、提示信息
隐藏目录