Chart databinding to hashtable

Thread is closed for posting
3 posts, 0 answers
  1. Philip
    Philip avatar
    70 posts
    Member since:
    Apr 2008

    Posted 09 Feb 2010 Link to this post

    I have a bar chart that I have dynamically (programatically) bound to a hash table. The binding works ok, but it is putting the Key on the Y axis and the Item on the X axis. Does anyone know how I can reverse the axis?
  2. Philip
    Philip avatar
    70 posts
    Member since:
    Apr 2008

    Posted 09 Feb 2010 Link to this post

    After looking at it closer, it is actually using both the item and the key as data values. What I need, is for it to show me a bar chart with the Key on the x axis, and the item as the value of the bar. The key should just be the label for the x axis.

    How can I get the chart to remove the key as a series and use it as a label?
  3. Velin
    Admin
    Velin avatar
    391 posts

    Posted 11 Feb 2010 Link to this post

    Hi Philip,

    Here is a sample code that should help you figure out how to bind RadChart in the desired way:
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ChartSeries s = new ChartSeries("s1", ChartSeriesType.Bar);
            s.DataYColumn = "Value";
            RadChart1.Series.Add(s);
     
            RadChart1.DataSource = GetSource();
            RadChart1.DataBind();
            RadChart1.BeforeLayout += new EventHandler<EventArgs>(RadChart1_BeforeLayout);
        }
     
        void RadChart1_BeforeLayout(object sender, EventArgs e)
        {
            Hashtable h = RadChart1.DataSource as Hashtable;
      
            int i = 0;
            foreach (var item in h.Keys)
            {
                RadChart1.PlotArea.XAxis.Items[i].TextBlock.Text = item.ToString();
                i++;
            }
        }
     
        Hashtable GetSource()
        {
            Hashtable h = new Hashtable();
            h.Add("a", 10);
            h.Add("b", 11);
            h.Add("c", 12);
            h.Add("d", 13);
     
            return h;
        }
     
    Hope this will help.

    Sincerely yours,
    Velin
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
Back to Top