
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();
}
/// <summary>
/// 折线图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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]);
}
}
/// <summary>
/// 折线图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
// X轴标签间距
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;
// X坐标轴颜色
//chartView.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#808080");
chartView.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.Black;
chartView.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
// X坐标轴标题
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;
// X轴网络线条
chartView.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
//chartView.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#E6E6FA");
// Y坐标轴颜色
//chartView.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#808080");
chartView.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Black;
chartView.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
// Y坐标轴标题
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 = "数量(台)";
// Y轴网格线条
chartView.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
//chartView.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#E6E6FA");
//chartView.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
// 背景渐变
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; // 设置X轴上的值类型
chartView.Series[0].Label = "#VAL"; // 设置显示X Y的值
chartView.Series[0].LabelForeColor = Color.Blue;
chartView.Series[0].ToolTip = "#VALX:#VAL(台)"; // 鼠标移动到对应点显示数值
chartView.Series[0].ChartType = SeriesChartType.Line; // 图类型 折线Line 饼图 Pie
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]["PieLineColor"] = "Blue";
// 绑定数据
chartView.Series[0].Points.DataBindXY(x, y);
chartView.Series[0].Points[0].Color = Color.Black;
// 绑定颜色
//chartView.Series[0].Palette = ChartColorPalette.BrightPastel;
}
}
}