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

[Solved] DataBinding RadCartesianChart & AreaSeries

13 Answers 719 Views
Chart for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Iris
Top achievements
Rank 1
Iris asked on 17 Jul 2012, 12:05 PM
How do you set the databinding for the RadCartesianChart with AreaSeries?
I've only been able to make it work in codebehind, but I would prefer to use databinding as with the other charts.

/with kind regards Iris

13 Answers, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 17 Jul 2012, 02:42 PM
Hello Iris,

 You can define series value and category binding using the following code:

<telerik:AreaSeries ItemsSource="{Binding Data1}" CombineMode="Stack">
             <telerik:AreaSeries.ValueBinding>
                 <telerik:PropertyNameDataPointBinding PropertyName="Weight"></telerik:PropertyNameDataPointBinding>
             </telerik:AreaSeries.ValueBinding>
             <telerik:AreaSeries.CategoryBinding>
                 <telerik:PropertyNameDataPointBinding PropertyName="EntryDateTime"></telerik:PropertyNameDataPointBinding>
             </telerik:AreaSeries.CategoryBinding>

 I have attached sample project demonstrating this setup.

Also, I see that you are interested in our suite, so I encourage you to download your help (chm format) from account download scetion. Although it still has small number of articles, it covers this and other topics you might find interesting.

Best regards,
Tsvyatko
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Iris
Top achievements
Rank 1
answered on 17 Jul 2012, 04:04 PM
Thank you for the answer, but it doesnt solve my problem.

I must be doing it wrong, I have this:

<telerik:RadCartesianChart x:Name="chart" Grid.RowSpan="3"
           Margin="0,30,0,0" ClipToBounds="False" MaxZoom="3,1"
           Palette="{Binding DefaultPalette}">
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:DateTimeCategoricalAxis DateTimeComponent="Day" LabelFormat="{}{0:d}" LabelStyle="{StaticResource ChartLabel}"/>
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis LabelStyle="{StaticResource ChartLabel}">
        </telerik:LinearAxis>
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:RadCartesianChart.Grid>
        <telerik:CartesianChartGrid MajorLinesVisibility="Y"/>
    </telerik:RadCartesianChart.Grid>
</telerik:RadCartesianChart>

Now where does the databinding go? I'm sorry for if the question(s) are silly, but I just can't make it work. The project.zip is empty , and I have downloaded the techEd example, but there the source is set in code, as I am doing atm. I havent had any problems with the other charts, just this one,- and that is why I am asking for help. I've ofcourse had a bit of a google, and have looked at other controls such as http://www.telerik.com/help/silverlight/radchartview-series-areaseries.html , but still can't get the databinding to work. Do you have a working example with this chart, with databinding, that I can have a look at?

with kind regards Iris
0
Tsvyatko
Telerik team
answered on 18 Jul 2012, 08:13 AM
Hi Iris,

 Please excuse me for the mistake, I am reataching the correct project. I will also try to summarize in short the chart components involved and the databinding implementation. Most of the info is a quote of the chm (we are working to make the online help available in 1-2 days ).

Axis
RadChart plots data points in a coordinate system defined by its two axes. Instead of having one axis type that does hundreds of things, we have a hierarchy of axes where each concrete axis type expose particular functionality.

In this scenario we will look for DateTimeCategorical and linear axes:

<telerik:RadCartesianChart.VerticalAxis>
    <telerik:LinearAxis></telerik:LinearAxis>
</telerik:RadCartesianChart.VerticalAxis>
<telerik:RadCartesianChart.HorizontalAxis>
    <telerik:DateTimeCategoricalAxis LabelFormat="{}{0:dd-MMM}" DateTimeComponent="Day"/>
</telerik:RadCartesianChart.HorizontalAxis>


Series
The actual data visualization in RadChartView is done by a set of series classes. Each series has a collection of data points, that is, a data source which it displays according to the series’ type.

In this particular scenario we we are interested in the following properties:

 - ValueBinding - this property of the series will be used to resolve the property of the data records that will be used for visualization by the data points.

Note that ValueBinding if of type PropertyValueBinding, rather than binding. Thus, when defined in XAML it needs to be setup as follows: 

<telerik:BarSeries.ValueBinding>
    <telerik:PropertyNameDataPointBinding PropertyName="Value"/>
</telerik:BarSeries.ValueBinding>

 - CategoryBinding - determine the category of each data point which will also correspond to a user defined property of the raw data records.

<telerik:BarSeries.CategoryBinding>
    <telerik:PropertyNameDataPointBinding PropertyName="Category"/>
</telerik:BarSeries.CategoryBinding>

That being said chart setup with area series will look like that:

<telerik:RadCartesianChart x:Name="Chart" PaletteName="DefaultDark">
           <telerik:RadCartesianChart.VerticalAxis>
               <telerik:LinearAxis></telerik:LinearAxis>
           </telerik:RadCartesianChart.VerticalAxis>
           <telerik:RadCartesianChart.HorizontalAxis>
               <telerik:DateTimeCategoricalAxis LabelFormat="{}{0:dd-MMM}" DateTimeComponent="Day"/>
           </telerik:RadCartesianChart.HorizontalAxis>
        
           <telerik:AreaSeries ItemsSource="{Binding Data1}" CombineMode="Stack">
               <telerik:AreaSeries.ValueBinding>
                   <telerik:PropertyNameDataPointBinding PropertyName="Weight"></telerik:PropertyNameDataPointBinding>
               </telerik:AreaSeries.ValueBinding>
               <telerik:AreaSeries.CategoryBinding>
                   <telerik:PropertyNameDataPointBinding PropertyName="EntryDateTime"></telerik:PropertyNameDataPointBinding>
               </telerik:AreaSeries.CategoryBinding>
           </telerik:AreaSeries>
           <telerik:AreaSeries ItemsSource="{Binding Data2}"  CombineMode="Stack">
               <telerik:AreaSeries.ValueBinding>
                   <telerik:PropertyNameDataPointBinding PropertyName="Weight"></telerik:PropertyNameDataPointBinding>
               </telerik:AreaSeries.ValueBinding>
               <telerik:AreaSeries.CategoryBinding>
                   <telerik:PropertyNameDataPointBinding PropertyName="EntryDateTime"></telerik:PropertyNameDataPointBinding>
               </telerik:AreaSeries.CategoryBinding>
           </telerik:AreaSeries>
       </telerik:RadCartesianChart>

where Data1 and Data 2 are collections of BodyWeight class objects:

  public class BodyWeight
    {
        public DateTime EntryDateTime { get; set; }
        public double Weight { get; set; }
}

Let me know if this helps. If you have any further questions do not hesitate to conact us.

I am also adding direct link to the xaml chm help file - http://www.telerik.com/account/your-products/trial-product-versions/download-trial-file.aspx?fileid=12897&pid=0&dispkey=True (when downloaded don't forget to unblock contents - properties -> unblock).

Greetings,
Tsvyatko
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Iris
Top achievements
Rank 1
answered on 18 Jul 2012, 11:26 AM
This is the weirdest thing ever. The code you posted is the exact same one I've been trying to use all week, but I keep getting errors (feeling kind of stupid as this does seem like basic stuff).

Even if I copy and paste it in the app, in a new page, same error.

I created a new application instead, and it works there. What the ??  Oh well, now it works. I'll just have to redo parts of the app. Have no idea why AreaSeries gave errors in the other app.

Thank you again for your help :) Thumbs up from Sweden
0
Tsvyatko
Telerik team
answered on 18 Jul 2012, 11:54 AM
Hello Iris,

 We are happy that you got this working from your side. Regarding, the issue you are describing, we have found out that the XAML parcer is very sensitive for space symbols (usually they are added by coping xaml from web or text editor) and breaks if they exist somewhere in the page.

All the best,
Tsvyatko
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Yiqing
Top achievements
Rank 1
answered on 03 Aug 2013, 12:27 AM
Hi Tsvyatko ,

I can understand almost all the stuff you were saying above. My problem is also about data binding to Rad bar chart. The data structure I have right now is:

public class CategoryDetail
    {
        public string CategoryName { get; set; }
        public List<Data> statuslist { get; set; }
 
    }
 
public class Data
    {
        public string Category;
        public int Value;
    }

And in the viewmodel, I set the dummy data as:
_barchart = new ObservableCollection<CategoryDetail>
            {
                new CategoryDetail{   CategoryName = "Primary", statuslist = _data1 },
                new CategoryDetail{   CategoryName = "Secondary", statuslist = _data2},
 
            };
 
            _data1 = new List<Data>
            {
                    new Data{  Category = "D", Value = 1},
                    new Data{ Category = "B", Value = 1},
                    new Data{  Category = "A", Value = 1},
                    new Data{  Category = "C", Value = 2},
            };
 
            _data2 = new List<Data>
            {
                    new Data{  Category = "D", Value = 3},
                    new Data{ Category = "B", Value = 4},
                    new Data{  Category = "A", Value = 5},
                    new Data{  Category = "C", Value = 8},
            };

What I want to do is to bind _barchart to the  barserie of RadCartesianChart control, and get a effect as the attached png. I also hope to display a legend for 'primary' and 'secondary'. Please give some guidance on how to do that. 

Thanks,
Yiqing


0
Yiqing
Top achievements
Rank 1
answered on 03 Aug 2013, 12:29 AM
Hi Tsvyatko ,

I can understand almost all the stuff you were saying above. My problem is also about data binding to Rad bar chart. The data structure I have right now is:

public class CategoryDetail
    {
        public string CategoryName { get; set; }
        public List<Data> statuslist { get; set; }
 
    }
 
public class Data
    {
        public string Category;
        public int Value;
    }

And in the viewmodel, I set the dummy data as:
_barchart = new ObservableCollection<CategoryDetail>
            {
                new CategoryDetail{   CategoryName = "Primary", statuslist = _data1 },
                new CategoryDetail{   CategoryName = "Secondary", statuslist = _data2},
 
            };
 
            _data1 = new List<Data>
            {
                    new Data{  Category = "D", Value = 1},
                    new Data{ Category = "B", Value = 1},
                    new Data{  Category = "A", Value = 1},
                    new Data{  Category = "C", Value = 2},
            };
 
            _data2 = new List<Data>
            {
                    new Data{  Category = "D", Value = 3},
                    new Data{ Category = "B", Value = 4},
                    new Data{  Category = "A", Value = 5},
                    new Data{  Category = "C", Value = 8},
            };

What I want to do is to bind _barchart to the  barserie of RadCartesianChart control, and get a effect as the attached png. I also hope to display a legend for 'primary' and 'secondary'.

The code I write for now is like the following. This has nothing to do with '_barchart', and what I hope is to pass _barchart to the control and let it show the same effect.

<Chart:RadCartesianChart.Series>
                <Chart:BarSeries CombineMode="Cluster" ItemsSource="{Binding data1}">
                    <Chart:BarSeries.ValueBinding>
                        <Chart:PropertyNameDataPointBinding PropertyName="Value"/>
                    </Chart:BarSeries.ValueBinding>
                    <Chart:BarSeries.CategoryBinding>
                        <Chart:PropertyNameDataPointBinding PropertyName="Category"/>
                    </Chart:BarSeries.CategoryBinding>
                </Chart:BarSeries>
 
                <Chart:BarSeries CombineMode="Cluster" ItemsSource="{Binding data2}">
                    <Chart:BarSeries.ValueBinding>
                        <Chart:PropertyNameDataPointBinding PropertyName="Value"/>
                    </Chart:BarSeries.ValueBinding>
                    <Chart:BarSeries.CategoryBinding>
                        <Chart:PropertyNameDataPointBinding PropertyName="Category"/>
                    </Chart:BarSeries.CategoryBinding>
                </Chart:BarSeries>
 
            </Chart:RadCartesianChart.Series>
Please give some guidance on how to do that. 

Thanks,
Yiqing


0
Ivaylo Gergov
Telerik team
answered on 05 Aug 2013, 03:28 PM
Hi Yiqing,

I have attached a project that demonstrates two possible approaches in the current scenario. You can either bind the BarSeries to the properties that hold the two generated lists or bind the chart to the _barchart collection and generate the series dynamically through the RadCartesianChart.SeriesProvider(to see the second scenario - uncomment the code and add RadCartesianChart.DataContext="{Binding BarChart}"). More about the dynamic series can be found here - http://www.telerik.com/help/windows-8-xaml/radchart-cartesianchart-series-dynamicseries-overview.html. I have also added a RadLegendControl that works in both cases.


I hope this helps. Let me know should you have any other questions.

 

Regards,
Ivaylo Gergov
Telerik
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Yiqing
Top achievements
Rank 1
answered on 06 Aug 2013, 03:08 AM
Hi Ivaylo,

That's quite helpful, I did it!

Thanks,
Yiqing 
0
Yiqing
Top achievements
Rank 1
answered on 06 Aug 2013, 11:12 PM
Hi Ivaylo,

Now I've dynamically display the barchart, I want to three more things (in the same senario as indicated before.)

1.show the legend. I tried the following, but it doesn't work.
<Chart:CategoricalSeriesDescriptor
                            LegendTitlePath="CategoryName"
                            ItemsSourcePath="StatusList"
                            CategoryPath="Status"
                            ValuePath="Count" >

then I tried this, which was what I have succeeded on pie chart, however it fails as well.
<Chart:RadCartesianChart x:Name="mybarchart" HorizontalAlignment="Left" >               
    <Chart:RadCartesianChart.SeriesProvider>
               <Chart:ChartSeriesProvider Source="{Binding barchart}">                   
                   <Chart:ChartSeriesProvider.SeriesDescriptors>
                       <Chart:CategoricalSeriesDescriptor
                           LegendTitlePath="CategoryName"
                           ItemsSourcePath="StatusList"
                           CategoryPath="Status"
                           ValuePath="Count" >
                           <Chart:CategoricalSeriesDescriptor.Style>
                               <Style TargetType="Chart:BarSeries">
                                   <Setter Property="CombineMode" Value="Cluster"/>
                                   <Setter Property="ShowLabels" Value="True"/>
                                   <Setter Property="Foreground" Value="Black"/>
                                   <Setter Property="FontSize" Value="30"/>
                               </Style>
                           </Chart:CategoricalSeriesDescriptor.Style>
                       </Chart:CategoricalSeriesDescriptor>
                   </Chart:ChartSeriesProvider.SeriesDescriptors>
               </Chart:ChartSeriesProvider>
                
           </Chart:RadCartesianChart.SeriesProvider>
           <Chart:BarSeries>
               <Chart:BarSeries.LegendTitleBinding>
                   <Chart:PropertyNameDataPointBinding PropertyName="CategoryName"/>
               </Chart:BarSeries.LegendTitleBinding>
               <Chart:BarSeries.ValueBinding>
                   <Chart:PropertyNameDataPointBinding PropertyName="Count"/>
               </Chart:BarSeries.ValueBinding>
           </Chart:BarSeries>
       </Chart:RadCartesianChart>
 
       <Primitives:RadLegendControl LegendProvider="{Binding ElementName=mybarchart}" Foreground="Black" >
           <Primitives:RadLegendControl.ItemsPanel>
               <ItemsPanelTemplate>
                   <StackPanel Orientation="Vertical"/>
               </ItemsPanelTemplate>
           </Primitives:RadLegendControl.ItemsPanel>
       </Primitives:RadLegendControl>

2. Change the color of axis.

Also, I wonder where can I change the color of the vertical and horizontal axis.

3.Change the color of grouped title.

As you can see in the attached picture, there's a very light title under each group ('passed', 'failed', 'pending'). I also want to make them more obvious.

Thanks,
Yiqing
0
Rosy Topchiyska
Telerik team
answered on 07 Aug 2013, 02:50 PM
Hi Yiqing,

I will answer to your questions consequently. For your convenience I have modified the example project from the previous answer to fulfill your requirements in case you need to refer to it.

First of all, in the code snippet you have sent you have to remove the <Chart:BarSeries> element. Here you can find a complete example that should give you a basic idea of how to generate a dynamic series in a BarChart.

About the LegendControl: On the picture you have attached the legend is actually displayed, but its text is not visualized for some reason or its color is the same as the color of the background. I can see that you have set a Foreground property of the LegendControl, but this property has no effect on the appearance of the control - 
here you can see all the properties of the LegendControl if you need to modify it. You can take a look at the example project with correctly visualized LegendControl in case you still have problems with making it work.

Labels and Axis appearance: You have to add RadCartesianChart.HorizontalAxis and RadCartesianChart.VerticalAxis to your chart. Then you have to set a custom Style to the LineStyle property and
 the LabelStyle property of the axis - you can see how in the project. You can read more about the axes properties here.

I hope this was useful and let us know if there is anything else we can assist you with.


Regards,
Rositsa Topchiyska
Telerik
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Yiqing
Top achievements
Rank 1
answered on 07 Aug 2013, 08:37 PM
Hi Rositsa,

Thanks for your very prompt suggestions!! I've figured out most of them. Could you indicate why the text of the legend doesn't show? Where did I make the color as the same as the backgroud??

Thanks,
Yiqing
0
Rosy Topchiyska
Telerik team
answered on 08 Aug 2013, 11:04 AM
Hello Yiqing,

Sorry for misleading you a bit with our previous reply -- indeed setting the RadLegendControl.Foreground property should be enough to control the color of the legend item labels. If your problem with the legend text persists, could you send us a modified version of our sample application that reproduces the issue so we can advise you properly how to proceed?

Hope this helps.


Regards,
Rositsa Topchiyska
Telerik
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
Tags
Chart for XAML
Asked by
Iris
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
Iris
Top achievements
Rank 1
Yiqing
Top achievements
Rank 1
Ivaylo Gergov
Telerik team
Rosy Topchiyska
Telerik team
Share this question
or