Chart view of the current month and previous month google Charts

Asked

Viewed 132 times

0

I need some help with Google Maps. I have a table where I store id, negotiator and date. I make a query to find out how many agreements that person made in the month and I can generate a graph, the problem is in putting the previous month to make the comparison. I don’t want to put it on separate charts, I want a chart only that does this, imagined comparing six months on separate charts?

Follow the code I have:

SELECT count(id) as idCont, negociador FROM acordos WHERE month(data_envio) = ".($mes)." GROUP BY negociador

$res_ma = mysql_query($sql_ma,conexao());

function drawChart2() {
    var data = google.visualization.arrayToDataTable([
      ['Negociador', 'Acordos - <?= $row_m['mes'].'/'.$row_m['ano']; ?>'],
      <?php
      while($row_ma = mysql_fetch_array($res_ma)){
        echo "['".$row_ma['negociador']."', '".$row_ma['idCont']."'],";
      }
    ?>
    ]);

    var options = {
      chart: {
        title: 'Desempenho da Equipe',
        subtitle: 'Demonstrativo de meses anteriores',
      },
      bars: 'horizontal' // Required for Material Bar Charts.
    };

    var chart = new google.charts.Bar(document.getElementById('barchart_meses'));

    chart.draw(data, google.charts.Bar.convertOptions(options));
  }

Thank you for your attention.

  • instead of negative, I could tell you where I can improve on the question...

1 answer

0


Just to leave it here as I solved this situation:

According to the question, to display two bars on the chart, I had to make two queries, one taking the current month and the other the previous month.

current month the loop of this query will stay inside the Function

$sql_gr = "SELECT count(id) as idCont, id, negociador FROM contador_acordos WHERE negociador <> 'master' AND negociador <> '' AND negociador <> 'usuario' AND negociador <> 'desenvolvedor' AND negociador <> 'admin' AND month(data_envio) = "mes_atual" GROUP BY negociador ORDER BY negociador ASC";
    $result = mysql_query($sql_gr,conexaoCobranca());

previous month this loop was just to put Count in an array and display in the chart and put the current month subtracting 1 in the query.

$sql_ma = "SELECT count(id) as idCont, id, negociador FROM contador_acordos WHERE negociador <> 'master' AND negociador <> '' AND negociador <> 'usuario' AND negociador <> 'desenvolvedor' AND negociador <> 'admin' AND month(data_envio) = ".(mes_atual-1)." GROUP BY negociador ORDER BY negociador ASC";
    $res_ma = mysql_query($sql_ma,conexaoCobranca());
    while($row_ma = mysql_fetch_array($res_ma)){
        $lista1[$row_ma['negociador']] = $row_ma['idCont'];
    }

script I put a ternary in case the previous month is empty.

function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Assessor', 'Acordos - 11/2019', 'Acordos - 12/2019'],
          <?php
            while($row = mysql_fetch_array($result)){
              echo "['".$row['negociador']."', ".($lista1[$row['negociador']] == '' ? 0 : $lista1[$row['negociador']]).", ".$row['idCont']."],";
            }
          ?>
        ]);

        var options = {
          chart: {
            title: 'Desempenho da Equipe',
            subtitle: 'Demonstrativo dos últimos 2 meses',
          }
        };

        var chart = new google.charts.Bar(document.getElementById('barchart_atual'));

        chart.draw(data, google.charts.Bar.convertOptions(options));
      }

That was my solution. Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.