Nicer AxisArrowStyle arrows for my Chart.Axes

核能气质少年 提交于 2019-12-02 02:12:59

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();
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!