ChartsChart components are based on Charts.js 2.7.x, an open source HTML5 based charting library.

Charts.js

To begin with, first you must install the charts.js package using npm and then include it in your project. An example with CLI would be;

  1. npm install chart.js --save
  2.  
  1. "scripts": [
  2. "../node_modules/chart.js/dist/Chart.js",
  3. //..others
  4. ],
  5.  

Chart Component

Chart component is used to display a chart on page. The classname is UIChart and element tag is p-chart.

Import

  1. import {ChartModule} from 'primeng/chart';
  2.  

Change Detection

In order to chart to redraw itself, a new data object needs to be created. Changing the array contents without creating a new array instance does not trigger change detection.

  1. <p-chart type="pie" [data]="data"></p-chart>
  2. <button type="button" pButton (click)="update($event)"></button>
  3.  
  1. update(event: Event) {
  2. this.data = //create new data
  3. }
  4.  

Properties

NameTypeDefaultDescription
typestringnullType of the chart.
dataanynullData to display.
optionsanynullOptions to customize the chart.
pluginsany[]nullArray of per-chart plugins to customize the chart behaviour.
widthstringnullWidth of the chart.
heightstringnullHeight of the chart.
responsivebooleantrueWhether the chart is redrawn on screen size change.
onDataSelectfunctionnullCallback to execute when an element on chart is clicked.

Methods

NameParametersDescription
refresh-Redraws the graph with new data.
reinit-Destroys the graph first and then creates it again.
generateLegend-Returns an HTML string of a legend for that chart. The legend is generated from the legendCallback in the options.

Chart Types

Chart type is defined using the type property. Currently there are 6 options available; pie, doughtnut, line(line or horizontalBar), bar, radar and polarArea.

Data

Data of a chart is provided using a binding to the data property, each type has its own format of data. Here is an example of a line chart.

  1. <p-chart type="line" [data]="data"></p-chart>
  2.  
  1. export class LineChartDemo {
  2. data: any;
  3. constructor() {
  4. this.data = {
  5. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  6. datasets: [
  7. {
  8. label: 'First Dataset',
  9. data: [65, 59, 80, 81, 56, 55, 40]
  10. },
  11. {
  12. label: 'Second Dataset',
  13. data: [28, 48, 40, 19, 86, 27, 90]
  14. }
  15. ]
  16. }
  17. }
  18. }
  19.  

Options

While a series can be customized per dataset, general chart options are defined with options property. Example below adds a title and customizes the legend position of the chart. For all available options refer to the charts.js documentation.

  1. <p-chart type="line" [data]="data" [options]="options"></p-chart>
  2.  
  1. export class LineChartDemo {
  2. data: any;
  3. options: any;
  4. constructor() {
  5. this.data = {
  6. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  7. datasets: [
  8. {
  9. label: 'First Dataset',
  10. data: [65, 59, 80, 81, 56, 55, 40]
  11. },
  12. {
  13. label: 'Second Dataset',
  14. data: [28, 48, 40, 19, 86, 27, 90]
  15. }
  16. ]
  17. }
  18. this.options = {
  19. title: {
  20. display: true,
  21. text: 'My Title',
  22. fontSize: 16
  23. },
  24. legend: {
  25. position: 'bottom'
  26. }
  27. };
  28. }
  29. }
  30.  

Events

When data is selected with click event, chart component provides onDataSelect callback to process the selected data.

  1. <p-chart type="line" [data]="data" (onDataSelect)="selectData($event)"></p-chart>
  2.  
  1. selectData(event) {
  2. //event.dataset = Selected dataset
  3. //event.element = Selected element
  4. //event.element._datasetIndex = Index of the dataset in data
  5. //event.element._index = Index of the data in dataset
  6. }
  7.  

Responsive

Charts are responsive by default and vw-vh units should be used when customizing the width and height of the chart.

  1. <p-chart type="line" [data]="data" width="40vw" height="80vh"></p-chart>
  2.  

If the chart is not responsive, other units should be preferred.

  1. <p-chart type="line" [data]="data" width="400px" height="400px" [responsive]="false"></p-chart>
  2.