添加的图表系列点与X轴不同步 [英] Chart series point added not sync with X axis
问题描述
我尝试通过C#以表为图片绘制图表. 但是,您可以看到日期中的A4数据:7和8/6应该保持相同的7和8/6 X轴,这是异常情况,所有这些都保留为5& ;; 6/6 X轴.您能帮我修复它吗?
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.
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;
}
}
推荐答案
常见错误.
string datetime = dataGridView1.Rows[i].Cells[2].Value.ToString();
这是错误的!如果将x值添加为字符串,它们都将添加为 0
,并且Series
无法将它们与X轴上的适当插槽对齐.因此,它们会按从左到右的顺序添加..
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..
相反,只需将x值添加为应该是的DateTimes
!
Instead simply add the x-values as the DateTimes
they are supposed to be!
因此,如果Cells
包含DateTime
值,请使用:
So if the Cells
contain DateTime
values use:
DateTime datetime = (DateTime) dataGridView1.Rows[i].Cells[2].Value;
如果没有,请将其转换为DateTime
If they don't, convert them to DateTime
DateTime datetime = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value);
要控制x值类型,请为每个系列设置XValueType
:
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;
要创建类似第1周"的字符串,您将
To create a string like "Week 1" you would
- 将
XValueType
设置为int16
- 将x值添加为星期数
- 将其格式化为
..axis.LabelStyle.Format = "Week #0";
- set the
XValueType
toint16
- add the x-value as the week numbers
- format it like
..axis.LabelStyle.Format = "Week #0";
要从按空格和Convert.ToInt16拆分的数据中提取数字!
To extract the number from your data split by space and Convert.ToInt16 !
如果确实需要将稀疏x值作为字符串插入,则必须在系列的每个间隙处插入 dummy DataPoint
.
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.
创建虚拟DataPoint
很简单:
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!
这篇关于添加的图表系列点与X轴不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!