Skip to content

主题定制

ChartView 提供 set_theme 方法,用于控制图表中的所有颜色——背景、K 线、网格、文字、十字线以及默认绘图色板。

内置主题

开箱即用提供两套预设:

rust
use navi_chart::Theme;

let dark  = Theme::dark();   // 深色背景,柔和配色
let light = Theme::light();  // 白色背景,鲜明配色

在创建后立即应用:

rust
cv.set_theme(Theme::dark());

调用 set_theme 后图表会立即重绘。

完全自定义主题

直接构造 Theme 结构体字面量来显式设置每个颜色:

rust
use navi_chart::{Theme, Stroke, LineStyle};
use navi_types::Color;

cv.set_theme(Theme {
    bg:             Color::from_rgb(18, 18, 18),
    surface:        Color::from_rgb(30, 30, 30),
    grid:           Color::from_rgba(255, 255, 255, 26),
    text:           Color::from_rgb(220, 220, 220),
    text_dim:       Color::from_rgb(120, 120, 120),
    bull:           Color::from_rgb(0, 200, 100),
    bear:           Color::from_rgb(220, 50, 50),
    crosshair_line: Color::from_rgba(200, 200, 200, 100),
    bull_text:      Color::from_rgb(0, 200, 100),
    bear_text:      Color::from_rgb(220, 50, 50),
    highlight:           Color::from_rgba(255, 255, 255, 200),
    legend_bg:           Color::from_rgba(18, 18, 18, 204),
    selection_handle:    Color::from_rgb(100, 160, 255),
    selection_handle_bg: Color::from_rgb(255, 255, 255),
    last_price_line: Stroke {
        color: Color::from_rgba(120, 120, 140, 180),
        width: 1.0,
        style: LineStyle::Dashed,
    },
    plot_colors: [
        Color::from_rgb(100, 160, 255),
        Color::from_rgb(255, 200, 80),
        Color::from_rgb(180, 120, 240),
        Color::from_rgb(80, 210, 180),
        Color::from_rgb(255, 140, 80),
        Color::from_rgb(240, 100, 180),
        Color::from_rgb(150, 160, 255),
        Color::from_rgb(100, 210, 130),
    ],
});

也可以从预设出发修改个别字段:

rust
let mut theme = Theme::dark();
theme.bull = Color::from_rgb(0, 230, 118);
theme.bear = Color::from_rgb(255, 82, 82);
cv.set_theme(theme);

Theme 字段参考

字段类型说明
bgColor画布 / 图表背景色
surfaceColor表面颜色(标签、分隔线)
gridColor网格线颜色
textColor主要文字颜色
text_dimColor次要 / 暗淡文字颜色
bullColor阳线(上涨)K 线颜色
bearColor阴线(下跌)K 线颜色
crosshair_lineColor十字线颜色
bull_textColor阳线 OHLC 数值文字颜色
bear_textColor阴线 OHLC 数值文字颜色
highlightColor高亮(选中)K 线边框颜色
legend_bgColor图例 / 标签文字行的半透明背景色
selection_handleColor选中标注、plot 等图表元素时控制点圆点的描边(边框)颜色
selection_handle_bgColor选中控制点圆点的填充(背景)颜色
last_price_lineStroke最后一根 K 线收盘价处水平线及右轴标签的样式(颜色、线宽、虚实)。显示开关由 set_last_price_line_visible 控制。
plot_colors[Color; 8]默认绘图色板(脚本未指定颜色时循环使用)

颜色编码Color 是 RGBA 格式的紧凑 u32。使用 Color::from_rgb(r, g, b) 表示不透明颜色,使用 Color::from_rgba(r, g, b, a) 表示带透明度的颜色(alpha 0 = 完全透明,255 = 完全不透明)。

基于 MIT 许可证发布。