1.基础用法
vue
<template>
<bar-line-chart2 v-bind="chartOption" ref="chartRef"></bar-line-chart2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const xAxisData = ['1月', '1-2月', '1-3月', '1-4月', '1-5月', '1-6月', '1-7月', '1-8月', '1-9月'];
const seriesData = [
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [163, 129, 123, 198, 152, 152, 178, 133, 193]
},
{
type: 'line',
yAxisIndex: 1,
data: [81, 17, 30, 94, 45, 54, 60, 1, 69]
}
];
const yAxisName = ['亿元', '%'];
const legendData = ['销售额', '贸易额'];
const chartOption = {
xAxisData,
seriesData,
yAxisName,
legendData
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 364px;
background-color: rgb(3, 43, 68);
}
</style>2.柱宽度调整
vue
<template>
<bar-line-chart2 v-bind="chartOption" ref="chartRef"></bar-line-chart2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const xAxisData = ['1月', '1-2月', '1-3月', '1-4月', '1-5月', '1-6月', '1-7月', '1-8月', '1-9月'];
const seriesData = [
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [163, 129, 123, 198, 152, 152, 178, 133, 193]
}
];
const yAxisName = ['亿元'];
const legendData = ['销售额'];
const chartOption = {
xAxisData,
seriesData,
yAxisName,
legendData
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 364px;
background-color: rgb(3, 43, 68);
}
</style>3.折线辅助线
vue
<template>
<bar-line-chart2 v-bind="chartOption" ref="chartRef"></bar-line-chart2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const xAxisData = ['1月', '1-2月', '1-3月', '1-4月', '1-5月', '1-6月', '1-7月', '1-8月', '1-9月'];
const seriesData = [
{
type: 'line',
yAxisIndex: 0,
data: [81, 17, 30, 94, 45, 54, 60, 1, 69]
}
];
const color = ['yellow'];
const yAxisName = ['%'];
const legendData = ['规上盈利性服务增速'];
const showLineArea = true;
const markLine = [
{
value: 44,
yAxisIndex: 0,
color: '#33FFBB'
},
{
value: 77,
yAxisIndex: 0,
color: '#F74768'
}
];
const chartOption = {
xAxisData,
seriesData,
yAxisName,
legendData,
markLine,
color,
showLineArea
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 364px;
background-color: rgb(3, 43, 68);
}
</style>4.多柱
vue
<template>
<bar-line-chart2 v-bind="chartOption" ref="chartRef"></bar-line-chart2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const xAxisData = ['1月', '1-2月', '1-3月', '1-4月', '1-5月', '1-6月', '1-7月', '1-8月', '1-9月'];
const seriesData = [
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [163, 129, 123, 198, 152, 152, 178, 133, 193]
},
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [80, 78, 97, 86, 114, 97, 91, 109, 114]
}
];
const yAxisName = ['亿元'];
const legendData = ['销售额', '贸易额'];
const color = ['blue', 'grey'];
const chartOption = {
xAxisData,
seriesData,
yAxisName,
legendData,
color
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 364px;
background-color: rgb(3, 43, 68);
}
</style>5.自定义x轴label
vue
<template>
<bar-line-chart2 v-bind="chartOption" ref="chartRef"></bar-line-chart2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
let xAxisLabelStart = 0;
const xAxisData = ['1-3月', '4-6月', '7-9月', '10-12月', '1-3月', '4-6月', '7-9月', '10-12月'];
const seriesData = [
{
// 需要指定类型
type: 'line',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [63, 129, 123, 198, 152, 152, 178, 133]
}
];
const yAxisName = ['亿元'];
const legendData = ['销售额'];
const color = ['yellow'];
const showLineArea = true;
const beforeSetOption = option => {
option.xAxis.forEach(item => {
Object.assign(item.axisLabel, {
margin: 0,
interval: 0,
formatter: (param) => {
// console.log(param, a, b, c, d)
return param.startsWith('1-') ? `{split|}{gap|}{label|${ param }}` : `{label|${ param }}`
},
rich: {
split: { width: 1, height: 32, backgroundColor: 'red' },
gap: { width: 12 },
label: { fontSize: 16, lineHeight: 32, verticalAlign: 'bottom' }
}
});
});
};
const afterSetOption = (option, chart) => {
chart.on('dataZoom', function () {
const count = xAxisData.length - 1;
const start = arguments[0].batch[0].start;
xAxisLabelStart = Math.round(start / 100 * count);
});
};
const chartOption = {
xAxisData,
seriesData,
yAxisName,
legendData,
color,
showLineArea,
beforeSetOption,
afterSetOption
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 364px;
background-color: rgb(3, 43, 68);
}
</style>6.指定区域高亮
vue
<template>
<bar-line-chart2 v-bind="chartOption" ref="chartRef"></bar-line-chart2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const xAxisData = ['1月', '1-2月', '1-3月', '1-4月', '1-5月', '1-6月', '1-7月', '1-8月', '1-9月'];
const seriesData = [
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [163, 129, 123, 198, 152, 152, 178, 133, 193]
},
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [80, 78, 97, 86, 114, 97, 91, 109, 114]
},
{
// 需要指定类型
type: 'line',
// 需要指定 y 轴索引
yAxisIndex: 1,
data: [80, 78, 97, 86, 114, 97, 91, 109, 114]
}
];
const yAxisName = ['亿元', '%'];
const legendData = ['销售额', '贸易额', '增速'];
const color = ['blue', 'grey', 'yellow'];
const xAxisHighlightArea = [2, 3, 4];
const chartOption = {
xAxisData,
seriesData,
yAxisName,
legendData,
color,
xAxisHighlightArea
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 364px;
background-color: rgb(3, 43, 68);
}
</style>7.拖拽区域为滚动条
vue
<template>
<bar-line-chart2 v-bind="chartOption" ref="chartRef"></bar-line-chart2>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const chartRef = ref();
const xAxisData = ['1月', '1-2月', '1-3月', '1-4月', '1-5月', '1-6月', '1-7月', '1-8月', '1-9月'];
const seriesData = [
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [163, 129, 123, 198, 152, 152, 178, 133, 193]
},
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [80, 78, 97, 86, 114, 97, 91, 109, 114]
},
{
// 需要指定类型
type: 'line',
lineType: 'dashed',
// 需要指定 y 轴索引
yAxisIndex: 1,
data: [80, 78, 97, 86, 114, 97, 91, 109, 114]
}
];
const yAxisName = ['亿元', '%'];
const legendData = ['销售额', '贸易额', '增速'];
const color = ['blue', 'grey', 'yellow'];
const xAxisHighlightArea = [2, 3, 4];
const showCount = 4;
const dataZoomType = 'slider';
const chartOption = {
xAxisData,
seriesData,
yAxisName,
legendData,
color,
xAxisHighlightArea,
showCount,
dataZoomType
};
onMounted(() => chartRef.value.renderChart());
</script>
<style lang="scss" scoped>
.zrx-chart {
height: 364px;
background-color: rgb(3, 43, 68);
}
</style>属性
属性名
说明
类型
默认值
参考值
xAxisData
x 轴坐标
Array
[]
['1月', '1-2月', '1-3月', '1-4月', '1-5月', '1-6月', '1-7月']
grid
上下左右边距
Object
({
top: 109,
right: 76,
bottom: 38,
left: 53
})
top: 109,
right: 76,
bottom: 38,
left: 53
})
{ top: 84, right: 18, bottom: 56, left: 56 }
seriesData
数据数组
Array
[]
[
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [163, 129, 123, 198, 152, 152, 178]
},
{
type: 'line',
yAxisIndex: 1,
data: [81, 17, 30, 94, 45, 54, 60]
}
]
{
// 需要指定类型
type: 'bar',
// 需要指定 y 轴索引
yAxisIndex: 0,
data: [163, 129, 123, 198, 152, 152, 178]
},
{
type: 'line',
yAxisIndex: 1,
data: [81, 17, 30, 94, 45, 54, 60]
}
]
yAxisName
y轴单位
String, Array
['']
['亿元', '%']
showCount
最多显示的数量(实际显示数量会根据输入值调整)
Number
5
3
dataZoomType
何种方式拖动 inside 内容区域拖动,slider 滑块拖动
String
'inside'
'slider'
dataZoomBottom
当 dataZoomType 为 slider 时,拖动区域距离底部的距离
Number
0
12
showLegend
是否显示 legend
Boolean
true
false
legendData
legend 数据
Array
[]
['统计金额', '开票金额']
markLine
标记线
Array
[]
[
{
value: 134,
yAxisIndex: 0,
color: '#33FFBB'
},
{
value: 166,
yAxisIndex: 0,
color: '#F74768'
}
]
{
value: 134,
yAxisIndex: 0,
color: '#33FFBB'
},
{
value: 166,
yAxisIndex: 0,
color: '#F74768'
}
]
color
图表项颜色
Array
['blue', 'yellow', 'grey']
['blue', 'yellow', 'grey']
showLineArea
线条是否显示区域颜色
Boolean
false
true
tooltipTitle
tooltip 标题
Array
null
['标题A']
xAxisHighlightArea
高亮区域的索引
Array
[]
[2, 4]
dataZoomStartAtEnd
从末尾开始显示图表
Boolean
true
false
smooth
是否平滑
Boolean, Number
false
true
zoomLock
是否锁定选择区域的大小
Boolean
false
true
tooltipConfine
是否将 tooltip 框限制在图表的区域内
Boolean
true
false
scale
图表缩放比例
Number
1
2
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()