迷你图表

迷你图表组件可以用来创建简单的图形,比如 GitHub 的贡献图。

除了下面列出的以外,还可以使用任何 SVG attribute

用例

迷你图表是一个提供数据可视化表示的小图表。

template script


  1. <template>
  2. <v-sparkline
  3. :value="value"
  4. :gradient="gradient"
  5. :smooth="radius || false"
  6. :padding="padding"
  7. :line-width="width"
  8. :stroke-linecap="lineCap"
  9. :gradient-direction="gradientDirection"
  10. :fill="fill"
  11. :type="type"
  12. :auto-line-width="autoLineWidth"
  13. auto-draw
  14. ></v-sparkline>
  15. </template>
  1. <script>
  2. const gradients = [
  3. ['#222'],
  4. ['#42b3f4'],
  5. ['red', 'orange', 'yellow'],
  6. ['purple', 'violet'],
  7. ['#00c6ff', '#F0F', '#FF0'],
  8. ['#f72047', '#ffd200', '#1feaea'],
  9. ]
  10. export default {
  11. data: () => ({
  12. width: 2,
  13. radius: 10,
  14. padding: 8,
  15. lineCap: 'round',
  16. gradient: gradients[5],
  17. value: [0, 2, 5, 9, 5, 10, 3, 5, 0, 0, 1, 8, 2, 9, 0],
  18. gradientDirection: 'top',
  19. gradients,
  20. fill: false,
  21. type: 'trend',
  22. autoLineWidth: false,
  23. }),
  24. }
  25. </script>

Sparklines(迷你图表) - 图1

API

从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。

Sparklines(迷你图表) - 图2

实战场

template script


  1. <template>
  2. <v-container fluid>
  3. <v-sparkline
  4. :value="value"
  5. :gradient="gradient"
  6. :smooth="radius || false"
  7. :padding="padding"
  8. :line-width="lineWidth"
  9. :stroke-linecap="lineCap"
  10. :gradient-direction="gradientDirection"
  11. :fill="fill"
  12. :type="type"
  13. :auto-line-width="autoLineWidth"
  14. auto-draw
  15. :show-labels="showLabels"
  16. :label-size="labelSize"
  17. ></v-sparkline>
  18. <v-divider></v-divider>
  19. <v-row>
  20. <v-col cols="12">
  21. <v-row class="fill-height" align="center">
  22. <v-subheader class="pl-0">Type</v-subheader>
  23. <v-btn-toggle v-model="type" mandatory>
  24. <v-btn small text value="bar">bar</v-btn>
  25. <v-btn small text value="trend">trend</v-btn>
  26. </v-btn-toggle>
  27. </v-row>
  28. </v-col>
  29. <v-col cols="12" md="6">
  30. <v-row class="fill-height" align="center">
  31. <v-subheader class="pl-0">Gradient</v-subheader>
  32. <v-item-group v-model="gradient" mandatory>
  33. <v-row>
  34. <v-item
  35. v-for="(gradient, i) in gradients"
  36. :key="i"
  37. v-slot:default="{ active, toggle }"
  38. :value="gradient"
  39. >
  40. <v-card
  41. :style="{
  42. background: gradient.length > 1
  43. ? `linear-gradient(0deg, ${gradient})`
  44. : gradient[0],
  45. border: '2px solid',
  46. borderColor: active ? '#222' : 'white'
  47. }"
  48. width="30"
  49. height="30"
  50. class="mr-2"
  51. @click.native="toggle"
  52. ></v-card>
  53. </v-item>
  54. </v-row>
  55. </v-item-group>
  56. </v-row>
  57. </v-col>
  58. <v-col cols="12" md="6">
  59. <v-row class="fill-height" align="center">
  60. <v-subheader class="pl-0">Gradient direction</v-subheader>
  61. <v-btn-toggle v-model="gradientDirection" mandatory>
  62. <v-btn small text value="top">top</v-btn>
  63. <v-btn small text value="right">right</v-btn>
  64. <v-btn small text value="left">left</v-btn>
  65. <v-btn small text value="bottom">bottom</v-btn>
  66. </v-btn-toggle>
  67. </v-row>
  68. </v-col>
  69. <v-col cols="12">
  70. <v-slider
  71. v-model="lineWidth"
  72. label="Line width"
  73. min="0.1"
  74. max="10"
  75. step="0.1"
  76. thumb-label
  77. :disabled="autoLineWidth"
  78. ></v-slider>
  79. </v-col>
  80. <v-col cols="12">
  81. <v-slider
  82. v-model="radius"
  83. label="Radius"
  84. min="0"
  85. max="16"
  86. thumb-label
  87. ></v-slider>
  88. </v-col>
  89. <v-col cols="12">
  90. <v-slider
  91. v-model="padding"
  92. label="Padding"
  93. min="0"
  94. max="16"
  95. thumb-label
  96. ></v-slider>
  97. </v-col>
  98. <v-col cols="6">
  99. <v-row class="fill-height" align="center">
  100. <v-subheader class="pl-0">Linecap</v-subheader>
  101. <v-btn-toggle v-model="lineCap" mandatory :disabled="type !== 'trend'">
  102. <v-btn small text value="butt">butt</v-btn>
  103. <v-btn small text value="round">round</v-btn>
  104. <v-btn small text value="square">square</v-btn>
  105. </v-btn-toggle>
  106. </v-row>
  107. </v-col>
  108. <v-col cols="6">
  109. <v-row justify="space-around">
  110. <v-switch v-model="showLabels" label="Show labels"></v-switch>
  111. <v-switch v-model="fill" label="Fill" :disabled="type !== 'trend'"></v-switch>
  112. <v-switch v-model="autoLineWidth" label="Auto-line-width" :disabled="type !== 'bar'"></v-switch>
  113. </v-row>
  114. </v-col>
  115. <v-col v-if="showLabels" cols="12">
  116. <v-slider
  117. v-model="labelSize"
  118. label="Label size"
  119. min="1"
  120. max="20"
  121. thumb-label
  122. ></v-slider>
  123. </v-col>
  124. </v-row>
  125. </v-container>
  126. </template>
  1. <script>
  2. const gradients = [
  3. ['#222'],
  4. ['#42b3f4'],
  5. ['red', 'orange', 'yellow'],
  6. ['purple', 'violet'],
  7. ['#00c6ff', '#F0F', '#FF0'],
  8. ['#f72047', '#ffd200', '#1feaea'],
  9. ]
  10. export default {
  11. data: () => ({
  12. showLabels: false,
  13. lineWidth: 2,
  14. labelSize: 7,
  15. radius: 10,
  16. padding: 8,
  17. lineCap: 'round',
  18. gradient: gradients[5],
  19. value: [0, 2, 5, 9, 5, 10, 3, 5, -4, -10, 1, 8, 2, 9, 0],
  20. gradientDirection: 'top',
  21. gradients,
  22. fill: false,
  23. type: 'trend',
  24. autoLineWidth: false,
  25. }),
  26. }
  27. </script>

Sparklines(迷你图表) - 图3

示例

下面是一些简单到复杂的例子。

填充

您可以使用 fill 属性创建一个 v-sparkline

template script


  1. <template>
  2. <v-container fluid>
  3. <v-sparkline
  4. :fill="fill"
  5. :gradient="gradient"
  6. :line-width="width"
  7. :padding="padding"
  8. :smooth="radius || false"
  9. :value="value"
  10. auto-draw
  11. ></v-sparkline>
  12. <v-divider></v-divider>
  13. <v-row>
  14. <v-col
  15. cols="12"
  16. md="6"
  17. >
  18. <v-row class="fill-height" align="center">
  19. <v-item-group v-model="gradient" mandatory>
  20. <v-row>
  21. <v-item
  22. v-for="(gradient, i) in gradients"
  23. :key="i"
  24. v-slot:default="{ active, toggle }"
  25. :value="gradient"
  26. >
  27. <v-card
  28. :style="{
  29. background: gradient.length > 1
  30. ? `linear-gradient(0deg, ${gradient})`
  31. : gradient[0],
  32. border: '2px solid',
  33. borderColor: active ? '#222' : 'white'
  34. }"
  35. width="30"
  36. height="30"
  37. class="mr-2"
  38. @click.native="toggle"
  39. ></v-card>
  40. </v-item>
  41. </v-row>
  42. </v-item-group>
  43. </v-row>
  44. </v-col>
  45. <v-col
  46. cols="12"
  47. md="6"
  48. >
  49. <v-slider
  50. v-model="width"
  51. label="Width"
  52. min="0.1"
  53. max="10"
  54. step="0.1"
  55. thumb-label
  56. ></v-slider>
  57. </v-col>
  58. <v-col cols="6">
  59. <v-row class="fill-height" align="center">
  60. <v-switch v-model="fill" label="Filled"></v-switch>
  61. </v-row>
  62. </v-col>
  63. <v-col
  64. cols="12"
  65. md="6"
  66. >
  67. <v-slider
  68. v-model="radius"
  69. label="Radius"
  70. min="0"
  71. max="25"
  72. thumb-label
  73. ></v-slider>
  74. </v-col>
  75. <v-col
  76. cols="12"
  77. md="6"
  78. offset-md="6"
  79. >
  80. <v-slider
  81. v-model="padding"
  82. label="Padding"
  83. min="0"
  84. max="25"
  85. thumb-label
  86. ></v-slider>
  87. </v-col>
  88. </v-row>
  89. </v-container>
  90. </template>
  1. <script>
  2. const gradients = [
  3. ['#222'],
  4. ['#42b3f4'],
  5. ['red', 'orange', 'yellow'],
  6. ['purple', 'violet'],
  7. ['#00c6ff', '#F0F', '#FF0'],
  8. ['#f72047', '#ffd200', '#1feaea'],
  9. ]
  10. export default {
  11. data: () => ({
  12. fill: true,
  13. gradient: gradients[4],
  14. gradients,
  15. padding: 8,
  16. radius: 10,
  17. value: [0, 2, 5, 9, 5, 10, 3, 5, 0, 0, 1, 8, 2, 9, 0],
  18. width: 2,
  19. }),
  20. }
  21. </script>

Sparklines(迷你图表) - 图4

休息一下

对于简明扼要的信息,完整的图表可能有点过头。 使用具有梯度的趋势线可以为用户提供足够的细节,而不会显示太多信息。

template script


  1. <template>
  2. <v-card
  3. class="mx-auto"
  4. color="grey lighten-4"
  5. max-width="600"
  6. >
  7. <v-card-title>
  8. <v-icon
  9. :color="checking ? 'red lighten-2' : 'indigo'"
  10. class="mr-12"
  11. size="64"
  12. @click="takePulse"
  13. >
  14. mdi-heart-pulse
  15. </v-icon>
  16. <v-row align="start">
  17. <div class="caption grey--text text-uppercase">
  18. Heart rate
  19. </div>
  20. <div>
  21. <span
  22. class="display-2 font-weight-black"
  23. v-text="avg || '—'"
  24. ></span>
  25. <strong v-if="avg">BPM</strong>
  26. </div>
  27. </v-row>
  28. <v-spacer></v-spacer>
  29. <v-btn icon class="align-self-start" size="28">
  30. <v-icon>mdi-arrow-right-thick</v-icon>
  31. </v-btn>
  32. </v-card-title>
  33. <v-sheet color="transparent">
  34. <v-sparkline
  35. :key="String(avg)"
  36. :smooth="16"
  37. :gradient="['#f72047', '#ffd200', '#1feaea']"
  38. :line-width="3"
  39. :value="heartbeats"
  40. auto-draw
  41. stroke-linecap="round"
  42. ></v-sparkline>
  43. </v-sheet>
  44. </v-card>
  45. </template>
  1. <script>
  2. const exhale = ms =>
  3. new Promise(resolve => setTimeout(resolve, ms))
  4. export default {
  5. data: () => ({
  6. checking: false,
  7. heartbeats: [],
  8. }),
  9. computed: {
  10. avg () {
  11. const sum = this.heartbeats.reduce((acc, cur) => acc + cur, 0)
  12. const length = this.heartbeats.length
  13. if (!sum && !length) return 0
  14. return Math.ceil(sum / length)
  15. },
  16. },
  17. created () {
  18. this.takePulse(false)
  19. },
  20. methods: {
  21. heartbeat () {
  22. return Math.ceil(Math.random() * (120 - 80) + 80)
  23. },
  24. async takePulse (inhale = true) {
  25. this.checking = true
  26. inhale && await exhale(1000)
  27. this.heartbeats = Array.from({ length: 20 }, this.heartbeat)
  28. this.checking = false
  29. },
  30. },
  31. }
  32. </script>

Sparklines(迷你图表) - 图5

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-cardv-sheet 很好地组合,创建定制的信息卡,完美的管理仪表板。这里,我们使用自定义标签为迷你图表提供额外的上下文。

template script style


  1. <template>
  2. <v-card
  3. class="mt-4 mx-auto"
  4. max-width="400"
  5. >
  6. <v-sheet
  7. class="v-sheet--offset mx-auto"
  8. color="cyan"
  9. elevation="12"
  10. max-width="calc(100% - 32px)"
  11. >
  12. <v-sparkline
  13. :labels="labels"
  14. :value="value"
  15. color="white"
  16. line-width="2"
  17. padding="16"
  18. ></v-sparkline>
  19. </v-sheet>
  20. <v-card-text class="pt-0">
  21. <div class="title font-weight-light mb-2">User Registrations</div>
  22. <div class="subheading font-weight-light grey--text">Last Campaign Performance</div>
  23. <v-divider class="my-2"></v-divider>
  24. <v-icon
  25. class="mr-2"
  26. small
  27. >
  28. mdi-clock
  29. </v-icon>
  30. <span class="caption grey--text font-weight-light">last registration 26 minutes ago</span>
  31. </v-card-text>
  32. </v-card>
  33. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. labels: [
  5. '12am',
  6. '3am',
  7. '6am',
  8. '9am',
  9. '12pm',
  10. '3pm',
  11. '6pm',
  12. '9pm',
  13. ],
  14. value: [
  15. 200,
  16. 675,
  17. 410,
  18. 390,
  19. 310,
  20. 460,
  21. 250,
  22. 240,
  23. ],
  24. }),
  25. }
  26. </script>
  1. <style>
  2. .v-sheet--offset {
  3. top: -24px;
  4. position: relative;
  5. }
  6. </style>

Sparklines(迷你图表) - 图6

自定义标签

通过提供 label 插槽,我们可以修改显示的内容并添加美元符号($)。 此插槽是 exclusively 用于文本内容。 有关 svg <text> 元素的更多信息,请 navigate here

template script


  1. <template>
  2. <v-card
  3. class="mx-auto text-center"
  4. color="green"
  5. dark
  6. max-width="600"
  7. >
  8. <v-card-text>
  9. <v-sheet color="rgba(0, 0, 0, .12)">
  10. <v-sparkline
  11. :value="value"
  12. color="rgba(255, 255, 255, .7)"
  13. height="100"
  14. padding="24"
  15. stroke-linecap="round"
  16. smooth
  17. >
  18. <template v-slot:label="item">
  19. ${{ item.value }}
  20. </template>
  21. </v-sparkline>
  22. </v-sheet>
  23. </v-card-text>
  24. <v-card-text>
  25. <div class="display-1 font-weight-thin">Sales Last 24h</div>
  26. </v-card-text>
  27. <v-divider></v-divider>
  28. <v-card-actions class="justify-center">
  29. <v-btn block text>Go to Report</v-btn>
  30. </v-card-actions>
  31. </v-card>
  32. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. value: [
  5. 423,
  6. 446,
  7. 675,
  8. 510,
  9. 590,
  10. 610,
  11. 760,
  12. ],
  13. }),
  14. }
  15. </script>

Sparklines(迷你图表) - 图7