0

将折线图的 x 轴标签从一段文本更改为另一段文本不起作用;请问我做错了什么?

我有一个折线图,其离散的 x 轴标有日期的文本表示。(我正在使用 corechart;我创建了一个 dataTable,基于它创建了一个 dataView,并将图表创建为 ChartWrapper)。

我正在根据文本日期过滤 dataView,因此我的初始 x 轴域值的格式为 2013-09-01... 并且有效。但现在我需要将 x 轴标签更改为 9/2013 格式。我在此找到的示例似乎很清楚,但图表没有绘制,而是被错误替换:“c is null”。谷歌搜索,问题听起来像我的域列是错误的数据类型,但我不明白这是怎么可能的。

你能指出我的错误吗?下面,我得到了我需要显示的列列表;这将是一个类似 [0,3,5] 的列表,其中 0 是域列。我先将其删除,以便设置新的格式化列:

// Format the x-axis as n/Y
// remove unformatted column 0;
view_col_list.splice(0, 1);
data_displayed.setColumns([
  {
    role: 'domain',
    calc: function(dataTable, row) {
      var my_date = new Date(dataTable.getValue(row, 0));
      console.info('the date I want to format: %o',my_date);
      // this does in fact produce "9/2013"
      console.info('the date I want to show' + my_date.getMonth() + '/' + my_date.getFullYear());
      return my_date.getMonth() + '/' + my_date.getFullYear();
    },
    type: 'string',
    sourceColumn: 0,
    id: 0
  },
  view_col_list
]);
4

1 回答 1

1

I would guess that your dates are probably not the problem, but there are a few things I would recommend changing with them: remove the "sourceColumn" attribute, as it isn't needed; and change the way you are constructing your new date string, as converting a string to a Date object is inconsistent across browsers. Also, the #getMonth method returns the 0-indexed month, so "2013-09-01" would get turned into "8/2013" in your code (assuming the date string conversion works). There is an easier way that doesn't involve converting to Date objects and back into strings:

var dateArray = dataTable.getValue(row, 0).split('-');
return dateArray[1] + '/' + dateArray[0];

I suspect the problem is caused by this:

view_col_list.splice(0, 1);
data_displayed.setColumns([{...}, view_col_list]);

which is equivalent to data_displayed.setColumns([{...}, [...]]); which definitely won't work. Rather than splice the first element from the view_col_list, replace it with your object:

view_col_list[0] = {...};
data_displayed.setColumns(view_col_list);
于 2013-09-13T18:23:36.733 回答