简体   繁体   English

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

[英]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. 此图使用来自PHP的数据并以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! 在图形页面中,我可以使用$ _POST ['pol_list“]从另一页的表单中调用所选的选项...那么如何在“从表单中选择的项”中转换“标题”呢?

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: 以及来自另一个PHP页面的表单代码:

    <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>

I got! 我有!

the element in "text" field must be a JSON. “文本”字段中的元素必须为JSON。

So this worked: 所以这工作:

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

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM