Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing Legend Shape in ng2-charts

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
Expected o/p

What I am getting

o/p

like image 787
Chris Avatar asked May 08 '19 09:05

Chris


People also ask

How do I add a legend in Chartjs?

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!

How to use legend in Angular?

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.

How do you hide a legend in Chartjs?

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, }, }, });

What is chart legend JavaScript?

The chart legend displays data about the datasets that are appearing on the chart.


1 Answers

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
      }
    }
  }
like image 90
Ved Avatar answered Sep 21 '22 21:09

Ved