【问题标题】:Create dynamic object for angular chart为角度图创建动态对象
【发布时间】:2017-07-31 21:44:24
【问题描述】:

我想创建一个像这样的动态对象,以便将它与 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] 为什么在like之间应该有双括号 [[28, 48, 40, 19, 86, 27, 90]].

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

【问题讨论】:

  • 如果你想要双倍[[,这意味着它将是另一个数组中的一个数组?您为 Data 声明的是一个 int public int[] Data { get; set; } 数组
  • 最好的方法是什么?
  • 如果 Data 应该像实际一样保存整数,为什么还要在另一个数组中添加一个数组?
  • 因为角度图表的数据应该像 [ [ data ] ] 那样保存,否则功能不起作用。比如悬停等。
  • 看看我发布的答案,如果它符合您的需求,请告诉我

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


【解决方案1】:

从收到的 cmets 那里我会这样做:

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;

【讨论】: