C#WindowForm图表

了解一下主流3个设计模式:
MVP  WindowForm  code-behind代码在后面,代码在设计器的后面,所见即所得。
MVC  Web项目  Java Web, 前端Vue, React等,asp.net, php。
MVVM  WPF,Web项目,从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。通过DataSource
chart1.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()
        {
            //List<int> listY = new List<int>() { 1, 2, 3, 4, 5 };
            //chart1.Series["Series1"].Points.DataBindY(listY);

            //List<string> listX = new List<string>() { "张三", "李四", "王五", "赵六", "孙七" };
            //chart1.Series["Series1"].Points.DataBindXY(listX, listY);
            //chart1.Series[0].Points.DataBindXY(listX, listY);
            //chart1.Series["Series2"].Points.DataBindXY(listX, listY);


            List<Person> list = new List<Person>()
            {
                new Person(){ Id=1,Name="张三", Age=18,Percent=20},
                new Person(){ Id=2,Name="李四", Age=18,Percent=40},
                new Person(){ Id=3,Name="王五", Age=18,Percent=30},
                new Person(){ Id=4,Name="赵六", Age=18,Percent=10}
            };
            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. 给图表绑定数据源,第二种写法:DataSource

        private void cht2()
        {
            List<Person> list = new List<Person>()
            {
                new Person(){ Id=1,Name="张三",Percent=20},
                new Person(){ Id=2,Name="李四",Percent=40},
                new Person(){ Id=3,Name="王五",Percent=30},
                new Person(){ Id=4,Name="赵六",Percent=10}
            };
            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;

            List<Person> list = new List<Person>()
            {
                new Person(){ Id=1,Name="张三", Age=18,Score=60,Percent=20},
                new Person(){ Id=2,Name="李四", Age=23,Score=40,Percent=40},
                new Person(){ Id=3,Name="王五", Age=34,Score=80,Percent=30},
                new Person(){ Id=4,Name="赵六", Age=12,Score=50,Percent=10}
            };
            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 现在几乎可以运行在任何平台,支持Maui,Uno 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。修改数据源(序列Series)
4。修改外观(细节,各种属性,查文档)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值