如何在一个php文件中从不同的数据表中绘制多个googlechart?

时间:2017-09-12 06:22:54

标签: php jquery ajax pygooglechart

我想从不同的数据表中绘制一些图表。数据表来自一个名为ajax.php的PHP文件中的查询。

这是代码:

//JOB POST BY SALARY
function jobsalary(){

global $DB;

//query by location total post
$sql = 'SELECT lcs.salaryrange_id, count(ljj.job_id) as count, lcs.salaryrange_item FROM {local_jobs_job} ljj INNER JOIN {local_cv_salaryrange} lcs ON ljj.job_salaryrangeid = lcs.salaryrange_id WHERE job_type = 0 GROUP BY lcs.salaryrange_item';


//get the query into record
$data = $DB->get_records_sql($sql);

//put the query into array
$rows = array();

$rows = array_map(function($item) {
return (object) ['c' => [
    (object) ['v' => $item->salaryrange_item, 'f' => null],
    (object) ['v' => intval($item->count), 'f' => null]
]];
}, array_values($data));


 // prepare return data
 $cols = [
(object) ['id' => '', 'label' => 'LABEL', 'pattern' => '', 'type' => 'string'],
(object) ['id' => '', 'label' => 'TOTAL', 'pattern' => '', 'type' => 'number'],
 ];


$returndata = new stdClass;
$returndata->cols = $cols;
$returndata->rows = $rows;

echo json_encode($returndata);

}

 //JOB POST BY JOB'S CATEGORY
 function jobcategory(){

 global $DB;

 //query by job's category total post
 $sql = 'SELECT ljc.category_id, count(ljj.job_id) as count, ljc.category_group FROM {local_jobs_job} ljj INNER JOIN {local_jobs_category} ljc ON ljj.job_categoryid = ljc.category_id  where ljj.job_type = 0 group by ljc.category_group';


 //get the query into record
 $data = $DB->get_records_sql($sql);

 //put the query into array
  $rows = array();

  $rows = array_map(function($item) {
  return (object) ['c' => [
    (object) ['v' => $item->category_group, 'f' => null],
    (object) ['v' => intval($item->count), 'f' => null]
  ]];
  }, array_values($data));


 // prepare return data
 $cols = [
(object) ['id' => '', 'label' => 'LABEL', 'pattern' => '', 'type' => 'string'],
(object) ['id' => '', 'label' => 'TOTAL', 'pattern' => '', 'type' => 'number'],
];


 $returndata = new stdClass;
 $returndata->cols = $cols;
 $returndata->rows = $rows;

echo json_encode($returndata);


}

该图表是在另一个名为report.php的php文件中绘制的。这是代码:

 google.charts.setOnLoadCallback(drawItems);

    function drawItems(){
            var functionName = JSON.stringify({values:"joblocation"});
            var jsonPieChartData = $.ajax({
                            url: "ajax.php",
                            contentType: "application/json",
                            data: "functionName",
                            dataType: "json",
                            async: false,

                            }).responseText;

                var piechartdata = new google.visualization.DataTable(jsonPieChartData);

                var optionsChart = {
                                //title: 'Job Posts by Location',
                                pieSliceText: 'label',
                                tooltip: {isHtml: true},
                                width: 500,
                                height: 300,
                                chartArea: { left:"5%",top:"20%",width:"90%",height:"90%" }
                              };

                // Instantiate and draw our pie chart, passing in some options.
                var chart = new google.visualization.PieChart(document.getElementById('chart_div'));


                //draw the chart      
                chart.draw(piechartdata, optionsChart);

                    }

但是,我不知道如何使用ajax调用为每个图表调用特定函数。以前,我只是使用ajax.php文件中的一个数据表绘制一个图表。

1 个答案:

答案 0 :(得分:0)

根据您的要求创建多个ajax文件并在一个php文件中调用所有ajax文件