Gauge 仪表盘
仪表盘是一种用于展示单一数据值与预设范围关系的图表,常用于显示速度、温度、功率等指标。
平台差异说明
App(vue) | App(nvue) | H5 | 小程序 |
---|---|---|---|
√ | √ | √ | √ |
基础仪表盘
通过在 [series] 中添加对象并设置数据,可以绘制基础仪表盘。
vue
<template>
<view>
<ly-charts-gauge
:width="300"
:height="300"
:option="gaugeOption"
></ly-charts-gauge>
</view>
</template>
<script setup>
import { ref } from 'vue'
const gaugeOption = ref({
pointer: {
length: '75%',
width: 16
},
series: [{
type: 'gauge',
startAngle: 225,
endAngle: -45,
data: [{ value: 60, name: '速度' }]
}]
})
</script>
我将把文档中的示例代码转换为组合式API(Composition API)风格。主要修改是将选项式API的data、methods等转换为setup函数中的ref和reactive等。
```markdown::/Users/jry/Documents/www/xyito/open/lyCharts-docs/docs/charts/gauge.md::c46b681d-6e7a-4ef9-b294-c5b38d229c95
带进度条的仪表盘
通过设置 series.progress.show
为 true
,可以显示进度条效果。
vue
<template>
<view>
<ly-charts-gauge
:width="300"
:height="300"
:option="gaugeOption"
></ly-charts-gauge>
</view>
</template>
<script setup>
import { ref } from 'vue'
const gaugeOption = ref({
series: [{
type: 'gauge',
min: 0,
max: 240,
progress: {
show: true,
color: 'red'
},
detail: {
valueAnimation: false
},
animationDuration: 1000,
axisLine: {
lineStyle: {
width: 12,
color: [[1, '#eee']]
}
},
data: [{ value: 60, name: '功率' }]
}]
})
</script>
自定义样式的仪表盘
通过配置 axisLine
、splitLine
、axisLabel
等属性,可以自定义仪表盘样式。
vue
<template>
<view>
<ly-charts-gauge
:width="300"
:height="300"
:option="gaugeOption"
></ly-charts-gauge>
</view>
</template>
<script setup>
import { ref } from 'vue'
const gaugeOption = ref({
series: [{
type: 'gauge',
progress: {
show: true,
width: 12
},
axisLine: {
lineStyle: {
width: 12,
color: [
[0.3, '#67C23A'],
[0.7, '#E6A23C'],
[1, '#F56C6C']
]
}
},
splitLine: {
splitNumber: 5,
length: 15,
lineStyle: {
width: 3,
color: '#999'
}
},
axisLabel: {
distance: 25,
color: '#666',
fontSize: 14
},
anchor: {
show: true,
size: 10,
itemStyle: {
borderWidth: 1,
borderColor: '#5572CA'
}
},
detail: {
show: true,
fontSize: 24,
color: '#333',
formatter: '{value}%'
},
data: [{ value: 20, name: '完成度' }]
}]
})
</script>
带动画效果的仪表盘
通过设置 detail.valueAnimation
为 true
,可以启用数值动画效果。
vue
<template>
<view>
<ly-charts-gauge
:width="300"
:height="300"
:option="gaugeOption"
></ly-charts-gauge>
</view>
</template>
<script setup>
import { ref } from 'vue'
const gaugeOption = ref({
series: [{
type: 'gauge',
progress: {
show: true,
width: 1
},
axisLine: {
splitNumber: 3,
lineStyle: {
width: 12,
color: [[1, '#f1f1f1']]
}
},
splitLine: {
length: 10,
lineStyle: {
width: 2,
color: '#999'
}
},
axisLabel: {
distance: 15,
color: '#666',
fontSize: 12
},
itemStyle: {
color: 'green'
},
anchor: {
show: true,
size: 8,
itemStyle: {
borderWidth: 1,
borderColor: '#5572CA'
}
},
detail: {
valueAnimation: true,
show: true,
fontSize: 25,
color: '#409EFF',
formatter: '{value}km/h',
offsetCenter: [0, 0]
},
data: [{ value: 65, name: '时速' }]
}]
})
</script>
多进度条仪表盘
通过在 series.data
中添加多个数据项,可以实现多进度条仪表盘。
vue
<template>
<view>
<ly-charts-gauge
:width="260"
:height="260"
:option="gaugeOption"
></ly-charts-gauge>
</view>
</template>
<script setup>
import { ref } from 'vue'
const gaugeOption = ref({
series: [{
type: 'gauge',
min: 0,
max: 130,
progress: {
show: true,
width: 38
},
startAngle: 270,
endAngle: -89,
pointer: {
show: false
},
detail: {
valueAnimation: true
},
axisTick: {
show: false
},
splitLine: {
show: false
},
animationDuration: 3000,
axisLine: {
lineStyle: {
width: 40,
color: [[1, '#f7f7f7']]
}
},
data: [
{ value: 60, name: '功率' },
{ value: 120, name: '功率2' },
{ value: 90, name: '功率3' },
{ value: 80, name: '功率4' }
]
}]
})
</script>
Props
参数 | 说明 | 类型 | 默认值 | 可选值 |
---|---|---|---|---|
option | 图表配置项,格式与 ECharts 相似 | Object | - | - |
width | 图表容器宽度 | String | Number | 300 | - |
height | 图表容器高度 | String | Number | 300 | - |
Events
事件名 | 说明 | 回调参数 |
---|---|---|
click | 点击图表时触发 | event对象 |
Options 属性
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
series | 系列列表 | Array | - |
series[].type | 系列类型 | String | gauge |
series[].name | 系列名称 | String | - |
series[].data | 系列数据 | Array | - |
series[].data[].value | 数据值 | Number | 0 |
series[].data[].name | 数据名称 | String | - |
series[].startAngle | 起始角度 | Number | 225 |
series[].endAngle | 结束角度 | Number | -45 |
series[].min | 最小值 | Number | 0 |
series[].max | 最大值 | Number | 100 |
series[].progress | 进度条配置 | Object | - |
series[].progress.show | 是否显示进度条 | Boolean | false |
series[].progress.width | 进度条宽度 | Number | 12 |
series[].progress.color | 进度条颜色 | String | - |
series[].axisLine | 轴线配置 | Object | - |
series[].axisLine.lineStyle.width | 轴线宽度 | Number | 12 |
series[].axisLine.lineStyle.color | 轴线颜色 | String/Array | #E6EBF8 |
series[].splitLine | 分割线配置 | Object | - |
series[].splitLine.length | 分割线长度 | Number | 10 |
series[].splitLine.splitNumber | 分割线数量 | Number | 10 |
series[].axisLabel | 轴标签配置 | Object | - |
series[].axisLabel.distance | 标签与轴线距离 | Number | 10 |
series[].axisLabel.color | 标签颜色 | String | #999 |
series[].axisLabel.fontSize | 标签字体大小 | Number | 12 |
series[].axisTick | 刻度线配置 | Object | - |
series[].axisTick.show | 是否显示刻度线 | Boolean | true |
series[].axisTick.splitNumber | 刻度线数量 | Number | 5 |
series[].pointer | 指针配置 | Object | - |
series[].pointer.show | 是否显示指针 | Boolean | true |
series[].pointer.length | 指针长度 | String/Number | 80% |
series[].pointer.width | 指针宽度 | Number | 8 |
series[].anchor | 锚点配置 | Object | - |
series[].anchor.show | 是否显示锚点 | Boolean | false |
series[].anchor.size | 锚点大小 | Number | 5 |
series[].detail | 详情配置 | Object | - |
series[].detail.show | 是否显示详情 | Boolean | true |
series[].detail.fontSize | 详情字体大小 | Number | 20 |
series[].detail.color | 详情颜色 | String | #333 |
series[].detail.formatter | 详情格式化 | String | |
series[].detail.valueAnimation | 是否启用数值动画 | Boolean | false |
series[].detail.offsetCenter | 相对于仪表盘中心的偏移位置 | Array | [0, 0] |
series[].animationDuration | 动画时长 | Number | 1000 |
series[].itemStyle | 项目样式 | Object | - |
series[].itemStyle.color | 项目颜色 | String | - |
方法
方法名 | 说明 | 参数 |
---|---|---|
refresh | 重新绘制图表 | - |
getChartInstance | 获取图表实例 | - |