Я настраиваю диаграмму в приложении JavaScript с использованием Vue.js, и мне нужно, чтобы метки и информация о диаграмме всегда были видны без необходимости наведения мыши.
Код моей диаграммы выглядит так:
<template>
<Pie :data = "ameacas.chartData" :options = "ameacas.chartOptions" style = "width: 300px; height: 300px;" />
</template>
<script lang = "ts">
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'
import { Pie } from 'vue-chartjs'
ChartJS.register(ArcElement, Tooltip, Legend)
export default {
name: 'TdPie',
components: {
Pie
},
props: {
threats: {
type: Array,
required: true
}
},
computed: {
ameacas: function () {
return {
chartData: {
labels: ['Alta', 'Média', 'Baixa'],
datasets: [
{
backgroundColor: ['#CC092F', '#DD3820', '#FCBA03'],
data: [this.getHighThreat, this.getMediumThreat, this.getLowThreat]
}
]
},
chartOptions: {
responsive: true,
maintainAspectRatio: false
}
}
},
getHighThreat: function () {
return this.threats
.filter(threat => threat.severity && threat.severity.toLowerCase() == 'high')
.length;
},
getMediumThreat: function () {
return this.threats
.filter(threat => threat.severity && threat.severity.toLowerCase() == 'medium')
.length;
},
getLowThreat: function () {
return this.threats
.filter(threat => threat.severity && threat.severity.toLowerCase() == 'low')
.length;
}
}
}
</script>
Я уже пробовал контент по этой ссылке и другим материалам сообщества.
Это сработало! Спасибо за ссылку. Я не знал, как настроить плагин, и эта ссылка мне очень помогла.
Я настроил «chartjs-plugin-datalabels», и код получился таким:
<template>
<Pie :data = "ameacas.chartData" :options = "ameacas.chartOptions" style = "width: 300px; height: 300px;" />
</template>
<script lang = "ts">
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'
import { Pie } from 'vue-chartjs'
import ChartDataLabels from 'chartjs-plugin-datalabels';
ChartJS.register(ArcElement, Tooltip, Legend)
ChartJS.register(ChartDataLabels);
export default {
name: 'TdPie',
components: {
Pie
},
props: {
threats: {
type: Array,
required: true
}
},
computed: {
ameacas: function () {
return {
chartData: {
labels: ['Alta', 'Média', 'Baixa'],
datasets: [
{
backgroundColor: ['#CC092F', '#DD3820', '#FCBA03'],
data: [this.getHighThreat, this.getMediumThreat, this.getLowThreat]
}
]
},
chartOptions: {
responsive: true,
maintainAspectRatio: false,
plugins: {
datalabels: {
backgroundColor: function (context) {
return context.dataset.backgroundColor;
},
borderColor: 'white',
borderRadius: 25,
borderWidth: 2,
color: 'white',
padding: 6,
formatter: Math.round
}
}
}
}
},
getHighThreat: function () {
return this.threats
.filter(threat => threat.severity && threat.severity.toLowerCase() == 'high')
.length;
},
getMediumThreat: function () {
return this.threats
.filter(threat => threat.severity && threat.severity.toLowerCase() == 'medium')
.length;
},
getLowThreat: function () {
return this.threats
.filter(threat => threat.severity && threat.severity.toLowerCase() == 'low')
.length;
}
}
}
</script>
И вот результат:
Вы пробовали chartjs-plugin-datalabels ?