简体   繁体   English

折线图(谷歌图表)自定义标签

[英]Line chart (google chart) custom labels

Well, I am using a LineChart with Angular 4 and I make sure to change labels according to window size like the following:好吧,我正在使用带有 Angular 4 的 LineChart,并且我确保根据窗口大小更改标签,如下所示:

@HostListener('window:resize', ['$event']) onResize(event) {
  if (event.target['innerWidth'] < 420) {
    this.stockAnalysisService.getOptionsY()['hAxis']['format'] = 'MMM';
  } else if (event.target['innerWidth'] < 760) {
    this.stockAnalysisService.getOptionsY()['hAxis']['format'] = 'MM. yy\'';
  } else { this.stockAnalysisService.getOptionsY()['hAxis']['format'] = 'MMM d, yyyy'; }
this.drawBasic();
}

This is just basic Angular syntax to detect resize or window and change the hAxis labels accordingly.这只是用于检测调整大小或窗口并相应地更改 hAxis 标签的基本 Angular 语法。

My question is, if I want a custom label where I present months on the labels and the months are presented with values of DAY OF THE MONTH and ONLY the first day of the month will have an addition of text to it like the following image:我的问题是,如果我想要一个自定义标签,我在标签上显示月份,并且月份显示为DAY OF THE MONTH 的值,并且只有当月的第一天才会添加文本,如下图所示:

在此处输入图片说明

RED : days of the month (jumps 5 days each time but not relevant) BLACK : first indication of the month (Should not be NOV 10, but NOV 1, not relevant)红色:一个月中的天数(每次跳 5 天但不相关)黑色:月份的第一个指示(不应该是 11 月 10 日,而是 11 月 1 日,不相关)

Any idea?任何的想法?

to have one or more labels different from than rest,有一个或多个不同于其他的标签,
will need to use option --> hAxis.ticks将需要使用选项 --> hAxis.ticks

this means you will need to build an array of the labels that should be displayed这意味着您需要构建一个应显示的标签数组

using object notation, for each tick you can provide使用对象表示法,对于每个刻度,您可以提供
the value of the tick ( v: )刻度的值 ( v: )
and the formatted value of the tick ( f: )和刻度的格式化值 ( f: )

{v: dateValue, f: displayValue}

the value ( v: ) should be the same type as the x-axis, in this case --> 'date'值 ( v: ) 应该与 x 轴的类型相同,在这种情况下 --> 'date'
the formatted value ( f: ) should be --> 'string'格式化的值 ( f: ) 应该是 --> 'string'

if you don't use object notation, and just provide a date for the tick,如果您不使用对象表示法,而只提供刻度的日期,
the label will be displayed according to --> hAxis.format标签将根据 --> hAxis.format显示

so, for the dates that should have the month prefix,所以,对于应该有月份前缀的日期,
use object notation, for the rest, just provide the date使用对象表示法,其余的,只需提供日期

see following working snippet for an example...有关示例,请参见以下工作片段...

 google.charts.load('current', { packages: ['controls', 'corechart', 'table'] }).then(function () { var data = new google.visualization.DataTable(); data.addColumn('date', 'x'); data.addColumn('number', 'y0'); data.addRows([ [new Date(2017, 7, 1), 2], [new Date(2017, 7, 2), 3], [new Date(2017, 7, 4), 1], [new Date(2017, 7, 8), 5], [new Date(2017, 7, 16), 6], [new Date(2017, 7, 20), 7], [new Date(2017, 7, 24), 1], [new Date(2017, 7, 26), 2], [new Date(2017, 7, 27), 3], [new Date(2017, 8, 1), 2], [new Date(2017, 8, 2), 3], [new Date(2017, 8, 4), 9], [new Date(2017, 8, 8), 5], [new Date(2017, 8, 16), 6], [new Date(2017, 8, 20), 7], [new Date(2017, 8, 24), 1], [new Date(2017, 8, 26), 2], [new Date(2017, 8, 27), 3] ]); var oneDay = (1000 * 60 * 60 * 24); var dateRange = data.getColumnRange(0); var formatMonth = new google.visualization.DateFormat({ pattern: 'MMM dd' }); // build ticks var ticksX = []; for (var i = dateRange.min.getTime(); i <= dateRange.max.getTime(); i = i + oneDay) { var rowDate = new Date(i); if (rowDate.getDate() === 1) { // add first day of month ticksX.push({ v: rowDate, f: formatMonth.formatValue(rowDate) }); } else if (((i - dateRange.min.getTime()) % 7) === 0) { // add date every seven days ticksX.push(rowDate); } } var container = document.getElementById('chart_div'); var chart = new google.visualization.LineChart(container); chart.draw(data, { chartArea: { bottom: 36, left: 48, right: 12, top: 12, width: '100%', height: '100%' }, hAxis: { format: 'dd', ticks: ticksX }, width: 800 }); });
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div>

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

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