我正在使用.Net Charts。在那里,我显示了一个间隔为28天的折线图。
这是我的代码:
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = 1;
Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min;
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max;
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 28;
但是,我的一个情况就像是,
28天间隔,35天间隔,28天间隔等。这可能有不同的间隔。
答案 0 :(得分:0)
否,Interval
是Axis
属性,只能一个。
您可以通过绘制网格线和标签来解决此限制。
假设您有一个停止点列表,即您希望DataPoints
出现的GridLine
的索引:
List<int> stops = new List<int>();
添加几个测试数字stops.AddRange(new[] { 12, 23, 42, 52, 82 });
后,我们可以对PostPaint
的{{1}}事件进行编码以绘制线条:
Chart
关闭原始private void chart_PostPaint(object sender, ChartPaintEventArgs e)
{
Graphics g = e.ChartGraphics.Graphics;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
ChartArea ca = chart.ChartAreas[0];
Font font = ca.AxisX.LabelStyle.Font;
Color col = ca.AxisX.MajorGrid.LineColor;
int padding = 10; // pad the labels from the axis
double aymin = ca.AxisY.Minimum;
double aymax = ca.AxisY.Maximum;
int y0 = (int)ca.AxisY.ValueToPixelPosition(aymin);
int y1 = (int)ca.AxisY.ValueToPixelPosition(aymax);
foreach (int sx in stops)
{
int x = (int)ca.AxisX.ValueToPixelPosition(chart.Series[0].Points[sx].XValue);
using (Pen pen = new Pen(col))
g.DrawLine(pen, x, y0, x, y1);
string s = chart.Series[0].Points[sx].XValue + "";
if (ca.AxisX.LabelStyle.Format != "") s = string.Format(ax.LabelStyle.Format, s);
SizeF sz = g.MeasureString(s, font, 999);
g.DrawString(s, font, Brushes.Black, (int)(x - sz.Width / 2) , y0 + padding);
}
}
等。
MajorGrid
..这是结果:
注意:
大多数代码只是简单的准备和参考。实际绘图是两种方法,还有三到四行来获取坐标..
我已将ChartArea ca = chart.ChartAreas[0];
ca.AxisX.MajorGrid.Enabled = false;
ca.AxisX.MajorTickMark.Enabled = false;
ca.AxisX.LabelStyle.Enabled = false;
索引存储在我的列表中。如果您希望自定义DataPoint
独立于GridLines
,则可以存储DataPoints
并将列表更改为Values
,并将List<double>
的两个引用更改为chart.Series[0].Points[sx].XValue
直接访问停止值sx
。
更改填充值以适合您..
我们可以自由访问轴的最小值和最大值,即使它们实际设置为Auto
。这是因为我们处于Paint
事件中。否则,我们必须在RecalculateAxesScale()
..
ChartArea
随意使Black
标签画笔动态化..