This is a migrated thread and some comments may be shown as answers.

Dynamic RadHtmlChart Type (bar or line)

1 Answer 85 Views
Chart (HTML5)
This is a migrated thread and some comments may be shown as answers.
Joel R
Top achievements
Rank 1
Joel R asked on 08 Mar 2019, 02:22 AM

I want the user to choose the desired chart type (bar or line).  In my code I am generating the chart and the only difference is in either creating the series with a ColumnSeries or a LineSeries. How do I declare the variable cs to use either type.

I tried defining cs as an Object but then to use it with the properties it needs to be casted but the type could be either.

 

if (RadComboBoxChartType.SelectedValue=="Bar")
   cs = new ColumnSeries();
else
   cs = new LineSeries();
 
cs.Name = row["Description"].ToString();
cs.Appearance.FillStyle.BackgroundColor = generator.NextColor();
cs.TooltipsAppearance.ClientTemplate = "#= series.name# : #= dataItem.value#";
cs.LabelsAppearance.Visible = false;
RadHtmlChartDashboard.PlotArea.Series.Add(cs);

 

 

1 Answer, 1 is accepted

Sort by
0
Vessy
Telerik team
answered on 12 Mar 2019, 02:07 PM
Hi Joel,

The base class of all series types inside the chart is an abstract one so you cannot create an instance of the same, that will be cast to the needed series types after that. 
The only way to achieve the desired approach at the moment is to create a new object with the desired type. Such approach is used in the AddChartSeries() method in the following Code library: https://www.telerik.com/support/code-library/group-radhtmlchart-data-source
private static void AddChartSeriesType(RadHtmlChart HtmlChart, string SeriesType, string DataFieldY, string DataFieldX, int Index, string SeriesName, string TooltipsTemplate, string LabelsFormatString)
{
    switch (SeriesType)
    {
        ...
        case "BarSeries":
            BarSeries barSeries1 = new BarSeries();
            barSeries1.Name = SeriesName;
            barSeries1.DataFieldY = DataFieldY + Index;
            barSeries1.TooltipsAppearance.ClientTemplate = TooltipsTemplate;
            barSeries1.LabelsAppearance.DataFormatString = LabelsFormatString;
            HtmlChart.PlotArea.Series.Add(barSeries1);
            break;
 
        case "ColumnSeries":
            ColumnSeries columnSeries1 = new ColumnSeries();
            columnSeries1.Name = SeriesName;
            columnSeries1.DataFieldY = DataFieldY + Index;
            columnSeries1.TooltipsAppearance.ClientTemplate = TooltipsTemplate;
            columnSeries1.LabelsAppearance.DataFormatString = LabelsFormatString;
            HtmlChart.PlotArea.Series.Add(columnSeries1);
            break;
 
        case "LineSeries":
            LineSeries lineSeries1 = new LineSeries();
            lineSeries1.Name = SeriesName;
            lineSeries1.DataFieldY = DataFieldY + Index;
            lineSeries1.TooltipsAppearance.ClientTemplate = TooltipsTemplate;
            lineSeries1.LabelsAppearance.DataFormatString = LabelsFormatString;
            HtmlChart.PlotArea.Series.Add(lineSeries1);
            break;
  ...
        default:
            break;
    }
}



Regards,
Vessy
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Chart (HTML5)
Asked by
Joel R
Top achievements
Rank 1
Answers by
Vessy
Telerik team
Share this question
or