Appearance
正负值柱状图(涨跌对比)
功能说明(What)
正负值柱状图用于展示涨跌或盈亏变化,通过 0 基线强调正负方向。
应用场景(When)
- 盈亏对比
- 增长/下降变化
快速上手(How - Quick)
ts
import { BaseBar } from '@ymhc/harmonycharts'; // 引入基础柱状图组件
import type { BaseBarConfig } from '@ymhc/harmonycharts'; // 引入柱状图配置类型
@Entry // 入口页面
struct Index { // 页面结构体
@Prop baseBarConfig: BaseBarConfig = { // 组件配置(可响应)
categories: ['Mon', 'Tue', 'Wed'], // X 轴类目
series: [{ name: 'Delta', data: [120, -90, 140] }], // 正负值数据
grid: { top: 24, left: 16, right: 16, bottom: 28 }, // 画布边距
tooltip: { show: true, trigger: 'axis', confine: true }, // 轴触发提示
legend: { show: true }, // 图例开关
yAxis: { // Y 轴配置
type: 'value', // 数值轴
includeZero: true, // 包含 0
baseline: { show: true, value: 0, lineStyle: { color: '#E74C3C', width: 1 } } // 0 基线
}
}; // 配置结束
build() { // 页面构建
Column() { // 纵向布局
BaseBar({ config: this.baseBarConfig, chartHeight: 220 }) // 渲染柱状图
} // 布局结束
} // 构建结束
}完整示例配置(整页可用)
ts
const option: HarmonyChartsOption = {
grid: { top: 24, left: 16, right: 16, bottom: 28 },
tooltip: { show: true, trigger: 'axis', confine: true },
legend: { show: true },
xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed'] },
yAxis: {
type: 'value',
includeZero: true,
baseline: { show: true, value: 0, lineStyle: { color: '#E74C3C', width: 1 } }
},
series: [
{ type: 'bar', name: 'Delta', data: [120, -90, 140] }
]
};示例图
配置项说明(API)
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| baseline | 0 基线 | BaselineOption | 未设置 |
事件与交互(Events)
ts
chart.on('click', (params) => {
// params.seriesType === 'bar'
});高级用法(Advanced)
- baseline 颜色可用于强调正负分界
注意事项 / 设计说明(Notes)
- 建议开启 baseline 以突出正负分界。