> Get(-6ren">
gpt4 book ai didi

javascript - 在 highcharts 中显示 .Net Core Web API 响应

转载 作者:行者123 更新时间:2023-12-01 01:18:49 24 4
gpt4 key购买 nike

我想在highcharts中显示数据,API返回json,highcharts显示数据需要任何转换吗?

代码:

[HttpGet("{id}")]
public ActionResult<List<Tuple<string, double>>> Get(int id)
{
using (StockContext stockContext = new StockContext())
{
var company = stockContext
.Companies
.Where(x => x.Id == id)
.FirstOrDefault();

var info = company
.Informations
.ToList()
.Select(x => new Tuple<string, double>(((long)x.Date.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds).ToString(), x.Close)), x.Close))
.ToList();
return new ObjectResult(info);
}
}

示例数据:

[{"item1":"1548201600000","item2":2470.0},

{"item1":"1548115200000","item2":2469.0},

{"item1":"1548028800000","item2":2440.0}]

客户端:

fetch(url)
.then(res => res.json())
.then(json => {
this.setState({
data: json, // data passed to highcharts but doesn't show anything
});
});

最佳答案

根据您要使用的 Highcharts 系列,您必须以三种允许的格式之一提供数据:

数组 - 仅 y - 值:

data: [0, 5, 3, 5]

数组 - xy 值:

data: [
[0, 1],
[1, 2],
[2, 8]
]

对象:

data: [{
x: 1,
y: 9,
name: "Point2",
color: "#00FF00"
}, {
x: 1,
y: 6,
name: "Point1",
color: "#FF00FF"
}]

现场演示:http://jsfiddle.net/BlackLabel/hpd6kt2z/

API引用:https://api.highcharts.com/highcharts/series.line.data

关于javascript - 在 highcharts 中显示 .Net Core Web API 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54455681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com