使用谷歌图表表构建数据透视表

时间:2018-11-07 22:13:45

标签: php json google-visualization pivot-table

我正在尝试构建一个表,该表在v轴上显示属性列表,在h轴上显示月份列表,并且该表应通过对应的值来构建。

我已经使用一系列查询和操作来构建我的Json对象,但是我终于实现了!

查询以获取json:

$rows = array();
    $table = array();

    $table['cols'] = array(
        array('label' => 'Fraccao', 'type' => 'string'),
        array('label' => 'Data', 'type' => 'date'),
        array('label' => 'Valor', 'type' => 'number')
    );

$query_detalhes = "SELECT * FROM TbFraccao INNER JOIN TbLocal on TbFraccao.FkLocal=TbLocal.PkLocal INNER JOIN TbEmpresa ON TbLocal.FkEmpresa=TbEmpresa.PkEmpresa";

$result_detalhe = mysqli_query($link, $query_detalhes);

// dados para grafico

$listagem = "";

while( $row_detalhe = mysqli_fetch_assoc($result_detalhe)){
 $id_fraccao = $row_detalhe['PkFraccao'];
 $id_nome = $row_detalhe['FraccaoNome'];
 $id_area = $row_detalhe['FraccaoArea'];
 $id_tipo = $row_detalhe['TipoNome'];
 $id_local = $row_detalhe['LocalNome'];
 $id_dono = $row_detalhe['EmpresaNome'];
 $id_morada = $row_detalhe['LocalMorada'];
 $id_cp = $row_detalhe['LocalCp'];

$id_coluna_tabel = $id_local . " - " . $id_nome;
// OBTER DATA MAIS ANTIGA
// decidir qual é a data mais velha que me interessa





$query_old_date = "SELECT MIN(ContratoFraccaoInicio) AS INICIO, MAX(ContratoFraccaoFim) AS FIM FROM TbContratoFraccao ORDER BY ContratoFraccaoInicio ASC";
$result_old_date = mysqli_query($link, $query_old_date);

while($datas = mysqli_fetch_assoc($result_old_date)){

    $old_date = $datas['INICIO'];
    $end_date = $datas['FIM'];


$data_inicio = date('Y-m-d', strtotime($old_date));
$data_fim = date('Y-m-d', strtotime($end_date));


$start    = (new DateTime($data_inicio));
$end      = (new DateTime($data_fim))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
   $mes =  $dt->format("Y-m-d");




    $query_valor_mes = "SELECT MAX(Data) AS DATAVALOR FROM TbContratoFracaoPreco WHERE FkFraccao='$id_fraccao'  and data<='" . $mes . "'";

    $result_valor_mes = mysqli_query($link, $query_valor_mes);

    while($row_valor_mes = mysqli_fetch_assoc($result_valor_mes)){
        $data_valor = $row_valor_mes['DATAVALOR'];




        $query_obter_valor = " SELECT * FROM TbContratoFracaoPreco WHERE FkFraccao='$id_fraccao' and data='" . $data_valor . "'";
        $result_obter_valor = mysqli_query($link, $query_obter_valor);

        while($row_renda = mysqli_fetch_assoc($result_obter_valor)){

            $rent = $row_renda['ContratoFracaoPreco'];
            $tabela_fraccao = $row_renda['$id_fraccao'];

            $date_year = date('Y', strtotime($mes));
            $date_month = date('m', strtotime($mes))-1;
            $date_day = date('d', strtotime($mes));

            $listing =  "Date(" . $date_year.", " . $date_month .", " . $date_day . ")";

//echo $id_local . ", " . $id_nome . " - ". $mes . " - ". $rent ."<br>";
                $temp = array();
  $temp[] = array('v' => (string) $id_coluna_tabel);
  $temp[] = array('v' => (string) $listing);
  $temp[] = array('v' => (float) $rent);
  $rows[] = array('c' => $temp);

$table['rows'] = $rows;

$dados_chart = json_encode($table);

        };



    };
};
};};

构建了Json对象($ dados_chart)之后,我定义了表:

google.charts.load('current', {'packages':['table']});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {

 var data = new google.visualization.DataTable(<?php echo $dados_chart;?>);

  var options = {




        };


  var table = new google.visualization.Table(document.getElementById('chart'));
   table.draw(data, {showRowNumber: false, width: '100%', height: '100%'});


}

现在我有一个表,希望像数据透视表一样显示它(在excel中非常简单!)

enter image description here

有人可以指出正确的方向吗?

1 个答案:

答案 0 :(得分:0)

除非您想自己进行聚合(这不像Excel那样容易),否则您可能会对在数据库引擎上工作的委托感兴趣,但并非所有数据库都支持数据透视表。

PostgreSQL允许使用tablefunc模块上的交叉表创建数据透视表。

PostgreSQL Docs - tablefunc

Creating Pivot Tables in PostgreSQL Using Crosstab

MariaDB(MySQL的一个分支)具有一个称为CON​​NECT的特殊存储引擎,具有

MariaDB docs - Pivoting in MariaDB