doucong3048 2017-06-15 11:18
浏览 55
已采纳

从Array值获取值以形成聊天

I'm pulling feedback from a table and analyzing it by converting it to the format below:

String: The food is very bad

Dominant: neg, scores: Array ( [neg] => 0.5 [neu] => 0.25 [pos] => 0.25 )

String: The car is very bad

Dominant: neg, scores: Array ( [neg] => 0.5 [neu] => 0.25 [pos] => 0.25 )

String: The bridge is very bad

Dominant: neg, scores: Array ( [neg] => 0.5 [neu] => 0.25 [pos] => 0.25 )

All I want to do is to compute all the values for negative, positive and neutral and present them in a pie chart

This is an example of the chart I want to develop using the array element

enter image description here

  • 写回答

1条回答 默认 最新

  • doutao5499 2017-06-15 18:01
    关注

    You can do it like below:-

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

    Output at my local end:-http://prntscr.com/fk71m2

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题