-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f62fc91
commit 3faf42a
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<template> | ||
<Pie :key="key" :options="options" :data="data"></Pie> | ||
</template> | ||
<script lang="ts"> | ||
import { Pie } from 'vue-chartjs'; | ||
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'; | ||
import { PropType, defineComponent } from 'vue'; | ||
ChartJS.register(ArcElement, Tooltip, Legend) | ||
export default defineComponent({ | ||
name: 'PieChart', | ||
components: { | ||
Pie | ||
}, | ||
props: { | ||
labels: { | ||
type: Object as PropType<string[]>, | ||
required: true, | ||
}, | ||
values: { | ||
type: Object as PropType<number[]>, | ||
required: true, | ||
} | ||
}, | ||
data() { | ||
const options = { | ||
responsive: true, | ||
maintainAspectRatio: false | ||
} | ||
const data = { | ||
labels: [] as string[], | ||
datasets: [ | ||
{ | ||
backgroundColor: [] as string[], | ||
data: [] as number[], | ||
} | ||
] | ||
}; | ||
const backgroundColors: string[] = []; | ||
return { | ||
options, | ||
data, | ||
backgroundColors, | ||
key: 0, | ||
} | ||
}, | ||
mounted() { | ||
this.backgroundColors = this.values.map(() => this.getRandomColor()); | ||
this.data.labels = this.labels; | ||
this.data.datasets[0].backgroundColor = this.backgroundColors; | ||
this.data.datasets[0].data = this.values; | ||
this.key += 1; | ||
}, | ||
methods: { | ||
getRandomColor() { | ||
const r = Math.floor(Math.random() * 255); | ||
const g = Math.floor(Math.random() * 255); | ||
const b = Math.floor(Math.random() * 255); | ||
return `rgba(${r}, ${g}, ${b}, 0.7)`; | ||
} | ||
} | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters