Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I try to draw Chart via C# with table as picture. However, as you can see A4 data in date: 7 and 8/6 should stay with same 7 and 8/6 X-Axis, abnormal here all of them left to 5 & 6/6 X-Axis. Could you help me to fix it.

enter image description here

for (int i = 0; i < 14; i++)
        {
            string productname = dataGridView1.Rows[i].Cells[0].Value.ToString();
            string datetime = dataGridView1.Rows[i].Cells[2].Value.ToString();
            int para = Convert.ToInt16(dataGridView1.Rows[i].Cells[1].Value);
            if (chart_dashboard.Series.IndexOf(productname) != -1)
            {
                chart_dashboard.Series[productname].Points.AddXY(datetime, para);
                chart_dashboard.ChartAreas[0].AxisX.Interval = 1;
            }
            else
            {
                chart_dashboard.Series.Add(productname);
                chart_dashboard.Series[productname].Points.AddXY(datetime, para);
                chart_dashboard.ChartAreas[0].AxisX.Interval = 1;
            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
557 views
Welcome To Ask or Share your Answers For Others

1 Answer

A common mistake.

string datetime = dataGridView1.Rows[i].Cells[2].Value.ToString();

This is wrong! If you add the x-values as strings they all are added as 0 and the Series can't align them to the proper slots on the X-Axis. So they get added from left to right sequentially..

Instead simply add the x-values as the DateTimes they are supposed to be!

So if the Cells contain DateTime values use:

DateTime datetime = (DateTime) dataGridView1.Rows[i].Cells[2].Value;

If they don't, convert them to DateTime

DateTime datetime = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value);

To control the x-values type set the XValueType for each series:

chart_dashboard.Series[yourSeries].XValueType = ChartValueType.DateTime;

To control the way the axis labels are displayed set their formatting:

chart_dashboard[ChartAreas[0].AxisX.LabelStyle.Format = someDateTimeFormatString;

To create a string like "Week 1" you would

  • set the XValueType to int16
  • add the x-value as the week numbers
  • format it like ..axis.LabelStyle.Format = "Week #0";

To extract the number from your data split by space and Convert.ToInt16 !


If one really needs to insert sparse x-values as strings one would have to insert a dummy DataPoint at each gap in a series.

Creating a dummy DataPoint is simple:

 DataPoint dp = new DataPoint() { IsEmpty = true};

But knowing where the gaps are in advance is the challenge! The 'best' way is to go over the data and filling in the before adding the points. Or go over it later and instead of adding, inserting the dummys at the gaps. Both is a lot more trouble than getting the data right in the first place!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...