c# chart 控件 曲线图
![](https://i-blog.csdnimg.cn/blog_migrate/9476ebeea8f4208f58f6b2b0b66756ea.png)
SeriesChartType.Line 折线 SeriesChartType.Spline 曲线
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsApp
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void btnLine_Click(object sender, EventArgs e)
{
List<int> ListPoint = new List<int>();
Random random = new Random();
int num = 0;
for (int i = 0; i < 10; i++)
{
num = random.Next(0, 10);
ListPoint.Add(num);
}
chartLine.Series[0].Points.Clear();
for (int i = 0; i < ListPoint.Count; i++)
{
chartLine.Series[0].Points.AddXY(i + 1, ListPoint[i]);
}
}
private void btnPie_Click(object sender, EventArgs e)
{
string[] x = new string[] { "1月", "2月", "3月", "4月", "5月" };
double[] y = new double[] { 500, 900, 700, 650, 450 };
chartView.Titles.Add("折线范例");
chartView.Titles[0].ForeColor = Color.Blue;
chartView.Titles[0].Font = new Font("微软雅黑", 12f, FontStyle.Regular);
chartView.Titles[0].Alignment = ContentAlignment.TopCenter;
chartView.Titles.Add("右标题");
chartView.Titles[1].ForeColor = Color.Blue;
chartView.Titles[1].Font = new Font("微软雅黑", 8f, FontStyle.Regular);
chartView.Titles[1].Alignment = ContentAlignment.TopRight;
chartView.BackColor = Color.Transparent;
chartView.ChartAreas[0].BackColor = Color.Transparent;
chartView.ChartAreas[0].BorderColor = Color.Transparent;
chartView.ChartAreas[0].AxisX.Interval = 1;
chartView.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
chartView.ChartAreas[0].AxisX.LabelStyle.Angle = -30;
chartView.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 14f, FontStyle.Regular);
chartView.ChartAreas[0].AxisX.TitleForeColor = Color.Black;
chartView.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.Black;
chartView.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
chartView.ChartAreas[0].AxisX.Title = "月份";
chartView.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
chartView.ChartAreas[0].AxisX.TitleForeColor = Color.Black;
chartView.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
chartView.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
chartView.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Black;
chartView.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
chartView.ChartAreas[0].AxisY.Title = "数量(台)";
chartView.ChartAreas[0].AxisY.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
chartView.ChartAreas[0].AxisY.TitleForeColor = Color.Black;
chartView.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
chartView.ChartAreas[0].AxisY.ToolTip = "数量(台)";
chartView.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
chartView.ChartAreas[0].BackGradientStyle = GradientStyle.None;
Legend legend = new Legend("销量");
legend.Title = "图例";
legend.TitleBackColor = Color.Transparent;
legend.BackColor = Color.Transparent;
legend.TitleForeColor = Color.Black;
legend.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
legend.Font = new Font("微软雅黑", 8f, FontStyle.Regular);
legend.ForeColor = Color.Black;
chartView.Series[0].XValueType = ChartValueType.String;
chartView.Series[0].Label = "#VAL";
chartView.Series[0].LabelForeColor = Color.Blue;
chartView.Series[0].ToolTip = "#VALX:#VAL(台)";
chartView.Series[0].ChartType = SeriesChartType.Line;
chartView.Series[0].Color = Color.SkyBlue;
chartView.Series[0].LegendText = legend.Name;
chartView.Series[0].IsValueShownAsLabel = true;
chartView.Series[0].LabelForeColor = Color.Black;
chartView.Series[0].CustomProperties = "DrawingStyle = Cylinder";
chartView.Series[0].CustomProperties = "PieLabelStyle = Outside";
chartView.Legends.Add(legend);
chartView.Legends[0].Position.Auto = true;
chartView.Series[0].IsValueShownAsLabel = true;
chartView.Series[0].IsVisibleInLegend = true;
chartView.Series[0].ShadowOffset = 0;
chartView.Series[0].Points.DataBindXY(x, y);
chartView.Series[0].Points[0].Color = Color.Black;
}
private void btnLinePoint_Click(object sender, EventArgs e)
{
chartLine.Series[0].Points.Clear();
List<string> x = new List<string>() { "A", "B", "C", "D", "E", "F", "G" };
List<int> y = new List<int>() { 10, 20, 30, 40, 35, 25, 15 };
chartLine.Series[0].Color = Color.Blue;
chartLine.Series[0].BorderWidth = 3;
chartLine.Series[0].MarkerBorderColor = Color.Black;
chartLine.Series[0].MarkerBorderWidth = 3;
chartLine.Series[0].MarkerColor = Color.Red;
chartLine.Series[0].MarkerSize = 8;
chartLine.Series[0].MarkerStyle = MarkerStyle.Square;
chartLine.Series[0]["PieLabelStyle"] = "Outside";
chartLine.Series[0]["PieLineColor"] = "Black";
chartLine.Series[0].Points.DataBindXY(x, y);
}
private void btnPieOnly_Click(object sender, EventArgs e)
{
List<string> x = new List<string>() { "A", "B", "C", "D", "E", "F", "G" };
List<int> y = new List<int>() { 10, 20, 30, 40, 35, 25, 15 };
chartPie.Series[0].ChartType = SeriesChartType.Pie;
chartPie.Series[0]["PieLabelStyle"] = "Outside";
chartPie.Series[0]["PieLineColor"] = "Black";
chartPie.Series[0].IsValueShownAsLabel = true;
chartPie.Series[0].Points.DataBindXY(x, y);
}
private void btnBar_Click(object sender, EventArgs e)
{
chartBar.Series.Clear();
Series a = new Series();
a.LegendText = "柱a";
Series b = new Series();
b.LegendText = "柱b";
Series c = new Series();
c.LegendText = "柱c";
Random r = new Random();
for (int i = 1; i < 10; i++)
{
a.Points.AddXY(i, r.Next(1, 9));
b.Points.AddXY(i, r.Next(10, 20));
c.Points.AddXY(i, r.Next(21, 30));
}
a.Color = Color.Green;
b.Color = Color.Red;
c.Color = Color.Black;
chartBar.Series.Add(a);
chartBar.Series.Add(b);
chartBar.Series.Add(c);
chartBar.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
}
private void btnPoint_Click(object sender, EventArgs e)
{
List<int> ListPoint = new List<int>();
Random random = new Random();
int num = 0;
for (int i = 0; i < 10; i++)
{
num = random.Next(0, 10);
ListPoint.Add(num);
}
chartPoint.Series[0].Points.Clear();
for (int i = 0; i < ListPoint.Count; i++)
{
chartPoint.Series[0].Points.AddXY(i + 1, ListPoint[i]);
}
chartPoint.Series[0].IsVisibleInLegend = false;
chartPoint.Series[0].ChartType = SeriesChartType.Point;
chartPoint.Series[0].MarkerColor = Color.Red;
chartPoint.Series[0].MarkerStyle = MarkerStyle.Circle;
}
private void btnSPLine_Click(object sender, EventArgs e)
{
List<int> ListPoint = new List<int>();
Random random = new Random();
int num = 0;
for (int i = 0; i < 10; i++)
{
num = random.Next(0, 10);
ListPoint.Add(num);
}
chartSPLine.Series[0].Points.Clear();
for (int i = 0; i < ListPoint.Count; i++)
{
chartSPLine.Series[0].Points.AddXY(i + 1, ListPoint[i]);
}
chartSPLine.Series[0].IsVisibleInLegend = false;
chartSPLine.Series[0].ChartType = SeriesChartType.Spline;
chartSPLine.Series[0].MarkerColor = Color.Red;
chartSPLine.Series[0].MarkerStyle = MarkerStyle.Circle;
}
}
}