Create RadCartesianChart by c# code

1 Answer 128 Views
Chart
Alessandro
Top achievements
Rank 1
Alessandro asked on 18 May 2023, 03:08 PM

I need to translate the following xaml code into c# to build the real-time graph, but I always get reported that there are no series defined. If instead I directly use the xaml code in binding with my object it works regularly. Where am I doing wrong?

 

                <TK:RadCartesianChart x:Name="RadCartesianChart"
                                      Margin="0,18,0,0"
                                      Palette="Windows8">
                    <TK:RadCartesianChart.HorizontalAxis>
                        <TK:CategoricalAxis LabelInterval="15"
                                            ShowLabels="True" />
                    </TK:RadCartesianChart.HorizontalAxis>

                    <TK:RadCartesianChart.VerticalAxis>
                        <TK:LinearAxis LabelInterval="15"
                                       ShowLabels="True" />
                    </TK:RadCartesianChart.VerticalAxis>

                    <TK:RadCartesianChart.SeriesProvider>
                        <TK:ChartSeriesProvider Source="{Binding Path=grafico[0].dataSERIE, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
                            <TK:ChartSeriesProvider.SeriesDescriptors>
                                <TK:CategoricalSeriesDescriptor CategoryPath="etichetta"
                                                                ItemsSourcePath="dataITEM"
                                                                ValuePath="valore">
                                    <TK:CategoricalSeriesDescriptor.Style>
                                        <Style TargetType="TK:BarSeries">
                                            <Setter Property="LegendSettings">
                                                <Setter.Value>
                                                    <TK:SeriesLegendSettings Title="{Binding Path=serie, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </TK:CategoricalSeriesDescriptor.Style>
                                </TK:CategoricalSeriesDescriptor>
                            </TK:ChartSeriesProvider.SeriesDescriptors>
                        </TK:ChartSeriesProvider>
                    </TK:RadCartesianChart.SeriesProvider>
                </TK:RadCartesianChart>

 

C# code

 

            RadCartesianChart ctrl = new RadCartesianChart() { Name = "RadCartesianChart" };
            ctrl.Palette = ChartPalettes.Windows8;
            ctrl.VerticalAxis = new LinearAxis() { ShowLabels = true, LabelInterval = 15 };
            ctrl.HorizontalAxis = new CategoricalAxis() { ShowLabels = true, LabelInterval = 15 };

            ChartSeriesProvider series = new ChartSeriesProvider()
            {
                Source = new Binding("grafico[0].dataSERIE") { Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged },
            };

            CategoricalSeriesDescriptor seriesDescriptor = new CategoricalSeriesDescriptor
            {
                ItemsSourcePath   = "dataITEM",
                CategoryPath      = "etichetta",
                ValuePath         = "valore",
                Style = new Style(typeof(BarSeries))
            };
            seriesDescriptor.Style.Setters.Add(new Setter(BarSeries.LegendSettingsProperty,
                                                          new Binding("serie") { Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }));
            series.SeriesDescriptors.Add(seriesDescriptor);
            ctrl.SeriesProvider = series;

Object data

        public ObservableCollection<OGRecGrafico> grafico
        {
            get { return _grafico; }
            set { _grafico = value; RaisePropertyChanged(() => grafico); }
        }
        private ObservableCollection<OGRecGrafico> _grafico = new ObservableCollection<OGRecGrafico>();

publicclassOGRecGrafico : NotificationObject { publicstring titolo { get; set; } public ObservableCollection<OGRecGraficoSerie> dataSERIE { get; set; } = new ObservableCollection<OGRecGraficoSerie>(); } publicclassOGRecGraficoSerie : NotificationObject { publicobject serie { get; set; } public ObservableCollection<OGRecGraficoItem> dataITEM { get; set; } = new ObservableCollection<OGRecGraficoItem>(); } publicclassOGRecGraficoItem : NotificationObject { publicobject serie { get { return _serie; } set { _serie = value; RaisePropertyChanged(() => serie); } } privateobject _serie = null; publicobject etichetta { get { return _etichetta; } set { _etichetta = value; RaisePropertyChanged(() => etichetta); } } privateobject _etichetta = null; publicobject valore { get { return _valore; } set { _valore = value; RaisePropertyChanged(() => valore); } } privateobject _valore = null; publicstring scriptSQL { get { return _scriptSQL; } set { _scriptSQL = value; RaisePropertyChanged(() => scriptSQL); } } privatestring _scriptSQL = null; }


 

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 19 May 2023, 11:00 AM

Hello Alessandro,

The issue occurs because of the following code:

ChartSeriesProvider seriesProvider = new ChartSeriesProvider()
{
	Source = new Binding("grafico[0].dataSERIE") { Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged },
};

The Source of the series provider expects an object of type collection. In order to attach a binding, you should use the SetBinding method, instead of assigning it directly to a property.

var sourceBinding = new Binding("grafico[0].dataSERIE") { Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
ChartSeriesProvider seriesProvider = new ChartSeriesProvider();
BindingOperations.SetBinding(seriesProvider, ChartSeriesProvider.SourceProperty, sourceBinding);

I also attached a project based on your code, which shows this suggestion.

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Chart
Asked by
Alessandro
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or