Skip to content

Data Labels Plugin

Display labels directly on chart elements using chartjs-plugin-datalabels.

Registration

Add to .vitepress/theme/index.ts:

typescript
import DefaultTheme from 'vitepress/theme'

export default {
  extends: DefaultTheme,
  async enhanceApp({ app }) {
    if (typeof window !== 'undefined') {
      const { Chart, registerables } = await import('chart.js')
      Chart.register(...registerables)
      
      const datalabelsPlugin = (await import('chartjs-plugin-datalabels')).default
      Chart.register(datalabelsPlugin)
    }
  }
}

Installation

bash
npm install chartjs-plugin-datalabels

Basic Labels

Loading chart...

Code:

yaml
```chart
type: bar
data:
  labels: [Jan, Feb, Mar, Apr, May]
  datasets:
    - label: Sales
      data: [65, 59, 80, 81, 56]
options:
  plugins:
    datalabels:
      display: true
      color: white
      font:
        weight: bold
```

Labels with Formatting

Loading chart...

Code:

yaml
```chart
type: bar
data:
  labels: [Q1, Q2, Q3, Q4]
  datasets:
    - label: Revenue ($K)
      data: [150, 200, 180, 250]
options:
  plugins:
    datalabels:
      display: true
      color: white
      font:
        weight: bold
        size: 14
      anchor: center
      align: center
```

Pie Chart Labels

Loading chart...

Code:

yaml
```chart
type: pie
data:
  labels: [Red, Blue, Yellow, Green]
  datasets:
    - data: [30, 25, 25, 20]
options:
  plugins:
    datalabels:
      display: true
      color: white
      font:
        weight: bold
        size: 14
```

Label Positioning

Loading chart...

Code:

yaml
```chart
type: bar
data:
  labels: [A, B, C, D, E]
  datasets:
    - label: Values
      data: [40, 60, 80, 70, 50]
options:
  plugins:
    datalabels:
      display: true
      anchor: end
      align: top
      offset: 4
      color: black
      font:
        weight: bold
```

Line Chart Labels

Loading chart...

Code:

yaml
```chart
type: line
data:
  labels: [Mon, Tue, Wed, Thu, Fri]
  datasets:
    - label: Orders
      data: [12, 19, 15, 25, 22]
      tension: 0.3
options:
  plugins:
    datalabels:
      display: true
      backgroundColor: rgba(54, 162, 235, 0.8)
      borderRadius: 4
      color: white
      padding: 4
```

Doughnut with Percentage Labels

Loading chart...

Code:

yaml
```chart
type: doughnut
data:
  labels: [Chrome, Firefox, Safari, Edge, Other]
  datasets:
    - data: [65, 15, 10, 7, 3]
options:
  plugins:
    datalabels:
      display: true
      color: white
      font:
        weight: bold
```

Horizontal Bar with Labels

Loading chart...

Code:

yaml
```chart
type: bar
data:
  labels: [Product A, Product B, Product C, Product D]
  datasets:
    - label: Sales
      data: [120, 190, 150, 80]
options:
  indexAxis: y
  plugins:
    datalabels:
      display: true
      anchor: end
      align: right
      offset: 4
```

Per-Dataset Configuration

You can configure datalabels differently for each dataset:

Loading chart...

Code:

yaml
```chart
type: bar
data:
  labels: [Q1, Q2, Q3, Q4]
  datasets:
    - label: 2023
      data: [50, 60, 70, 80]
      datalabels:
        color: white
        anchor: center
    - label: 2024
      data: [55, 70, 85, 95]
      datalabels:
        color: black
        anchor: end
        align: top
options:
  plugins:
    datalabels:
      display: true
```

Configuration Levels

Plugin options can be set at 3 different levels (evaluated with this priority):

  1. Per dataset: dataset.datalabels.* (highest priority)
  2. Per chart: options.plugins.datalabels.*
  3. Globally: Via VitePress config (lowest priority)
yaml
data:
  datasets:
    - label: Dataset 1
      data: [10, 20, 30]
      datalabels:           # Per-dataset options (highest priority)
        color: '#FFCE56'
options:
  plugins:
    datalabels:             # Per-chart options
      color: '#36A2EB'

Configuration Reference

Basic Options

OptionTypeDefaultDescription
displayboolean/stringtrueShow labels: true, false, or 'auto'
colorstring'#666'Label text color
backgroundColorstringnullLabel background color
borderColorstringnullLabel border color
borderRadiusnumber0Border corner radius
borderWidthnumber0Border width
paddingnumber/object4Label padding
opacitynumber1Label opacity (0-1)

Font Options

OptionTypeDefaultDescription
font.familystring'sans-serif'Font family
font.sizenumber12Font size in pixels
font.stylestring'normal'Font style: 'normal', 'italic', 'oblique'
font.weightstring/number'normal'Font weight: 'normal', 'bold', or number
font.lineHeightnumber1.2Line height

Positioning Options

OptionTypeDefaultDescription
anchorstring'center'Anchor point: 'start', 'center', 'end'
alignstring'center'Alignment: 'start', 'end', 'center', 'top', 'bottom', 'left', 'right'
offsetnumber4Distance from anchor point in pixels
rotationnumber0Label rotation in degrees
clampbooleanfalseClamp label to chart area
clipbooleanfalseClip label to chart area

Anchor & Align Visualization

                  align: 'top'

                      |
align: 'left' ← [ anchor ] → align: 'right'
                      |

                 align: 'bottom'

Enabling Globally

In your VitePress config:

ts
// .vitepress/config.mts
import { withChartjs } from 'vitepress-plugin-chartjs'

export default withChartjs(
  defineConfig({
    chartjs: {
      enableDatalabels: true,  // Enable for all charts
    },
  })
)

Or per-chart:

yaml
options:
  plugins:
    datalabels:
      display: true
      # ... other options

To disable for a specific chart when globally enabled:

yaml
options:
  plugins:
    datalabels: false

Resources

Released under the MIT License