Databinding Unique Values on x-axis

Thread is closed for posting
2 posts, 1 answers
  1. Michael
    Michael avatar
    19 posts
    Member since:
    Oct 2008

    Posted 09 Jan 2009 Link to this post

    Hi,

    I am databinding multiple series to my chart.  Each series is money spent per month per person.

    So, say I have three people a, b, and c.  I am trying to plot their spending over aperiod of 6 months (all the same months Jan 2008 through June 2008).

    My data is coming back via an ObjectDataSource with these fields:

    Person | Amount | Month

    The key portion of my code looks like this:
           
            //add a series for each person 
            for (int i = 0; i <= (selectedPersons.Count - 1); i++) 
            { 
                ChartSeries chartSeries = new ChartSeries(); 
                chartSeries.Name = selectedPersonNames[i].ToString(); 
                chartSeries.Type = ChartSeriesType.Line; 
                chartSeries.DataYColumn = "$"
                usageChart.Series.Add(chartSeries); 
            } 
             
     
            //set the x axis labels, adjust any appearance, and bind the data 
            usageChart.DataSource = personExpenses
            usageChart.PlotArea.XAxis.DataLabelsColumn = "Month"
            usageChart.DataBind(); 


    The problem I am running into is that the x-axis is adding a total of 18 months (6 for each of the three people).  The x-axis looks like this:

    Jan Feb Mar Apr May Jun Jul Jan Feb Mar Apr May Jun Jul Jan Feb Mar Apr May Jun Jul

    I want 6 months and three series -- How can I get the x-axis to display only unique values?

    Thanks,

    Mike
  2. Answer
    Ves
    Admin
    Ves avatar
    2879 posts

    Posted 12 Jan 2009 Link to this post

    Hi Michael,

    You can take advantage of the DataGroupColumn property of RadChart. You can find more details here.

    Here is a simple example of databinding RadChart in this manner:

    protected void Page_Load(object sender, EventArgs e) 
        { 
     
            List<MyObject> myList = new List<MyObject>(); 
     
            myList.Add(new MyObject { Amount = 3, Person = "Person1", Month = "Jan" }); 
            myList.Add(new MyObject { Amount = 6, Person = "Person2", Month = "Jan" }); 
            myList.Add(new MyObject { Amount = 7, Person = "Person3", Month = "Jan" }); 
            myList.Add(new MyObject { Amount = 4, Person = "Person1", Month = "Feb" }); 
            myList.Add(new MyObject { Amount = 5, Person = "Person2", Month = "Feb" }); 
            myList.Add(new MyObject { Amount = 9, Person = "Person3", Month = "Feb" }); 
     
            RadChart1.DataSource = myList; 
            RadChart1.DataGroupColumn = "Person"
            RadChart1.PlotArea.XAxis.DataLabelsColumn = "Month"
            RadChart1.DataBind(); 
        } 
     
        public class MyObject  
        { 
            public double Amount { getset; } 
            public string Person { getset; } 
            public string Month { getset; } 
        } 

    The chart configuration would remain the same when used with ObjectDatasource. Hope this helps.

    All the best,
    Ves
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top