Api project in 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;
}
Which is received in the calling Angular Project:
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>
Array of data is received, but the Date is a string not a Date() so the Dygraph chart isnt loaded!
Dygraphs is set up correctly(tested with csv data).
Even if I pass the Date as a DateTime it will be converted to a string! So my question is, how do I cast the first element of each array into a Date() inside an Angular project. I cant find any help on this at all!
See docs: http://dygraphs.com/data.html#array
CONSTRUCTOR AFTER CHANGES:
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));
Above changes array to the following
{
"_isScalar": false,
"source": {
"_isScalar": false,
"array": [
[
"2013/01/01 00:00:00",
58096
],
[
"2013/01/02 00:00:00",
50893
],
Which unfortunately wont work! (Unless I've made an error.)
Need to change string to Date in DOM/Html page as the altered array was still displaying the date as a string!
You need to create date objects from the strings. You can do this using rxjs map
.
Example:
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));
In your case you'd obviously not map to a single date object but an array consisting of a date object and a value.
EDIT:
Ok, here's a more complete example for your case: 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));
}
Or actually you could make things even simpler by using array.map
in your subscribe callback:
this.httpGet
.subscribe(result => {
const converted = result.map(i => [ new Date(i[0]), i[1] ]);
console.log(converted);
});