Skip to content
产品中心
setOptionlegend

色标。默认色块、横向、底部居中,宽度 100%。不写 pieces 时用产品默认色标。

调用

ts
chart.setOption({
    legend: {
        show: true,
        type: 'rect'
    }
})
  • type: 'rect':色块。点击可显示或隐藏对应区间
  • type: 'line':连续色条。拖动两端滑块按数值过滤,也可用 range 设初始范围

show: false 时不画、不占位,可用 getLegend() 取当前色标后自己画。

字段

字段说明类型可选值默认
show是否显示booleantrue / falsetrue
type图例类型。rect 色块,line 连续色条stringrect / linerect
pieces各档颜色和文字。value 为区间下沿object[]产品默认
rangeline 模式下选中的数值范围。不写则全部显示[number, number]全开
direction排列方向stringhorizontal / verticalhorizontal
width / height整块图例(含文字)。像素或相对容器的百分比number / string横向宽 100%
x水平位置。关键字、像素或百分比string / numberleft / center / rightcenter
y垂直位置。关键字、像素或百分比string / numbertop / bottom / center横向默认 bottom
radius色条圆角,像素number0
label图例文字object显示

containLabeltrue 时,横向图例限制在绘图区内。

pieces

字段说明类型可选值默认
show该档是否显示。点击色块会切换booleantrue / falsetrue
value区间下沿,到下一段之前用此色number
color颜色string
label色标文字。'-' 表示该档不画字;不传则回退为 valuestringvalue
ts
legend: {
    pieces: [
        { show: true, value: 0, color: '#0426cc', label: '0' },
        { show: true, value: 1500, color: '#1a5cff', label: '1.5e+3' },
    ],
}

label

字段说明类型可选值默认
show是否显示文字booleantrue / falsetrue
position文字位置。横向用 top / bottom,纵向用 left / rightstringtop / bottom / left / right横向 bottom,纵向 right
fontFamily字体stringsans-serif
fontSize字号,像素number14
color文字颜色string#6b7280

联动

ts
chart.on('legendselectchanged', payload => {})
chart.dispatchAction({ type: 'legendToggleSelect', index: 0 })
chart.dispatchAction({ type: 'legendRange', min, max })

在线示例

在线体验见 示例 · 图例配置。自己画图例见 自定义图例