Jump to content
JChartFX Community
  • 0

Use json data from a dynamic PHP array


flapane

Question

Hi,

my PHP script generates a bi-dimensional array.

Jchartfx uses JSON as main way to populate graphs, I used json_encode php() function.

The result is:

{"-2":{"Angle of Attack":-2,"Lift coefficient":-0.092},"-1":{"Angle of 

Attack":-1,"Lift coefficient":0.019},"0":{"Angle of Attack":0,"Lift

coefficient":0.131},"1":{"Angle of Attack":1,"Lift

coefficient":0.241},"2":{"Angle of Attack":2,"Lift

coefficient":0.353},"3":{"Angle of Attack":3,"Lift

coefficient":0.464},"4":{"Angle of Attack":4,"Lift

coefficient":0.574},"5":{"Angle of Attack":5,"Lift

coefficient":0.686},"6":{"Angle of Attack":6,"Lift

coefficient":0.797},"7":{"Angle of Attack":7,"Lift

coefficient":0.908},"8":{"Angle of Attack":8,"Lift

coefficient":1.019},"9":{"Angle of Attack":9,"Lift

coefficient":1.131},"10":{"Angle of Attack":10,"Lift

coefficient":1.241},"11":{"Angle of Attack":11,"Lift

coefficient":1.353},"12":{"Angle of Attack":12,"Lift

coefficient":1.464},"13":{"Angle of Attack":13,"Lift

coefficient":1.575}}

Let's put it in a more readable way:

Array

(

[-2] => Array

(

[Angle of Attack] => -2

[Lift coefficient] => -0.092

)

[-1] => Array

(

[Angle of Attack] => -1

[Lift coefficient] => 0.019

)

[0] => Array

(

[Angle of Attack] => 0

[Lift coefficient] => 0.131

)

...up to the N-th element of the array. 

 How do I dinamically pass the JSON bi-dimensional array to jchartfx, which uses js scripts?


            var data = [

            { "Angle of Attack": "xxx", "Lift coefficient": yyy },

            { "Angle of Attack": "xxx", "Lift coefficient": yyy },

            { "Angle of Attack": "xxx", "Lift coefficient": yyy },

            { "Angle of Attack": "xxx", "Lift coefficient": yyy },

            { "Angle of Attack": "xxx", "Lift coefficient": yyy },

            { "Angle of Attack": "xxx", "Lift coefficient": yyy }            ];

            liftcurve.setDataSource(data);

I wonder if saving the JSON bi-dimensional array to a file and then loading the same JSON file with jchartfx would do the trick.

However, I couldn't understand how to use the jquery example described at: http://support.softwarefx.com/jChartFX/article/2601043#fb9ec7bf-1b86-e211-84a5-0019b9e6b500

$.ajax({

            url

: ("/services/SamplesService.asmx/jChartFXDataSvc"),

            data

: "{'year':'2011'}",

            type

: "POST",

            contentType

: "application/json; charset=utf-8",

            dataType

: "json",

            success

: function (result) {

                chart1

.setDataSource(result.d);

           

},

            error

: function (xhr, txt, err) {

                alert

("error connecting to data: " + txt);            }

       

});

Thanks in advance

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Fixed.

Let $arrData be the 2-dim php array, "Amgle of Attack" the x value and "Lift coefficient" the y value:

 

$liftcurve_plot[$i] = "{\"Angle of Attack\": \"".$arrData[$i][1]."\", \"Lift coefficient\": ".$arrData[$i][2]."}"; //plot

 

 Then:

var data = [
                <?php echo implode($liftcurve_plot, ','); ?>
                ];

 

Et voil

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...