Dynamic Pie Chart Data in Javascript and PHP

2019-02-25 00:20发布

问题:

The following code represent the data that will be shown in a pie chart visualization in javascript.

<script type="text/javascript">

    var agg = { label: 'Aggressive', pct: [60, 10, 6, 30, 14, 10] },
        bal = { label: 'Balanced',   pct: [24,  7, 2, 18, 13, 36] },
        mod = { label: 'Moderate',   pct: [12,  4, 2, 10, 11, 61] },
        inc = { label: 'Income',     pct: [ 0,  0, 0,  0,  0,100] },

</script>

I would like to have the pct values as php variables, and not fixed as in the code above. How do I do this?

I would like to have it as var agg = <?php echo json_encode($data); ?>;

$data being the php variable

I would like to have it as in the answer found at: Dynamic Javascript Tree Structure

The second answer to the above linked question with the var treeData and function toTree, seems exactly like what I want to do. But,how do I apply that code and functions to my pie chart?

I would like to make the pct array of 6 values dynamic through a php variable or php array or json encode.

Instead of pct: [60, 10, 6, 30, 14, 10] as seen above, I would like to ake each of 6 values a php variable. Like: pct: [$r1, $r2, $r3, $r4, $r5, $r6] . How do I do this?

The code that I am working with can be found at http://jsfiddle.net/MX7JC/9/

Can I use a loop or recursive function to populate the pie chart data?