javascript - 从数组值中获取值以形成聊天

标签 javascript php jquery arrays charts

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

字符串:食物很糟糕

主导:负,分数:数组 ( [neg] => 0.5 [neu] => 0.25 [pos] => 0.25 )

字符串:这辆车很糟糕

主导:负,分数:数组 ( [neg] => 0.5 [neu] => 0.25 [pos] => 0.25 )

字符串:桥非常糟糕

主导:负,分数:数组 ( [neg] => 0.5 [neu] => 0.25 [pos] => 0.25 )

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

这是我想使用数组元素开发的图表示例

enter image description here

最佳答案

你可以像下面这样做:-

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

关于javascript - 从数组值中获取值以形成聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44566152/

相关文章:

php - 如何遍历每个数组(内部数组)的数组

php - 如何从浏览器游戏设计数据库访问

javascript - 获取子输入元素值更新

javascript - jQuery 数据 $(...).data ('foo' ) 有时会在 Backbone 应用程序中返回 undefined

javascript - Bootstrap-选择多显示问题

php - 如何使用 PhpSpreadsheet 检测单元格合并

javascript - 更新输入文本框的数据值 oninput 不起作用

jquery - Ajax POST 和 Django Tastypie

Javascript - 等高问题

javascript - 如何在 javascript 中通过列 ID 更改列可见性?