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

How to draw a line on bar chart

1 Answer 283 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 2
Dan asked on 18 Feb 2011, 05:45 PM
I have a vertical bar chart where I am filling the chart series from the code-behind, and the x-axis values are dates.
I need to draw a line across the chart that represents a "target" value.
I have read the documentation for using the ChartMarkedZone for this purpose, so I tried this code but it does not work.
Telerik.Reporting.Processing.Chart chart = (Telerik.Reporting.Processing.Chart)sender;
Telerik.Reporting.Chart chartDef = (Telerik.Reporting.Chart)chart.ItemDefinition;
Telerik.Reporting.Charting.ChartSeries series = new Telerik.Reporting.Charting.ChartSeries();
 while (dtThisDate <= dtEndDate)
  {
                seriesItem = new Telerik.Reporting.Charting.ChartSeriesItem();
                seriesItem.YValue = (long)barvalue;
                seriesItem.Label.TextBlock.Appearance.TextProperties.Color = Color.Black;
                series.AddItem(seriesItem);
                chartDef.PlotArea.XAxis.Items.Add(new ChartAxisItem(dtThisDate.Month.ToString() + "/" + dtThisDate.Day.ToString() + "/" + dtThisDate.Year.ToString()));
}
if (targetParm != "" && targetParm != "0")
                {
                    Telerik.Reporting.Charting.ChartMarkedZone mzone = new ChartMarkedZone();
                    mzone.ValueStartY = Convert.ToInt32(targetParm);
                    mzone.ValueEndY = Convert.ToInt32(targetParm);
                    mzone.ValueStartX = chartDef.PlotArea.XAxis.MinValue;
                    mzone.ValueEndX = chartDef.PlotArea.XAxis.MaxValue;
                    mzone.Appearance.Border.Color = Color.Red;
                    mzone.Appearance.Border.Width = 3;
                    chartDef.PlotArea.Add(mzone);
                }

The problem is that setting mzone.ValueStartX = chartDef.PlotArea.XAxis.MinValue
does not work.  But what do I set the ValueStartX and ValueEndX values to when my x-axis points are dates?

Thanks for any help.  Dan

1 Answer, 1 is accepted

Sort by
0
Accepted
Giuseppe
Telerik team
answered on 23 Feb 2011, 02:42 PM
Hi Dan,

Generally if you want to draw a marked zone across the whole chart, you do not need to set ValueStartX / ValueEndX property values at all; alternatively if you want to extract the desired value from DateTime object, you need to use the DateTime.ToOADate() method as RadChart works only with numeric data.

Also note that you need to add the marked zone to the PlotArea.MarkedZones collection (and not directly to PlotArea):
private void chart1_NeedDataSource(object sender, EventArgs e)
{
    ChartSeries series = new ChartSeries();
    series.DataXColumn = "OADate";
    series.DataYColumn = "YValue";
    chart1.Series.Add(series);
 
    chart1.PlotArea.XAxis.Appearance.ValueFormat = ChartValueFormat.ShortDate;
    chart1.PlotArea.XAxis.Appearance.LabelAppearance.RotationAngle = 320;
 
    chart1.PlotArea.XAxis.IsZeroBased = false;
    chart1.PlotArea.XAxis.AutoScale = false;
 
    chart1.PlotArea.Appearance.Dimensions.Margins.Bottom = Telerik.Reporting.Charting.Styles.Unit.Percentage(20);
 
    double startTime = DateTime.Today.ToOADate();
    double endTime = DateTime.Today.AddDays(4).ToOADate();
    chart1.PlotArea.XAxis.AddRange(startTime, endTime, 1);
 
    chart1.DataSource = GetData();
 
    ChartMarkedZone mzone = new ChartMarkedZone();
    mzone.ValueStartY = 12;
    mzone.ValueEndY = 12;
    mzone.Appearance.Border.Color = Color.Red;
    mzone.Appearance.Border.Width = 2;
    chart1.PlotArea.MarkedZones.Add(mzone);
}
 
private static List<ChartData> GetData()
{
    List<ChartData> data = new List<ChartData>();
    data.Add(new ChartData() { Date = DateTime.Today, YValue = 21 });
    data.Add(new ChartData() { Date = DateTime.Today.AddDays(1), YValue = 11 });
    data.Add(new ChartData() { Date = DateTime.Today.AddDays(2), YValue = 15 });
    data.Add(new ChartData() { Date = DateTime.Today.AddDays(3), YValue = 17 });
    data.Add(new ChartData() { Date = DateTime.Today.AddDays(4), YValue = 19 });
 
    return data;
}
 
public class ChartData
{
    public double OADate
    {
        get
        {
            return this.Date.ToOADate();
        }
    }
 
    public DateTime Date
    {
        get;
        set;
    }
 
    public double YValue
    {
        get;
        set;
    }
}

Hope this helps.


Kind regards,
Giuseppe
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
General Discussions
Asked by
Dan
Top achievements
Rank 2
Answers by
Giuseppe
Telerik team
Share this question
or