简体   繁体   English

AG-Grid 图表上的格式标签

[英]Format labels on AG-Grid charts

How can I format the labels that appear on AG-Grid chart's axis?如何格式化出现在 AG-Grid 图表轴上的标签? I have a lot of time-series data so I expect users frequently to produce charts where the horizontal axis is a date.我有很多时间序列数据,所以我希望用户经常制作横轴是日期的图表。 Unfortunately, this produces unreadable chart labels because the dates are not formatted (see attached image) - The labels look like "Thu Jan 09 2020 00:00:00 GMT+0000 (Greenwich Mean Time)" when all I would like is simply "2020-01-09".不幸的是,这会产生不可读的图表标签,因为日期没有格式化(见附图) - 标签看起来像“Thu Jan 09 2020 00:00:00 GMT+0000(格林威治标准时间)”,而我想要的只是“ 2020-01-09”。 My The dates in the grid look fine thanks to a valueFormatter for dates.由于日期的 valueFormatter,我的网格中的日期看起来很好。

It is also very common for users to produce pivot tables using the date.用户使用日期生成数据透视表也很常见。 This produces similarly terrible results for the labels, but I've found I can use "processSecondaryColGroupDef" to format dates that appear in the column headers.这对标签产生了同样糟糕的结果,但我发现我可以使用“processSecondaryColGroupDef”来格式化列标题中出现的日期。 Is there a similar way to do this for charts?有没有类似的方法可以对图表执行此操作?

Thankyou, Troy.谢谢你,特洛伊。 在此处输入图片说明

From the docs -从文档 -

For time axes, a format string can be provided, which will be used to format the data for display as axis labels对于时间轴,可以提供格式字符串,用于格式化数据以显示为轴标签

You can either explicitly set the axis type to 'time', but you can also remove this and the chart will still use the time axis as it automatically detects the axis type from the data in the Date column.您可以将轴类型明确设置为“时间”,但您也可以将其删除,图表仍将使用时间轴,因为它会自动从日期列中的数据检测轴类型。

You can implement processChartOptions callback and add your customizations -您可以实现processChartOptions回调并添加您的自定义 -

processChartOptions(params) {
    var options = params.options;
    var dateFormatter = function(params) {
      return params.value.value && para[enter link description here][1]ms.value.value.toLocaleDateString
        ? params.value.value.toLocaleDateString()
        : params.value;
    };
    if (["line"].indexOf(params.type) < 0) {
      if (options.xAxis && options.yAxis) {
        options.xAxis.label.formatter = dateFormatter;
        options.yAxis.label.formatter = dateFormatter;
      }
    } else {
      options.xAxis.type = "time";
      options.xAxis.label.format = "%d %B";
    }

Example and details here示例和详细信息在这里

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

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