【问题标题】:Set a c# polar diagram to a specific amout of rings and segments将 c# 极坐标图设置为特定数量的环和段
【发布时间】:2016-08-14 09:12:28
【问题描述】:

我想在 C#(无库)中创建一个具有固定数量的环和段的极坐标图。是否也可以更改侧面的 digrees 使 0 在右侧?如果在 C# 中不可能,是否有它的库?

【问题讨论】:

    标签: c# charts diagram


    【解决方案1】:

    使用MSChart 控件一点也不难。

    你可以使用它的Polar ChartType,并设置两个Axes的各种属性来达到你想要的效果:

    这是一个例子;为你添加一个Chart chart1 并设置如下:

    Series s = chart1.Series[0];           // a reference to the default series
    ChartArea ca = chart1.ChartAreas[0];   // a reference to the default chart area..
    Axis ax = ca.AxisX;                    // and the ewo.. 
    Axis ay = ca.AxisY;                    // ..axes  
    
    s.ChartType = SeriesChartType.Polar;   // set the charttype of the series
    s.MarkerStyle = MarkerStyle.Circle;    // display data as..
    s.SetCustomProperty("PolarDrawingStyle", "Marker");  //.. points, not lines
    

    让辐条以 15° 的步长从 0° 转到 360° 旋转 90° 设置这些轴值:

    ax.Minimum = 0;    
    ax.Maximum = 360;
    ax.Interval = 15;
    ax.Crossing = 90;
    

    控制环比较棘手,因为它最终必须考虑您的数据值! 假设 y 值在 0-100 之间,我们可以使用这些设置来获得 10 个环:

    ay.Minimum = 0;    
    ay.Maximum = 100;  
    ay.Interval = (ay.Maximum - ay.Minimum) / 10;
    

    如果您的数据值有不同的范围,您应该调整这些值!

    因此,X-Axis 的辐条数为 (Maximum - Minimum) / Interval。除了Y-Axis,环的数量是相同的。要同时控制两者,最好全部设置,不要依赖默认的自动设置!

    如果你想要一个空的中心,你应该

    • 在 y 最小值或 -1 或 -2 间隔中包含缓冲区
    • 多做 1 或 2 个环
    • 在中心画一个白色圆圈,有点棘手..

    作为替代方案,您可以在中心添加一个虚拟数据点并为其设置样式:

    int cc = s.Points.AddXY(0, ay.Minimum);
    DataPoint dpc = s.Points[cc];
    dpc.MarkerColor = Color.White;
    dpc.MarkerSize = centerwidth;  // tricky!
    

    要为centerwidth 找到合适的尺寸,您必须进行测试,或者,如果您想要缩放工作,请在xxxPaint 事件中进行测量;这超出了这个答案的范围..

    【讨论】:

    • 非常感谢。我如何将参数更改为看起来像我帖子中的第二张图片,所以 10 个环和 24 个段?
    • 这取决于你的价值观。对于辐条来说,这很容易:它们从 x-minimum=0 到 x-maximum = 360,x-interval = 15。环更棘手。有点难以阅读,但似乎它们是由内而外的?而且他们不是都有完全不同的价值观吗?这不是一件简单的事情要实现。是否有一些基于数据的逻辑,或者它是定义数据而不是显示数据的图表? - 用上面的公式得到十个环不是问题,但最后你可能希望所有的标签都出现..?!
    • 是的,对不起,我只需要 10 圈图片中的数字与图表无关。
    • 啊,好吧,那么任何事情都会做,只要 range/10 = interval: y-minimum=0 to y-maximum = 100 with an y-interval = 10. 但也许你宁愿添加一圈弥补拥挤的内圈:最大值= 110。我也添加了一个旋转的示例。
    • 注意简化答案!
    【解决方案2】:

    这在winforms 中使用GDI 很容易实现。创建一个新的 UserControl,覆盖 OnPaint 功能:

    1. 画你的圆 (e.Graphics.DrawArc)
    2. 绘制标签(e.Graphics.DrawString)
    3. 绘制数据点和线 (e.Graphics.DrawLine)

    ---------------- 编辑 ------------------ 创建一个新的UserControl:右键项目->添加->用户控件

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }
    
            private void UserControl1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.DrawEllipse(Pens.Blue, 0, 0, this.Width, this.Height);
                e.Graphics.DrawString("90", this.Font, Brushes.Black, new PointF(0, 0));
                e.Graphics.DrawLine(Pens.Red, 0,0, this.Width, this.Height );
            }
        }
    }
    

    【讨论】:

    • 你能给我一个例子或者一个我不知道UserControl的来源..
    • 好的,我添加了它,但是如何调用 Paint 函数?我将用户控件添加到表单中,但现在我只能(在表单中)调用 Paint 事件?
    • 订阅用户控件绘制事件。任何时候需要重绘控件时都会触发。如果需要显式重绘调用 .Refresh()
    • 在此处查看 [hook up] (stackoverflow.com/questions/33275763/…) 事件(即“订阅”它们 ;-)
    • 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多