将 c# 极坐标图设置为特定数量的环和线段
Set a c# polar diagram to a specific amout of rings and segments
我想在 C#(无库)中创建一个具有固定环和线段数量的极坐标图。是否也可以更改侧面的 digrees 以便 0 在右边?如果在 C# 中不可能,是否有相应的库?
这在 winforms 中使用 GDI 很容易实现。创建一个新的 UserControl,覆盖 OnPaint 功能为:
- 画出你的圆(e.Graphics.DrawArc)
- 绘制标签 (e.Graphics.DrawString)
- 绘制数据点和线 (e.Graphics.DrawLine)
----------------编辑----------------
新建一个UserControl:右击project -> add -> User Control
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 );
}
}
}
使用 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
。要同时控制两者,最好 全部设置 而不要依赖默认的 automatic 设置!
如果你想要一个空的中心,你应该
- 在 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
事件中进行测量;这超出了这个答案的范围..
我想在 C#(无库)中创建一个具有固定环和线段数量的极坐标图。是否也可以更改侧面的 digrees 以便 0 在右边?如果在 C# 中不可能,是否有相应的库?
这在 winforms 中使用 GDI 很容易实现。创建一个新的 UserControl,覆盖 OnPaint 功能为:
- 画出你的圆(e.Graphics.DrawArc)
- 绘制标签 (e.Graphics.DrawString)
- 绘制数据点和线 (e.Graphics.DrawLine)
----------------编辑---------------- 新建一个UserControl:右击project -> add -> User Control
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 );
}
}
}
使用 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
。要同时控制两者,最好 全部设置 而不要依赖默认的 automatic 设置!
如果你想要一个空的中心,你应该
- 在 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
事件中进行测量;这超出了这个答案的范围..