【问题标题】:How to change legend icon in pie chart, i am using charts_flutter 0.9.0如何更改饼图中的图例图标,我使用的是 charts_flutter 0.9.0
【发布时间】:2020-08-12 13:13:04
【问题描述】:

我正在尝试将我的饼图的图例图标从圆形更改为矩形。我为此使用以下几行,但马上就出错了。

defaultRenderer: new charts.ArcRendererConfig(
          symbolRenderer: new IconRenderer(Icons.cloud)
      ),

我收到错误消息(附截图) 请帮我更改饼图中图例的默认图标。

【问题讨论】:

  • 您遇到的错误是什么?
  • @MidhunMP 即使它没有编译,android studio 本身也会像这样弹出错误 - defaultRenderer: new charts.ArcRendererConfig( symbolRenderer: new IconRenderer(Icons.cloud) ),

标签: flutter dart charts legend


【解决方案1】:

感谢@Midhun MP 的提示,实际上我们需要使用CustomSymbolRenderer 而不是SymbolRenderer。下面的代码解决了我的问题

class IconRenderer extends charts.CustomSymbolRenderer {
  final IconData iconData;

  IconRenderer(this.iconData);

  @override
  Widget build(BuildContext context, {Size size, Color color, bool enabled}) {
    // Lighten the color if the symbol is not enabled
    // Example: If user has tapped on a Series deselecting it.
    if (!enabled) {
      color = color.withOpacity(0.26);
    }
    return new SizedBox.fromSize(
        size: size, child: new Icon(iconData, color: color, size: 12.0));
  }
}

【讨论】:

    【解决方案2】:

    您收到该错误是因为默认情况下没有 IconRenderer 类。您必须创建一个自定义 SymbolRenderer,如下所示:

    class IconRenderer extends charts.SymbolRenderer {
      final IconData iconData;
    
      IconRenderer(this.iconData);
    
      @override
      Widget build(BuildContext context,
          {Size size, Color color, bool isSelected}) {
        return new SizedBox.fromSize(
            size: size, child: new Icon(iconData, color: color, size: 12.0));
      }
    }
    

    参考legend_custom_symbol

    【讨论】:

    • 感谢@Midhun MP 的提示,实际上我们需要使用CustomSymbolRenderer 而不是SymbolRenderer。
    猜你喜欢
    • 2023-03-20
    • 2019-01-22
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多