ng2-char: How to show the percentage inside the graph?

Asked

Viewed 25 times

0

I’m using ng2-Chart to generate the graphics. The problem I wanted to put the percentage or even the values inside the graph, but I couldn’t do that. In [options] step a plugin I saw the people on the Internet talking to do but still does not work:

  optionsGraficoDespesa: ChartOptions = {
    responsive: true,
    tooltips: {
      enabled: true,
      callbacks: {
        label: function (tooltipItem, data) {
          let label = data.labels[tooltipItem.index];
          let count: any = data
            .datasets[tooltipItem.datasetIndex]
            .data[tooltipItem.index];
          return label + ": " + new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(count);
        },
      },
    },
    plugins: {
      datalabels: {
        formatter: (value, ctx) => {
          let sum = 0;
          let dataArr = ctx.chart.data.datasets[0].data;
          dataArr.map(data => {
            sum += data;
          });
          let percentage = (value * 100 / sum).toFixed(2) + "%";
          return percentage;
        },
        color: '#fff',
      }
    }
  };

<canvas baseChart [data]="dashboard.graficoDespesaMensal.valorTotalDespesa"
                [labels]="dashboard.graficoDespesaMensal.descricaoCategoria" [options]="optionsGraficoDespesa"
                [chartType]="'doughnut'">
              </canvas>

Like this: inserir a descrição da imagem aqui

What I wanted was for you to show the percentage within each circle. Someone has already done that?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.