如何更改谷歌材料设计折线图中的日期格式?

时间:2020-12-08 15:30:28

I am trying to create a line chart using google material design. The chart is displaying correctly however, I want to change the format of the date shown on the chart.

我正在尝试使用谷歌材料设计创建折线图。图表正确显示但是,我想更改图表上显示的日期格式。

I want only "Month, Year" (e.g. Jun, 1994) format instead of the current format which is "Month/Day/Year Hours:Seconds" How do I do that?

我只想要“月,年”(例如Jun,1994)格式而不是当前格式,即“月/日/年时:秒”我该怎么做?

Also, I would like to increase the width of the line. The "linewidth" option is also not working.

另外,我想增加线的宽度。 “linewidth”选项也无效。

How do I increase width of the line chart? Also, How do I control the number of labels on the x-axis?

如何增加折线图的宽度?另外,如何控制x轴上的标签数量?

The code for the chart is as shown below.

图表的代码如下所示。

如何更改谷歌材料设计折线图中的日期格式?

google.charts.load('current', {
  'packages': ['line']
});

google.charts.setOnLoadCallback(drawChart);

function drawChart() {
  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Year');
  data.addColumn('number', 'DataSize');
  data.addRows(22);
  data.setValue(0, 0, new Date('1994-01-01'));
  data.setValue(0, 1, 25506);
  data.setValue(1, 0, new Date('1994-02-01'));
  data.setValue(1, 1, 26819);
  data.setValue(2, 0, new Date('1994-03-01'));
  data.setValue(2, 1, 31685);
  data.setValue(3, 0, new Date('1994-04-01'));
  data.setValue(3, 1, 25611);
  data.setValue(4, 0, new Date('1994-05-01'));
  data.setValue(4, 1, 29976);
  data.setValue(5, 0, new Date('1994-06-01'));
  data.setValue(5, 1, 32590);
  data.setValue(6, 0, new Date('1994-07-01'));
  data.setValue(6, 1, 33309);
  data.setValue(7, 0, new Date('1994-08-01'));
  data.setValue(7, 1, 35825);
  data.setValue(8, 0, new Date('1994-09-01'));
  data.setValue(8, 1, 41973);
  data.setValue(9, 0, new Date('1994-10-01'));
  data.setValue(9, 1, 54067);
  data.setValue(10, 0, new Date('1994-11-01'));
  data.setValue(10, 1, 45895);
  var formatter_medium = new google.visualization.DateFormat({
    formatType: 'medium'
  });
  formatter_medium.format(data, 1);
  var chart = new google.charts.Line(document.getElementById('dvRise'));
  chart.draw(data, {
    lineWidth: '3',
    left: 0,
    top: 0,
    'height': '300',
    'width': '450',
    colors: ['#44AFED'],
    legend: {
      position: 'none'
    },
    hAxis: {}
  });
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div style="text-align: center; height: 320px" id="dvRise"></div>

4 个解决方案

#1


6  

As for the Date Format, that can be set on the hAxis, which changes the hover value as well.
No need for a formatter...

至于日期格式,可以在hAxis上设置,也可以更改悬停值。不需要格式化程序......

As for lineWidth, that option doesn't appear to work for Material Charts, even with...

至于lineWidth,该选项似乎不适用于材质图表,即使有...

google.charts.Line.convertOptions

google.charts.Line.convertOptions

google.charts.load('current', {
  'packages': ['line']
});

google.charts.setOnLoadCallback(drawChart);

function drawChart() {
  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Year');
  data.addColumn('number', 'DataSize');
  data.addRows(22);
  data.setValue(0, 0, new Date('1994-01-01'));
  data.setValue(0, 1, 25506);
  data.setValue(1, 0, new Date('1994-02-01'));
  data.setValue(1, 1, 26819);
  data.setValue(2, 0, new Date('1994-03-01'));
  data.setValue(2, 1, 31685);
  data.setValue(3, 0, new Date('1994-04-01'));
  data.setValue(3, 1, 25611);
  data.setValue(4, 0, new Date('1994-05-01'));
  data.setValue(4, 1, 29976);
  data.setValue(5, 0, new Date('1994-06-01'));
  data.setValue(5, 1, 32590);
  data.setValue(6, 0, new Date('1994-07-01'));
  data.setValue(6, 1, 33309);
  data.setValue(7, 0, new Date('1994-08-01'));
  data.setValue(7, 1, 35825);
  data.setValue(8, 0, new Date('1994-09-01'));
  data.setValue(8, 1, 41973);
  data.setValue(9, 0, new Date('1994-10-01'));
  data.setValue(9, 1, 54067);
  data.setValue(10, 0, new Date('1994-11-01'));
  data.setValue(10, 1, 45895);

  var chart = new google.charts.Line(document.getElementById('dvRise'));
  chart.draw(data, google.charts.Line.convertOptions({
    lineWidth: 10,
    left: 0,
    top: 0,
    'height': '300',
    'width': '450',
    colors: ['#44AFED'],
    legend: {
      position: 'none'
    },
    hAxis: {format: 'MMM, yyyy'}
  }));
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div style="text-align: center; height: 320px" id="dvRise"></div>

#2


0  

try this..

尝试这个..

var date_formatter = new google.visualization.DateFormat({ 
        pattern: "MMM dd, yyyy"
}); 
date_formatter.format(data_table, 0);

#3


0  

You can set your format in hAxis options while drawing chart

您可以在绘制图表时在hAxis选项中设置格式

ex

// 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);

Working example https://jsfiddle.net/api/post/library/pure/

工作示例https://jsfiddle.net/api/post/library/pure/

#4


0  

Try This

尝试这个

function convertDate(inputFormat) {
    function pad(s) { return (s < 10) ? '0' + s : s; }
    var d = new Date(inputFormat);
    return [pad(d.getDate()),pad(d.getMonth()+1),d.getFullYear()].join('/');
}
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Year');
data.addColumn('number', 'DataSize');
data.addRows(22);
data.setValue(0, 0, convertDate('1994-01-01'));
data.setValue(0, 1, 25506);
data.setValue(1, 0, convertDate('1994-02-01'));
data.setValue(1, 1, 26819);
data.setValue(2, 0, convertDate('1994-03-01'));
data.setValue(2, 1, 31685);
data.setValue(3, 0, convertDate('1994-04-01'));
data.setValue(3, 1, 25611);
data.setValue(4, 0, convertDate('1994-05-01'));
data.setValue(4, 1, 29976);
data.setValue(5, 0, convertDate('1994-06-01'));
data.setValue(5, 1, 32590);
data.setValue(6, 0, convertDate('1994-07-01'));
data.setValue(6, 1, 33309);
data.setValue(7, 0, convertDate('1994-08-01'));
data.setValue(7, 1, 35825);
data.setValue(8, 0, convertDate('1994-09-01'));
data.setValue(8, 1, 41973);
data.setValue(9, 0, convertDate('1994-10-01'));
data.setValue(9, 1, 54067);
data.setValue(10, 0, convertDate('1994-11-01'));
data.setValue(10, 1, 45895);
var formatter_medium = new google.visualization.DateFormat({
formatType: 'medium'
});

In the convert date function you can change the format of your date as you require in the output

在转换日期功能中,您可以根据需要在输出中更改日期的格式

#1


6  

As for the Date Format, that can be set on the hAxis, which changes the hover value as well.
No need for a formatter...

至于日期格式,可以在hAxis上设置,也可以更改悬停值。不需要格式化程序......

As for lineWidth, that option doesn't appear to work for Material Charts, even with...

至于lineWidth,该选项似乎不适用于材质图表,即使有...

google.charts.Line.convertOptions

google.charts.Line.convertOptions

google.charts.load('current', {
  'packages': ['line']
});

google.charts.setOnLoadCallback(drawChart);

function drawChart() {
  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Year');
  data.addColumn('number', 'DataSize');
  data.addRows(22);
  data.setValue(0, 0, new Date('1994-01-01'));
  data.setValue(0, 1, 25506);
  data.setValue(1, 0, new Date('1994-02-01'));
  data.setValue(1, 1, 26819);
  data.setValue(2, 0, new Date('1994-03-01'));
  data.setValue(2, 1, 31685);
  data.setValue(3, 0, new Date('1994-04-01'));
  data.setValue(3, 1, 25611);
  data.setValue(4, 0, new Date('1994-05-01'));
  data.setValue(4, 1, 29976);
  data.setValue(5, 0, new Date('1994-06-01'));
  data.setValue(5, 1, 32590);
  data.setValue(6, 0, new Date('1994-07-01'));
  data.setValue(6, 1, 33309);
  data.setValue(7, 0, new Date('1994-08-01'));
  data.setValue(7, 1, 35825);
  data.setValue(8, 0, new Date('1994-09-01'));
  data.setValue(8, 1, 41973);
  data.setValue(9, 0, new Date('1994-10-01'));
  data.setValue(9, 1, 54067);
  data.setValue(10, 0, new Date('1994-11-01'));
  data.setValue(10, 1, 45895);

  var chart = new google.charts.Line(document.getElementById('dvRise'));
  chart.draw(data, google.charts.Line.convertOptions({
    lineWidth: 10,
    left: 0,
    top: 0,
    'height': '300',
    'width': '450',
    colors: ['#44AFED'],
    legend: {
      position: 'none'
    },
    hAxis: {format: 'MMM, yyyy'}
  }));
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div style="text-align: center; height: 320px" id="dvRise"></div>

#2


0  

try this..

尝试这个..

var date_formatter = new google.visualization.DateFormat({ 
        pattern: "MMM dd, yyyy"
}); 
date_formatter.format(data_table, 0);

#3


0  

You can set your format in hAxis options while drawing chart

您可以在绘制图表时在hAxis选项中设置格式

ex

// 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);

Working example https://jsfiddle.net/api/post/library/pure/

工作示例https://jsfiddle.net/api/post/library/pure/

#4


0  

Try This

尝试这个

function convertDate(inputFormat) {
    function pad(s) { return (s < 10) ? '0' + s : s; }
    var d = new Date(inputFormat);
    return [pad(d.getDate()),pad(d.getMonth()+1),d.getFullYear()].join('/');
}
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Year');
data.addColumn('number', 'DataSize');
data.addRows(22);
data.setValue(0, 0, convertDate('1994-01-01'));
data.setValue(0, 1, 25506);
data.setValue(1, 0, convertDate('1994-02-01'));
data.setValue(1, 1, 26819);
data.setValue(2, 0, convertDate('1994-03-01'));
data.setValue(2, 1, 31685);
data.setValue(3, 0, convertDate('1994-04-01'));
data.setValue(3, 1, 25611);
data.setValue(4, 0, convertDate('1994-05-01'));
data.setValue(4, 1, 29976);
data.setValue(5, 0, convertDate('1994-06-01'));
data.setValue(5, 1, 32590);
data.setValue(6, 0, convertDate('1994-07-01'));
data.setValue(6, 1, 33309);
data.setValue(7, 0, convertDate('1994-08-01'));
data.setValue(7, 1, 35825);
data.setValue(8, 0, convertDate('1994-09-01'));
data.setValue(8, 1, 41973);
data.setValue(9, 0, convertDate('1994-10-01'));
data.setValue(9, 1, 54067);
data.setValue(10, 0, convertDate('1994-11-01'));
data.setValue(10, 1, 45895);
var formatter_medium = new google.visualization.DateFormat({
formatType: 'medium'
});

In the convert date function you can change the format of your date as you require in the output

在转换日期功能中,您可以根据需要在输出中更改日期的格式