1.基础用法
vue
<template>
<bar-chart-2 ref="chartRef" v-bind="option"></bar-chart-2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const yAxisData = [
['农业', '工业', '建筑业', '批发和零售业', '交通运输', '住宿和餐饮业', '金融业', '房地产业', '其他服务业'],
['农', '工', '建', '批', '交', '住', '金', '房', '其']
];
const seriesData = [
[54, -89, -86, 65, 54, 53, -72, 65, -60],
[95, -97, 75, 72, 90, -88, 54, -77, -98]
];
const legendData = ['统计金额', '开票金额'];
// 组合配置项
const option = {
yAxisData,
seriesData,
legendData
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 664px;
background-color: rgb(3, 43, 68);
}
</style>2.滑块间距设置
vue
<template>
<bar-chart-2 ref="chartRef" v-bind="option"></bar-chart-2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const yAxisData = [
['农业', '工业', '建筑业', '批发和零售业', '交通运输', '住宿和餐饮业', '金融业', '房地产业', '其他服务业'],
['农', '工', '建', '批', '交', '住', '金', '房', '其']
];
const seriesData = [
[54, -89, -86, 65, 54, 53, -72, 65, -60],
[95, -97, 75, 72, 90, -88, 54, -77, -98],
];
const legendData = ['统计金额', '1开票金额'];
const grid = { right: 100 };
const dataZoomRight = 32;
// 组合配置项
const option = {
yAxisData,
seriesData,
legendData,
grid,
dataZoomRight
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 664px;
background-color: rgb(3, 43, 68);
}
</style>3.修改 tooltip 标题
vue
<template>
<bar-chart-2 ref="chartRef" v-bind="option"></bar-chart-2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const yAxisData = [
['农业', '工业', '建筑业', '批发和零售业', '交通运输', '住宿和餐饮业', '金融业', '房地产业', '其他服务业'],
['农', '工', '建', '批', '交', '住', '金', '房', '其']
];
const seriesData = [
[54, -89, -86, 65, 54, 53, -72, 65, -60],
[95, -97, 75, 72, 90, -88, 54, -77, -98]
];
const legendData = ['统计金额', '开票金额'];
const tooltipTitle = new Array(10).fill().map((n, i) => `第 ${ i + 1 } 个 tooltip 标题`);
// 组合配置项
const option = {
yAxisData,
seriesData,
legendData,
tooltipTitle
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 664px;
background-color: rgb(3, 43, 68);
}
</style>4.部分区域高亮
vue
<template>
<bar-chart-2 ref="chartRef" v-bind="option"></bar-chart-2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const yAxisData = [
['农业', '工业', '建筑业', '批发和零售业', '交通运输', '住宿和餐饮业', '金融业', '房地产业', '其他服务业'],
['农', '工', '建', '批', '交', '住', '金', '房', '其']
];
const seriesData = [
[54, -89, -86, 65, 54, 53, -72, 65, -60],
[95, -97, 75, 72, 90, -88, 54, -77, -98]
];
const legendData = ['统计金额', '开票金额'];
const yAxisHighlightArea = [2, 4];
// 组合配置项
const option = {
yAxisData,
seriesData,
legendData,
yAxisHighlightArea,
highlightAreaColor: 'rgba(255, 0, 0, 0.3)'
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 664px;
background-color: rgb(3, 43, 68);
}
</style>5.隐藏 y 轴每项背景
vue
<template>
<bar-chart-2 ref="chartRef" v-bind="option"></bar-chart-2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const yAxisData = [
['农业', '工业', '建筑业', '批发和零售业', '交通运输', '住宿和餐饮业', '金融业', '房地产业', '其他服务业'],
['农', '工', '建', '批', '交', '住', '金', '房', '其']
];
const seriesData = [
[54, -89, -86, 65, 54, 53, -72, 65, -60],
[95, -97, 75, 72, 90, -88, 54, -77, -98]
];
const legendData = ['统计金额', '开票金额'];
const showItemBackground = false;
// 组合配置项
const option = {
yAxisData,
seriesData,
legendData,
showItemBackground
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 664px;
background-color: rgb(3, 43, 68);
}
</style>6.隐藏辅助刻度线
vue
<template>
<bar-chart-2 ref="chartRef" v-bind="option"></bar-chart-2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const yAxisData = [
['农业', '工业', '建筑业', '批发和零售业', '交通运输', '住宿和餐饮业', '金融业', '房地产业', '其他服务业'],
['农', '工', '建', '批', '交', '住', '金', '房', '其']
];
const seriesData = [
[54, -89, -86, 65, 54, 53, -72, 65, -60],
[95, -97, 75, 72, 90, -88, 54, -77, -98]
];
const legendData = ['统计金额', '开票金额'];
const showSplitLine = false;
// 组合配置项
const option = {
yAxisData,
seriesData,
legendData,
showSplitLine
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 664px;
background-color: rgb(3, 43, 68);
}
</style>7.柱宽与数值显示
vue
<template>
<bar-chart-2 ref="chartRef" v-bind="option"></bar-chart-2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const yAxisData = [
['农业', '工业', '建筑业', '批发和零售业', '交通运输', '住宿和餐饮业', '金融业', '房地产业', '其他服务业'],
['农', '工', '建', '批', '交', '住', '金', '房', '其']
];
const seriesData = [
[54, -89, -86, 65, 54, 53, -72, 65, -60],
[95, -97, 75, 72, 90, -88, 54, -77, -98]
];
const legendData = ['统计金额', '开票金额'];
const barWidth = 12;
const showSeriesLabel = false;
// 组合配置项
const option = {
yAxisData,
seriesData,
legendData,
barWidth,
showSeriesLabel
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 664px;
background-color: rgb(3, 43, 68);
}
</style>属性
属性名
说明
类型
默认值
参考值
yAxisData
y 轴坐标
Array
[]
[
['农业', '工业', '建筑', '批发', '交通', '住宿', '金融'],
['农', '工', '建', '批', '交', '住', '金']
]
['农业', '工业', '建筑', '批发', '交通', '住宿', '金融'],
['农', '工', '建', '批', '交', '住', '金']
]
seriesData
数据数组
Array
[]
[
[54, 89, 86, 65, 54, 53, 72, 65, 60],
[95, 97, 75, 72, 90, 88, 54, 77, 98]
]
[54, 89, 86, 65, 54, 53, 72, 65, 60],
[95, 97, 75, 72, 90, 88, 54, 77, 98]
]
grid
上下左右边距
Object
({
top: 84,
right: 58,
bottom: 56,
left: 106
})
top: 84,
right: 58,
bottom: 56,
left: 106
})
{ top: 84, right: 58, bottom: 56, left: 106 }
legendData
legend 数据
Array
[]
['统计金额', '开票金额']
showCount
最多显示的数量(实际显示数量会根据输入值调整)
Number
4
4
dataZoomType
何种方式拖动 inside 内容区域拖动,slider 滑块拖动
String
'inside'
'slider'
dataZoomRight
当 dataZoomType 为 slider 时,拖动区域距离右侧的距离
Number
0
12
showLegend
是否显示 legend
Boolean
true
false
legendIcon
legend 图表,支持字符串或数组
String, Array
['rect']
['rect']
color
图表项颜色
String, Array
['blue', 'grey']
['blue', 'grey']
tooltipTitle
tooltip 标题
Array
null
['标题']
yAxisHighlightArea
高亮区域的索引
Array
[]
[2, 4]
highlightAreaColor
高亮区域颜色
String
'rgba(14, 143, 255, 0.2)'
'rgba(255, 143, 255, 0.2)'
unit
数据的单位
String
''
'元'
barWidth
柱子的宽度
Number
24
12
showSplitLine
是否显示辅助刻度线
Boolean
true
false
showItemBackground
是否显示每一项的背景色
Boolean
true
false
itemBackgroundColor
每一项的背景色
String
'rgba(255, 255, 255, 0.12)'
'rgba(255, 0, 0, 0.12)'
showSeriesLabel
是否显示柱子对应的数值
Boolean
true
false
barBorderRadius
圆柱的圆角
Number, Array
[0]
[4, 4, 0, 0]
zoomLock
是否锁定选择区域的大小
Boolean
false
true
scale
图表缩放比例
Number
1
2
tooltipConfine
是否将 tooltip 框限制在图表的区域内
Boolean
true
false
beforeSetOption
万能方法,图表渲染之前执行
Function
null
function (option, chart) {
return '执行对 option 的修改,绑定自定义事件等'
}
return '执行对 option 的修改,绑定自定义事件等'
}
afterSetOption
万能方法,图表渲染之后执行
Function
null
function (option, chart) {
return '执行对 option 的修改,绑定自定义事件等'
}
return '执行对 option 的修改,绑定自定义事件等'
}
支持方法
方法名
说明
例子
renderChart
渲染图表
chartRef?.value?.renderChart()
clearChart
清除图表
chartRef?.value?.clearChart()