0

我正在使用 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":""}]

任何帮助或见解将不胜感激。

4

1 回答 1

0

按照他们的指南在这里,很少有事情可能会导致问题。

首先,引用:

"url": '@Url.Action("GetChartEvents", "ChartController")',

注意 URL 周围的单引号。没有它们,您的 js 将无效,这就是图表不显示的原因。

其次,他们使用“dataLoader”而不是“eventDataLoader”属性。

第三,确保包含数据加载器插件:

<script src="amcharts/plugins/dataloader/dataloader.min.js" type="text/javascript"></script>
于 2017-12-19T17:12:44.003 回答