简体   繁体   English

从 ngx-line-chart 中删除时间

[英]Remove time from ngx-line-chart

This should be simple, but I can't find it anywhere.这应该很简单,但我在任何地方都找不到。 My charts have started showing the time in the xAxis like this:我的图表已开始在 xAxis 中显示时间,如下所示:

在此处输入图片说明

Notice how the points in the line graph all land on a day, so I have no idea why it's showing time at all.注意折线图中的点是如何在一天内着陆的,所以我完全不知道它为什么显示时间。 The API I am calling returns the data like this:我正在调用的 API 返回这样的数据:

{
"success":true,
"failure":false,
"error":null,
"result":[
    {
    "brandId":329,
    "brandName":"Lutron",
    "date":"2021-11-06T00:00:00",
    "total":20,
    "numberOfRecipients":38.4413844870790
    },
    {
    "brandId":329,
    "brandName":"Lutron",
    "date":"2021-11-03T00:00:00",
    "total":18,
    "numberOfRecipients":28.8687847799206
    },
    {
    "brandId":329,
    "brandName":"Lutron",
    "date":"2021-11-09T00:00:00",
    "total":14,
    "numberOfRecipients":18.3646815040250
    },
    {
    "brandId":329,
    "brandName":"Lutron",
    "date":"2021-11-07T00:00:00",
    "total":8,
    "numberOfRecipients":11.9368140491344
    },
    {
    "brandId":329,
    "brandName":"Lutron",
    "date":"2021-11-04T00:00:00",
    "total":8,
    "numberOfRecipients":14.0534477877340
    },
    {
    "brandId":329,
    "brandName":"Lutron",
    "date":"2021-11-05T00:00:00",
    "total":16,
    "numberOfRecipients":30.2606736221572
    },
    {
    "brandId":329,
    "brandName":"Lutron",
    "date":"2021-11-08T00:00:00",
    "total":10,
    "numberOfRecipients":13.8163797040256
    }
],
"message":null
}

As you can see, all the dates show midnight and then I use a method to convert these into the series like this:如您所见,所有日期都显示为午夜,然后我使用一种方法将它们转换为如下系列:

private createData(name: string, totals: BrandDemoTotals[]): any {
    return {
        name,
        series: totals.map((total: BrandDemoTotals) => {
            return {
                name: new Date(total.date),
                value: total.numberOfRecipients,
            };
        }),
    };
}

Does anyone know how I can get rid of the times?有谁知道我怎样才能摆脱时代?


Update 1更新 1

I should mention that these times only appear when the screen resolution is wide enough, so they have done it to have more data points, but it's actually giving false data.我应该提一下,这些时间只在屏幕分辨率足够宽时才会出现,所以他们这样做是为了有更多的数据点,但它实际上提供了错误的数据。 In the image below, it shows 34 people at 12pm on Friday the 5th.在下图中,它显示了 5 日星期五中午 12 点的 34 人。 This is not true :(这不是真的 :(

The chart is simple, it looks like this:图表很简单,看起来像这样:

<ngx-charts-line-chart [id]="id" [legend]="legend" [legendPosition]="legendPosition"
    [legendTitle]="legendTitle" [scheme]="colorScheme" [showXAxisLabel]="showXAxisLabel"
    [showYAxisLabel]="showYAxisLabel" [xAxis]="xAxis" [yAxis]="yAxis" [xAxisLabel]="xAxisLabel"
    [yAxisLabel]="yAxisLabel" [timeline]="timeline" [results]="data" *ngIf="!loading && data">
</ngx-charts-line-chart>

And the code that supplies it is this:提供它的代码是这样的:

@Input() id: string;
@Input() loading = true;
@Input() data: any[];
@Input() title: string;

public docDefinition: any;
public legend = true;
public legendPosition = LegendPosition.Right;
public legendTitle = '';
public showLabels = true;
public animations = true;
public xAxis = true;
public yAxis = true;
public showYAxisLabel = true;
public showXAxisLabel = true;
public xAxisLabel = 'Date';
public yAxisLabel = 'Count';
public timeline = false;

public colorScheme: Color = {
  domain: ['#00c8b7', '#081d4d', '#b24ebe', '#00a4eb'],
  name: 'test',
  group: null,
  selectable: true,
};

I think false data is for your date formatting.我认为错误数据用于您的日期格式。 the easiest way to format date is using moment.js library.格式化日期的最简单方法是使用moment.js库。 so I use that and change your method:所以我使用它并改变你的方法:

import moment=require('moment');
  
createData(name: string, totals: any[]): any {
    return {
     name,
     series: totals.map((total: any) => {
        return {
            name: moment(total.date).format('MMMM DD YYYY, h:mm a'),
            value: total.numberOfRecipients,
        };
    }),
  };
}

now name return like November 06 2021, 12:00 am现在名称返回如November 06 2021, 12:00 am

But if you want disappear xAxis totally, just set public xAxis = false in chart config.但是如果你想完全消失xAxis ,只需在图表配置中设置public xAxis = false

Hope this answer can help you!希望这个回答能帮到你!

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

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