0

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?

Mike
  • 785
  • 1
  • 13
  • 29

1 Answers1

1

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' />
Sean
  • 14,359
  • 13
  • 74
  • 124
  • The variables are the right way around (it's just using a count of the events and the crazy x values are a converted timestamp). And while I can get it to work the way you do above, you are predetermining the string in xs as `'Your Name`, I need it to be the value passed into `Header` in the function call (since I don't know what that value will be until it's passed in). – Mike May 27 '15 at 21:06
  • 1
    Ok, but it's easy to use a passed in variable. You simply use: `xAxis[0] = Header;` to get it into the first position of the array, and then your `xs` object simply needs to be `{ Header: 'y' }`. I'll update the snippet. – Sean May 28 '15 at 05:36
  • Perfect! The adjusted code worked exactly as I needed - thanks for the help! – Mike May 29 '15 at 14:19