
了解一下主流3个设计模式MVP WindowForm code-behind代码在后面代码在设计器的后面所见即所得。MVC Web项目 Java Web, 前端Vue, React等asp.net, php。MVVM WPFWeb项目从MVC进化过来的Model 模型本质上数据View 视图本质上界面Presenter 呈现器 充当“桥梁”负责两部分工作1。把模型中存储的数据渲染到视图。2。把视图搜集的信息再存储到模型。Model 模型本质上数据View 视图本质上界面Control 控制器 充当“桥梁”负责两部分工作1。把模型中存储的数据渲染到视图。2。把视图搜集的信息再存储到模型。Model 模型本质上数据View 视图本质上界面ModelView 模型视图 充当“桥梁”负责两部分工作1。把模型中存储的数据渲染到视图。2。把视图搜集的信息再存储到模型。一、官方控件库图表1.重点图表内容官方图表控件Chart重要的组成部分ChartAreas属性绘图区可以有多个每个区域可以绘制不同的图形如柱状图(Column Bar)饼状图线性图等 ****Series属性序列即绘制的图形 ***Legends属性图例一般用来解释某个序列的意思。注解 ***Titles图表标题集合设置图表的标题 ***图表的数据从数据库来****2.官方图表控件Chart应用步骤1。拖控件2。设数据3。更改属性注意chart在运行中默认不显示需要绑定数据源Legends图例Legends修改后面的名字在Legends属性上找不到在Series属性上找LegendText重点绑定数据源Series序列Titles标题Legends图例添加轴标题ChartsAreas集合里的AxesX但是直接寻找不能找到可以·直接写代码进行添加轴标题3.Chart控件绑定数据两种方式1。通过DataBindXY()chart1.Series[Series1].Points.DataBindXY(XList, YList);chart1.Series[0].Points.DataBind(list, X, Y, null);2。通过DataSourcechart1.DataSource result;chart1.Series[0].XValueMember CategoryName;chart1.Series[0].YValueMembers Num;3.3代码详细了解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 MYN_ChartDemo{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){cht1();cht2();cht3();}// A. 给图表绑定数据源第一种方式的三种写法DataBind()private void cht1(){//Listint listY new Listint() { 1, 2, 3, 4, 5 };//chart1.Series[Series1].Points.DataBindY(listY);//Liststring listX new Liststring() { 张三, 李四, 王五, 赵六, 孙七 };//chart1.Series[Series1].Points.DataBindXY(listX, listY);//chart1.Series[0].Points.DataBindXY(listX, listY);//chart1.Series[Series2].Points.DataBindXY(listX, listY);ListPerson list new ListPerson(){new Person(){ Id1,Name张三, Age18,Percent20},new Person(){ Id2,Name李四, Age18,Percent40},new Person(){ Id3,Name王五, Age18,Percent30},new Person(){ Id4,Name赵六, Age18,Percent10}};chart1.Series[0].Points.DataBind(list, Name, Age, null);chart1.Series[1].Points.DataBind(list, Name, Age, null);chart1.Titles.Add(title1);chart1.Titles[0].Text 第一个图标题;chart1.Titles[0].ForeColor Color.Red;chart1.Legends[0].Title 图例标题;chart1.Series[0].LegendText 人名;// 特殊关键字#VAL, #VALX, #VALY, #PERCENT#TOTAL, #SERIESNAME等chart1.Series[0].Label #VAL; //设置显示Y的值 #VAL默认Y轴的值chart1.Series[0].Label #VALY; //设置显示Y的值//chart1.Series[0].Label #SERIESNAME; //设置显示X Y的值chart1.Series[1].Points.DataBind(list, Name, Percent, null);chart1.Series[1].Label #VALX; //设置显示X的值chart1.Series[1].ToolTip #VALX:#VALY;}// B. 给图表绑定数据源第二种写法DataSourceprivate void cht2(){ListPerson list new ListPerson(){new Person(){ Id1,Name张三,Percent20},new Person(){ Id2,Name李四,Percent40},new Person(){ Id3,Name王五,Percent30},new Person(){ Id4,Name赵六,Percent10}};chart2.DataSource list;// 数据源chart2.Series[0].XValueMember Name;// X轴chart2.Series[0].YValueMembers Percent;// Y轴}private void cht3(){chart3.Series.Add(Series2);chart3.Series.Add(Series3);chart3.Series[1].ChartType SeriesChartType.Line;chart3.Series[2].ChartType SeriesChartType.Spline;ListPerson list new ListPerson(){new Person(){ Id1,Name张三, Age18,Score60,Percent20},new Person(){ Id2,Name李四, Age23,Score40,Percent40},new Person(){ Id3,Name王五, Age34,Score80,Percent30},new Person(){ Id4,Name赵六, Age12,Score50,Percent10}};chart3.DataSource list;chart3.Series[0].XValueMember Name;chart3.Series[0].YValueMembers Age;chart3.Series[1].YValueMembers Score;chart3.Series[2].YValueMembers Percent;//启用/禁用网格线//ChartAreas[0]获取图表中的第一个绘图区域一个 Chart 可包含多个 ChartArea。//AxisX/AxisY分别对应 X 轴通常为类别轴和 Y 轴数值轴。//MajorGrid主网格线即从轴刻度延伸出的辅助线chart3.ChartAreas[0].AxisX.MajorGrid.Enabled true;chart3.ChartAreas[0].AxisY.MajorGrid.Enabled true;//设置网格线颜色chart3.ChartAreas[0].AxisX.MajorGrid.LineColor Color.Red;chart3.ChartAreas[0].AxisY.MajorGrid.LineColor Color.Blue;// 设置轴标题颜色chart3.ChartAreas[0].AxisX.TitleForeColor Color.Red;//设置轴文本方向chart3.ChartAreas[0].AxisX.TextOrientation TextOrientation.Auto;//设置标签样式旋转角度、字体、颜色chart3.ChartAreas[0].AxisX.LabelStyle.Angle 90;chart3.ChartAreas[0].AxisX.LabelStyle.Font new Font(微软雅黑, 9f, FontStyle.Regular);chart3.ChartAreas[0].AxisX.LabelStyle.ForeColor Color.Red;}}public class Person{public int Id { get; set; }public string Name { get; set; }public int Age { get; set; }public double Score { get; set; }public double Percent { get; set; }}}二、LiveCharts图表库1。LiveCharts2 是什么LiveCharts2 是一个简单、灵活、交互式以及功能强大的跨平台图表库。LiveCharts是一个.net的数据可视化库可以跨多个设备和框架运行它在MIT许可证下运行(免费)并提供了一个付费包来提高性能和扩展功能。LiveCharts2 现在几乎可以运行在任何平台支持MauiUno Platform、Avalonia、Etoforms、Xamarin、Blazor-wasm、WPF、Winforms、WinUI、UWP等。LiveCharts2 是LiveCharts(0)的升级它修复了前身的主要设计问题专注于在任何地方运行在不丢失V0中已有的特性情况下提高了灵活性。2。LiveCharts2 可以做什么LiveCharts2 提供了折线图、饼图、柱状图、散点图、面积图等多种类型的图表。此外它还支持多个图表联动显示支持动态更新数据、自定义样式和动画效果等功能。可参考【livechart2可绘制的图类型.png】3。怎么使用a. 创建一个.net framework版本/.net core(出现版本不兼容问题)的winform项目b. 安装LiveChartsCore.SkiaSharpView.WinForms类库c. 抄文档。LiveCharts2官网https://livecharts.dev/注意LiveChart2 版本2.0.5版本支持.net core。.net framework不能显示图表是官方更新问题。使用LiveChart2步骤1。安装LiveChartsCore.SkiaSharpView.WinForms一次性。2。创建图表实例并添加到窗体上。某种图表 注意标题图例序列3。修改数据源序列Series4。修改外观细节各种属性查文档