我正在使用 amcharts 股票图表。使用以下代码时,我的图表加载正常并且一切正常:
var chart = AmCharts.makeChart("chartdiv", {
"type": "stock",
"theme": "light",
"dataSets": [{
"title": "Values",
"color": "#b0de09",
"fieldMappings": [{
"fromField": "value",
"toField": "value"
}, {
"fromField": "average",
"toField": "average"
}],
"dataProvider": @Html.Raw(ViewBag.ChartData),
"categoryField": "date"
}],
...
我的控制器:
public class ChartController : Controller
{
public IActionResult Index()
{
ViewBag.ChartData = JsonConvert.SerializeObject(GetChartData ());
return View();
}
}
但是,当我尝试通过添加以下代码来添加股票事件时,会发生两件事:1) 图表不再显示,2) 控制器中的 GetChartEvents 操作永远不会被调用,因此显然永远不会返回任何数据。
var chart = AmCharts.makeChart("chartdiv", {
"type": "stock",
"theme": "light",
"dataSets": [{
"title": "Values",
"color": "#b0de09",
"fieldMappings": [{
"fromField": "value",
"toField": "value"
}, {
"fromField": "average",
"toField": "average"
}],
"dataProvider": @Html.Raw(ViewBag.ChartData),
"categoryField": "date",
"eventDataLoader": {
"url": @Url.Action("GetChartEvents", "ChartController"),
"format": "json",
"showCurtain": true,
"showErrors": true,
"async": true
}
}],
...
并在控制器中
public class ChartController : Controller
{
…
[HttpGet]
public IActionResult GetChartEvents()
{
ChartEvent[] events = new ChartEvent[3];
events[0] = new ChartEvent(new DateTime(2007, 12, 17));
events[1] = new ChartEvent(new DateTime(2008, 10, 27));
events[2] = new ChartEvent(new DateTime(2009, 07, 06));
return Json(events);
}
}
ChartEvent 是我自己的类,但因为从未调用 GetChartEvents,所以我认为问题不在于返回数据的格式。在视图中,如果我替换
"url": @Url.Action("GetChartEvents", "ChartController"),
和
"url": "/Chart/GetChartEvents",
GetChartEvents 仍然从未被调用,但图表会显示自己。我什至尝试将事件放在 ViewBag 中,但这也不起作用
"dataProvider": @Html.Raw(ViewBag.ChartData),
"stockEvents": @Html.Raw(ViewBag.ChartEvents)
作为最后一点(和健全性检查!),如果我将@Html.Raw(ViewBag.ChartEvents) 的内容输出到页面,我会得到以下内容:
[{"date":"Date(2007,12,17)","type":"arrowUp","backgroundColor":"#00CC00","graph":"g1","description":""}, {"date":"Date(2008,10,27)","type":"arrowUp","backgroundColor":"#00CC00","graph":"g1","description":""}, { "date":"Date(2009,07,06)","type":"arrowUp","backgroundColor":"#00CC00","graph":"g1","description":""}]
任何帮助或见解将不胜感激。