迷你图表
迷你图表组件可以用来创建简单的图形,比如 GitHub 的贡献图。
除了下面列出的以外,还可以使用任何 SVG attribute
用例
迷你图表是一个提供数据可视化表示的小图表。
template script
<template>
<v-sparkline
:value="value"
:gradient="gradient"
:smooth="radius || false"
:padding="padding"
:line-width="width"
:stroke-linecap="lineCap"
:gradient-direction="gradientDirection"
:fill="fill"
:type="type"
:auto-line-width="autoLineWidth"
auto-draw
></v-sparkline>
</template>
<script>
const gradients = [
['#222'],
['#42b3f4'],
['red', 'orange', 'yellow'],
['purple', 'violet'],
['#00c6ff', '#F0F', '#FF0'],
['#f72047', '#ffd200', '#1feaea'],
]
export default {
data: () => ({
width: 2,
radius: 10,
padding: 8,
lineCap: 'round',
gradient: gradients[5],
value: [0, 2, 5, 9, 5, 10, 3, 5, 0, 0, 1, 8, 2, 9, 0],
gradientDirection: 'top',
gradients,
fill: false,
type: 'trend',
autoLineWidth: false,
}),
}
</script>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<v-container fluid>
<v-sparkline
:value="value"
:gradient="gradient"
:smooth="radius || false"
:padding="padding"
:line-width="lineWidth"
:stroke-linecap="lineCap"
:gradient-direction="gradientDirection"
:fill="fill"
:type="type"
:auto-line-width="autoLineWidth"
auto-draw
:show-labels="showLabels"
:label-size="labelSize"
></v-sparkline>
<v-divider></v-divider>
<v-row>
<v-col cols="12">
<v-row class="fill-height" align="center">
<v-subheader class="pl-0">Type</v-subheader>
<v-btn-toggle v-model="type" mandatory>
<v-btn small text value="bar">bar</v-btn>
<v-btn small text value="trend">trend</v-btn>
</v-btn-toggle>
</v-row>
</v-col>
<v-col cols="12" md="6">
<v-row class="fill-height" align="center">
<v-subheader class="pl-0">Gradient</v-subheader>
<v-item-group v-model="gradient" mandatory>
<v-row>
<v-item
v-for="(gradient, i) in gradients"
:key="i"
v-slot:default="{ active, toggle }"
:value="gradient"
>
<v-card
:style="{
background: gradient.length > 1
? `linear-gradient(0deg, ${gradient})`
: gradient[0],
border: '2px solid',
borderColor: active ? '#222' : 'white'
}"
width="30"
height="30"
class="mr-2"
@click.native="toggle"
></v-card>
</v-item>
</v-row>
</v-item-group>
</v-row>
</v-col>
<v-col cols="12" md="6">
<v-row class="fill-height" align="center">
<v-subheader class="pl-0">Gradient direction</v-subheader>
<v-btn-toggle v-model="gradientDirection" mandatory>
<v-btn small text value="top">top</v-btn>
<v-btn small text value="right">right</v-btn>
<v-btn small text value="left">left</v-btn>
<v-btn small text value="bottom">bottom</v-btn>
</v-btn-toggle>
</v-row>
</v-col>
<v-col cols="12">
<v-slider
v-model="lineWidth"
label="Line width"
min="0.1"
max="10"
step="0.1"
thumb-label
:disabled="autoLineWidth"
></v-slider>
</v-col>
<v-col cols="12">
<v-slider
v-model="radius"
label="Radius"
min="0"
max="16"
thumb-label
></v-slider>
</v-col>
<v-col cols="12">
<v-slider
v-model="padding"
label="Padding"
min="0"
max="16"
thumb-label
></v-slider>
</v-col>
<v-col cols="6">
<v-row class="fill-height" align="center">
<v-subheader class="pl-0">Linecap</v-subheader>
<v-btn-toggle v-model="lineCap" mandatory :disabled="type !== 'trend'">
<v-btn small text value="butt">butt</v-btn>
<v-btn small text value="round">round</v-btn>
<v-btn small text value="square">square</v-btn>
</v-btn-toggle>
</v-row>
</v-col>
<v-col cols="6">
<v-row justify="space-around">
<v-switch v-model="showLabels" label="Show labels"></v-switch>
<v-switch v-model="fill" label="Fill" :disabled="type !== 'trend'"></v-switch>
<v-switch v-model="autoLineWidth" label="Auto-line-width" :disabled="type !== 'bar'"></v-switch>
</v-row>
</v-col>
<v-col v-if="showLabels" cols="12">
<v-slider
v-model="labelSize"
label="Label size"
min="1"
max="20"
thumb-label
></v-slider>
</v-col>
</v-row>
</v-container>
</template>
<script>
const gradients = [
['#222'],
['#42b3f4'],
['red', 'orange', 'yellow'],
['purple', 'violet'],
['#00c6ff', '#F0F', '#FF0'],
['#f72047', '#ffd200', '#1feaea'],
]
export default {
data: () => ({
showLabels: false,
lineWidth: 2,
labelSize: 7,
radius: 10,
padding: 8,
lineCap: 'round',
gradient: gradients[5],
value: [0, 2, 5, 9, 5, 10, 3, 5, -4, -10, 1, 8, 2, 9, 0],
gradientDirection: 'top',
gradients,
fill: false,
type: 'trend',
autoLineWidth: false,
}),
}
</script>
示例
下面是一些简单到复杂的例子。
填充
您可以使用 fill
属性创建一个 v-sparkline
。
template script
<template>
<v-container fluid>
<v-sparkline
:fill="fill"
:gradient="gradient"
:line-width="width"
:padding="padding"
:smooth="radius || false"
:value="value"
auto-draw
></v-sparkline>
<v-divider></v-divider>
<v-row>
<v-col
cols="12"
md="6"
>
<v-row class="fill-height" align="center">
<v-item-group v-model="gradient" mandatory>
<v-row>
<v-item
v-for="(gradient, i) in gradients"
:key="i"
v-slot:default="{ active, toggle }"
:value="gradient"
>
<v-card
:style="{
background: gradient.length > 1
? `linear-gradient(0deg, ${gradient})`
: gradient[0],
border: '2px solid',
borderColor: active ? '#222' : 'white'
}"
width="30"
height="30"
class="mr-2"
@click.native="toggle"
></v-card>
</v-item>
</v-row>
</v-item-group>
</v-row>
</v-col>
<v-col
cols="12"
md="6"
>
<v-slider
v-model="width"
label="Width"
min="0.1"
max="10"
step="0.1"
thumb-label
></v-slider>
</v-col>
<v-col cols="6">
<v-row class="fill-height" align="center">
<v-switch v-model="fill" label="Filled"></v-switch>
</v-row>
</v-col>
<v-col
cols="12"
md="6"
>
<v-slider
v-model="radius"
label="Radius"
min="0"
max="25"
thumb-label
></v-slider>
</v-col>
<v-col
cols="12"
md="6"
offset-md="6"
>
<v-slider
v-model="padding"
label="Padding"
min="0"
max="25"
thumb-label
></v-slider>
</v-col>
</v-row>
</v-container>
</template>
<script>
const gradients = [
['#222'],
['#42b3f4'],
['red', 'orange', 'yellow'],
['purple', 'violet'],
['#00c6ff', '#F0F', '#FF0'],
['#f72047', '#ffd200', '#1feaea'],
]
export default {
data: () => ({
fill: true,
gradient: gradients[4],
gradients,
padding: 8,
radius: 10,
value: [0, 2, 5, 9, 5, 10, 3, 5, 0, 0, 1, 8, 2, 9, 0],
width: 2,
}),
}
</script>
休息一下
对于简明扼要的信息,完整的图表可能有点过头。 使用具有梯度的趋势线可以为用户提供足够的细节,而不会显示太多信息。
template script
<template>
<v-card
class="mx-auto"
color="grey lighten-4"
max-width="600"
>
<v-card-title>
<v-icon
:color="checking ? 'red lighten-2' : 'indigo'"
class="mr-12"
size="64"
@click="takePulse"
>
mdi-heart-pulse
</v-icon>
<v-row align="start">
<div class="caption grey--text text-uppercase">
Heart rate
</div>
<div>
<span
class="display-2 font-weight-black"
v-text="avg || '—'"
></span>
<strong v-if="avg">BPM</strong>
</div>
</v-row>
<v-spacer></v-spacer>
<v-btn icon class="align-self-start" size="28">
<v-icon>mdi-arrow-right-thick</v-icon>
</v-btn>
</v-card-title>
<v-sheet color="transparent">
<v-sparkline
:key="String(avg)"
:smooth="16"
:gradient="['#f72047', '#ffd200', '#1feaea']"
:line-width="3"
:value="heartbeats"
auto-draw
stroke-linecap="round"
></v-sparkline>
</v-sheet>
</v-card>
</template>
<script>
const exhale = ms =>
new Promise(resolve => setTimeout(resolve, ms))
export default {
data: () => ({
checking: false,
heartbeats: [],
}),
computed: {
avg () {
const sum = this.heartbeats.reduce((acc, cur) => acc + cur, 0)
const length = this.heartbeats.length
if (!sum && !length) return 0
return Math.ceil(sum / length)
},
},
created () {
this.takePulse(false)
},
methods: {
heartbeat () {
return Math.ceil(Math.random() * (120 - 80) + 80)
},
async takePulse (inhale = true) {
this.checking = true
inhale && await exhale(1000)
this.heartbeats = Array.from({ length: 20 }, this.heartbeat)
this.checking = false
},
},
}
</script>
Material Dashboard Pro
Vuetify Material Dashboard PRO is a beautiful theme built over Vuetify, Vuex and Vuejs. Vuetify Material Dashboard PRO is the official Vuejs version of the Original Material Dashboard PRO.
ads by Vuetify
](https://www.creative-tim.com/product/vuetify-material-dashboard-pro?ref=vuetifyjs.com&partner=116160)
仪表盘卡片
v-sparkline
组件可以与 v-card
和 v-sheet
很好地组合,创建定制的信息卡,完美的管理仪表板。这里,我们使用自定义标签为迷你图表提供额外的上下文。
template script style
<template>
<v-card
class="mt-4 mx-auto"
max-width="400"
>
<v-sheet
class="v-sheet--offset mx-auto"
color="cyan"
elevation="12"
max-width="calc(100% - 32px)"
>
<v-sparkline
:labels="labels"
:value="value"
color="white"
line-width="2"
padding="16"
></v-sparkline>
</v-sheet>
<v-card-text class="pt-0">
<div class="title font-weight-light mb-2">User Registrations</div>
<div class="subheading font-weight-light grey--text">Last Campaign Performance</div>
<v-divider class="my-2"></v-divider>
<v-icon
class="mr-2"
small
>
mdi-clock
</v-icon>
<span class="caption grey--text font-weight-light">last registration 26 minutes ago</span>
</v-card-text>
</v-card>
</template>
<script>
export default {
data: () => ({
labels: [
'12am',
'3am',
'6am',
'9am',
'12pm',
'3pm',
'6pm',
'9pm',
],
value: [
200,
675,
410,
390,
310,
460,
250,
240,
],
}),
}
</script>
<style>
.v-sheet--offset {
top: -24px;
position: relative;
}
</style>
自定义标签
通过提供 label 插槽,我们可以修改显示的内容并添加美元符号($)。 此插槽是 exclusively 用于文本内容。 有关 svg <text>
元素的更多信息,请 navigate here。
template script
<template>
<v-card
class="mx-auto text-center"
color="green"
dark
max-width="600"
>
<v-card-text>
<v-sheet color="rgba(0, 0, 0, .12)">
<v-sparkline
:value="value"
color="rgba(255, 255, 255, .7)"
height="100"
padding="24"
stroke-linecap="round"
smooth
>
<template v-slot:label="item">
${{ item.value }}
</template>
</v-sparkline>
</v-sheet>
</v-card-text>
<v-card-text>
<div class="display-1 font-weight-thin">Sales Last 24h</div>
</v-card-text>
<v-divider></v-divider>
<v-card-actions class="justify-center">
<v-btn block text>Go to Report</v-btn>
</v-card-actions>
</v-card>
</template>
<script>
export default {
data: () => ({
value: [
423,
446,
675,
510,
590,
610,
760,
],
}),
}
</script>