juliansteenbakker / community_charts

This project is a fork of google/charts.
Apache License 2.0
84 stars 34 forks source link

[Pie Chart] Hide label only when value is 0 #23

Closed pmatatias closed 3 months ago

pmatatias commented 3 months ago

Is it possible to hide the label when the value is 0?

for example:

  static List<charts.Series<LinearSales, int>> _createSampleData() {
    final data = [
      new LinearSales(0, 100),
      new LinearSales(1, 0),
      new LinearSales(2, 75),
      new LinearSales(3, 0),
      new LinearSales(4, 0),
      new LinearSales(5, 0),
    // others zero value 
    ];

    return [
      new charts.Series<LinearSales, int>(
        id: 'Sales',
        domainFn: (LinearSales sales, _) => sales.year,
        measureFn: (LinearSales sales, _) => sales.sales,
        data: data,
        // Set a label accessor to control the text of the arc label.
        labelAccessorFn: (LinearSales row, _) => '${row.year}: ${row.sales}',
      )
    ];
  }

Current result:

Screenshot_20240524_150851

or when i set the showLeaderLines:false, its only hide the line.

Screenshot_20240524_150818


what i expect is, i can conditionally hide and show label. maybe something like:

 defaultRenderer:
            charts.ArcRendererConfig(arcWidth: 120, arcRendererDecorators: [
          charts.ArcLabelDecorator(
            showLeaderLines:  (value !=0 ),
            outsideLabelStyleSpec: (if value==0 => hide)
            ),
        ])

expected results :

Screenshot_20240524_151014

TIA

pmatatias commented 3 months ago

I just found workaround for this, by add conditional labelAccessorFn. Maybe for someone who face same issue, you can try this,

labelAccessorFn: (LinearSales row, _) => row.sales == 0 ? '' :  '${row.year}: ${row.sales}'

Thank you.