【问题标题】:Nicer AxisArrowStyle arrows for my Chart.Axes我的 Chart.Axes 更好的 AxisArrowStyle 箭头
【发布时间】:2016-03-20 20:55:47
【问题描述】:

我回答了question,关于如何设置Chart 使其看起来像一个常规的数学图,即轴居中,顶部和右侧有漂亮的箭头..

但是我发现内置的AxisArrowStyle.Triangle 相当大,没有办法让它变小。

线条 - 线形箭头用于相关轴。

无 - 相关轴不使用箭头。

SharpTriangle - 一个尖锐的三角形箭头用于相关轴。

三角形 - 三角形箭头用于相关轴。

这是它的原始外观:

那么我们该如何解决这个问题呢?

【问题讨论】:

    标签: c# charts axis


    【解决方案1】:

    图表的axis.AxisArrowStyle 枚举不允许我们选择更小的箭头,只能选择更细的箭头。

    所以我们需要自己画出来:

    这是一个简单但有效的代码,可以实现这一点:

    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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2022-11-17
      • 2018-03-15
      • 2019-08-03
      • 1970-01-01
      • 2014-11-20
      • 2017-08-09
      相关资源
      最近更新 更多