Dygraph:日期预期错误!在 Angular 7 项目中使用 Array 作为数据源时
Dygraph: Date expected error! when using Array as data source in Angular 7 project
Api 项目在 ASP
[Route("api/[controller]")]
public class SampleDataController : Controller
{
...
[HttpGet("[action]")]
public Array GetGraphData()
{
var tsData = timeFloatService_.GetLatestDataRecords(4790, DataRecordStatus.Live, DataRecordAbnormal.Normal, 1).Get();
object[][] result = tsData.Select(x => new object[] { x.Index.ToString("yyyy/MM/dd HH:mm:ss"), x.Value }).Take(10).ToArray();
return result;
}
在调用Angular项目中收到:
import { Component, OnInit, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
private forecasts: any;
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
http.get<any[]>(baseUrl + 'api/SampleData/GetGraphData').subscribe(result => {
this.forecasts = result;
}, error => console.error(error));
}
<p>
test works!
</p>
<pre>{{forecasts|json}}</pre>
<ng-dygraphs [data]="forecasts"
[options]=VALID OPTIONS NOT SHOWN FOR BREVITY
</ng-dygraphs>
收到数据数组,但日期是字符串而不是 Date(),因此未加载 Dygraph 图表!
Dygraphs 设置正确(使用 csv 数据测试)。
即使我将日期作为日期时间传递,它也会被转换为字符串!所以我的问题是,如何将每个数组的第一个元素转换为 Angular 项目中的 Date() 。我根本找不到任何帮助!
查看文档:http://dygraphs.com/data.html#array
更改后的构造函数:
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
http.get<any[]>(baseUrl + 'api/SampleData/GetGraphData').subscribe(result => {
this.forecasts = from(result).pipe(map(item => [new Date(item[0]), item[1]]));
}, error => console.error(error));
以上将数组更改为以下
{
"_isScalar": false,
"source": {
"_isScalar": false,
"array": [
[
"2013/01/01 00:00:00",
58096
],
[
"2013/01/02 00:00:00",
50893
],
不幸的是,这行不通! (除非我犯了错误。)
需要在 DOM/Html 页面中将字符串更改为日期,因为更改后的数组仍将日期显示为字符串!
您需要从字符串创建日期对象。你可以使用 rxjs map
.
来做到这一点
示例:
import { from } from 'rxjs';
import { map } from 'rxjs/operators';
// ...
// observable array
const dates = from([
"2013/01/01 00:00:00",
"2013/01/02 00:00:00"
]);
dates.pipe(map(ds => new Date(ds))).subscribe(date => console.log(date));
在您的情况下,您显然不会映射到单个日期对象,而是映射到一个由日期对象和值组成的数组。
编辑:
好的,这里有一个更完整的例子:https://stackblitz.com/edit/angular-jfx4gr
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
// ...
httpGet = of([
[
"2013/01/01 00:00:00",
58096
], [
"2013/01/02 00:00:00",
50893
]
]);
constructor() {
this.httpGet
.pipe(map(item => item.map(i => [ new Date(i[0]), i[1] ])))
.subscribe(result => console.log(result));
}
或者实际上您可以通过在订阅回调中使用 array.map
使事情变得更简单:
this.httpGet
.subscribe(result => {
const converted = result.map(i => [ new Date(i[0]), i[1] ]);
console.log(converted);
});
Api 项目在 ASP
[Route("api/[controller]")]
public class SampleDataController : Controller
{
...
[HttpGet("[action]")]
public Array GetGraphData()
{
var tsData = timeFloatService_.GetLatestDataRecords(4790, DataRecordStatus.Live, DataRecordAbnormal.Normal, 1).Get();
object[][] result = tsData.Select(x => new object[] { x.Index.ToString("yyyy/MM/dd HH:mm:ss"), x.Value }).Take(10).ToArray();
return result;
}
在调用Angular项目中收到:
import { Component, OnInit, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
private forecasts: any;
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
http.get<any[]>(baseUrl + 'api/SampleData/GetGraphData').subscribe(result => {
this.forecasts = result;
}, error => console.error(error));
}
<p>
test works!
</p>
<pre>{{forecasts|json}}</pre>
<ng-dygraphs [data]="forecasts"
[options]=VALID OPTIONS NOT SHOWN FOR BREVITY
</ng-dygraphs>
收到数据数组,但日期是字符串而不是 Date(),因此未加载 Dygraph 图表!
Dygraphs 设置正确(使用 csv 数据测试)。
即使我将日期作为日期时间传递,它也会被转换为字符串!所以我的问题是,如何将每个数组的第一个元素转换为 Angular 项目中的 Date() 。我根本找不到任何帮助!
查看文档:http://dygraphs.com/data.html#array
更改后的构造函数:
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
http.get<any[]>(baseUrl + 'api/SampleData/GetGraphData').subscribe(result => {
this.forecasts = from(result).pipe(map(item => [new Date(item[0]), item[1]]));
}, error => console.error(error));
以上将数组更改为以下
{
"_isScalar": false,
"source": {
"_isScalar": false,
"array": [
[
"2013/01/01 00:00:00",
58096
],
[
"2013/01/02 00:00:00",
50893
],
不幸的是,这行不通! (除非我犯了错误。)
需要在 DOM/Html 页面中将字符串更改为日期,因为更改后的数组仍将日期显示为字符串!
您需要从字符串创建日期对象。你可以使用 rxjs map
.
示例:
import { from } from 'rxjs';
import { map } from 'rxjs/operators';
// ...
// observable array
const dates = from([
"2013/01/01 00:00:00",
"2013/01/02 00:00:00"
]);
dates.pipe(map(ds => new Date(ds))).subscribe(date => console.log(date));
在您的情况下,您显然不会映射到单个日期对象,而是映射到一个由日期对象和值组成的数组。
编辑:
好的,这里有一个更完整的例子:https://stackblitz.com/edit/angular-jfx4gr
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
// ...
httpGet = of([
[
"2013/01/01 00:00:00",
58096
], [
"2013/01/02 00:00:00",
50893
]
]);
constructor() {
this.httpGet
.pipe(map(item => item.map(i => [ new Date(i[0]), i[1] ])))
.subscribe(result => console.log(result));
}
或者实际上您可以通过在订阅回调中使用 array.map
使事情变得更简单:
this.httpGet
.subscribe(result => {
const converted = result.map(i => [ new Date(i[0]), i[1] ]);
console.log(converted);
});