Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自定义mysqli查询中的谷歌饼图_Php_Jquery_Sql_Google Visualization - Fatal编程技术网

Php 自定义mysqli查询中的谷歌饼图

Php 自定义mysqli查询中的谷歌饼图,php,jquery,sql,google-visualization,Php,Jquery,Sql,Google Visualization,我试着用php,mysqli实现google图表。我想为一个课程活动制作一个包含3个部分的饼图,3个部分是三列:计数(60%)。我试着用不同的程序进行搜索,从我的角度尝试了不同的可能性,但无法满足这种需求。我不能在代码中添加超过两列 要求:我正在寻找一个饼图,上面有第十一课活动,显示了所有三列:cond1、cond2和cond3(在一个饼图中) 我来到stack overflow与高素质的开发人员共享我的代码 请分享你的建议 表格:课程成绩 id lesson

我试着用php,mysqli实现google图表。我想为一个课程活动制作一个包含3个部分的饼图,3个部分是三列:计数(60%)。我试着用不同的程序进行搜索,从我的角度尝试了不同的可能性,但无法满足这种需求。我不能在代码中添加超过两列

要求:我正在寻找一个饼图,上面有第十一课活动,显示了所有三列:cond1、cond2和cond3(在一个饼图中)

我来到stack overflow与高素质的开发人员共享我的代码

请分享你的建议

表格:课程成绩

id            lesson           lesson_activity   count(<50%)   count(Between 50-60%)   count(>60%)
5      Community Health Care        CHC.001            9                 7                   2
5      Community Health Care        CHC.002            2                 5                   6
5      Community Health Care        CHC.003            9                 2                   0
13     Spaceship                    SS.001             1                 13                  7
3      Risk Analysis                RA.001             1                 13                  7
3      Risk Analysis                RA.002             1                 9                   0
3      Risk Analysis                RA.003             1                 3                   4
3      Risk Analysis                RA.004             0                 7                   21
3      Risk Analysis                RA.005             0                 30                  11
15     Community Qualifications     CA.001             1                 13                  32
id课程\u活动计数(60%)
5社区卫生保健CHC.001 9 7 2
5社区卫生保健CHC.002 2 5 6
5社区卫生保健CHC.003 9 2 0
13太空船SS.001 1 13 7
3风险分析RA.001 1 13 7
3风险分析RA.002 1 9 0
3风险分析RA.003 1 3 4
3风险分析RA.004 0 7 21
3风险分析RA.005 0 30 11
15社区资格CA.001 1 13 32
代码

<?php

  $DB_NAME = 'lessons';
  $DB_HOST = 'localhost';
  $DB_USER = '---';
  $DB_PASS = '---';

  $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

  if (mysqli_connect_errno()) 
  {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }
$sql = $mysqli->query('SELECT id, lesson, lesson_activity, count(<50%) AS cond1, count(Between 50-60%) AS cond2, count(>60%) AS cond3  FROM lesson_grades');

$rows = array();
$flag = true;

$table = array();

$table['cols'] = array(
  array('label' => 'lesson_activity', 'type' => 'string'),
  array('label' => 'cond1', 'type' => 'number'),
  array('label' => 'cond2', 'type' => 'number'),
  array('label' => 'cond3', 'type' => 'number'),
);

$rows = array();
while($r = mysqli_fetch_assoc($sql)) 
{
    $temp = array();
    $temp[] = array('v' => (string) $r['lesson_activity']); 
    $temp[] = array('v' => $r['cond1'], $r['cond2'], $r['cond3']); 
    $rows[] = array('c' => $temp);
}

$table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;

?>

<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 = new google.visualization.DataTable(<?php echo $jsonTable; ?>);
      var options = 
      {
           title: 'My Weekly Plan',        
           legend: 'none',        
           slices: {0: {color: 'black'}, 1: {color: 'red'}} 
      };     
      var chart = new google.visualization.PieChart(document.getElementById('piechart'));
      chart.draw(data, options);
    }
    </script>
  </head>
  <body><div id="piechart" style="width:500px;height:500px;"></div></body>   
  </html>

load('visualization','1',{'packages':['corechart']});
setOnLoadCallback(drawChart);
函数绘图图()
{
var data=new google.visualization.DataTable();
变量选项=
{
标题:“我的每周计划”,
图例:“无”,
切片:{0:{color:'black'},1:{color:'red'}
};     
var chart=new google.visualization.PieChart(document.getElementById('PieChart');
图表绘制(数据、选项);
}

我肯定有人会给你一个纯javascript版本,但这里有一个与php一起工作的类,如果你感兴趣,它会自动生成javascript。如果不是,则最下面的代码是要尝试的输出:

库:

<html>
<head>
<?php
// Settings for the first chart
// This is so you can make multiple charts
$settings["incr"]           =   1;
// Id name for the chart (where to put the chart)
$settings["id"]             =   "piechart".$settings["incr"];
$settings["data"]["Task"]   =   "Lessons/Activity";

//**********************************//
// Your numbers from your db go here
//**********************************//
$settings["data"]['count(<50%)']            =   7;
$settings["data"]['count(Between 50-60%)']  =   3;
$settings["data"]['count(>60%)']            =   6;
//**********************************//
//**********************************//

// Create instance of GoogleCharts class
$Googlizer  =   new GoogleCharts();
// Create the pie chart
$Googlizer->CreatePie($settings);
// Save the instance of the js data array
$chart1_data    =   $Googlizer->ChartData();
// Save the instance of the js function
$chart1_inst    =   $Googlizer->ChartInstance();
// Write the whole shebangle to the page
echo $Googlizer ->ChartOptions(array("title"=>"My Weekly Plan","legend"=>"none"))
                ->ChartKind(GoogleCharts::PIE)
                ->CreateJavascript(array("data"=>array($chart1_data, $chart1_inst),"wrap"=>true,"lib"=>true));
?>
</head>
<body>
    <div id="piechart1" style="width: 900px; height: 500px;"></div>
</body>
</html>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

google.load("visualization", "1", {packages:["corechart"]});
// Let the callback run a function
google.setOnLoadCallback(function() {
var DataSet1   =   [
                    ['Task', 'Lessons/Activity'],
                    ['count(<50%)', 7],
                    ['count(Between 50-60%)', 3],
                    ['count(>60%)', 6]
                ]


drawChart(DataSet1,'piechart1');


    });

// Give the function some arguments, first is data, second id
// You could do a third for the options attribute
function drawChart(ArrayElem,IdElem)
    {
        var data = google.visualization.arrayToDataTable(ArrayElem);
        var options = {
            title: 'My Weekly Plan',
            legend: 'none'
        };


        var chart = new google.visualization.PieChart(document.getElementById(IdElem));

        chart.draw(data, options);
    }
</script>
</head>
<body>
    <div id="piechart1" style="width: 900px; height: 500px;"></div>
</body>
</html>
更新:

id            lesson           lesson_activity   count(<50%)   count(Between 50-60%)   count(>60%)
5      Community Health Care        CHC.001            9                 7                   2
5      Community Health Care        CHC.002            2                 5                   6
5      Community Health Care        CHC.003            9                 2                   0
13     Spaceship                    SS.001             1                 13                  7
3      Risk Analysis                RA.001             1                 13                  7
3      Risk Analysis                RA.002             1                 9                   0
3      Risk Analysis                RA.003             1                 3                   4
3      Risk Analysis                RA.004             0                 7                   21
3      Risk Analysis                RA.005             0                 30                  11
15     Community Qualifications     CA.001             1                 13                  32

能否提供打印($rows)的输出在你
json\u编码之前
it?会更容易复制。当然。。先生……请稍等。是一行数据:
社区卫生保健-9-7-2
一个图表,使用三个数字作为切片?@h3n-@Rasclatt-是的,先生,所有9-7-2在一个饼图中@Rasclatt,在上面的代码中:$settings[“data”][“count(60%)”=6;你能告诉我如何从我的sql查询中提取数据吗?你有没有尝试过实际的代码,即使只是虚拟的数字,以确保它是你想要的?@Rasclatt-你正在获取静态值,但我需要从mysqli query dataWell中获取它。我硬编码了php是静态值,但你正在从数据库中调用它们,因此,您只需使用
while
循环来动态分配数字。从数据库调用后,数组的外观是什么?