码迷,mamicode.com
首页 > Web开发 > 详细

chartjs显示数值标签插件:chartjs-plugin-datalabels

时间:2019-09-30 09:44:06      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:conf   url   http   --   default   www   tps   ota   extra   

 

Getting Started

#Installation

#npm

技术图片 技术图片

npm install chartjs-plugin-datalabels --save

This plugin can also be installed using Bower.

#CDN

技术图片 技术图片

By default, https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels returns the latest (minified) version, however it‘s highly recommended to always specify a version in order to avoid breaking changes. This can be achieved by appending @{version} to the url:

https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@1.1.2    // exact version
https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@1        // latest 1.x.x

Read more about jsDeliver versioning on their website.

#Download

技术图片 技术图片

You can download the latest version of chartjs-plugin-datalabels from the GitHub releases:

  • chartjs-plugin-datalabels.js (recommended for development)
  • chartjs-plugin-datalabels.min.js (recommended for production)
  • chartjs-plugin-datalabels.zip (contains .js and .min.js versions + samples)

#Integration

#HTML

<script src="https://cdn.jsdelivr.net/npm/chart.js@2.7.3/dist/Chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.7.0"></script>

IMPORTANT

chartjs-plugin-datalabel must be loaded after the Chart.js library!

Once imported, the plugin is available under the global property ChartDataLabels. This is useful to register/unregister the plugin afterward.

#Module

import Chart from ‘chart.js‘;
import ChartDataLabels from ‘chartjs-plugin-datalabels‘;

#Registration

This plugin registers itself globally, meaning that once imported, all charts will display labels. In case you want it enabled only for a few charts, you first need to unregister it globally:

// NOTE: when imported as a <script> tag, use the global property ‘ChartDataLabels‘
Chart.plugins.unregister(ChartDataLabels);

Then, you can enabled the plugin only for specific charts:

var chart = new Chart(ctx, {
    plugins: [ChartDataLabels],
    options: {
        // ...
    }
})

See also Chart.js › Using plugins.

DEPRECATION

From version 1.x, this plugin will no longer be registered automatically (see #42 for details).

#Configuration

The plugin options can be changed at 3 different levels and are evaluated with the following priority:

  • per dataset: dataset.datalabels.*
  • per chart: options.plugins.datalabels.*
  • or globally: Chart.defaults.global.plugins.datalabels.*

For example:

// Change default options for ALL charts
Chart.helpers.merge(Chart.defaults.global.plugins.datalabels, {
    color: ‘#FE777B‘
});

var chart = new Chart(ctx, {
    options: {
        plugins: {
            // Change options for ALL labels of THIS CHART
            datalabels: {
                color: ‘#36A2EB‘
            }
        }
    },
    data: {
        datasets: [{
            // Change options only for labels of THIS DATASET
            datalabels: {
                color: ‘#FFCE56‘
            }
        }]
    }
});

chartjs显示数值标签插件:chartjs-plugin-datalabels

标签:conf   url   http   --   default   www   tps   ota   extra   

原文地址:https://www.cnblogs.com/xbzhu/p/11610796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!