github的语言占比图如何用vue来实现
- User
github的语言占比图如何用vue来实现
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
github的语言占比图如何用vue来实现
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
要用Vue实现GitHub风格的语言占比图,主要有几种方式:
<template>
<div class="language-stats">
<canvas ref="canvas" :width="width" :height="height"></canvas>
<div class="language-list">
<div
v-for="(lang, index) in languages"
:key="lang.name"
class="language-item"
>
<span
class="color-dot"
:style="{ backgroundColor: lang.color }"
></span>
<span class="language-name">{{ lang.name }}</span>
<span class="percentage">{{ lang.percentage.toFixed(1) }}%</span>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'LanguageChart',
props: {
languages: {
type: Array,
required: true
},
width: {
type: Number,
default: 400
},
height: {
type: Number,
default: 8
}
},
mounted() {
this.drawChart()
},
watch: {
languages: {
handler() {
this.drawChart()
},
deep: true
}
},
methods: {
drawChart() {
const canvas = this.$refs.canvas
const ctx = canvas.getContext('2d')
let currentX = 0
const totalLines = this.languages.reduce((sum, lang) => sum + lang.lines, 0)
this.languages.forEach(lang => {
const width = (lang.lines / totalLines) * this.width
ctx.fillStyle = lang.color
ctx.fillRect(currentX, 0, width, this.height)
currentX += width
})
}
}
}
</script>
<style scoped>
.language-stats {
margin: 20px 0;
}
.language-list {
display: flex;
flex-wrap: wrap;
gap: 16px;
margin-top: 8px;
}
.language-item {
display: flex;
align-items: center;
font-size: 12px;
}
.color-dot {
width: 12px;
height: 12px;
border-radius: 50%;
margin-right: 6px;
}
.language-name {
margin-right: 4px;
}
.percentage {
color: #656d76;
}
</style>
<template>
<div class="language-stats">
<div class="progress-bar">
<div
v-for="lang in languages"
:key="lang.name"
class="progress-segment"
:style="{
width: lang.percentage + '%',
backgroundColor: lang.color
}"
></div>
</div>
<div class="language-list">
<div
v-for="lang in languages"
:key="lang.name"
class="language-item"
>
<span
class="color-dot"
:style="{ backgroundColor: lang.color }"
></span>
<span class="language-name">{{ lang.name }}</span>
<span class="percentage">{{ lang.percentage.toFixed(1) }}%</span>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'LanguageChart',
props: {
data: {
type: Array,
required: true
}
},
computed: {
languages() {
const total = this.data.reduce((sum, item) => sum + item.lines, 0)
return this.data.map(item => ({
...item,
percentage: (item.lines / total) * 100
}))
}
}
}
</script>
<style scoped>
.progress-bar {
display: flex;
height: 8px;
border-radius: 4px;
overflow: hidden;
background-color: #f6f8fa;
}
.progress-segment {
height: 100%;
}
.language-list {
display: flex;
flex-wrap: wrap;
gap: 16px;
margin-top: 8px;
}
.language-item {
display: flex;
align-items: center;
font-size: 12px;
}
.color-dot {
width: 12px;
height: 12px;
border-radius: 50%;
margin-right: 6px;
}
</style>
<template>
<div>
<LanguageChart :data="languageData" />
</div>
</template>
<script>
import LanguageChart from './LanguageChart.vue'
export default {
components: {
LanguageChart
},
data() {
return {
languageData: [
{ name: 'JavaScript', lines: 15420, color: '#f1e05a' },
{ name: 'Vue', lines: 8930, color: '#41b883' },
{ name: 'CSS', lines: 3240, color: '#563d7c' },
{ name: 'HTML', lines: 1890, color: '#e34c26' },
{ name: 'TypeScript', lines: 1200, color: '#3178c6' }
]
}
}
}
</script>
CSS方法更简单易维护,Canvas方法则提供更多自定义控制。根据你的具体需求选择合适的实现方式。如果需要添加动画效果或交互功能,可以进一步扩展这些基础实现。