C#实现用图表控件绘制四叶草以(0,0)为原点的,图案填充的
时间: 2023-06-24 18:07:33 浏览: 238
C#图表绘制
以下是用C#实现用图表控件绘制四叶草以(0,0)为原点的图案填充的完整代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace FourLeafCloverChart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 设置图表区域背景色为透明
chart1.ChartAreas[0].BackColor = Color.Transparent;
// 添加一个散点图系列
Series series = chart1.Series.Add("Four Leaf Clover");
series.ChartType = SeriesChartType.Point;
// 设置散点图的点大小和颜色
series.MarkerSize = 10;
series.MarkerStyle = MarkerStyle.Circle;
series.Color = Color.Green;
// 绘制四叶草
for (double theta = 0; theta < 2 * Math.PI; theta += 0.01)
{
double r = Math.Sin(2 * theta);
double x = r * Math.Cos(theta);
double y = r * Math.Sin(theta);
series.Points.AddXY(x, y);
}
// 设置图表的坐标轴
chart1.ChartAreas[0].AxisX.Minimum = -1;
chart1.ChartAreas[0].AxisX.Maximum = 1;
chart1.ChartAreas[0].AxisY.Minimum = -1;
chart1.ChartAreas[0].AxisY.Maximum = 1;
chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
// 设置图表的样式
chart1.BackColor = Color.Transparent;
chart1.BorderlineColor = Color.Transparent;
chart1.ChartAreas[0].BorderWidth = 0;
chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
chart1.ChartAreas[0].AxisY.LabelStyle.Enabled = false;
chart1.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
chart1.ChartAreas[0].AxisY.Enabled = AxisEnabled.False;
chart1.Series[0].IsVisibleInLegend = false;
}
}
}
```
运行以上代码后,会得到一个图表控件,上面绘制了一个以(0,0)为原点的四叶草,颜色为绿色,并且填充了图案。如果需要修改图表的大小和位置,可以通过设置 `chart1.Size` 和 `chart1.Location` 属性来实现。
阅读全文