1

I want to create dynamic an Object like this for using it with Angular Chart.

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

I created a class named 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);
        }
    }

The problem is with the Data, I have it in a form like this : [28, 48, 40, 19, 86, 27, 90] why there should be double brackets between like [[28, 48, 40, 19, 86, 27, 90]].

What is the best way to get your data between double brackets? Like [[ and here goes my data ]]

5
  • if you want double [[ that means it would be an array in another array? What you declared for Data is an array of int public int[] Data { get; set; } Commented Mar 10, 2017 at 14:03
  • What is the best way to do this? Commented Mar 10, 2017 at 14:06
  • Why would you need an array inside another if Data is supposed to hold your integers like it actually does? Commented Mar 10, 2017 at 14:09
  • Because for angular chart's the data should be hold like [ [ data ] ] otherwise the functionalities are not working. Like the hovering etc. Commented Mar 10, 2017 at 14:11
  • take a look at the answer I posted and let me know if it fits your needs Commented Mar 10, 2017 at 14:19

1 Answer 1

2

from comments received here's how I would do it:

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.