C3 bar chart unknown number of bars

Marc Rasmussen

I am using C3.js to create a bar chart.

My case is as such:

I want to collect an X number of data and display them in my chart for this i have created the following script:

    $.ajax({
    type: 'POST',
    url: '/AcademyStat/academy_module_user_report',
    dataType: 'json',
    data: {
        request: 'ajax',
        team_id: team_id,
        module_id: module_id
    },
    success: function (data)
    {
        if(data != null)
        {
            for (var i = 0; i < data.length; i++)
            {
                var add_data = [data[i]['name'], data[i]['score']]
                char_data.push(add_data);
            }
            var chart = c3.generate({
                bindto: '#score_chart',
                data: {
                    columns: [
                        char_data

                    ],
                    type: 'bar'
                }
            });
        }
    }
});

As you can see from the above example i am looping through the data and then adding it to an array untill i have an array of array's (unknown number of data).

However when i run this in my browser i get the following error in the console:

    Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z

However if i instead use the method pop the data is displayed fine. The only problem with pop(); is that i would have to know how many arrays my char_data array has and that is impossible for me to know.

Has anyone tried something similar or know how i might fix this issue?

Gavin Hellyer

It looks like you are setting the charts columns to an array inside an array columns: [ [ ['name', 'score'] ] ]

Change the columns: [ char_data ] to columns: char_data

$.ajax({
    type: 'POST',
    url: '/AcademyStat/academy_module_user_report',
    dataType: 'json',
    data: {
        request: 'ajax',
        team_id: team_id,
        module_id: module_id
    },
    success: function (data) {
        if (data != null) {
            for (var i = 0; i < data.length; i++) {
                var add_data = [data[i]['name'], data[i]['score']]
                char_data.push(add_data);
            }
            var chart = c3.generate({
                bindto: '#score_chart',
                data: {
                    columns: char_data,
                    type: 'bar'
                }
            });
        }
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c3 bar chart chart - client want numbers above the bars

From Dev

C3 bar chart - JSON input

From Dev

Bars chart for different number of bars in D3.js

From Dev

C3 / D3 bar chart with horizontal scroll

From Dev

Gnuplot Bar chart with error bars

From Dev

unresponsive bars in otherwise responsive grouped d3 bar chart

From Dev

Adding Error Bars to Grouped Bar Chart with D3.js

From Dev

How to add text legends in bars of a bar chart in D3?

From Dev

D3 Bar Chart Bars Color Scaling

From Dev

How to add text legends in bars of a bar chart in D3?

From Dev

Finding the mean of bars in bar chart - d3

From Dev

unresponsive bars in otherwise responsive grouped d3 bar chart

From Dev

D3: Stacked bar chart exit().remove() not removing bars

From Dev

d3.js label bars of bar chart

From Dev

Why are some bars are off center in d3 bar chart?

From Dev

D3 - Stacked bar chart, position bars

From Dev

C3 bar chart width is not working properly

From Dev

Generating grouped bar chart in R program with uneven number of bars per group

From Dev

How to control number of bars in a bar chart based on date selection by user in Tableau

From Dev

How to add space between bars in a grouped bar chart in a nvd3 grouped multibar chart?

From Dev

R lattice bar chart: choose order of bars?

From Dev

Choosing order of bars in Bokeh bar chart

From Dev

How to determine the order of bars in a matplotlib bar chart

From Dev

Cannot align ticks to bars in bar chart

From Dev

Different Color Bars for Flot Categories Bar Chart

From Dev

Labels for Bars in R Lattice Bar Chart

From Dev

How to combine two bars of a bar chart in tableau?

From Dev

Is there a way to have groupings of bars in a bar chart in?

From Dev

PHP pChart change color of bars of bar chart

Related Related

  1. 1

    c3 bar chart chart - client want numbers above the bars

  2. 2

    C3 bar chart - JSON input

  3. 3

    Bars chart for different number of bars in D3.js

  4. 4

    C3 / D3 bar chart with horizontal scroll

  5. 5

    Gnuplot Bar chart with error bars

  6. 6

    unresponsive bars in otherwise responsive grouped d3 bar chart

  7. 7

    Adding Error Bars to Grouped Bar Chart with D3.js

  8. 8

    How to add text legends in bars of a bar chart in D3?

  9. 9

    D3 Bar Chart Bars Color Scaling

  10. 10

    How to add text legends in bars of a bar chart in D3?

  11. 11

    Finding the mean of bars in bar chart - d3

  12. 12

    unresponsive bars in otherwise responsive grouped d3 bar chart

  13. 13

    D3: Stacked bar chart exit().remove() not removing bars

  14. 14

    d3.js label bars of bar chart

  15. 15

    Why are some bars are off center in d3 bar chart?

  16. 16

    D3 - Stacked bar chart, position bars

  17. 17

    C3 bar chart width is not working properly

  18. 18

    Generating grouped bar chart in R program with uneven number of bars per group

  19. 19

    How to control number of bars in a bar chart based on date selection by user in Tableau

  20. 20

    How to add space between bars in a grouped bar chart in a nvd3 grouped multibar chart?

  21. 21

    R lattice bar chart: choose order of bars?

  22. 22

    Choosing order of bars in Bokeh bar chart

  23. 23

    How to determine the order of bars in a matplotlib bar chart

  24. 24

    Cannot align ticks to bars in bar chart

  25. 25

    Different Color Bars for Flot Categories Bar Chart

  26. 26

    Labels for Bars in R Lattice Bar Chart

  27. 27

    How to combine two bars of a bar chart in tableau?

  28. 28

    Is there a way to have groupings of bars in a bar chart in?

  29. 29

    PHP pChart change color of bars of bar chart

HotTag

Archive