7

I am implementing Highcharts in my application. It needs data in specific format.

The data in my table is as follows

enter image description here

The javascript needs data in below format

enter image description here

When I var_dump my x_axis array and y_axis array, I get below result

enter image description here

Which php functions should I use to format my array elements and pass to that JavaScript in that format?

data:[
     [<? echo PHP_some_function(" ' ", x_axis) ?>, <? echo (y_axis) ?>]   //quotes for x, no quotes for y value
]  //Moreover it should run for all the values in x_axis and y_axis

I need some logic here..

My final graph would look like

enter image description here

3
  • you probably don't want to use those variables. You probably want to extract your data as more complete record sets, each record containing x_axis, y_axis, and possibly si_no, ordered by y_axis descending. Then it should be relatively straightforward Commented Dec 20, 2012 at 15:26
  • On the other side, that's one of the well asked questions I've seen recently! Commented Dec 20, 2012 at 15:30
  • This looks a lot like homework. Even the values are the same. stackoverflow.com/questions/13954081/… Commented Dec 20, 2012 at 16:33

4 Answers 4

2

The problem is your query.

It should be like the following.

SELECT x_axis, y_axis FROM yourTableName;

This way you'll get exactly the format that Highcharts needs. You just have to insert it inside an array.

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming:

$x_axis = array('Safari', 'Opera', 'Firefox', 'IE', 'Chrome', 'Others');
$y_axis = array(10, 6, 40.5, 20, 10.6, 0.5);

This should work:

$data = array();
$length = count($x_axis);
for ($i = 0; $i < $length; $i++) {
    $data[] = array($x_axis[i], $y_axis[i]);
}

4 Comments

This does not take into account the sort that was requested.
@ScottSauyet: OP did not request the data to be sorted, and I wouldn't be surprised if that graph's library took care of that
but look at the required data format, which has the data in a sorted order, one which does not match the input order.
"@ScottSauyet Its ok if there is no sort. – StrataGeeks CEO 9 hours ago" - OP on another answer. Don't nitpick what doesn't need to be fixed.
1

[<? echo "'". json_encode($x_axis). "', " . json_encode($y_axis) ?>]

Demo: http://codepad.org/G5JAtXWu

Nix that I was confused.

What you want to do is this:

foreach($x_axis as $key=>$x) {
    echo "['" . $x . "', " . $y_axis[$key] . "]";
}

Demo: http://codepad.org/SKNk1VaX

To wrap it all up:

$d = array();
foreach($x_axis as $key=>$x) {
    $d[] = "['" . $x . "', " . $y_axis[$key] . "]";
}
echo json_encode($d);

Demo: http://codepad.org/KhofwXCi

5 Comments

This does not take into account the sort that was requested.
@ScottSauyet I see no sort.
Look at the order of the data requested in the chart configuration. Clearly descending values. I have no idea if that's actually important, though.
@ScottSauyet Its ok if there is no sort.
@StrataGeeksCEO: Ok, did you end up getting this working, with or without a sort?
0

probably array combine and than json encode and replace? just in case for diversity;)

<?php 

$x_axis = array(
'Safari', 'Opera', 'fireFox'
);

$y_axis = array(
10, 30.4, 50
);

$keyFilled = array_combine($x_axis, $y_axis);

arsort($keyFilled);

$jsonData = json_encode($keyFilled);

$jsonDataReplaced = str_replace(array(',', '{', '}', ':'), array('],[', '[', ']', ','), $jsonData);
echo '<pre>';
var_dump('['.$jsonDataReplaced.']');

?>

output is:

string(45) "[["fireFox",50],["Opera",30.4],["Safari",10]]"

http://phpfiddle.org/main/code/jq4-cgb

2 Comments

This does not take into account the sort that was requested.
@ScottSauyet, not really sure that it was requested, but it is easy to fix, updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.