Search code examples
javascriptd3.jsc3.jsc3

Setting the graph name in C3.js


I have a graph that in which I load new data, using the following function:

function insertGraph(yAxis, xAxis, Header) {
    setTimeout(function () {
        chart.load ({
            bindto: "#graph",
            xs: {
                'y':'x'
            },
            columns: [
                yAxis, 
                xAxis
            ]
        });
    }, 100);
}

An Example of values for yAxis, xAxis, and Header that are passed in look as follows:

yAxis:

["y", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

xAxis:

["x", 0, 131.35, 26971.3, 27044.75, 27351.4, 27404.483333333334, 27419.416666666668, 33128.96666666667, 33549.13333333333, 34049.48333333333, 77464.26666666666, 77609.71666666666, 174171.85, 259166.98333333334]

Header:

MakeModeChange

Everything works great except that when the chart is loaded it gives the data name "y", I need it to have Header (in this case MakeModeChange) as the data name. I tried using name like the code below but nothing happened:

function insertGraph(yAxis, xAxis, Header) {
    setTimeout(function () {
        console.log(Header);
        console.log(yAxis);
        chart.load ({
            bindto: "#graph",
            xs: {
                'y':'x'
            },
            columns: [
                yAxis, 
                xAxis
            ],
            names: {
                y: 'Header'
            }   
        });
    }, 100);
}

I also tried changing what I passed into yAxis to look like this:

["MakeModeChange", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

And then change the load function to look like this:

function insertGraph(yAxis, xAxis, Header) {
    setTimeout(function () {
        chart.load ({
            bindto: "#graph",
            xs: {
                Header:'x'
            },
            columns: [
                yAxis, 
                xAxis
            ], 
        });
    }, 100);
}

But then I get the following error:

Uncaught Error: x is not defined for id = "MakeModeChange".

Any idea how to make this work?


Solution

  • Are your x & y axis variable/arrays the wrong way round? I would have expected the x-axis values to be 1, 2, 3, 4, etc and the y-axis values to be the 0, 131.35, etc.

    Be that as it may, the y value in the array will be the series name, and then you use the xs object to specify the array of x-values. The name of this x-values array is irrelevant.

    See/run the code snippet below.

    function createGraph(xAxis, yAxis, Header) {
    
      // create the xs object with a key name of the header variable
      var myxs = {};
      myxs[Header] = 'x';
      
      // set the 1st position value in the yaxis to the header
      yAxis[0] = Header;
      
      c3.generate({
        data: {
          xs: myxs,
          columns: [
            xAxis,
            yAxis
    
          ]
        }
      });
    }
    
    createGraph(
      ["x", 0, 131.35, 26971.3, 27044.75, 27351.4, 27404.483333333334, 27419.416666666668, 33128.96666666667, 33549.13333333333, 34049.48333333333, 77464.26666666666, 77609.71666666666, 174171.85, 259166.98333333334],
      ["y", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
      "Variable Name"
    );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    <div id='chart' />