添加的图表系列点与 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;
<小时>
要创建一个像Week 1"这样的字符串,你会
To create a string like "Week 1" you would
- 将
XValueType
设置为int16
- 添加 x 值作为周数
- 将其格式化为
..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
很简单:
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!
这篇关于添加的图表系列点与 X 轴不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!