从Array值获取值以形成聊天

时间:2017-06-15 11:18:21

标签: javascript php jquery arrays charts

我从表中提取反馈并通过将其转换为以下格式进行分析:

字符串:食物非常糟糕

显性:否定,分数:Array ( [neg] => 0.5 [neu] => 0.25 [pos] => 0.25 )

字符串:车很糟糕

显性:否定,分数:Array ( [neg] => 0.5 [neu] => 0.25 [pos] => 0.25 )

字符串:桥梁非常糟糕

显性:否定,分数:Array ( [neg] => 0.5 [neu] => 0.25 [pos] => 0.25 )

我想做的就是计算所有负值,正值和中性值,并将它们显示在饼图中

这是我想要使用数组元素

开发的图表示例

enter image description here

1 个答案:

答案 0 :(得分:1)

你可以这样做: -

<?php
$a = Array ( 'neg' => 0.5, 'neu' => 0.25, 'pos' => 0.25 );
$key = array_keys($a); //get te keys of the array
$b = Array ( 'neg' => 0.5, 'neu' => 0.25, 'pos' => 0.25 );
$c=  Array ( 'neg' => 0.5, 'neu' => 0.25, 'pos' => 0.25 );
$d = array_map(function () {
    return (array_sum(func_get_args())/3)*100; // add all three array corresponding keys values and convert them to percentage
}, $a, $b,$c);

$d = array_combine($key,$d); // now combine key array and sum array
$d = array('Sentiment'=>'rating') + $d; // add which type of chart it is as a key value pair
$rating_data = array(); //create a new array variable

foreach($d as $key=>$val){
    $rating_data[] = array($key,$val); //convert summ array to key,value sub-array and assig it to new array
}
 $encoded_data = json_encode($rating_data); //json encode the new array
?>

<html>
    <head>
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
            google.load("visualization", "1", {packages:["corechart"]});
            google.setOnLoadCallback(drawChart);
            function drawChart() 
            {
             var data = google.visualization.arrayToDataTable(
             <?php  echo $encoded_data; ?>
             );
             var options = {
              title: "Sentiment vs Rating"
             };
             var chart = new google.visualization.PieChart(document.getElementById("employee_piechart"));
             chart.draw(data, options);
            }
        </script>
        <style>
            body
            {
             margin:0 auto;
             padding:0px;
             text-align:center;
             width:100%;
             font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
             background-color:#FAFAFA;
            }
            #wrapper
            {
             margin:0 auto;
             padding:0px;
             text-align:center;
             width:995px;
            }
            #wrapper h1
            {
             margin-top:50px;
             font-size:45px;
             color:#585858;
            }
            #wrapper h1 p
            {
             font-size:18px;
            }
            #employee_piechart
            {
             padding:0px;
             width:600px;
             height:400px;
             margin-left:190px;
            }
        </style>
    </head>
    <body>
        <div id="employee_piechart" style="width: 900px; height: 500px;"></div>
    </body>
</html>

我本地的输出: - http://prntscr.com/fk71m2