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

本文档介绍了如何安装和使用chartjs-plugin-datalabels插件,以便在Chart.js图表中显示数值标签。插件可以通过npm、CDN或下载的方式获取,并在HTML中引入。注册后,可通过配置选项在特定图表或全局启用该插件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Getting Started

#Installation

#npm

npm npm downloads

npm install chartjs-plugin-datalabels --save

This plugin can also be installed using Bower.

#CDN

jsdelivr jsdelivr hits

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

github github downloads

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' } }] } });

转载于:https://www.cnblogs.com/xbzhu/p/11610796.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值