【问题标题】:Highcharts doesn't display dataHighcharts 不显示数据
【发布时间】:2017-04-29 06:26:14
【问题描述】:

我是使用 Highcharts 的新手。我创建了一个示例 ASP.NET MVC 应用程序,以使用从控制器返回到视图页面的 json 数据创建折线图。另外,我使用ajax通过成功回调函数获取图表的数据。

这是数据的结构(示例 4 行类型为 List 的 TagTimeValue)

private List<TagTimeValue> GetTagTimeValues()
    {
        var tagTimeValues = new List<TagTimeValue>
        {
            new TagTimeValue { ID=1, Name="CDT158", Value="23.9483", Time="04/01/2017", Good="true", Questionable="No", Units="vol", Substituted="not"},
            new TagTimeValue { ID=2, Name="CDT158", Value="24.1183", Time="04/02/2017", Good="true", Questionable="No", Units="vol", Substituted="not"},
            new TagTimeValue { ID=3, Name="CDT158", Value="25.2383", Time="04/03/2017", Good="false", Questionable="yes", Units="vol", Substituted="not"},
            new TagTimeValue { ID=4, Name="CDT158", Value="25.6713", Time="04/04/2017", Good="false", Questionable="yes", Units="vol", Substituted="not"}
        };

        return tagTimeValues;
    }

这是ajax调用调用的方法

public ActionResult UpdateTrend()
    {
        var values = GetTagTimeValues();
        return Json(values, JsonRequestBehavior.AllowGet);
    }

Index.cshtml中通过ajax调用UpdateTrend()的脚本

$(document).ready(function () {
        $.ajax({
            url: "/home/UpdateTrend",
            type: 'GET',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            cache: false
        }).success(function (dataChart) {
            var Xaxis = [];
            var dataSeries = [];

            for (var i = 0; i < dataChart.length; i++) {
                var items = dataChart[i];
                var XcategoriesItem = items.Time;
                var seriesData = items.Value;

                Xaxis.push(XcategoriesItem);
                dataSeries.push(seriesData);  
            }

            console.log(dataSeries);
            getChart(Xaxis, dataSeries);

        }).error(function (er, xhr, e) {
            console.log("Error: ", er, xhr, e);
        })
    });

还有,getChart 函数

function getChart(Xaxis, dataseries)
    {
        var myChart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'line'
            },
            title: {
                text: 'Alarms/Events Chart'
            },
            plotOptions: {
                series: {
                    dataLabels: {
                        enabled: true,
                        format: '{y}%'
                    }
                }
            },
            xAxis: {
                type: 'datetime',
                title: {
                    text: 'Time'
                },
                categories: Xaxis
            },
            yAxis: {
                title: {
                    text: 'Value'
                }
            },
            series: dataseries
        });
    };

它将图表呈现到带有id 容器的&lt;div id="container" style="width:100%; height: 500px;"&gt;&lt;/div&gt;

根据脚本,我希望我的图表具有 x 轴时间和 y 轴值作为系列。但是,当我运行应用程序时,没有显示任何数据(截图如下)。

我还检查了 Google 的 chrome console 面板中的 json 数据,可以看到 Time(x 轴)和 Value(系列)的数组已成功解析。

我的图表中是否缺少不允许显示系列的配置?或者,我的图表脚本有问题吗?

我在谷歌上搜索了类似的问题,发现了很多可能的解决方案,但很困惑我应该遵循哪一个。

非常感谢任何帮助。

【问题讨论】:

  • 我相信您正在为每个数据点制作一个系列,而不是一个包含多个数据点的系列。 highcharts 期望的是 series: [{ name: 'some name', data: [1, 2, 3, 4]}] 之类的东西,而您提供的是 series: [ 1, 2, 3, 4 ]
  • @Rob 感谢您的回复。那么,您是说 Highcharts 不接受每个数据点一个系列?如果是这种情况,那么在我的函数getChart 中,它应该类似于series: { name: 'some name', data: dataseries },其中dataseries 仍然是一个值数组。
  • 它接受,如果这是你提供的;我的意思是你错误地使用了 API; series 不是一组数据点,它更详细地描述了该系列。在文档中查看here。如果您想要每个数据点一个系列;这很好 - 但您仍然需要为每个系列提供一组数据(即使该数组包含一个元素)。所以是的,我相信你在评论中的例子应该有效
  • @Rob,我已将 series 更改为 series: [{ name: 'series 1', data: [23.9483, 24.1183, 25.2383, ....]}] 并显示图表。也感谢您分享系列 | 上的链接Highcharts 文档。我还注意到,当数组的元素是series 中的字符串时,折线图不会显示。

标签: c# jquery ajax asp.net-mvc highcharts


【解决方案1】:

自从发布这个问题以来,我对 Highchart 没有任何了解。经过 2 周的研究和使用该库,我已经学会了在图表中显示数据的基本配置。为了其他寻求帮助或刚开始使用 Highchart 的开发人员的利益,我就是这样实现它的。

ajax 调用从服务器获取数据并绘制到图表上

    function plotTrendData(data)
{
    $.ajax({
        type: 'GET',
        url: '/your url goes here/',
        data: { requestData: data },
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (data)
        {
            if (data.length > 4) {
                prepareChartData(data);
                if (myChart === null) {
                    plotChartData(dataSeries, xTitle);
                }
                else {
                    addChartSeries(dataSeries, xTitle);
                }
            }
        }
    });
}

准备图表数据的函数(dataChart 参数包含json 数据)

    function prepareChartData(dataChart)
{
    if (dataSeries.length > 0) dataSeries = [];
    for (var i = 0; i < dataChart.length; i++)
    {
        var items = dataChart[i];
        var xDate = +moment(items.Time);
        var seriesData = parseFloat(items.Value);

        dataSeries.push([xDate, seriesData]);
        xTitle = items.Name;
    }
}

将系列添加到现有图表的功能(用于多系列图表)

    function addChartSeries(dataSeries, xTitle)
{
    myChart.addSeries({
        name: xTitle,
        data: dataSeries
    });
}

当然还有在你的div元素上创建图表的函数,id为container

    function plotChartData(dataseries, xtitle)
{
    myChart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'line',
            zoomType: 'xy',
            panning: true,
            panKey: 'shift',
            plotBorderWidth: 1
        },
        credits: {
            enabled: false
        },
        title: {
            text: 'My Sample Chart'
        },
        legend: {
            layout: 'horizontal',
            align: 'left',
            itemDistance: 10,
            borderWidth: 0,
            itemMarginTop: 0,
            itemMarginBottom: 0,
            padding: 20
        },
        plotOptions: {
            series: {
                states: {
                    hover: {
                        enabled: false
                    }
                },
                dataLabels: {
                    enabled: false,
                    format: '{y}'
                },
                allowPointSelect: false
            }
        },
        xAxis: {
            type: 'datetime',
            labels: {
                rotation: -60,
                format: '{value:%d %b %Y %I:%M}',
                style: {
                    fontSize: '9px',
                    fontFamily: 'Verdana, sans-serif'
                }
            },
            crosshair: {                  
                enabled: true
            }
        },
        yAxis: {
            gridLineColor: '#DDDDDD',
            gridLineWidth: 0.5
        },
        tooltip: {
            positioner: function () {
                return {
                    x: this.chart.plotLeft,
                    y: this.chart.plotTop
                }
            },
            useHTML: true,
            pointFormat: '<small><font color="{series.color}"><strong>{series.name}</strong></font>: <strong>{point.y}</strong></small><br/>',
            headerFormat: '<span style="font-size: 8px">{point.key}</span><br/>',
            xDateFormat: '%d-%m-%Y %H:%M:%S',
            shared: true,
            valueDecimals: 2,
            followPointer: true,
            shadow: false,
            borderWidth: 0,
            backgroundColor: 'rgba(255,255,255,0.8)'
        },
        series: [{
            name: xtitle,
            data: dataseries
        }]
    });
}

上面的脚本和配置将数据显示到我的 highchart 中。是不是很神奇!

【讨论】:

    猜你喜欢
    • 2020-01-03
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多