更好AxisArrowStyle箭头为我Chart.Axes [英] Nicer AxisArrowStyle arrows for my Chart.Axes
问题描述
我回答了如何建立一个问题
图看起来像一个普通的数学图形,即以中心轴和漂亮箭头顶部和右侧。
I answered a question on how to set up a Chart
to look like a regular mathematical graph, i.e. with the axes centered and with nice arrows to the top and to the right..
不过,我发现内置的 AxisArrowStyle.Triangle 是相当大的,发现没有办法使之更小。
However I found the built-in AxisArrowStyle.Triangle to be rather big and found no way to make it smaller.
线 - 线形箭头用于相关轴。
Lines - A line-shaped arrow is used for the relevant axis.
无 - 没有箭头用于相关轴。
None - No arrow is used for the relevant axis.
SharpTriangle - 锋利的三角箭头用于相关轴
SharpTriangle - A sharp triangular arrow is used for the relevant axis.
三角 - 使用三角箭头对于相关的轴
Triangle - A triangular arrow is used for the relevant axis.
下面是它原来的样子:
那么,如何才能解决这个问题?
So how can we fix this?
推荐答案
图表的 axis.AxisArrowStyle
枚举没有让我们挑一个小箭头,只有一个苗条的。
The Chart's axis.AxisArrowStyle
enumeration doesn't let us pick a smaller arrow, only a slimmer one.
因此,我们需要绘制自己:
So we need to draw it ourselves:
下面是一个简单而有效的一段代码,实现了这一点:
Here is a simple but effective piece of code that achieves just that:
private void chart1_PrePaint(object sender, ChartPaintEventArgs e)
{
if (e.ChartElement.ToString().StartsWith("ChartArea-") )
{
// get the positions of the axes' ends:
ChartArea CA = chart1.ChartAreas[0];
float xx = (float)CA.AxisX.ValueToPixelPosition(CA.AxisX.Maximum);
float xy = (float)CA.AxisY.ValueToPixelPosition(CA.AxisY.Crossing);
float yx = (float)CA.AxisX.ValueToPixelPosition(CA.AxisX.Crossing);
float yy = (float)CA.AxisY.ValueToPixelPosition(CA.AxisY.Maximum);
// a simple arrowhead geometry:
int arrowSize = 18; // size in pixels
Point[] arrowPoints = new Point[3] { new Point(-arrowSize, -arrowSize / 2),
new Point(-arrowSize, arrowSize / 2), Point.Empty };
// draw the two arrows by moving and/or rotating the graphics object:
e.ChartGraphics.Graphics.TranslateTransform(xx + arrowSize, xy);
e.ChartGraphics.Graphics.FillPolygon(Brushes.Black, arrowPoints);
e.ChartGraphics.Graphics.TranslateTransform(yx -xx -arrowSize, yy -xy -arrowSize);
e.ChartGraphics.Graphics.RotateTransform(-90);
e.ChartGraphics.Graphics.FillPolygon(Brushes.Black, arrowPoints);
e.ChartGraphics.Graphics.ResetTransform();
}
}
这篇关于更好AxisArrowStyle箭头为我Chart.Axes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!