How to change data graph and convert a php variable to javascript from another page

问题: I am trying to change the "title text" of a graph that I am using. This graph uses data from PHP and get settings plot in Javascript. But I would like to configure my graph...

问题:

I am trying to change the "title text" of a graph that I am using. This graph uses data from PHP and get settings plot in Javascript. But I would like to configure my graph title through a variable from a form of other page. In graph page I can call the selected option from the form of the other page by using $_POST['pol_list"]... So how can I transform "title" in "Selected item from the form"? Thanks!

Here the graph code:

    <?php

$dataPoints = array( 
    array("label"=>"Chrome", "y"=>64.02),
    array("label"=>"Firefox", "y"=>12.55),
    array("label"=>"IE", "y"=>8.47),
    array("label"=>"Safari", "y"=>6.08),
    array("label"=>"Edge", "y"=>4.29),
    array("label"=>"Others", "y"=>4.59)
)

?>
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function() {


var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    title: {
        text: "Usage Share of Desktop Browsers"
    },
    subtitles: [{
        text: "November 2017"
    }],
    data: [{
        type: "pie",
        yValueFormatString: "#,##0.00"%"",
        indexLabel: "{label} ({y})",
        dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
    }]
});
chart.render();

}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html> 

And the form code from another PHP page:

    <form action="../view/PlotaGrafico.php" method="post">
     <select name="pol_list">
       <option value="p1">Item 1</option>
       <option value="p2"> Item 2</option>
       <option value="p3"> Item 3</option>
       <option value="p4"> Item 4</option>
      <input type="submit">
     </select>
    </form>

回答1:

I got!

the element in "text" field must be a JSON.

So this worked:

 var chart = new CanvasJS.Chart("chartContainer", {
 animationEnabled: true,
 title: {
     text: <?php echo json_encode($pol_sel); ?>

Thanks!

  • 发表于 2019-07-04 18:32
  • 阅读 ( 253 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除