c3js / c3

:bar_chart: A D3-based reusable chart library
http://c3js.org
MIT License
9.34k stars 1.39k forks source link

0.7.5 release breaks pie chart #2688

Closed alexhisen closed 5 years ago

alexhisen commented 5 years ago

The following code works with 0.7.4:

c3.generate({
        bindto: `#mydiv`,
        size: { width: 150, height: 150 },
        padding: { right: 0, left: 0, top: 0, bottom: 0 }, // no effect but possible perf. optimization
        transition: {
          duration: 500,
        },
        interaction: {
          enabled: true,
        },
        pie: {
          label: {
            // By default c3 renders all values with floating point (10.0%)
            // This formatting function takes the number and renders it as: 10%
            format: (value) => {
              return `${value}%`;
            },
            /* threshold: 0.05 (5%) is the default which sticks out a bit but ok, 0.08 fits perfectly */
          },
        },
        data: {
          columns: [
            'r',
            'l',
            'd',
          ],
          type: 'pie',
          colors: {
            'r': 'red',
            'l': 'green',
            'd': 'blue',
          },
        },
        legend: {
          show: false,
        },
      });

      this.chart.load(
        {
          columns: [
            ['r', 75],
            ['l', 20],
            ['d', 5],
          ],
        },
      );

But with 0.7.5, it throws this error:

c3.js:7049 Uncaught TypeError: Reduce of empty array with no initial value
    at Array.reduce (<anonymous>)
    at ChartInternal.getTotalDataSum (c3.js:7049)
    at ChartInternal.getRatio (c3.js:7519)
    at ChartInternal.textForArcLabel (c3.js:5562)
    at SVGTextElement.<anonymous> (d3-selection.js:585)
    at Selection.selection_each [as each] (d3-selection.js:365)
    at Selection.selection_text [as text] (d3-selection.js:592)
    at ChartInternal.redrawArc (c3.js:5880)
    at ChartInternal.redraw (c3.js:1816)
    at ChartInternal.initWithData (c3.js:1506)
panthony commented 5 years ago

Sorry about that I'll add a test case for this

panthony commented 5 years ago

@alexhisen Thanks for reporting, this will be fix in the next release, sorry for that regression.

In the mean time you may be able to simply workaround the issue by passing '0' as initial value if that works for you.