为角度图表创建动态对象

时间:2017-03-10 14:01:40

标签: javascript c# angularjs model-view-controller

我想创建一个像这样的动态对象,以便将它与Angular Chart一起使用。

[{
id:0,
name: 'WI',
type: 'NI',
labels: ["January", "February", "March", "April", "May", "June", "July"],
data: [[28, 48, 40, 19, 86, 27, 90]]
}];

我创建了一个名为LineChart的类:

public class LineChart 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string[] Labels { get; set; }
    public int[] Data { get; set; }
}


public async Task<HttpResponseMessage> GetInformation()
    {
        try
        {

            string[] labels = new string[7] {"January", "February", "March", "April", "May", "June", "July"};
            int[] numbers = new int[] { 28, 48, 40, 19, 86, 27, 90 };

            List<LineChart> line = new List<LineChart>();
            LineChart linechart = new LineChart();
            linechart.Id = 0;
            linechart.Name = "WI";
            linechart.Data = numbers;
            linechart.Labels = labels;

            line.Add(linechart);
            return Request.CreateResponse(HttpStatusCode.OK, line);
        }
        catch (SqlException)
        {
            return Request.CreateErrorResponse(
                HttpStatusCode.ServiceUnavailable,
                "The service is temporarily unavailable. Please try again at a later time.");
        }
        catch (Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, e);
        }
    }

问题在于数据,我有这样的形式:[28,48,40,19,86,27,90]为什么两者之间应该有双括号 [[28,48,40,19,86,27,90]]。

在双括号之间获取数据的最佳方法是什么?喜欢[[这里是我的数据]]

1 个答案:

答案 0 :(得分:2)

来自此处收到的评论&#39;我将如何做:

string[] labels = new string[7] {"January", "February", "March", "April", "May", "June", "July"};
        int[] numbers = new int[] { 28, 48, 40, 19, 86, 27, 90 };
        var arrayHolder = new List<int[]>();
        arrayHolder.Add(numbers);

        List<LineChart> line = new List<LineChart>();
        LineChart linechart = new LineChart();
        linechart.Id = 0;
        linechart.Name = "WI";
        linechart.Data = arrayHolder.ToArray();
        linechart.Labels = labels;