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

Chart databinding to hashtable

2 Answers 136 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Philip
Top achievements
Rank 1
Philip asked on 09 Feb 2010, 03:29 PM
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 Answers, 1 is accepted

Sort by
0
Philip
Top achievements
Rank 1
answered on 09 Feb 2010, 03:38 PM
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?
0
Velin
Telerik team
answered on 11 Feb 2010, 09:47 AM
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.
Tags
Chart (Obsolete)
Asked by
Philip
Top achievements
Rank 1
Answers by
Philip
Top achievements
Rank 1
Velin
Telerik team
Share this question
or