drfals1307 2019-07-03 12:55
浏览 76

如何更改数据图并将php变量从另一个页面转换为javascript

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条回答 默认 最新

  • du3932066 2019-07-03 17:19
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能