Chart.js Integration

Last modified by sequilox on 2024/02/19 00:12

chart_pieCreate various types of charts using chart.js
Recommended
TypeXAR
CategoryMacro
Developed by

Marius Dumitru Florea, slauriere, Manuel Leduc

Active Installs41
Rating
1 Votes
LicenseGNU Lesser General Public License 2.1
Compatibility

XWiki 8.4+

Installable with the Extension Manager

Description

Create various types of charts using Chart.js.

ChartJS Macro

{{chartjs}} macro allows you to use all the features of Chart.js inside a wiki page.

Usage

{{chartjs type="..." options="{...}"}}
{
  "labels": [...],
  "datasets": [...]
}
{{/chartjs}}

Parameters

NameDescriptionMandatoryDefault
type

The type of chart. Choose from: line, bar, radar, pie, doughnut, polarArea, bubble, scatter.

Yes
title

Chart title, added as aria-label to the generated canvas

No
id

Chart identifier

No
options

Advanced options specified as JSON. Checkout the Chart.js documentation for the available configuration options. In addition to the options that can be defined in the standard ChartJS library, the following options can be used in this macro:

  • "opacity": 0.8: makes it possible to define an opacity level for all the background colors.
  • "backgroundColors": ["#ff16e8", "#0018f4","#065d00"]: for charts of type line, bar, horizontalBar or radar, this option makes it possible to define one background color per data set or one per value, withtout the need to define the colors for each data set individually.
  • "afterValue": "%": suffixes the data label values with the given sign or string (e.g. "%", "€", etc.).
  • "options.scales.xAxes[0].ticks.afterValue: "%": (same for yAxes) suffixes the data labels with the given string.
  • "skipZeros": true: instructs the macro to not display values equalling to 0.
  • "tooltips": {"maxCharactersPerLine": 50}: in case of long data labels, this option makes it possible to display the full data label in the tooltips, on several lines.
  • "tooltips": {"position": {"left": 0.5, "bottom": 0.2}}: allows to position the tooltips at a specific position of the canvas computed as the canvas' width or height multiplied by the given value (of "left" or "bottom"). If no value is provided, the "average" positioner is used.
  • Chart.js 1.3+ "localization": {"locale": "fr-FR", "options": {"minimumFractionDigit": 0, "maximumFractionDigit": 1}}: allows to localize the labels and tooltips. The options need to be declared along the ones of the JavaScript toLocaleString function. This option is experimental for now.
No
width

Width of the generated chart.

No
height

Height of the generated chart.

No
cssClass

Additional CSS class to be added to the chart container. It can be useful in particular for setting the canvas dimensions at the CSS level.

No

The content of the macro is in JSON format and matches the 'data' parameter from the Chart.js documentation. The content is mandatory.

Examples

{{chartjs type="pie"
  options="{~"backgroundColors~": [~"#ff16e8~", ~"#0018f4~",~"#065d00~"]}"}}
{
  "labels": [
    "Bug",
    "Improvement",
    "New Feature"
  ],
  "datasets": [{
    "label": "CKEditor",
    "data": [23, 17, 5]
  }, {
    "label": "Diagram",
    "data": [34, 11, 6]
  }]
}
{{/chartjs}}

pie.png

{{chartjs type="pie" title="Issues" id="issues-pie"
  options="{~"backgroundColors~": [~"#ff16e8~", ~"#0018f4~",~"#065d00~"],
            ~"legend~": {~"display~": false},
            ~"layout~": {
            ~"padding~": {
              ~"left~": 50,
              ~"right~": 50,
              ~"top~": 50,
              ~"bottom~": 50
              }
            },
            ~"plugins~": {~"datalabels~":
            {~"anchor~":~"end~", ~"align~": ~"end~"}}}"}}
{
  "labels": [
    "Bug",
    "Improvement",
    "New Feature"
  ],
  "datasets": [{
    "label": "CKEditor",
    "data": [23, 17, 5]
  }]
}
{{/chartjs}}

pie-with-labels.png

Table to ChartJS Macro

Draw charts using Chart.js and the data from a table.

Usage

(% id="myTable" %)
|=Header1|=Header2
|label|numeric value
...

{{tableToChartJS type="..." title="myChart" table="myTable" /}}

Parameters

NameDescriptionMandatoryDefault
type

The type of chart. Choose from: line, bar, radar, pie, doughnut, polarArea, bubble, scatter.

Yes
title

Chart title, added as aria-label to the generated canvas

No
id

Chart identifier

No
multipleDataSets

Whether the specified table includes multiple data sets.

Nofalse
table

Identifies the source table.

Yes
dataSetLabel

The index of the table column that represents the data set label.

No
options

Advanced chart options specified as JSON. Please refer to the documentation of the same 'option' parameter of the ChartJS macro.

No
width

Width of the generated chart.

No
height

Height of the generated chart.

No
cssClass

Additional CSS class to be added to the chart container.

No

This macro doesn't have any content.

Examples

Single Data Set

(% id="singleDataSet" %)
|=Issue Type|=Count
|Bug|23
|Improvement|17
|New Feature|5

{{tableToChartJS type="polarArea" table="singleDataSet" /}}

polarArea.png

Multiple Data Sets

(% id="multipleDataSets" %)
|=Issue Type|=CKEditor|=Diagram
|Bug|23|34
|Improvement|17|11
|New Feature|5|6

{{tableToChartJS type="doughnut" table="multipleDataSets" multipleDataSets="true" /}}

doughnut.png

(% id="multipleDataSetsLine" %)
|=Month|=2016|=2017
|January|65|61
|February|59|73
|March|80|82
|April|81|69
|May|56|70
|June|55|58
|July|40|47

{{tableToChartJS type="line" table="multipleDataSetsLine" multipleDataSets="true"/}}

line.png

(% id="multipleDataSetsRadar" %)
|=Month|=2016|=2017
|Eating|65|28
|Running|40|100
|Cycling|55|27
|Coding|56|96
|Designing|81|19
|Sleeping|90|40
|Drinking|59|48

{{tableToChartJS type="radar" table="multipleDataSetsRadar" multipleDataSets="true"/}}

radar.png

(% id="multipleDataSets" %)
|=Issue Type|=Project|=Count
|Bug|CKEditor|23
|Bug|Diagram|34
|Improvement|CKEditor|17
|Improvement|Diagram|11
|New Feature|CKEditor|5
|New Feature|Diagram|6

{{velocity}}
#set ($stackedBarOptions = {
  'scales': {
    'xAxes': [{
      'stacked': true
    }],
    'yAxes': [{
      'stacked': true
    }]
  }
})
{{tableToChartJS type="horizontalBar" table="multipleDataSets" multipleDataSets="true" dataSetLabel="1"
  options='$jsontool.serialize($stackedBarOptions)'/}}
{{/velocity}}

stackedHorizontalBar.png

Prerequisites & Installation Instructions

We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager). Note that installing Extensions when being offline is currently not supported and you'd need to use some complex manual method.

You can also use the following manual method, which is useful if this extension cannot be installed with the Extension Manager or if you're using an old version of XWiki that doesn't have the Extension Manager:

  1. Log in the wiki with a user having Administration rights
  2. Go to the Administration page and select the Import category
  3. Follow the on-screen instructions to upload the downloaded XAR
  4. Click on the uploaded XAR and follow the instructions
  5. You'll also need to install all dependent Extensions that are not already installed in your wiki

Release Notes

v1.6.3

The following translations have been updated with this release:

v1.6.2

Added Manuel Leduc to the list of developers

v1.6.1

v1.6

v1.5

v1.4

v1.3

v1.2

v1.1.6

v1.1.5

v1.1.4

v1.1.3

v1.1.2

v1.1.1

v1.1

v1.0

Dependencies

Dependencies for this extension (org.xwiki.contrib:application-chartjs-ui 1.6.3):

Tags:
    

Get Connected