【问题标题】:Google Charts Date axis labels not correct谷歌图表日期轴标签不正确
【发布时间】:2016-10-13 14:24:36
【问题描述】:

我一直在研究具有日期轴的 Google 折线图。我注意到轴标签的刻度由网格线的数量决定,并且与我传递的数据不同。有人可以告诉我如何强制轴标签与我传递的数据同步。 请找到小提琴的链接:https://jsfiddle.net/FarhanI/5ga6xu44/

  function drawChart() {

    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Time of Day');
    data.addColumn('number', 'Rating');

    data.addRows([
      [new Date(2015, 0, 1), 5],
      [new Date(2015, 0, 7), 3],[new Date(2015, 0, 14), 3], [new Date(2015, 0, 21), 2],[new Date(2015, 0, 28), 8],
    ]);


    var options = {
      title: 'Rate the Day on a Scale of 1 to 10',
      width: 900,
      height: 500,
      hAxis: {
        format: 'M/d/yy',
        gridlines: {count: 9}
      },
      vAxis: {
        gridlines: {color: 'none'},

      }
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

    chart.draw(data, options);

    var button = document.getElementById('change');

    button.onclick = function () {

      // If the format option matches, change it to the new option,
      // if not, reset it to the original format.
      options.hAxis.format === 'M/d/yy' ?
      options.hAxis.format = 'MMM dd, yyyy' :
      options.hAxis.format = 'M/d/yy';

      chart.draw(data, options);
    };
  }

我还试图让网格线显示为 y 轴,因为我无法使用折线图获得 Y 轴。我尝试将网格线指定为 1,但我也只能在 X 轴的中间获得 1 条网格线。

有人可以告诉我是否可以使用折线图获得 Y 轴。

感谢您的帮助, 法尔汉。

【问题讨论】:

    标签: charts google-visualization


    【解决方案1】:

    您可以提供自定义hAxis.ticks

    要与数据同步,请使用每行的日期构建一个数组

    // load custom ticks
    var ticks = [];
    for (var i = 0; i < data.getNumberOfRows(); i++) {
      ticks.push(data.getValue(i, 0));
    }
    

    然后在配置选项中使用数组

      hAxis: {
        format: 'M/d/yy',
        ticks: ticks
      }
    

    注意:不遵循 y 轴所需的内容,请您澄清一下...
    --> 用折线图获取Y轴

    如果您只是想查看网格线,请从配置选项中删除以下内容...

      vAxis: {
        gridlines: {color: 'none'}
      }
    

    请参阅以下工作 sn-p...

    google.charts.load('current', {
      callback: function () {
        var data = new google.visualization.DataTable();
        data.addColumn('date', 'Time of Day');
        data.addColumn('number', 'Rating');
    
        data.addRows([
          [new Date(2015, 0, 1), 5],
          [new Date(2015, 0, 7), 3],
          [new Date(2015, 0, 14), 3],
          [new Date(2015, 0, 21), 2],
          [new Date(2015, 0, 28), 8]
        ]);
    
        // load custom ticks
        var ticks = [];
        for (var i = 0; i < data.getNumberOfRows(); i++) {
          ticks.push(data.getValue(i, 0));
        }
    
        var options = {
          title: 'Rate the Day on a Scale of 1 to 10',
          width: 900,
          height: 500,
          hAxis: {
            format: 'M/d/yy',
            ticks: ticks
          }
        };
    
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    
        var button = document.getElementById('change');
        button.onclick = function () {
          // If the format option matches, change it to the new option,
          // if not, reset it to the original format.
          options.hAxis.format === 'M/d/yy' ?
          options.hAxis.format = 'MMM dd, yyyy' :
          options.hAxis.format = 'M/d/yy';
          chart.draw(data, options);
        };
      },
      packages:['corechart']
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <input type="button" id="change" value="Change" />
    <div id="chart_div"></div>

    【讨论】:

    • @WhiteHat...非常感谢。这肯定有助于将轴标签与数据同步。最初我有一个以字符串表示的日期折线图。有了这个,我无法获得 Y 轴(基线)。所以我现在尝试将日期字符串转换为日期对象并将网格线显示为 Y 轴(基线)。我的最终目标是只有 Y 轴 - 请告诉我这是否可以将日期作为字符串或我需要转换为日期对象。
    • 对,使用离散轴(字符串),然后无法控制网格线等 -- 必须是连续轴(日期、数字等...) -- discrete vs. continuous --有关更多轴标签格式提示,请参阅 this answer...
    • 感谢您的精彩解释!!根据您的建议进行了更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多