|
New series when ID changes |
Posted by Kacie on Oct-20-2012 01:49 |
|
Hello, Im reading data in the following format. I can use a datareader, datatable or dbtable. This will go on a stacked bar chart, starting a new bar each time the ID changes. What is the best approach to accomplish this? Do I need to split these up manually into separate arrays? Is there a function in chart director that will do this for me?
Thanks!
ID Period Qty
4 1 1650000
4 2 1500000
4 3 1350000
4 4 1450000
4 5 1425000
4 6 1000000
12 1 1150000
12 2 1050000
12 3 1100000
12 4 1100000
12 5 1000000
12 6 1000000
432 1 13080000
432 2 7440000
432 3 7920000
432 4 8400000
432 5 8664000
432 6 8184000 |
Re: New series when ID changes |
Posted by Peter Kwan on Oct-23-2012 01:42 |
|
Hi Kacie,
If your database supports cross tabulation or pivot query, you can use this feature to create the data series you need. Otherwise, you would need to write your own code to separate the data. It takes around 5 additional lines of code.
An example is as follows:
//Please make sure you sort the data by Period, and then by ID (use the ORDER BY clause in your SQL statement)
//Assume your data are from dbTable, like:
double[] myId = table.getCol(0);
double[] myPeriod = table.getCol(1);
double[] myQty = table.getCol(2);
//The stacked bar layer
BarLayer bLayer = c.addBarLayer2(Chart.Stack);
//Separate the data and add them as datasets
ArrayList series = new ArrayList();
for (int i = 0; i < myPeriod.Length; ++i) {
series.Add(myQty[i]);
if ((i == myPeriod.Length - 1) || (myPeriod[i] != myPeriod[i + 1])) {
bLayer.addDataSet((double[])series.ToArray(typeof(double)), -1, "Period = " + myPeriod[i]);
series.Clear();
}
}
Hope this can help.
Regards
Peter Kwan |
Re: New series when ID changes |
Posted by Kacie on Oct-23-2012 01:48 |
|
Thank you for your help! |
|