0

I have following data & need to know if this can be displayed with line chart or not.

Data :

VerNo | Start Date  | End Date
1.1   | 01-Jan-2013 | 31-Jan-2013
1.2   | 01-Feb-2013 | 31-Dec-2099
2.1   | 10-Jan-2013 | 25-Jan-2013
2.2   | 26-Jan-2013 | 16-Feb-2013
3.1   | 16-Mar-2013 | 30-Apr-2013

I need a line chart with dates in X-axis & VerNo in Y-axis & horizontal line should display start & end date of each version.

Thanks!!!

niklodeon
  • 1,320
  • 5
  • 20
  • 51

1 Answers1

0

With my little knowledge of the Chart control I tried something. First I bind the data with Id=0 on Y-axis to get the dates (can probably done better) Then I looped over the data and made a serie per row. Every serie I made a random color, but sometimes the color is too white, so it doesn't show.

    public class VersionData
    {
        public int Id  { get; set; }
        public double VersionNo { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string dateFormat =  "yyyy MMM dd";
        List<VersionData> version = new List<VersionData>();
        version.Add(new VersionData() { Id=0, VersionNo = 1.1, StartDate = new DateTime(2013, 1, 1), EndDate = new DateTime(2013, 1, 31) });
        version.Add(new VersionData() { Id=0, VersionNo = 1.2, StartDate = new DateTime(2013, 2, 1), EndDate = new DateTime(2013, 12, 31) });
        version.Add(new VersionData() {Id=0,  VersionNo = 2.1, StartDate = new DateTime(2013, 1, 10), EndDate = new DateTime(2013, 1, 25) });
        version.Add(new VersionData() {Id=0,  VersionNo = 2.2, StartDate = new DateTime(2013, 1, 26), EndDate = new DateTime(2013, 2, 16) });
        version.Add(new VersionData() { Id=0, VersionNo = 3.1, StartDate = new DateTime(2013, 3, 16), EndDate = new DateTime(2013, 4, 30) });

        Chart1.Series[0].YValueMembers = "Id";
        Chart1.DataSource = version;

        Random randomGen = new Random();
        KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));

        for (int i = 0; i < version.Count; i++)
        {
            Series s = new Series("s" + i.ToString());
            s.ChartType = SeriesChartType.Line;
            s.Color = Color.FromKnownColor(names[randomGen.Next(names.Length)]);
            s.BorderWidth = 4;
            Chart1.Series.Add(s);
            DataPoint p = new DataPoint();

            p.SetValueXY(version[i].StartDate, version[i].VersionNo);
            s.Points.Add(p);
            DataPoint p2 = new DataPoint();
            p2.SetValueXY(version[i].EndDate, version[i].VersionNo);
            s.Points.Add(p2);
        }

        Chart1.Series[0].XValueMember = "StartDate";
        Chart1.ChartAreas[0].AxisX.Interval = 1;
        Chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months;
        Chart1.ChartAreas[0].AxisX.LabelStyle.Format = dateFormat;
        Chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
        Chart1.ChartAreas[0].AxisY.Interval = 0.5;
        Chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0.0";
    }
Hans Derks
  • 1,027
  • 7
  • 7