score:1

Accepted answer

even i researched a lot, couldn't find anything for this kind of behavior in chartjs library, so come up with a hack.

hack is something like, do not let chart js to draw legends, instead we just get the html of legends from chart js library, and put it in our container. by doing this we have full access of legends and we can do whatever we want.

https://jsfiddle.net/1kxenzpr/

const data = [70, 30, 0];
debugger;
var ctx = document.getelementbyid("mychart").getcontext('2d');
var chart = new chart(ctx, {
  type: 'pie',
  data: {
    labels: ["green", "blue", "gray", "purple", "yellow", "red", "black"],
    datasets: [{
      backgroundcolor: [
        "#2ecc71",
        "#3498db",
        "#95a5a6",
        "#9b59b6",
        "#f1c40f",
        "#e74c3c",
        "#34495e"
      ],
      data: data
    }]
  },
  options: {
    legend: {
      display: false
    },
  }
});
var mylegendcontainer = document.getelementbyid("legend");
mylegendcontainer.innerhtml = chart.generatelegend();
var legenditems = mylegendcontainer.getelementsbytagname('li');
for (var i = 0; i < legenditems.length; i++) {
  legenditems[i].queryselectorall('span')[0].innerhtml = data[i] + '%'
}
.container {
  width: 80%;
  margin: 15px auto;
}

[class$="-legend"] {
  list-style: none;
  cursor: pointer;
  padding-left: 0;
}

[class$="-legend"] li {
  display: inline-block;
  padding: 0 5px;
}

[class$="-legend"] li.hidden {
  text-decoration: line-through;
}

[class$="-legend"] li span {
  display: inline-block;
  margin-right: 10px;
  width: 50px;
  font-size: 12px;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.min.js"></script>

<div class="container">
  <div>
    <canvas id="mychart"></canvas>
    <div id="legend"></div>
  </div>
</div>

score:1

no way (by setting x or y). you should use legendcallback: https://www.chartjs.org/docs/latest/configuration/legend.html#html-legends

in general, your q does not follow stackoverflow guidelines (this is more a mission, not q). https://stackoverflow.com/help/how-to-ask

anyway, this is +- the idea (convert to % by basic js). to take this step forward you should generate full generate html legend (to put a number inside a color div). related: custom legend with chartjs v2.0

var mydata = [4, 9, 5];

var data = {
  labels: ["africa", "asia", "europe"],
  datasets: [{
    label: "population (millions)",
    backgroundcolor: ["#3e95cd", "#8e5ea2","#3cba9f"],
    data: mydata
  }]
};

/* get total */
const reducer = (accumulator, currentvalue) => accumulator + currentvalue;
var total = mydata.reduce(reducer);

var options = {
  responsive: true,
  title: {
    text: 'show % calucate on fly',
    position: 'top',
    display: true
  },
  legend: {
    display: true,
    labels: {
      /* generatelabels */
      generatelabels(chart) {
        const data = chart.data;
        if (data.labels.length && data.datasets.length) {
          /* inner loop throw lables */
          return data.labels.map((label, i) => {
            var backgroundcolor = data.datasets[0].backgroundcolor[i];
            var current = data.datasets[0].data[i];
            var percentage =  ((current * 100) / total).tofixed(2) + '%'; 
            return {
              text: label + " | " + percentage,
              fillstyle: backgroundcolor,
              // extra data used for toggling the correct item
              index: i
            };
          });
        }
        return [];
      }

    },

  },
  scales: {
    xaxes: [{
      display: true
    }],
    yaxes: [{
      display: true
    }]
  }
};


new chart(document.getelementbyid("chart"), {
  type: 'pie',
  data: data,
  options: options
});
<canvas id="chart" width="800" height="450"></canvas>

<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>


Related Query

More Query from same tag