Bar 柱状图
柱状图是一种使用矩形条表示数据大小的图表,用于比较不同类别的数据。
平台差异说明
App(vue) | App(nvue) | H5 | 小程序 |
---|---|---|---|
√ | √ | √ | √ |
基础柱状图
通过在 [series] 中添加对象,可以绘制柱状图。
vue
<template>
<view>
<ly-charts-bar ref="uChartsBar" :options="chartOption"
:height="220" @click="handleBarClick"></ly-charts-bar>
</view>
</template>
<script setup>
import { ref } from 'vue'
const chartOption = ref({
title: {
text: '销售数据统计',
textStyle: {
fontSize: 16,
color: '#333'
}
},
xAxis: {
type: 'category',
data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
axisLabel: {
color: '#666',
fontSize: 12
}
},
yAxis: {
type: 'value',
axisLabel: {
color: '#666',
fontSize: 12
}
},
series: [{
name: '销售额',
type: 'bar',
data: [120, 200, 150, 80, 70, 110, 130],
itemStyle: {
color: '#5470c6'
}
}, {
name: '订单量',
type: 'bar',
data: [60, 80, 90, 50, 40, 70, 85],
itemStyle: {
color: '#91cc75'
}
}],
legend: {
top: 0,
data: ['销售额', '订单量'],
textStyle: {
color: '#666'
}
},
backgroundColor: 'transparent'
})
</script>
堆叠柱状图
通过设置 [series] 中的 [stack]属性,可以实现堆叠柱状图。
vue
<template>
<view>
<ly-charts-bar :options="chartOption" height="400"></ly-charts-bar>
</view>
</template>
<script setup>
import { ref } from 'vue'
const chartOption = ref({
title: {
text: '销售数据统计'
},
legend: {
data: ['销售额', '订单量'],
bottom: 0
},
xAxis: {
data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
},
yAxis: {},
series: [
{
name: '销售额',
type: 'bar',
stack: 'order',
data: [120, 200, 150, 80, 70, 110, 130],
itemStyle: {
color: '#5470c6'
}
},
{
name: '订单量',
type: 'bar',
stack: 'order',
data: [60, 80, 90, 50, 40, 70, 85],
itemStyle: {
color: '#91cc75'
}
}
]
})
</script>
温度计形图
通过设置两个堆叠的柱状图,其中一个使用透明色,可以实现温度计效果。
vue
<template>
<view>
<ly-charts-bar :options="chartOption" height="400"></ly-charts-bar>
</view>
</template>
<script setup>
import { ref } from 'vue'
const chartOption = ref({
title: {
text: '温度计形柱状图'
},
legend: {
data: ['销量', '订单量'],
top: 0
},
xAxis: {
data: ['产品A', '产品B', '产品C', '产品D', '产品E']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
stack: 'order',
data: [120, 180, 150, 90, 110],
itemStyle: {
color: '#94C973',
borderColor: '#3992DE',
borderWidth: 1
}
}, {
name: '订单量',
type: 'bar',
stack: 'order',
data: [60, 80, 90, 50, 40, 70, 85],
itemStyle: {
color: 'rgba(142, 230, 157, 0.4)'
}
}]
})
</script>
圆角柱状图
通过设置 [series] 中的 itemStyle.borderRadius
属性,可以实现圆角柱状图。
vue
<template>
<view>
<ly-charts-bar :options="chartOption" height="400"></ly-charts-bar>
</view>
</template>
<script setup>
import { ref } from 'vue'
const chartOption = ref({
title: {
text: '圆角柱状图示例'
},
legend: {
data: ['销量']
},
xAxis: {
data: ['产品A', '产品B', '产品C', '产品D', '产品E']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [120, 180, 150, {
value: 80,
itemStyle: {
color: '#ff6b6b',
borderColor: '#b56bff',
borderWidth: 1
}
}, 110],
itemStyle: {
color: '#2ed573',
borderRadius: 10
}
}]
})
</script>
山峰形状柱状图
通过设置 [series] 中的 [symbol]属性为 mountain
,可以实现山峰形状的柱状图。
vue
<template>
<view>
<ly-charts-bar :options="chartOption" height="400"></ly-charts-bar>
</view>
</template>
<script setup>
import { ref } from 'vue'
const chartOption = ref({
title: {
text: '山峰图示例'
},
xAxis: {
data: ['产品A', '产品B', '产品C', '产品D', '产品E']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
symbol: 'mountain',
data: [120, 180, 150, 90, 110],
itemStyle: {
color: '#5470c6'
}
}]
})
</script>
圆角山峰形状柱状图
通过设置 [series] 中的 [symbol]属性为 roundedMountain
,可以实现圆角山峰形状的柱状图。
vue
<template>
<view>
<ly-charts-bar :options="chartOption" height="400"></ly-charts-bar>
</view>
</template>
<script setup>
import { ref } from 'vue'
const chartOption = ref({
title: {
text: '圆角山峰图示例'
},
xAxis: {
data: ['产品A', '产品B', '产品C', '产品D', '产品E']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
symbol: 'roundedMountain',
data: [120, 180, 150, {
value: 90,
itemStyle: {
color: '#1990FF'
}
}, 110],
itemStyle: {
color: '#91cc75'
}
}]
})
</script>
尖角山峰形状柱状图
通过设置 [series] 中的 [symbol]属性为 sharpMountain
,可以实现尖角山峰形状的柱状图。
vue
<template>
<view>
<ly-charts-bar :options="chartOption" height="400"></ly-charts-bar>
</view>
</template>
<script setup>
import { ref } from 'vue'
const chartOption = ref({
title: {
text: '尖角山峰图示例'
},
xAxis: {
data: ['产品A', '产品B', '产品C', '产品D', '产品E']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
symbol: 'sharpMountain',
data: [120, 180, 150, 90, 110],
itemStyle: {
color: '#fac858'
}
}]
})
</script>
自然山峰形状柱状图
通过设置 [series] 中的 [symbol]属性为 realMountain
,可以实现自然山峰形状的柱状图。
vue
<template>
<view>
<ly-charts-bar :options="chartOption" height="400"></ly-charts-bar>
</view>
</template>
<script setup>
import { ref } from 'vue'
const chartOption = ref({
title: {
text: '自然山峰图示例'
},
xAxis: {
data: ['产品A', '产品B', '产品C', '产品D', '产品E']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
symbol: 'realMountain',
data: [120, 180, 150, {
value: 90,
itemStyle: {
color: '#1990FF'
}
}, 110],
itemStyle: {
color: '#91cc75'
}
}]
})
</script>
Props
参数 | 说明 | 类型 | 默认值 | 可选值 |
---|---|---|---|---|
options | 图表配置项,格式与 ECharts 相似 | Object | - | - |
width | 图表容器宽度 | String | Number | 100% | - |
height | 图表容器高度 | String | Number | 400 | - |
Events
事件名 | 说明 | 回调参数 |
---|---|---|
click | 点击数据点时触发 | { componentType, seriesType, seriesName, name, value, color, event } |
Options 属性
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
title | 标题配置 | Object | - |
title.text | 标题文本 | String | - |
legend | 图例配置 | Object | - |
legend.data | 图例数据 | Array | - |
xAxis | x轴配置 | Object | - |
xAxis.data | x轴数据 | Array | - |
yAxis | y轴配置 | Object | - |
series | 系列列表 | Array | - |
series[].type | 系列类型 | String | bar |
series[].name | 系列名称 | String | - |
series[].data | 系列数据 | Array | - |
series[].stack | 堆叠分组名称 | String | - |
series[].symbol | 柱状图形状 | String | rect |
series[].itemStyle | 柱状图样式 | Object | - |
series[].itemStyle.color | 柱状图颜色 | String | - |
series[].itemStyle.borderColor | 边框颜色 | String | - |
series[].itemStyle.borderWidth | 边框宽度 | Number | 0 |
series[].itemStyle.borderRadius | 圆角大小 | Number | 0 |
Symbol 类型
类型 | 说明 |
---|---|
rect | 矩形(默认) |
rounded | 圆角矩形 |
mountain | 山峰形状(三角形) |
roundedMountain | 圆角山峰形状 |
sharpMountain | 尖角山峰形状 |
realMountain | 自然山峰形状 |
方法
方法名 | 说明 | 参数 |
---|---|---|
setOption | 设置图表配置项 | option, notMerge |
resize | 重新调整图表大小 | - |