Adding series at runtime (Series type unknown)

Apr 9, 2010 at 6:31 AM
Edited Apr 9, 2010 at 6:32 AM

Hi - I want to let my users choose what type of series they want to add at runtime. Currently I have hardcoded my chart like this...

 

ColumnSeries cs = new ColumnSeries();
cs.Title = dR["attribute"].ToString();
cs.IndependentValueBinding = new Binding("Key");
cs.DependentValueBinding = new Binding("Value");
cs.ItemsSource = aSeries.ToArray(typeof(KeyValuePair<string, decimal>));
chtAFR.Series.Add(cs);

 

 ...where dR is a DataRow and aSeries is an ArrayList. How can I dynamically set the series type. If I use an If condition to first add a series and later try to set the attributes... something like this... 

if 

(sType == "Bar")
{
    BarSeries s = new BarSeries();
    chtAFR.Series.Add(s);
}
else
{
   
ColumnSeries s = new ColumnSeries();
    chtAFR.Series.Add(s);
}

 Chart1.Series[Chart1.Series.Count - 1].Title = "My Title";

...I cannot set the Title, ItemsSource, etc. attributes, unless I use Casting. Since I don't know the variable until runtime, what can I do?

 

 

Apr 23, 2010 at 12:34 PM

Well you could use the advantages of inheritance... well that is very basic object oriented programming...

 

DataPointSeries s;
if (sType == "Bar")
{
    s = new BarSeries();    
}
else
{
    s = new ColumnSeries();
}
s.Title = "My Title";
chtAFR.Series.Add(s);