简体   繁体   English

Google Charts:如何自定义轴上显示的日期格式?

[英]Google Charts: How to customize the Date format displayed on the axis?

I follow the documentation on axes customization here but it does not say how to customize the date format on the AXES, only on the columns.我按照此处的轴自定义文档进行操作但它没有说明如何在 AXES 上自定义日期格式,仅在列上说明。

I need my axes format to be "dd/MM/yy" but unable to achieve such a simple taks...我需要我的轴格式为“dd/MM/yy”,但无法实现如此简单的任务...

Here is the codepen这是代码笔

 google.charts.load("current", { packages: ["line"] }); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ["Date", "A", "B", "C", "D", "E", "F"], [new Date(2006, 1, 1), 5394, 5014, 2480, 9380, 4852, 7128], [new Date(2006, 1, 8), 5697, 5147, 2480, 9187, 7644, 7134], [new Date(2006, 1, 16), 5780, 5192, 2480, 8941, 7729, 7148], [new Date(2006, 1, 25), 5993, 5211, 2480, 8750, 7768, 7151], [new Date(2006, 2, 2), 6207, 5282, 2480, 8636, 7827, 7195], [new Date(2006, 2, 11), 6334, 5361, 2548, 8515, 7874, 7140], [new Date(2006, 2, 16), 6687, 5346, 2566, 8347, 7895, 7131], [new Date(2006, 2, 28), 6967, 5398, 2802, 8220, 7831, 7141], [new Date(2006, 3, 1), 7061, 5419, 2818, 8198, 7827, 7031], [new Date(2006, 3, 2), 7335, 5457, 2829, 8211, 7959, 6966] ]); var options = { chart: { title: "my graph" }, curveType: "function", legend: { position: "none" }, axes: { x: { 0: { side: "top", label: "axes label", format: "dd/MM/yy", color: "red" } } }, hAxis: { format: "dd/MM/yyyy" }, vAxis: { format: "MMM d, y" } }; var chart = new google.charts.Line(document.getElementById("curve_chart")); chart.draw(data, options); }
 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <div class="container"> <div class="row"> <div class="col-md-12"> <div id="curve_chart"></div> </div> </div> </div>

there are several options that are not supported by material charts.有几种材料图表不支持的选项。
see --> Tracking Issue for Material Chart Feature Parity请参阅 --> 材料图表功能奇偶校验的跟踪问题

material chart --> google.charts.Line -- packages: ["line"]材料图表 --> google.charts.Line -- packages: ["line"]

classic chart --> google.visualization.LineChart -- packages: ["corechart"]经典图表 --> google.visualization.LineChart -- packages: ["corechart"]


for the options that are supported by material charts,对于材料图表支持的选项,
you need to convert those option to material options before drawing the chart...您需要在绘制图表之前将这些选项转换为材料选项...

google.charts.Line.convertOptions(options)

see following working snippet...请参阅以下工作片段...

 google.charts.load("current", { packages: ["line"] }); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ["Date", "A", "B", "C", "D", "E", "F"], [new Date(2006, 1, 1), 5394, 5014, 2480, 9380, 4852, 7128], [new Date(2006, 1, 8), 5697, 5147, 2480, 9187, 7644, 7134], [new Date(2006, 1, 16), 5780, 5192, 2480, 8941, 7729, 7148], [new Date(2006, 1, 25), 5993, 5211, 2480, 8750, 7768, 7151], [new Date(2006, 2, 2), 6207, 5282, 2480, 8636, 7827, 7195], [new Date(2006, 2, 11), 6334, 5361, 2548, 8515, 7874, 7140], [new Date(2006, 2, 16), 6687, 5346, 2566, 8347, 7895, 7131], [new Date(2006, 2, 28), 6967, 5398, 2802, 8220, 7831, 7141], [new Date(2006, 3, 1), 7061, 5419, 2818, 8198, 7827, 7031], [new Date(2006, 3, 2), 7335, 5457, 2829, 8211, 7959, 6966] ]); var options = { chart: { title: "my graph" }, curveType: "function", legend: { position: "none" }, axes: { x: { 0: { side: "top", label: "axes label", format: "dd/MM/yy", color: "red" } } }, hAxis: { format: "dd/MM/yyyy" } }; var chart = new google.charts.Line(document.getElementById("curve_chart")); chart.draw(data, google.charts.Line.convertOptions(options)); }
 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <div class="container"> <div class="row"> <div class="col-md-12"> <div id="curve_chart"></div> </div> </div> </div>


note: in the above chart, the y-axis contains numbers,注意:在上图中,y 轴包含数字,
a date format will not work on the vAxis ...日期格式不适用于vAxis ...

vAxis: { format: "MMM d, y" }  // <-- removed from above snippet

EDIT编辑

when loading google charts, the default locale is --> 'en' to load a different locale, specify the language in the load statement...加载谷歌图表时,默认语言环境是 --> 'en'加载不同的语言环境,在load语句中指定语言...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM