Page 1 of 1

Adding multiple series using an array

Posted: Tue Oct 19, 2010 1:28 pm
by 15357428
Hi All.

I have spent hours looking through the forum for a simple way to add mutiple series using arrays with no luck :(

So i have come for some help.... let me explain.
I have a application where the user can select a variable number of series to chart. Its working with a fixed number of variables... but not a dynamic set.
I get the data from the database and it comes back in a result set.
I calculate how many rows and columns are in my result set and put the data into a two dimensional array of the same size. (The method that works loads into 3 arraylists, which i later on convert into an array of doubles.)

Code: Select all

int m = 0;

                while(rs.next()) {
                    lsx.add((double)rs.getLong(1));
                    lsy.add(rs.getDouble(2));
                    lsy2.add(rs.getDouble(3));
                    a[0][m] = (double)rs.getLong(1);
                    for (l=1; l < selections.length; l++) {
                        a[l][m] = rs.getDouble(l+1);
                    }
                    m++;
                }
I then declare my array of FastLine objects

Code: Select all

FastLine Line1[] = new FastLine[selections.length];
Now i want to go through each series that is selected and add it to the chart.

Code: Select all

xarray = new double[countRows];
                yarray = new double[countRows];
                for (int k=0; k<selections.length; k++)    {
                    Line1[k] = new FastLine(tChart1.getChart());       //instantiate the object

                    for (l=0; l < countRows; l++) {  //load data into one dimension array
                        xarray[l] = a[0][l];
                        yarray[l] = a[k+1][l];
                    }

                    Line1[k].add(xarray,yarray); //add to chart
                    numberOfPoints += countRows;
                }
So since my data is in a two dimenstional array... im trying to put it into one dimensional array. so i can use the add method.
Unfortunately it doesnt seem to be working and im not sure why. There is no error thrown but the graph just displays as straight lines.
When i look at the data that is in the graph it is all 0. but when i debug the array is populated correctly when the add method is called.

Any help greatly appreciated.

Asim

Re: Adding multiple series using an array

Posted: Tue Oct 19, 2010 3:03 pm
by yeray
Hi Asim,

If I understand well your code, in your bidimensional array "a" you store the X values of all the series in a[0] (a[0][0] for the first X value, a[0][1] for the second,...). And the Y values of the lines are in a[1] for the first series (a[1][0] for the first Y value of the fist series, a[1][1] for the second Y value of the fist series), a[2] for the second series (a[2][0] for the first Y value of the second series, a[2][1] for the second Y value of the second series),...
I've made a simple example with a few modifications from your code and it seems to work as expected. Note that copying the arrays into unidimensional array is optional:

Code: Select all

        tChart1.getAspect().setView3D(false);

        java.util.Random r = new java.util.Random();

        int nSeries = 3;
        int nValues = 20;
        double a[][] = new double[nSeries+1][nValues];

        for(int m=0;m<nValues;m++)
        {
            a[0][m] = m;
            for(int l=1;l<=nSeries;l++)
            {
                a[l][m] = r.nextDouble()*1000;
            }
        }

        com.steema.teechart.styles.FastLine Line1[] = new com.steema.teechart.styles.FastLine[nSeries];

        for (int k=0; k<nSeries; k++)
        {
            Line1[k] = new com.steema.teechart.styles.FastLine(tChart1.getChart());       //instantiate the object

            //option 1
//            Line1[k].add(a[0], a[k+1]);

            //option 2. Copying the arrays into unidimentional arrays
            double xarray[] = new double[nValues];
            double yarray[] = new double[nValues];
            for (int l=0; l < nValues; l++)
            {
                xarray[l] = a[0][l];
                yarray[l] = a[k+1][l];
            }


            Line1[k].add(xarray, yarray); //add to chart
        }
The only changes things I've observed in your code are:
- When you populate the a array, you loop from l=1 to selections.length-1. Note that this is selections.length-1 series, not selections.length series. That's why in my code I have (l<=nSeries) as end of for condition.
- When you copy the values to unidimensional arrays, you declare and create them only once. I declare and create them once for each series. Ok, you don't need to redeclarate them each time, but to recreate them. Otherwise, you'll be assigning the same arrays to all the series.

Re: Adding multiple series using an array

Posted: Tue Oct 19, 2010 4:31 pm
by 15357428
Now Working. Thanks!