PHP variable does not update chat in JS

Asked

Viewed 42 times

1

I have the following code :

// Set new default font family and font color to mimic Bootstrap's default styling
Chart.defaults.global.defaultFontFamily = 'Nunito', '-apple-system,system-ui,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif';
Chart.defaults.global.defaultFontColor = '#858796';

function number_format(number, decimals, dec_point, thousands_sep) {
  // *     example: number_format(1234.56, 2, ',', ' ');
  // *     return: '1 234,56'
  number = (number + '').replace(',', '').replace(' ', '');
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
    dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
    s = '',
    toFixedFix = function(n, prec) {
      var k = Math.pow(10, prec);
      return '' + Math.round(n * k) / k;
    };
  // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  if (s[0].length > 3) {
    s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1).join('0');
  }
  return s.join(dec);
}

// Bar Chart Example
var abril =     "<?php $abrilv =     mysqli_fetch_row($abril);     echo $abrilv[0];     ?>";
var junho =     "<?php $junhov =     mysqli_fetch_row($junho);     echo $junhov[0];     ?>";
var julho =     "<?php $julhov =     mysqli_fetch_row($julho);     echo $julhov[0];     ?>";
var agosto =    "<?php $agostov =    mysqli_fetch_row($agosto);    echo $agostov[0];    ?>";
var setembro =  "<?php $setembrov =  mysqli_fetch_row($setembro);  echo $setembrov[0];  ?>";
var outubro =   "<?php $outubrov=    mysqli_fetch_row($outubro);   echo $outubrov[0];    ?>";

var ctx = document.getElementById("myBarChartDBK");
var myBarChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho"],
    datasets: [{
      label: "Valor",
      backgroundColor: "#4e73df",
      hoverBackgroundColor: "#2e59d9",
      borderColor: "#4e73df",
      data: [abril, junho, julho,agosto, 9821, 14984],
    }],
  },
  options: {
    maintainAspectRatio: false,
    layout: {
      padding: {
        left: 10,
        right: 25,
        top: 25,
        bottom: 0
      }
    },
    scales: {
      xAxes: [{
        time: {
          unit: 'month'
        },
        gridLines: {
          display: false,
          drawBorder: false
        },
        ticks: {
          maxTicksLimit: 6
        },
        maxBarThickness: 25,
      }],
      yAxes: [{
        ticks: {
          min: 0,
          max: 3500000,
          maxTicksLimit: 5,
          padding: 10,
          // Include a dollar sign in the ticks
          callback: function(value, index, values) {
            return 'R$' + number_format(value);
          }
        },
        gridLines: {
          color: "rgb(234, 236, 244)",
          zeroLineColor: "rgb(234, 236, 244)",
          drawBorder: false,
          borderDash: [2],
          zeroLineBorderDash: [2]
        }
      }],
    },
    legend: {
      display: false
    },
    tooltips: {
      titleMarginBottom: 10,
      titleFontColor: '#6e707e',
      titleFontSize: 14,
      backgroundColor: "rgb(255,255,255)",
      bodyFontColor: "#858796",
      borderColor: '#dddfeb',
      borderWidth: 1,
      xPadding: 15,
      yPadding: 15,
      displayColors: false,
      caretPadding: 10,
      callbacks: {
        label: function(tooltipItem, chart) {
          var datasetLabel = chart.datasets[tooltipItem.datasetIndex].label || '';
          return datasetLabel + ': R$' + number_format(tooltipItem.yLabel);
        }
      }
    },
  }
});
<?php 

include_once ("./connect.php");
ini_set('display_errors',1);
error_reporting(E_ALL);

$sql_abril ="SELECT valor from INTMesvendas where ano = '2019' and mes ='4'";
$abril = $mysqli->query($sql_abril) or die ($mysqli->error);

$sql_maio ="SELECT valor from INTMesvendas where ano = '2019' and mes ='5'";
$maio = $mysqli->query($sql_maio) or die ($mysqli->error);

$sql_junho ="SELECT valor from INTMesvendas where ano = '2019' and mes ='6'";
$junho = $mysqli->query($sql_junho) or die ($mysqli->error);

$sql_julho ="SELECT valor from INTMesvendas where ano = '2019' and mes ='7'";
$julho = $mysqli->query($sql_julho) or die ($mysqli->error);

$sql_agosto ="SELECT valor from INTMesvendas where ano = '2019' and mes ='8'";
$agosto = $mysqli->query($sql_agosto) or die ($mysqli->error);

$sql_setembro ="SELECT valor from INTMesvendas where ano = '2019' and mes ='9'";
$setembro = $mysqli->query($sql_setembro) or die ($mysqli->error);



?>

Javascript is graphical, the problem is that the php variables come out blank , wanted a help [Grafico como fica[1]

  • The JS code is inside an external . js file or in . php itself served by the application?

1 answer

-4

If this js code is out of php, it wouldn’t be possible to do it that way. What you would do is make an ajax for a file. php that gets the data and then pass to js using json....

Browser other questions tagged

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