0

I am trying to add two arraylists to a 2 dimensional array but I am coming across a problem. If I fill the array manually, shown in the code below as array arr1, and bind this array to a chart, the chart displays as expected. However, when I add the two arraylists to the array using a for loop and bind this array to a chart, it does not display as expected.

My Code:

DateTime date1 = new DateTime(2012, 10, 1);
DateTime date2 = new DateTime(2012, 11, 2);
DateTime date3 = new DateTime(2012, 12, 3);
DateTime date4 = new DateTime(2013, 01, 4);
DateTime date5 = new DateTime(2013, 02, 8);

//Create time arraylist
ArrayList al1 = new ArrayList();
al1.Add(date1);
al1.Add(date2);
al1.Add(date3);
al1.Add(date4);
al1.Add(date5);

int int1 = 9;
int int2 = 15;
int int3 = 20;
int int4 = 13;
int int5 = 17;

//Create int arraylist
ArrayList al2 = new ArrayList();
al2.Add(int1);
al2.Add(int2);
al2.Add(int3);
al2.Add(int4);
al2.Add(int5);

//Tester 2D array
Object[,] arr1 = new Object[,] { {date1, int1}, {date2, int2}, {date3, int3}, {date4,int4}, {date5, int5} };

//Create 2D array
Object[,] arr = new Object[2, al2.Count];

for (int k = 0; k <al2.Count; k++)
        {
            arr[0, k] = al1[k];
            arr[1, k] = al2[k];
        }

Data d1 = new Data(arr);

Series s1 = new Series { Name = "Series 1", Data=d1};

DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart");

chart.SetXAxis(new XAxis
{
Type = AxisTypes.Datetime
});

chart.SetSeries(s1);

ltrChart.Text = chart.ToHtmlString();

Is my problem with in the adding of the arraylists to the array or is it something within dotnet highcharts?

4
  • And what are the two results. What is the expected and what is the actual result? Commented Jun 19, 2014 at 9:20
  • The expected results is a line graph of time against int, { {1/10/2012, 9} {2/11/2012, 15} etc }, which is what I get when I use the arr1 array. The result when I use arr array is just two points on the graph, { {1/1/1970, 15}, {30/09/2012, 131581440000} } Commented Jun 19, 2014 at 9:28
  • 1
    Take a look at this tutorial. Commented Jun 19, 2014 at 9:37
  • unless you are stuck pre .Net 2.0, don't use ArrayList, if "HighCharts" only accepts a multidimensional ArrayList don't use "HighCharts". Commented Jun 19, 2014 at 9:38

2 Answers 2

1

Your loop creates a different array, because you mixed up the dimensions. Do it this way:

Object[,] arr = new Object[al2.Count, 2];

for (int k = 0; k < al2.Count; k++)
{
    arr[k, 0] = al1[k];
    arr[k, 1] = al2[k];
}
Sign up to request clarification or add additional context in comments.

2 Comments

You could also check for al1.Count == al2.Count, else it could throw an exception.
@fabigler He fills them manually in the code, how could they have different sizes? And even if he didn't, if they do, there is a mistake somewhere else. Fix that then.
0

You could also try a List<KeyValuePair<Date,int>> and fill your 2D array from there, or if the dates are unique, a Dictionary<Date, int>

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.