infinum / flutter-charts

Customizable charts library for flutter.
https://pub.dev/packages/charts_painter
MIT License
144 stars 42 forks source link

Offset argument contained a NaN value. when ChartData.fromList receives a list with zeroes only. #40

Closed bettdouglas closed 2 years ago

bettdouglas commented 2 years ago

Hi, many thanks for this awesome package. It's really simple and intuitive to use.

I'm getting a bug when rendering ChartState.bar graph. When the bar ChartData.fromList() contains all zeroes, the error below is thrown. It renders okay, but there's a failing assertion.

Hope the example below is easily reproducable.

The relevant error-causing widget was
Chart<dynamic>
package:flutter/…/scheduler/binding.dart:862
(elided 6 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer, and dart:async-patch)
The following RenderObject was being processed when the exception was fired: _RenderChartDecoration<Object>#d1591
RenderObject: _RenderChartDecoration<Object>#d1591
    parentData: offset=Offset(0.0, 0.0)
    constraints: BoxConstraints(0.0<=w<=174.5, 0.0<=h<=46.4)
    size: Size(174.5, 36.4)
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
Offset argument contained a NaN value.
'dart:ui/painting.dart':
Failed assertion: line 43 pos 10: '<optimized out>'

The relevant error-causing widget was
Chart<dynamic>
lib/page.dart:312
  @override
  Widget build(BuildContext context) {
    final byCount = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    //final byCount = [0, 0, 0, 0, 0, 0, 1, 0, 0, 0];
    print(byCount);
    return chart.Chart(
      state: chart.ChartState.bar(
        chart.ChartData.fromList(
          byCount.map((e) => chart.BarValue(e.toDouble())).toList(),
        ),
        backgroundDecorations: [
          chart.GridDecoration(
            showHorizontalGrid: false,
            showVerticalGrid: false,
            showVerticalValues: true,
            verticalAxisValueFromIndex: (idx) => '${idx + 1}',
            gridWidth: 2,
            textStyle: Theme.of(context)
                .textTheme
                .subtitle2!
                .copyWith(fontSize: 8, fontWeight: FontWeight.bold),
            gridColor: Theme.of(context).dividerColor,
          ),
          chart.ValueDecoration(
            alignment: Alignment.topCenter,
            // valueGenerator: (_),
            textStyle:
                Theme.of(context).textTheme.subtitle1!.copyWith(fontSize: 8),
          ),
        ],
        // foregroundDecorations: [
        //   chart.HorizontalAxisDecoration(lineColor: Colors.brown),
        // ],
      ),
    );
}

Looking foward to your assistance. I tried digging into the code, but I got abit lost.

lukaknezic commented 2 years ago

Hey, thanks for using the lib 😄

I checked the code, thanks for providing the easily testable code example. The issue was with ValueDecoration when all items are 0. I fixed that now

You can use fix with reference for now, I will fix some other issues before releasing new version:

charts_painter:
  git: 
    url: https://github.com/infinum/flutter-charts
    ref: fix/value-decoration-fix
bettdouglas commented 2 years ago

Thank you so much for the fix. It's working well

lukaknezic commented 2 years ago

Version 2.0.0+2 includes this fix 😃

bettdouglas commented 2 years ago

Thanks for your support bro. Much love.