I am working on Pie Chart in ng2-charts. I am trying to customize the default shape of legend. I could change the position of legend but Is it is possible to change the shape? Does it have any inbuilt property or we have to customize?
HTML
<div class="chart">
<canvas baseChart
[data]="pieChartData"
[labels]="pieChartLabels"
[chartType]="pieChartType"
[options]="pieChartOptions"
[plugins]="pieChartPlugins"
[colors]="pieChartColors"
[legend]="pieChartLegend">
</canvas>
</div>
TS
public pieChartLabels: Label[] = [['Download', 'Sales'], ['In', 'Store', 'Sales'], 'Mail Sales'];
public pieChartData: number[] = [300, 500, 100];
public pieChartType: ChartType = 'pie';
public pieChartOption: any = {
legend: {
position: 'right',
labels: {
fontSize: 10
}
}
}
What I want
What I am getting
The following example will create a chart with the legend enabled and turn all of the text red in color. const chart = new Chart(ctx, { type: 'bar', data: data, options: { plugins: { legend: { display: true, labels: { color: 'rgb(255, 99, 132)' } } } } }); Copied!
You can set title for legend using title property in legendSettings . You can also customize the fontStyle , size , fontWeight , color , textAlignment , fontFamily , opacity and textOverflow of legend title. titlePosition is used to set the legend position in Top , Left and Right position.
To remove legend on charts with Chart. js v2 and JavaScript, we can set the options. legend to false . const chart1 = new Chart(canvas, { type: "pie", data: data, options: { legend: { display: false, }, tooltips: { enabled: false, }, }, });
The chart legend displays data about the datasets that are appearing on the chart.
ng2-charts uses chartjs and in chartjs there is legend.labels.usePointStyle, you have to make it true.
public pieChartLabels: Label[] = [['Download', 'Sales'], ['In', 'Store', 'Sales'],
'Mail Sales'];
public pieChartData: number[] = [300, 500, 100];
public pieChartType: ChartType = 'pie';
public pieChartOption: any = {
legend: {
position: 'right',
labels: {
fontSize: 10,
usePointStyle: true
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With