Jump to content

Looping value increase resulting in odd and even numbers


engy123

Recommended Posts

I'm using the following function/php code to create some JSON for some NVD3 charts I am using. The problem I have is that for each KEY, the $i variable will count even and odd numbers. Here is the data below as you can see in the first line, every x:counts as 0, 2, 4 , 6.

and for KEY:IDNB it counts 1,3,5,7.

I need them all to count equally so that they are against the same line on my graph.

How can I make them count 1,2,3,4  for both "keys"

[{"key":"IDNA","values":[{"x":0,"y":387.73},{"x":2,"y":388.57},{"x":4,"y":388.04},{"x":6,"y":387.94},{"x":8,"y":388.24},{"x":10,"y":388.44},{"x":12,"y":387.99},{"x":14,"y":388.53},{"x":16,"y":388.79},{"x":18,"y":388.6},{"x":20,"y":388.55},{"x":22,"y":388.48},{"x":24,"y":388.72},{"x":26,"y":389.02},{"x":28,"y":388.59},{"x":30,"y":388.07},{"x":32,"y":388.14},{"x":34,"y":388.58},{"x":36,"y":388.12},{"x":38,"y":388.76},{"x":40,"y":388.15},{"x":42,"y":388.26},{"x":44,"y":388.09},{"x":46,"y":388.58},{"x":48,"y":386.85},{"x":50,"y":388.23},{"x":52,"y":388.16},{"x":54,"y":388.83},{"x":56,"y":388.79},{"x":58,"y":388.74},{"x":60,"y":388.78},{"x":62,"y":388.54},{"x":64,"y":388.36},{"x":66,"y":388.7},{"x":68,"y":388.54},{"x":70,"y":388.02},{"x":72,"y":388.05},{"x":74,"y":388.31},{"x":76,"y":388.65},{"x":78,"y":388.98},{"x":80,"y":387.78},{"x":82,"y":389.42},{"x":84,"y":388.86},{"x":86,"y":388.66},{"x":88,"y":388.51},{"x":90,"y":388.27},{"x":92,"y":389},{"x":94,"y":387.8},{"x":96,"y":388.18},{"x":98,"y":388.66}]},{"key":"IDNB","values":[{"x":1,"y":387.33},{"x":3,"y":388.5},{"x":5,"y":387.75},{"x":7,"y":387.98},{"x":9,"y":388.57},{"x":11,"y":388.23},{"x":13,"y":387.83},{"x":15,"y":388.31},{"x":17,"y":388.99},{"x":19,"y":388.7},{"x":21,"y":388.49},{"x":23,"y":388.21},{"x":25,"y":388.4},{"x":27,"y":389.05},{"x":29,"y":388.26},{"x":31,"y":387.88},{"x":33,"y":388.2},{"x":35,"y":388.3},{"x":37,"y":388.08},{"x":39,"y":388.29},{"x":41,"y":388.04},{"x":43,"y":387.91},{"x":45,"y":388.23},{"x":47,"y":388.47},{"x":49,"y":386.83},{"x":51,"y":387.55},{"x":53,"y":388.03},{"x":55,"y":388.98},{"x":57,"y":388.7},{"x":59,"y":388.94},{"x":61,"y":388.47},{"x":63,"y":388.5},{"x":65,"y":387.66},{"x":67,"y":388.17},{"x":69,"y":388.29},{"x":71,"y":387.73},{"x":73,"y":388},{"x":75,"y":388.36},{"x":77,"y":388.35},{"x":79,"y":388.75},{"x":81,"y":387.36},{"x":83,"y":389.32},{"x":85,"y":388.74},{"x":87,"y":388.86},{"x":89,"y":388.57},{"x":91,"y":388.21},{"x":93,"y":388.74},{"x":95,"y":387.84},{"x":97,"y":388.18},{"x":99,"y":388.7}]}]

Here is the PHP im using to make the data from a query

$lrs = CDB::ExecuteQuery($sql);
if($lrs) {
$jsonData = convert($lrs);
}
function convert($lrs) {
$i = 0;
$intermediate = array();

while ($vals = CDB::GetAssoc($lrs)) {
$key = $vals['VARIABLE'];
$x = $i;
$y = $vals['VAR_MEASURE_1'];
$intermediate[$key][] = array('x' => $x, 'y' => $y);
$i++;
}

$output = array();
foreach($intermediate as $key => $values) {
$output[] = array(
"key" => $key,
'values' => $values
);
}

return json_encode($output, JSON_NUMERIC_CHECK);


}

can anyone point me in the right direction?

 

Link to comment
Share on other sites

Well, if you number the elements as they come in, then of course you'll end up with arbitrary x values within one group, and you won't get the same value again.

 

Instead, number the elements per group. For example:

$intermediate[$key][] = array('x' => count($intermediate[$key][]), 'y' => $y);

The first element of a group will have the number 0, the second is 1 etc. If you want to start with 1, simply add 1 to the result of count().

Link to comment
Share on other sites

Well, if you number the elements as they come in, then of course you'll end up with arbitrary x values within one group, and you won't get the same value again.

 

Instead, number the elements per group. For example:

$intermediate[$key][] = array('x' => count($intermediate[$key][]), 'y' => $y);

The first element of a group will have the number 0, the second is 1 etc. If you want to start with 1, simply add 1 to the result of count().

 

 

Hi mate, I tried putting this into my code - 

$intermediate[$key][] = array('x' => count($intermediate[$key][]), 'y' => $y);

and i got the following error -  Fatal error: Cannot use [] for reading in C:\wamp\www\multibar.html.php on line 23

 

 

So i tried  -

 $intermediate[$key][] = array('x' => count($intermediate[$key]), 'y' => $y);

But got lots of un-identified index errors, although the X was counting correctly in the data output.

 

 

 

Any ideas?

 

Thanks

Edited by engy123
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.