4

使用ScottPlot库在.NET WinForms中快速实现大型数据集的交互式显示

 6 months ago
source link: https://www.cnblogs.com/Can-daydayup/p/18067442
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
1336199-20240312004711018-684726613.png

新建WinForms项目

新建一个名为ScottPlotWinFormsExercise的项目。

1336199-20240312004726535-199264549.png
1336199-20240312004732593-1512273836.png
1336199-20240312004739567-540567100.png
1336199-20240312004817554-902921398.png

安装ScottPlot.WinForms包

搜索ScottPlot.WinForms包安装:

1336199-20240312004838061-2078587943.png

折线图实现

创建名为:LineChart窗体。

1336199-20240312004851273-2087403173.png
1336199-20240312004856985-83728154.png

FormsPlot (ScottPlot.WinForms)从工具箱拖到窗体中:

1336199-20240312004913341-643864495.png

输入以下代码:

    public partial class LineChart : Form
    {
        public LineChart()
        {
            double[] dataX = GetRandomNum(20).Distinct().OrderByDescending(x => x).ToArray();
            double[] dataY = GetRandomNum(19).Distinct().OrderByDescending(x => x).ToArray();
            formsPlot1.Plot.Add.Scatter(dataX, dataY);
            formsPlot1.Refresh();
        }

        public double[] GetRandomNum(int length)
        {
            double[] getDate = new double[length];
            Random random = new Random(); //创建一个Random实例
            for (int i = 0; i < length; i++)
            {
                getDate[i] = random.Next(1, 100); //使用同一个Random实例生成随机数
            }
            return getDate;
        }
    }

运行效果展示:

1336199-20240312004930420-1666763292.png

柱状图实现

创建名为:BarChart窗体。

1336199-20240312004944524-988141616.png

FormsPlot (ScottPlot.WinForms)从工具箱拖到窗体中:

1336199-20240312004958181-1317991940.png

输入以下代码:

    public partial class BarChart : Form
    {
        public BarChart()
        {
            double[] values = { 5, 10, 7, 13, 22, 18, 33, 16 };
            formsPlot1.Plot.Add.Bars(values);
            formsPlot1.Refresh();
        }
    }

运行效果展示:

1336199-20240312005014148-2011421082.png

创建名为:PieChart窗体。

1336199-20240312005026785-1106109413.png

FormsPlot (ScottPlot.WinForms)从工具箱拖到窗体中:

1336199-20240312005040227-597956876.png

输入以下代码:

    public partial class PieChart : Form
    {
        public PieChart()
        {
            double[] values = { 3, 2, 8, 4, 8, 10 };
            formsPlot1.Plot.Add.Pie(values);
            formsPlot1.Refresh();
        }
    }

运行效果展示:

1336199-20240312005053161-595296584.png

散点图实现

创建名为:ScatterChart窗体。

1336199-20240312005104680-1775508854.png

FormsPlot (ScottPlot.WinForms)从工具箱拖到窗体中:

1336199-20240312005117459-350097076.png

输入以下代码:

    public partial class ScatterChart : Form
    {
        public ScatterChart()
        {
            //从原始数据开始
            double[] xs = Generate.Consecutive(100);
            double[] ys = Generate.NoisyExponential(100);

            //对数据进行对数缩放,并处理负值
            double[] logYs = ys.Select(Math.Log10).ToArray();

            //将对数缩放的数据添加到绘图中
            var sp = formsPlot1.Plot.Add.Scatter(xs, logYs);
            sp.LineWidth = 0;

            //创建一个次要刻度生成器,用于放置对数分布的次要刻度
            ScottPlot.TickGenerators.LogMinorTickGenerator minorTickGen = new();

            //创建一个数值刻度生成器,使用自定义的次要刻度生成器
            ScottPlot.TickGenerators.NumericAutomatic tickGen = new();
            tickGen.MinorTickGenerator = minorTickGen;

            //创建一个自定义刻度格式化程序,用于设置每个刻度的标签文本
            static string LogTickLabelFormatter(double y) => $"{Math.Pow(10, y):N0}";

            //告诉我们的主要刻度生成器仅显示整数的主要刻度
            tickGen.IntegerTicksOnly = true;

            //告诉我们的自定义刻度生成器使用新的标签格式化程序
            tickGen.LabelFormatter = LogTickLabelFormatter;

            //告诉左轴使用我们的自定义刻度生成器
            formsPlot1.Plot.Axes.Left.TickGenerator = tickGen;

            //显示次要刻度的网格线
            var grid = formsPlot1.Plot.GetDefaultGrid();
            grid.MajorLineStyle.Color = Colors.Black.WithOpacity(.15);
            grid.MinorLineStyle.Color = Colors.Black.WithOpacity(.05);
            grid.MinorLineStyle.Width = 1;

            formsPlot1.Refresh();
        }
    }

运行效果展示:

1336199-20240312005134506-816000850.png

项目演示入口

1336199-20240312010559022-1595886518.png
        private void Btn_ScatterChart_Click(object sender, EventArgs e)
        {
            ScatterChart formScatterChart = new ScatterChart();
            // 显示目标窗体
            formScatterChart.Show();
        }

        private void Btn_PieChart_Click(object sender, EventArgs e)
        {
            PieChart formPieChart = new PieChart();
            // 显示目标窗体
            formPieChart.Show();
        }

        private void Btn_BarChart_Click(object sender, EventArgs e)
        {
            BarChart formbarChart = new BarChart();
            // 显示目标窗体
            formbarChart.Show();
        }

        private void Btn_LineChart_Click(object sender, EventArgs e)
        {
            LineChart formLineChart = new LineChart();
            // 显示目标窗体
            formLineChart.Show();
        }

项目源码地址

更多项目实用功能和特性欢迎前往项目开源地址查看👀,别忘了给项目一个Star支持💖。

优秀项目和框架精选

该项目已收录到C#/.NET/.NET Core优秀项目和框架精选中,关注优秀项目和框架精选能让你及时了解C#、.NET和.NET Core领域的最新动态和最佳实践,提高开发工作效率和质量。坑已挖,欢迎大家踊跃提交PR推荐或自荐(让优秀的项目和框架不被埋没🤞)。

DotNetGuide技术社区交流群

  • DotNetGuide技术社区是一个面向.NET开发者的开源技术社区,旨在为开发者们提供全面的C#/.NET/.NET Core相关学习资料、技术分享和咨询、项目推荐、招聘资讯和解决问题的平台。
  • 在这个社区中,开发者们可以分享自己的技术文章、项目经验、遇到的疑难技术问题以及解决方案,并且还有机会结识志同道合的开发者。
  • 我们致力于构建一个积极向上、和谐友善的.NET技术交流平台,为广大.NET开发者带来更多的价值和成长机会。

欢迎加入DotNetGuide技术社区微信交流群👪


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK