简体   繁体   English

高图(高库存)-数据不会显示

[英]HighCharts (highstock) - data will not display

I am experimenting with the HighStock API and am experiencing an issue displaying the data returned from my Server. 我正在尝试使用HighStock API并且遇到了显示从服务器返回的数据的问题。

The javascript query is as follows: javascript查询如下:

var displayOHLCChart = function () {
    var $input = $(this);
    var listedId = $('[data-field="Id"]').val();
    var url = '/ListedSecurities/GetChartData/' + listedId;
    $.getJSON(url, function (data) {
        $('#ohlc-container').highcharts('StockChart', {
            rangeSelector: {
                selected: 2
            },

            title: {
                text: 'AAPL Stock Price'
            },

            series: [{
                type: 'ohlc',
                name: 'AAPL Stock Price',
                data: data,
                dataGrouping: {
                    enabled: false
                }

            }]
        });
    });
};

$("#ohlc-container").each(displayOHLCChart);

My call to /ListedSecurities/GetChartData/1 returns the following JSON 我对/ListedSecurities/GetChartData/1调用返回以下JSON

Edit: Copy and paste left off some commas 编辑:复制并粘贴一些逗号

[{"date":"1/07/1996","DayOpen":2.95,"DayHigh":2.97,"DayLow":2.89,"DayClose":2.89},
{"date":"2/07/1996","DayOpen":2.91,"DayHigh":2.99,"DayLow":2.91,"DayClose":2.99},
{"date":"3/07/1996","DayOpen":3.00,"DayHigh":3.00,"DayLow":2.95,"DayClose":2.97},
{"date":"4/07/1996","DayOpen":2.95,"DayHigh":2.96,"DayLow":2.90,"DayClose":2.96},
{"date":"5/07/1996","DayOpen":2.96,"DayHigh":2.98,"DayLow":2.95,"DayClose":2.96},
{"date":"8/07/1996","DayOpen":2.95,"DayHigh":3.00,"DayLow":2.93,"DayClose":2.98},
{"date":"9/07/1996","DayOpen":2.98,"DayHigh":2.98,"DayLow":2.95,"DayClose":2.96}]

When the page renders I see the Chart Title, zoom buttons, timeline scroller but no data. 页面呈现后,我会看到“图表标题”,​​缩放按钮,时间轴滚动条,但没有数据。

If I look at 'data' in my java script, it says that is contains 7 objects (one for each date above), and I can drill down to see the actual elements. 如果我在Java脚本中查看“数据”,它说其中包含7个对象(上面的每个日期都包含一个对象),我可以向下钻取以查看实际元素。 If the issue is 'data' is return objects inside objects, how do I change the following LINQ query to return the correct JSON? 如果问题是“数据”是对象内部的返回对象,如何更改以下LINQ查询以返回正确的JSON?

public ActionResult GetChartData(int? id)
    {
        var EODData = _unitOfWork.Repository<EODSecurityData>()
            .Query()
            .OrderBy(q => q
                .OrderBy(c => c.EODDate))
            .Filter(x => x.ListedSecurityId == id)
            .Get().ToList()
            .Select(r => new { date= r.EODDate.ToShortDateString(), r.DayOpen, r.DayHigh, r.DayLow, r.DayClose });
        return Json(EODData, JsonRequestBehavior.AllowGet);
    }

Thanks. 谢谢。

There is a problem with the json, the last two items in the collection are not separated by a comma. json出现问题,集合中的最后两项没有用逗号分隔。

This (line 5 & 6): 这(第5和6行):

[{"date":"1/07/1996","DayOpen":2.95,"DayHigh":2.97,"DayLow":2.89,"DayClose":2.89},
{"date":"2/07/1996","DayOpen":2.91,"DayHigh":2.99,"DayLow":2.91,"DayClose":2.99},  
{"date":"3/07/1996","DayOpen":3.00,"DayHigh":3.00,"DayLow":2.95,"DayClose":2.97},
{"date":"4/07/1996","DayOpen":2.95,"DayHigh":2.96,"DayLow":2.90,"DayClose":2.96},
{"date":"5/07/1996","DayOpen":2.96,"DayHigh":2.98,"DayLow":2.95,"DayClose":2.96}    
{"date":"8/07/1996","DayOpen":2.95,"DayHigh":3.00,"DayLow":2.93,"DayClose":2.98}   
{"date":"9/07/1996","DayOpen":2.98,"DayHigh":2.98,"DayLow":2.95,"DayClose":2.96}]

Should be this: 应该是这样的:

[{"date":"1/07/1996","DayOpen":2.95,"DayHigh":2.97,"DayLow":2.89,"DayClose":2.89},
{"date":"2/07/1996","DayOpen":2.91,"DayHigh":2.99,"DayLow":2.91,"DayClose":2.99},  
{"date":"3/07/1996","DayOpen":3.00,"DayHigh":3.00,"DayLow":2.95,"DayClose":2.97},
{"date":"4/07/1996","DayOpen":2.95,"DayHigh":2.96,"DayLow":2.90,"DayClose":2.96},
{"date":"5/07/1996","DayOpen":2.96,"DayHigh":2.98,"DayLow":2.95,"DayClose":2.96},    
{"date":"8/07/1996","DayOpen":2.95,"DayHigh":3.00,"DayLow":2.93,"DayClose":2.98},   
{"date":"9/07/1996","DayOpen":2.98,"DayHigh":2.98,"DayLow":2.95,"DayClose":2.96}]

Here is a jsFiddle with the corrected data: 这是带有更正数据的jsFiddle:

http://jsfiddle.net/hutchonoid/99DUu/ http://jsfiddle.net/hutchonoid/99DUu/

Update 更新

I think the date has to be represented in milliseconds. 我认为日期必须以毫秒为单位。

If you see the example jsFiddle here: 如果您在此处看到示例jsFiddle:

http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/stock/demo/basic-line/ http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/stock/demo/basic-line/

It represents the data as follows, millisecond date and plot 它代表以下数据,毫秒日期和图表

...
/* May 2006 */
[1147651200000,67.79],
[1147737600000,64.98],
[1147824000000,65.26],
[1147910400000,63.18],
[1147996800000,64.51],
[1148256000000,63.38],
[1148342400000,63.15],
[1148428800000,63.34],
[1148515200000,64.33],
[1148601600000,63.55],
[1148947200000,61.22],
[1149033600000,59.77],
...

I found it was related to the way my data was being passed back through the JSON object. 我发现这与通过JSON对象传回我的数据的方式有关。 Props to hutchonoid whose jsfiddle examples pointed me in the right direction. hutchonoid的支持,其jsfiddle示例为我指明了正确的方向。

To get the correctly formatted JSON result I needed to generate a generic object with the relevant properties and also had to modified the DateTime element to a Javascript datetime element. 为了获得格式正确的JSON结果,我需要生成具有相关属性的通用对象,并且还必须将DateTime元素修改为Javascript datetime元素。 The code has ended up looking like this. 该代码最终看起来像这样。

public ActionResult GetChartData(int? id)
    {
        if (id == null)
        {
            return Json(null);
        }
        var EODData = _unitOfWork.Repository<EODSecurityData>()
            .Query()
            .OrderBy(q => q
                .OrderBy(c => c.EODDate))
            .Filter(x => x.ListedSecurityId == id)
            .Get().ToList()
            .Select(r => new { date= r.EODDate, r.DayOpen, r.DayHigh, r.DayLow, r.DayClose });

        List<object> output = new List<object>();
        foreach (var dataElement in EODData)
        {
            output.Add(new object[] { ToJsonTicks(dataElement.date), dataElement.DayOpen, dataElement.DayHigh, dataElement.DayLow, dataElement.DayClose });
        }

        return Json(output, JsonRequestBehavior.AllowGet);
    }

    public long ToJsonTicks(DateTime value)
    {
        return (value.ToUniversalTime().Ticks - ((new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks)) / 10000;
    }

Notice the looping through of the Entity Framework results to add each to an Object list and the conversion of the DateTime property with the ToJsonTicks method. 请注意,遍历实体框架结果以将每个结果添加到对象列表,并使用ToJsonTicks方法转换DateTime属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM