RadChart Multiple Lines X-axis

Thread is closed for posting
7 posts, 0 answers
  1. Sam
    Sam avatar
    19 posts
    Member since:
    Nov 2011

    Posted 15 Mar 2012 Link to this post

    Hi,

    I have a radchart and want to show multiple lines using datatable(I know datatable is not supported , but im adding series mannually). User selects thae date from calendar and I query the database for those range. so here is my code how I add the series I get one datatable at time and assign it to chart, i get datatable in this format :

    Tours  agencyname  dtMonthno dtMonthBrand dtYear
    6  agents May 2011
    8  agents June 2011
    agents 7  July 2011
    23  agents 8  August 2011
    126  agents 9  September 2011
    101  agents 10  October 2011
    85  agents  11  November 2011
    92  agents 12  December 2011
    115  agents 1  January 2012
    102  agents 2  February 2012
    48  agents March 2012  


    foreach (string aId in aAgency)
                        {
                            DataTable dtBrands = count.GetBrandsChartData(Convert.ToDateTime(txtStartDate.Text), Convert.ToDateTime(txtEndDate.Text), Convert.ToInt32(aId));
     
                            bool morethenTwelve = false;
     
                            if (dtBrands.Rows.Count > 0)
                            {
                                ChartSeries brandChartSeries = new ChartSeries();
                                brandChartSeries.Name = dtBrands.Rows[0][1].ToString();
                                brandChartSeries.Type = ChartSeriesType.Line;
     
                                foreach (DataRow dr in dtBrands.Rows)
                                {
                                    ChartSeriesItem chartitem = new ChartSeriesItem();
                                    chartitem.YValue = Convert.ToDouble(dr["Tours"]);
     
                                    if (morethenTwelve == false)
                                    {
                                        dtMontno = Convert.ToInt32(dr["dtMonthno"]);
                                    }
     
                                    if (dtMontno == 12)
                                    {
                                        chartitem.XValue = Convert.ToDouble(dr["dtMonthno"]);
                                        morethenTwelve = true;
                                        dtMontno = 13;
                                    }
                                    else if (dtMontno > 12)
                                    {
                                        chartitem.XValue = Convert.ToDouble(dtMontno += 1);
                                        morethenTwelve = true;
                                    }
                                    else
                                    {
                                        chartitem.XValue = Convert.ToDouble(dr["dtMonthno"]);
                                        morethenTwelve = false;
                                    }
     
                                    
                                    brandChartSeries.AddItem(chartitem);
     
                                    radBrands.PlotArea.XAxis[dtMonth - 1].TextBlock.Text = dr["dtMonthBrand"].ToString();
                                }
     
                                radBrands.PlotArea.XAxis.LayoutMode = Telerik.Charting.Styles.ChartAxisLayoutMode.Inside;
     
                                radBrands.ChartTitle.TextBlock.Text = "Brands";
                                radBrands.PlotArea.XAxis.AutoScale = false;
                                 
                                radBrands.AddChartSeries(brandChartSeries);
                                radBrands.Series.Add(brandChartSeries);
                            }
                        }


    Now I am doing the if statement in above code so that the line doesn't come back to 1 axis once the dtmonthno is more than 12.

    My question is how do I show these month names in x-axis i have tried setting the autoscale to false and trying to set the x-axis but simply doesnt work here is my code for it that i add in the above foreach loop:

    radBrands.PlotArea.XAxis.AddRange(1, 16, 1);
    int dtMonth = Convert.ToInt32(dr["dtMonthno"]);
    radBrands.PlotArea.XAxis[dtMonth - 1].TextBlock.Text = dr["dtMonthBrand"].ToString();

    Your help would be greatly appreciated.

    Thanks
  2. Sam
    Sam avatar
    19 posts
    Member since:
    Nov 2011

    Posted 16 Mar 2012 Link to this post

    Hi,

    right now with above code and addition of following for loop i get the image attached :

                                if (dtBrands.Rows.Count > countBrand)
                                {
                                    countBrand = dtBrands.Rows.Count;
                                    radBrands.PlotArea.XAxis.AddRange(1, countBrand, 1);
                                    for (int i = 0; i < dtBrands.Rows.Count; i++)
                                    {
                                        radBrands.PlotArea.XAxis[i].TextBlock.Text = dtBrands.Rows[i][3].ToString();
                                    }
                                }

    Anyone can give me some tips?

    thanks
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 20 Mar 2012 Link to this post

    Hello Sam,

    Your original question was how to add the month name in the axis label. I see that you have achieved this. I can also see that the scale is not in the correct position - for instance the September axis label is where the data for May is. 

    I guess that you are also not seeing all of the data. This is because you have set XAxis.AutoScale = false but you have not specified a minimum and a maximum. I think that when you specify a correct minimum and maximum - it will be fine.

    But in general I would suggest that instead of this code:
    for (int i = 0; i < dtBrands.Rows.Count; i++)
    {
      radBrands.PlotArea.XAxis[i].TextBlock.Text = dtBrands.Rows[i][3].ToString();
    }

    You do something like this:
    foreach (var axisItem in radBrands.PlotArea.XAxis.Items)
    {
      int monthIndex = Int32.Parse(axisItem.TextBlock.Text);
      axisItem.TextBlock.Text = GetMonthNameFromIndex(monthIndex);
    }

    where you have defined the GetMonthNameFromIndex to return a proper string for the month indexes. One appropriate place for this foreach can be the BeforeLayout event of the chart.

    Hope this helps.

    All the best,
    Petar Marchev
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  5. Joseph
    Joseph avatar
    11 posts
    Member since:
    Aug 2010

    Posted 03 Dec 2012 Link to this post

    I am doing this exact same thing in vb and cannot get it to work either.  Does anyone have an example of how I can get this order right.  My code is almost identicle but I am missing the min and max, when it hits the loop in the before chart event I get type mismatches using the toOAdate in my range function.  I need to get this figured out before the 1st of the new year.  I am trending over a twelve month timespan so when jan 2013 hits, the jan 2012 will drop out of the trending chart, but keeps ordering at the beginning.
  6. Joseph
    Joseph avatar
    11 posts
    Member since:
    Aug 2010

    Posted 04 Dec 2012 Link to this post

    Were you able to solve this, I am at the end of my rope, I am doing the same thing you are doing only in vb.net, cannot figure out the range issue.  Any assistance would be greatly appretiated as jan 1 2013 is fast approaching and I need to solve this year rollover issue.

    Thanks,
    Joe
  7. Sam
    Sam avatar
    19 posts
    Member since:
    Nov 2011

    Posted 04 Dec 2012 Link to this post

    Well send you the full code by Monday dude if I find it
  8. Joseph
    Joseph avatar
    11 posts
    Member since:
    Aug 2010

    Posted 04 Dec 2012 Link to this post

    Thanks SOOOOOOO Much!  I would have thought that more people would be trying to trend this way and telerik would have made it a little easier or had some better examples.

    Joe
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017