.Net图表-具有不同间隔的X轴 [英] .Net Charts - X Axis with Different Intervals
问题描述
我正在使用.Net图表.在这种情况下,我显示了一个间隔为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天间隔等.是否可能具有不同的间隔?
否,Interval
是Axis
属性,并且只能一个. /p>
您可以通过自己绘制网格线和标签来解决此限制.
让我们假设您有一个停止点列表,即要在其中显示GridLine
的DataPoints
的索引:
List<int> stops = new List<int>();
添加一些测试编号stops.AddRange(new[] { 12, 23, 42, 52, 82 });
之后,我们可以编写Chart
的PostPaint
事件以绘制线条:
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;
..这是结果:
注意:
-
大多数代码只是简单的准备和参考.实际的绘图是2种方法以及另外3或4条线来获取坐标..
-
我已将
DataPoint
索引存储在我的列表中.如果希望自定义GridLines
独立于DataPoints
,则可以存储Values
并将列表更改为List<double>
,并将两个引用从chart.Series[0].Points[sx].XValue
更改 直接访问停止值sx
. -
更改填充值以适合您..
-
即使实际上将它们设置为
Auto
,我们也可以自由访问轴的最小值和最大值.这是因为我们处于Paint
事件中.否则,我们将不得不在ChartArea
上调用RecalculateAxesScale()
.. -
还可以随意使
Black
标签刷动态化..
I am using .Net Charts. In that, I have displayed a line chart with an Interval of 28 Days.
Here is my code:
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = 1;
Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min;
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max;
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 28;
But, one of my situation comes like,
28 Days Interval, 35 Days Interval, 28 Days Interval etc. Is that possible to have different Intervals.
No, Interval
is an Axis
property and there can only be one.
You can work around this restriction by drawing gridlines and labels on your own.
Let's assume you have a list of stop points, i.e. the indices of the DataPoints
where you want a GridLine
to appear:
List<int> stops = new List<int>();
After adding a few test numbers stops.AddRange(new[] { 12, 23, 42, 52, 82 });
we can code the PostPaint
event of the Chart
to draw lines:
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);
}
}
After turning off the original MajorGrid
etc..
ChartArea ca = chart.ChartAreas[0];
ca.AxisX.MajorGrid.Enabled = false;
ca.AxisX.MajorTickMark.Enabled = false;
ca.AxisX.LabelStyle.Enabled = false;
..this is the result:
Notes:
Most of the code are just simple preparations and references. The actual drawing are 2 methods and three or four more lines to get the coordinates..
I have stored the
DataPoint
indices in my List. If you want the customGridLines
to be independent ofDataPoints
you can instead store theValues
and change the List to aList<double>
andthe two references fromchart.Series[0].Points[sx].XValue
to accessing the stop valuessx
directly.Change the padding value to suit you..
We can access the axes' minima and maxima values freely, even if they are actually set to
Auto
. This is because we are in aPaint
event. Otherwise we would have to callRecalculateAxesScale()
on theChartArea
..Feel free to make the
Black
label brush dynamic as well..
这篇关于.Net图表-具有不同间隔的X轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!