Installation
You can install ng2-charts using npm
npm install ng2-charts --save
You need to install and include Chart.js
library in application via html or webpack bundler (more options can be found in official chart.js Documentation)
E.g.,
npm install chart.js --save
Important: Embedding Chart.js in application is mandatory!
<script src="node_modules/chart.js/src/chart.js"></script>
Or
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.min.js"></script>
Demo and API details of ng2-charts can be found here: Demo and Source code.
Ticks Congiguration
The tick configuration is nested under the scale configuration in the ticks key. It defines options for the tick marks that are generated by the axis.
Beginning From Zero
scales: { |
Change Scale Label
Creating Custom Tick Formats
The callback method may be used for advanced tick customization. In the following example, every label of the Y axis would be displayed in scientific notation. If the callback returns null or undefined the associated grid line will be hidden.
scales: { |
Two y-scales
scales: { |
with corresponding datasetthis.barChartData.push({data: data1, label: 'a', yAxisID: "y-axis-0"});
this.barChartData.push({data: data2, label: 'b', yAxisID: "y-axis-1"});
Tooltips Congiguration
Sometime we have to apply tooltips in special style in chart.js.
For example, in the following code, the 1st and 2nd data sets show tooltips in currency format, while the 3rd data set has tooltip with percentage symbole as sufix.
barChartOptions:any = { |
Here, we implemented tooltip label
callback function.
Callback | Arguments | Description |
---|---|---|
label | tooltipItem, data | Text to render for an individual item in the tooltip |
All functions are called with the same arguments: a tooltip item and the data object passed to the chart. All functions must return either a string or an array of strings. Arrays of strings are treated as multiple lines of text.
Tooltip Item Interface
The tooltip items passed to the tooltip callbacks implement the following interface.
{ |
Ref