好用的WinForm图表库ScottPlot

 

先上成品图,看起来还不错对吧。

WinForm下引入包

ScottPlot.WinForms

据说WPF也可以用,目前没有测试过,也不是本文讲解。WPF中引入的包叫这个,有兴趣的可以了解下

1.通过Nuget安装 ScottPlot.WPF
2.添加一个 WpfPlot 组件到布局中, 并设置Name

   <WpfPlot Name="WpfPlot1" />

 

 

WinForm下安装好就有这个了

 

 

 拖出来

 

 

 

我拖了4个,然后放了个timer

 

 

那图是怎么生成呢,特别简单,先上个示例

double[] dataX = new double[] {1, 2, 3, 4, 5};
double[] dataY = new double[] {1, 4, 9, 16, 25};
formsPlot1.Plot.AddScatter(dataX, dataY);
formsPlot1.Refresh();

 

这样就OK了,

重置图表怎么搞?

Reset就可以。

formsPlot1.Reset();

 

一定记得最后要Refresh一下才可以。

 

剩下的其他样式呢?

我提供我上面有的这4个吧

        int pointCount = 20;
            double[] xs1 = ScottPlot.DataGen.RandomWalk(r, pointCount);
            double[] ys1 = ScottPlot.DataGen.RandomWalk(r, pointCount);
            double[] xs2 = ScottPlot.DataGen.RandomWalk(r, pointCount);
            double[] ys2 = ScottPlot.DataGen.RandomWalk(r, pointCount);

            // plot the data
            formsPlot1.Reset();
            formsPlot1.plt.PlotScatter(xs1, ys1);
            formsPlot1.plt.PlotScatter(xs2, ys2);

            // additional styling
            formsPlot1.plt.Title($"Scatter Plot ({pointCount} points per group)");
            formsPlot1.plt.XLabel("Horizontal Axis Label");
            formsPlot1.plt.YLabel("Vertical Axis Label");
            formsPlot1.Render();



            pointCount = 10_000;
            ys1 = ScottPlot.DataGen.RandomWalk(r, pointCount);
            ys2 = ScottPlot.DataGen.RandomWalk(r, pointCount);

            // plot the data
            formsPlot2.Reset();
            formsPlot2.plt.PlotSignal(ys1);
            formsPlot2.plt.PlotSignal(ys2);

            // additional styling
            formsPlot2.plt.Title($"Line Plot ({10_000:N0} points each)");
            formsPlot2.plt.XLabel("Horizontal Axis Label");
            formsPlot2.plt.YLabel("Vertical Axis Label");
            formsPlot2.Render();



            pointCount = 5;
            double[] xs = ScottPlot.DataGen.Consecutive(pointCount);
            double[] ys = ScottPlot.DataGen.RandomWalk(r, pointCount, mult: 50, offset: 100);

            // plot the data
            formsPlot3.Reset();
            formsPlot3.plt.PlotBar(xs, ys);

            // additional styling
            formsPlot3.plt.Title("Simple Bar Graph");
            formsPlot3.plt.XLabel("Horizontal Axis Label");
            formsPlot3.plt.YLabel("Vertical Axis Label");
            //formsPlot3.plt.Axis(y1: 0);
            formsPlot3.Render();



            pointCount = 5;
            ys1 = ScottPlot.DataGen.RandomWalk(r, pointCount, mult: 50, offset: 100);
            ys2 = ScottPlot.DataGen.RandomWalk(r, pointCount, mult: 50, offset: 100);

            // collect the data into groups
            string[] groupLabels = { "One", "Two", "Three", "Four", "Five" };
            string[] seriesLabels = { "Group A", "Group B" };
            double[][] barHeights = { ys1, ys2 };

            // plot the data
            formsPlot4.Reset();
            formsPlot4.plt.PlotBarGroups(groupLabels, seriesLabels, barHeights);

            // additi4nal styling
            formsPlot4.plt.Title("Bar Graph");
            formsPlot4.plt.XLabel("Horizontal Axis Label");
            formsPlot4.plt.YLabel("Vertical Axis Label");
            //formsPlot4.plt.Legend(location: ScottPlot.legendLocation.upperLeft);
            //formsPlot4.plt.Axis(y1: 0);
            formsPlot4.Render();

 

 

就这样吧~

 

posted @ 2021-11-26 10:14  王月半子  阅读(8674)  评论(0编辑  收藏  举报