У меня есть компонент vue chart.js, как показано ниже
Проблема в том, что при использовании нескольких экземпляров диаграммы текст в середине перекрывается.
Но они перекрываются. Я пытался поменять название, но ничего не вышло. Как я могу это исправить?
Мне нравится контролировать HTML / все, что выводится на этикетку.
<script>
import { Doughnut } from "vue-chartjs";
export default {
name: "doughnutChart",
extends: Doughnut,
props: {
data1: {
type: String,
// default: function() {
// return;
// }
}
},
mounted() {
var value = this.data1;
var data = {
labels: ["My val", ""]
};
this.renderChart(
{
datasets: [
{
borderWidth: 1,
backgroundColor: ["#FF6384", "#AAAAAA"],
hoverBackgroundColor: ["#FF6384", "#AAAAAA"],
hoverBorderColor: ["#FF6384", "#ffffff"],
data: [value, 100 - value]
}
]
},
{
responsive: true,
maintainAspectRatio: true,
legend: { display: false },
cutoutPercentage: 70,
elements: {
center: {
text: '50%' //set as you wish
}
},
tooltips: {
filter: function(item, data) {
var label = data.labels[item.index];
if (label) return item;
}
}
}
);
this.textCenter(value)
},
methods:{
textCenter(val) {
Chart.pluginService.register({
beforeDraw: function(chart) {
var width = chart.chart.width,
height = chart.chart.height,
ctx = chart.chart.ctx;
ctx.restore();
var fontSize = (height / 114).toFixed(2);
ctx.font = fontSize + "em sans-serif";
ctx.textBaseline = "middle";
var text = val + "%",
textX = Math.round((width - ctx.measureText(text).width) / 2),
textY = height / 2;
ctx.fillText(text, textX, textY);
ctx.save();
}
});
}
}
};
</script>