Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

76
Visualizações
Google Chart columnChart haxis dates are wrong

I have a Google Chart (column-chart) showing a single dated (Jan-2010 = 2010-01-01) column - but the resulting column seems to run from 1-Jul-09 through to 1-Jul-10 (note this seems to change depending on the width of the screen); how can I fix this so that the column sits only on the 01-Jan-2010 date? (**Note, the dates/values are variable and can include one or hundreds of column values so we CANNOT simply hard code this or change the column type from 'date' to 'string').

  var arr = eval("[[new Date(2010, 0, 1), 0,1]]");
  var data = new google.visualization.DataTable();

  data.addColumn('date', 'Dt');
  data.addColumn('number', 'Open');
  data.addColumn('number', 'Closed');
  data.addRows(arr);

  var options_stacked = {
    isStacked: true,
    height: 300,
    colors: ['#111', '#a00'],
    hAxis: {
     slantedText: false,
     format: 'd/MMM/yy',
    },
    legend: {
      position: 'top',
    },
    vAxis: {
    minValue: 0
  }
};

var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options_stacked);

A demonstrator for this can be found on https://jsfiddle.net/Abeeee/d5fojtp2/34/

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you can add custom ticks to ensure each column "sits" on the correct date.

the ticks option expects an array of values.
we can use DataTable method --> getDistinctValue(colIndex)
to return the date values from the data table.

var xTicks = data.getDistinctValues(0);

hAxis: {
  slantedText: false,
  format: 'd.MMM.yy',      <---- changed from 'd/MMM/yy' to avoid line breaks
  ticks: xTicks
},

see following working snippet...

function doTest() {
  var arr = eval("[[new Date(2010, 0, 1), 0,1]]");
  var data = new google.visualization.DataTable();

  data.addColumn('date', 'Dt');
  data.addColumn('number', 'Open');
  data.addColumn('number', 'Closed');
  data.addRows(arr);
  
  var xTicks = data.getDistinctValues(0);

  var options_stacked = {
    isStacked: true,
    height: 300,
    colors: ['#111', '#a00'],
    hAxis: {
      slantedText: false,
      format: 'd/MMM/yy',
      ticks: xTicks
    },
    legend: {
      position: 'top',
    },
    vAxis: {
      minValue: 0
    }
  };

  var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
  chart.draw(data, options_stacked);
}

window.addEventListener('resize', doTest);

google.charts.load('current', {
  packages: ['corechart', 'bar']
});
google.charts.setOnLoadCallback(doTest);
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<h1>Google Charts</h1>
<div id="chart_div"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda