You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Syncfusion Flutter柱状图:对数Y轴下小于1值致柱子倒置问题

Syncfusion Flutter Chart对数Y轴柱子倒置问题解决方案

当使用Syncfusion Flutter Chart的对数Y轴且所有数据值均小于1时,柱状图柱子会向下生长,导致视觉误导、数据展示不准确。以下是问题分析和解决方法:

已尝试的无效方案

  • 设置对数轴isInverted: true:柱子虽翻转,但Y轴数值变为从高到低显示,不符合需求
  • 将对数Y轴最小值设为数据最低值:无法解决柱子倒置问题

问题代码片段

class Values {
  final String x;
  final double y;

  Values(this.x, this.y);
}

List<Values> getChartData() {
  return [
    Values('CFI', 0.3),
    Values('CC', 0.1),
    Values('BLC', 0.03),
    Values('BDA', 0.01),
  ];
}

SfCartesianChart(
  title: ChartTitle(text: "Bar Graph"),
  primaryXAxis: const CategoryAxis(
    title: AxisTitle(
      text: 'Headers',
      textStyle: TextStyle(color: Color.fromARGB(255, 4, 20, 63)),
    ),
  ),
  primaryYAxis: const LogarithmicAxis(
    title: AxisTitle(
      text: 'Values',
      textStyle: TextStyle(color: Color.fromARGB(255, 4, 20, 63)),
    ),
  ),
  series: <CartesianSeries>[
    ColumnSeries<Values, String>(
      dataSource: getChartData(),
      xValueMapper: (Values course, _) => course.x,
      yValueMapper: (Values course, _) => course.y,
      dataLabelSettings: DataLabelSettings(isVisible: true),
      color: Color.fromARGB(255, 4, 20, 63),
      pointColorMapper: (Values data, _) => data.y < 20 ? Colors.red : Color.fromARGB(255, 4, 20, 63),
    ),
  ],
)

解决方法

核心思路是指定柱子的基准线为对数轴的最小值,让柱子从最小值向上延伸到数据点:

  1. 给LogarithmicAxis设置一个小于所有数据的minimum值(比如0.001,确保所有数据都在该值之上)
  2. 在ColumnSeries中设置baseValue为这个最小值,定义柱子的起始位置

修改后的代码:

SfCartesianChart(
  title: ChartTitle(text: "Bar Graph"),
  primaryXAxis: const CategoryAxis(
    title: AxisTitle(
      text: 'Headers',
      textStyle: TextStyle(color: Color.fromARGB(255, 4, 20, 63)),
    ),
  ),
  primaryYAxis: const LogarithmicAxis(
    title: AxisTitle(
      text: 'Values',
      textStyle: TextStyle(color: Color.fromARGB(255, 4, 20, 63)),
    ),
    minimum: 0.001, // 设置小于所有数据的最小值
  ),
  series: <CartesianSeries>[
    ColumnSeries<Values, String>(
      dataSource: getChartData(),
      xValueMapper: (Values course, _) => course.x,
      yValueMapper: (Values course, _) => course.y,
      dataLabelSettings: DataLabelSettings(isVisible: true),
      color: Color.fromARGB(255, 4, 20, 63),
      pointColorMapper: (Values data, _) => data.y < 20 ? Colors.red : Color.fromARGB(255, 4, 20, 63),
      baseValue: 0.001, // 柱子从该值开始向上生长
    ),
  ],
)

原理说明

对数轴中,数值小于1时对应的对数为负数,默认柱子会从0(对数负无穷方向)向下延伸。通过设置minimum为一个极小的正值,同时将柱子的baseValue设为该值,柱子就会从这个基准线向上延伸到数据点,既保留对数轴的刻度逻辑,又实现了柱子向上生长的视觉效果。

内容的提问来源于stack exchange,提问作者Vidhaan appaji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 15:25:12