Hi,
Im probably being silly, but i cant seem to bind my data to the chart. Im using a helper class to set my data and there is no examples or any documentation to point in a direction other than doing it with a ViewModel class which i do not have.
Here is the code in my helper class:
public static List<LoadCapacity> GetLoadVCapacityData()
{
List<LoadCapacity> result = new List<LoadCapacity>();
var ItemCollection = new ObservableCollection<LoadCapacity>();
CultureInfo cultureInfo = new CultureInfo("en-GB");
Calendar calendar = cultureInfo.Calendar;
CalendarWeekRule calendarWeekRule = cultureInfo.DateTimeFormat.CalendarWeekRule;
DayOfWeek dayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek;
var CurrentYear = DateTime.Now.Year;
var CurrentWeekNo = calendar.GetWeekOfYear(DateTime.Now, calendarWeekRule, dayOfWeek);
DateTime StartWeek = ISOWeek.ToDateTime(CurrentYear, CurrentWeekNo, DayOfWeek.Monday);
DateTime EndWeek = ISOWeek.ToDateTime(CurrentYear, CurrentWeekNo, DayOfWeek.Friday);
for ((int i, DateTime date) = (0, StartWeek); i <= 12; CurrentWeekNo++, i++, date = date.AddDays(7))
{
foreach (var WC in Memory.LoadCapacity)
{
LoadCapacity loadcapacity = new LoadCapacity();
loadcapacity.WeekNo = CurrentWeekNo;
loadcapacity.Load = WC.Load;
loadcapacity.Capacity = WC.Capacity;
loadcapacity.DateFrom = date;
loadcapacity.DateUntil = date.AddDays(6);
loadcapacity.WorkCentre = WC.WorkCentre;
loadcapacity = CalculateCapacity(loadcapacity);
loadcapacity = CalculateLoad(loadcapacity);
ItemCollection.Add(loadcapacity);
}
}
return result;
}
I will provide my XAML for the chart user control also :
<Grid Background="#FF262626">
<Grid.Resources>
<localHelpers:ChartBuilder x:Key="chartBuilder"/>
</Grid.Resources>
<Grid.DataContext>
<localHelpers:ChartBuilder/>
</Grid.DataContext>
<syncfusion:SfChart x:Name="loadCapacityChart" Margin="10" Palette="Custom" FontSize="14">
<chart:SfChart.Legend>
<chart:ChartLegend
DockPosition="Bottom"/>
</chart:SfChart.Legend>
<syncfusion:SfChart.PrimaryAxis>
<syncfusion:CategoryAxis Header="Work Centers" FontSize="14" FontWeight="Bold"/>
</syncfusion:SfChart.PrimaryAxis>
<syncfusion:SfChart.SecondaryAxis>
<syncfusion:NumericalAxis Header="Load V Capacity" FontSize="14" FontWeight="Bold" Width="38"/>
</syncfusion:SfChart.SecondaryAxis>
<chart:StackingColumnSeries
Label="Load"
Interior="#FFF35B02"
ItemsSource="{Binding Source=load}"
XBindingPath="WorkCentre"
YBindingPath="Load">
<syncfusion:StackingColumnSeries.AdornmentsInfo>
<syncfusion:ChartAdornmentInfo
ShowLabel="True"
SegmentLabelContent="Percentage"
LabelPosition="Inner"
Background="#FF262626" />
</syncfusion:StackingColumnSeries.AdornmentsInfo>
</chart:StackingColumnSeries>
<chart:StackingColumnSeries
x:Name="Capacity"
Label="Capacity"
Interior="#FF464646"
ItemsSource="{Binding result, Source={StaticResource chartBuilder}}"
XBindingPath="WorkCentre"
YBindingPath="Capacity">
<syncfusion:StackingColumnSeries.AdornmentsInfo>
<syncfusion:ChartAdornmentInfo
ShowLabel="True"
SegmentLabelContent="Percentage"
LabelPosition="Inner"
Background="#FF262626" />
</syncfusion:StackingColumnSeries.AdornmentsInfo>
</chart:StackingColumnSeries>
</syncfusion:SfChart>
</Grid>
</UserControl>
Any Help Would be Appreciated
Hi Alessandro,
Greetings from Syncfusion support.
With the provided static class, we have noted that the static member returns the empty collection. So, we have stored the collection on public property and bind it to the chart itemssource.
Please check the code changes below. Please let us know if we have misunderstood your request.
Helper class:
public class ChartBuilder { public ObservableCollection<LoadCapacity> Result { get; set; }
public static ObservableCollection<LoadCapacity> GetLoadVCapacityData() { var ItemCollection = new ObservableCollection<LoadCapacity>(); CultureInfo cultureInfo = new CultureInfo("en-GB"); . . . . . . for (int i = 0; i < 10; i++) { . . .
ItemCollection.Add(loadcapacity); }
return ItemCollection;
}
public ChartBuilder() { Result = GetLoadVCapacityData(); } } |
Xaml:
<Grid> <Grid.Resources> <local:ChartBuilder x:Key="chartBuilder"/> </Grid.Resources>
<chart:SfChart >
<chart:StackingColumnSeries Label="Load" Interior="#FFF35B02" ItemsSource="{Binding Result, Source={StaticResource chartBuilder}}" XBindingPath="WorkCentre" YBindingPath="Load"> <chart:StackingColumnSeries.AdornmentsInfo> . . . </chart:StackingColumnSeries.AdornmentsInfo> </chart:StackingColumnSeries>
<chart:StackingColumnSeries x:Name="Capacity" Label="Capacity" Interior="#FF464646" ItemsSource="{Binding Result, Source={StaticResource chartBuilder}}" XBindingPath="WorkCentre" YBindingPath="Capacity"> <chart:StackingColumnSeries.AdornmentsInfo> . . . </chart:StackingColumnSeries.AdornmentsInfo> </chart:StackingColumnSeries> </chart:SfChart>
</Grid> |
Regards,
Saravanan
Hi,
The changes above has made some progress, but it has created another issue.
When trying to run the application i am getting the following error message:
'System.Windows.Markup.XamlParseException: "Set property 'Syncfusion.UI.Xaml.Charts.ChartSeriesBase.ItemSource' threw an exception.' Line number 37 and line position 17"
Inner Exception
InvalidCastException: Unable to cast object of type 'Models.WorkCentre' to type 'System.IConvertible'.
If i was to change my code and just pass in a null instance of WorkCentre then data shows however it looks like this
Where as it should be showing all 9 work centers
EDIT: the above picture is showing data, it is showing 117 records as i cannot cast workcentre to the chart to show 9 columns.
Hi Alessandro,
Query: XamlParseException occurred.
Chart series ItemsSource accepts any IEnumerable object as a value. Please check that the binding object was a collection.
Query: The above picture is showing data, it is showing 117 records as i cannot cast workcentre to the chart to show 9 columns.
If you are expecting to show only 9 records from 117, we have suggested chart axis auto scrolling delta support. Please refer to the guidelines below.
https://help.syncfusion.com/wpf/charts/axis#autoscrollingdelta
Even though we still do not understand the issue you encountered when binding the source, can you possible to revert by modifying the attachment. It will help us to assist you earlier.
Regards,
Saravanan
Hi, Thanks for your reply.
Apologies for the poor explanation.
I have a list of work centers(9) and i have data set to them that i wish to display. I have 12 weeks worth of data, what i was trying to achieve was to show all 9 work centers on the x axis with the current weeks capacity and load time. i am able to display all 117 records, where as i wish to have a list of all my work centers showing the current weeks data.
I hope this makes more sense. I have attached the updated sample
Hi Alessandro Cirignaco,
We have validated the provided sample, and we would like to let you know that the exception faced because of the XBindingPath assigned was a class object. which is not applicable.
XBindingPath and YBindingPath represent the property paths which need to be shown on the axis. Please find the modified sample in the attachment.
Query: what i was trying to achieve was to show all 9 work centers on the x axis with the current weeks capacity and load time.
To show a specified range of data, we suggest using auto-scrolling delta support of the axis. Please check the guidelines below.
[Swipe right to see previous weeks]
https://help.syncfusion.com/wpf/charts/axis#autoscrollingdelta
https://help.syncfusion.com/wpf/charts/interactive-features/zoompan
Regards,
Devakumar D
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.