LoginSignup
3
2

More than 3 years have passed since last update.

【Google Charts】x軸日付の表示範囲を設定する方法

Posted at

はじめに

グーグルチャートの横軸を変更したい。
具体的には、左から今月の1日〜今月末でグラフを表示させたいときに、
ドキュメントが英語パラダイスで、どのoptionsを使うべきなのか理解しづらかったので備忘録。

解決方法

options の hAxis.viewWindow.max/min を使う

var fromThisMonth = new Date();     // 今月の1日
fromThisMonth.setDate(1);
var toThisMonth = new Date();       // 今月末
toThisMonth.setMonth(toThisMonth.getMonth() + 1);
toThisMonth.setDate(0);

var chartType = new google.visualization.ChartWrapper({
  chartType: 'ColumnChart',
  containerId: 'chart_div',
  options: {
    : //一部options省略
    :
    hAxis: {
      viewWindow: {
        min: fromThisMonth, // 最小表示
        max: toThisMonth    // 最大表示
      }
    }
  }
});

image.png

さいごに

日付の両端が2020年10月1日とか2020年10月31日とか出ないのは、
さらに options を追加したり、取得する「時・分」のところまでしっかり指定してあげないといけなかったりいろいろ原因はありそう。。。

参考:Google Charts > Column Charts > Configuration options

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2