We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Custom values in adorners

I have one more qustion. Seams like the last one for today. :)
Lets say I have some info-class and use a list of instances of this class to draw a bar chart:
public class Candidate
{
public Candidate(int aPlace, string aDisplayLabel, int aVoteCount)
{
Place = aPlace;
DisplayLabel = aDisplayLabel;
VoteCount = aVoteCount;
}

public string DisplayLabel
{ get; set; }

public int VoteCount
{ get; set; }

public int Place
{ get; set; }

public string Percent
{
get { return (VoteCount / AllVotes * 100).ToString("P02"); }
}

private static int AllVotes
{
get { return 100000; }
}
}

I use Place for one axis, VoteCount for another axis and DisplayLabel for Labels on "place" axis. I need to put value of Percent on bars of a bar chart. How can I do that? I try to do it using ChartAdornmentInfo but cannot figure out how to get the data-item used for the bar to use it in a DataTemplate.


1 Reply

AD Administrator Syncfusion Team January 29, 2008 01:22 PM UTC

Hi Mike Eshva,

You can display the percentage of the votes by implementing the Candidates class with INotifyChanged class. The following lines of codes displays the Candidates class.

public class Candidates:INotifyPropertyChanged
{
private double place;
private string displaylabel;
private double votecount;
private string percent;
public event PropertyChangedEventHandler PropertyChanged;
public Candidates()
{}
public Candidates(int aPlace, string aDisplayLabel, int aVoteCount)
{
Place = aPlace;
DisplayLabel = aDisplayLabel;
VoteCount = aVoteCount;
}

public string DisplayLabel
{get{return displaylabel;}
set
{
displaylabel = value;
OnPropertyChanged("Displaylabel");
}
}

public double VoteCount
{
get { return votecount; }
set
{
votecount = value;
OnPropertyChanged("Votecount");
}
}

public double Place
{
get { return place; }
set
{
place = value;
OnPropertyChanged("Place");
}
}

public string Percent
{
get { return (VoteCount / AllVotes * 100).ToString("P02"); }
}

private static int AllVotes
{
get
{ return 10000;}
}
protected void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{handler(this, new PropertyChangedEventArgs(info));}
}
}



The data to the chart can be added by creating a ObservableCollection of type Candidates and bind the collection to the ChartSeries. The following lines of code display how to create an ObservableCollection for Candidates class and add data to the collection.


public class candidate : ObservableCollection
{
public candidate()
: base()
{
Add(new Candidates(10, "USA", 9));
Add(new Candidates(11, "India", 40));
Add(new Candidates(12, "UK", 15));
Add(new Candidates(13, "France", 7));
Add(new Candidates(14, "Italy", 3));
Add(new Candidates(15, "Canada", 10));
Add(new Candidates(16, "Germany", 6));
Add(new Candidates(17, "China", 10));
}

}


The following lines of code is used to bind the ObservableCollection with the ChartSeries, ChartAxis and ChartSeries’s AdornmentsInfo.

public void adddatafromcollection()
{

ChartBindingData bindingdata = new ChartBindingData();
bindingdata.BeginInit();
bindingdata.XPath = "Place";
bindingdata.YPaths = new string[] { "VoteCount" };
bindingdata.Source = Candidate;
bindingdata.EndInit();

Binding binding = new Binding();
binding.Source = bindingdata;
BindingOperations.SetBinding(chartseries, ChartSeries.DataProperty, binding);

chartseries.Type = ChartTypes.Column;
chartseries.Interior = this.Resources["SeriesInt"] as Brush;
chart1.Areas[0].Series.Add(chartseries);

ChartAxis cpr = chart1.Areas[0].PrimaryAxis;
cpr.ContentPath = "DisplayLabel";
cpr.PositionPath = "Place";
cpr.LabelsSource = Candidate;

chartseries.AdornmentsInfo.LabelContentPath = "DataPoint.Item.Percent";
chartseries.AdornmentsInfo.Visible = true;
}



Let me know if you have any queries.

Thanks for your interest in Syncfusion products.

Regards,
Asem.



Loader.
Live Chat Icon For mobile
Up arrow icon