Skip to content

Line Chart ​

Vue 已实现 · 还原验收 0/1 · 上游演示 · 固定版本源码

预览 ​

Monthly revenue

Demo data, in thousands of dollars

Illustrative monthly revenue data
CategoryThis yearLast yearTarget
Jan$36.4K$36.5K$36.0K
Feb$43.0K$35.8K$38.5K
Mar$43.9K$36.3K$41.0K
Apr$44.2K$39.9K$43.5K
May$49.1K$43.3K$46.0K
Jun$52.2K$40.9K$48.5K
Jul$53.6K$44.2K$51.0K
Aug$58.7K$45.6K$53.5K
Sep$63.8K$51.1K$56.0K
Oct$67.1K$55.5K$58.5K
Nov$71.6K$53.7K$61.0K
Dec$75.6K$55.5K$63.5K

用法 ​

vue
<script setup lang="ts">
import { computed, onBeforeUnmount, shallowRef } from 'vue'
import { LineChart, Tabs } from 'beui-vue'

const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
const variant = shallowRef<'line' | 'area'>('area')
const seed = shallowRef(7)
const loading = shallowRef(false)
const series = computed(() => {
  let state = seed.value
  function walk(start: number, drift: number) {
    let current = start
    return months.map(() => {
      state = (state * 16807) % 2147483647
      current = Math.max(4, current + drift + (state / 2147483647 - 0.45) * 9)
      return Math.round(current * 10) / 10
    })
  }
  return [
    { id: 'this-year', name: 'This year', color: '#2563eb', values: walk(38, 2.4) },
    { id: 'last-year', name: 'Last year', color: '#a1a1aa', values: walk(32, 1.2) },
    { id: 'target', name: 'Target', color: '#0d9488', values: months.map((_, month) => 36 + month * 2.5) },
  ]
})
let timer: ReturnType<typeof setTimeout> | undefined
function regenerate() { seed.value = (seed.value * 48271) % 2147483647 }
function reload() {
  loading.value = true
  timer = setTimeout(() => { regenerate(); loading.value = false }, 1800)
}
onBeforeUnmount(() => clearTimeout(timer))
</script>

<template>
  <div class="chart-example">
    <div class="chart-heading">
      <div><strong>Monthly revenue</strong><p>Demo data, in thousands of dollars</p></div>
      <div class="chart-actions">
        <Tabs :items="[{ value: 'line', label: 'Line' }, { value: 'area', label: 'Area' }]" :model-value="variant" :labels="{ list: 'Chart style' }"
          @update:model-value="variant = $event === 'line' ? 'line' : 'area'" />
        <button type="button" :disabled="loading" @click="regenerate">New data</button>
        <button type="button" :disabled="loading" @click="reload">Reload</button>
      </div>
    </div>
    <LineChart :series="series" :categories="months" :variant="variant" :status="loading ? 'loading' : 'ready'"
      label="Illustrative monthly revenue" :format-value="value => `$${value.toFixed(1)}K`" :format-tick="value => value === 0 ? '$0' : `$${value}K`" />
  </div>
</template>

<style scoped>
.chart-example { width: 100%; max-width: 48rem; margin: auto; }
.chart-heading, .chart-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.chart-heading { justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.chart-heading strong { font-size: 14px; }
.chart-heading p { margin: 2px 0 0; font-size: 12px; line-height: 1.5; opacity: .7; }
.chart-actions { --beui-primary: var(--beui-background); --beui-primary-foreground: var(--beui-foreground); }
.chart-actions button { border-radius: 18px; font: 500 12px/1.4 inherit; font-size: 12px; min-height: 28px; padding: 3px 9px; cursor: pointer; }
.chart-actions button[aria-pressed='true'] { background: var(--vp-c-bg, Canvas); box-shadow: 0 1px 4px #0002; }
.chart-actions button:disabled { opacity: .5; }
</style>

API ​

series 定义数据系列。支持 v-model:active-id 与 v-model:hidden-series,可组合图表、图例与提示层。

还原验收 ​

基于 beUI 的独立 Vue 移植,依 MIT 许可证分发。