aardio调用C# NPlot画图表
图表控件一直是很难找的,特别是免费又强大的。NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等,而且它免费又开源,使用起来也非常符合程序员的习惯。
NPlot的命名空间包括NPlot,NPlot.Bitmap,NPlot.Web,NPlot.Web.Design,NPlot.Windows等,其中最核心的,管理各种图表的类都属于NPlot命名空间,NPlot.Bitmap针对位图的管理,NPlot.Web,NPlot.Web.Design和NPlot.Windows则可视为NPlot图表在Web Form和Windows Form上的容器(PlotSurface2D)。这些容器可以拖到Form上,也可以位于其他容器之中。
PlotSurface2D对象是NPlot图表的容器,所有的图表图形,坐标,标题(都继承IDrawable接口)等各种信息都可以被加入PlotSurface2D。PlotSurface2D拥有一个非常重要的方法:Add。各种图表图形,坐标,标题都可以通过Add加入PlotSurface2D对象。
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add()
/*}}*/
import dotNet;
import System.Windows.Forms;
dll = dotNet.loadFile("NPlot.dll");
Nplot = dll.import("NPlot");
plot = dll.new("NPlot.Windows.PlotSurface2D");
sp1 = dll.new("StepPlot");
sp1.OrdinateData = { 0, 1, 2 };
sp1.AbscissaData = { 4, 5, 6 };
sp1.Label = "高度";
sp1.Pen.Width = 2;
plot.Add(sp1);
System.Windows.Forms.CreateEmbed(plot, winform);
winform.show();
win.loopMessage();
柱状图

//柱状图
hist = dll.new("HistogramPlot");
hist.OrdinateData= {20, 40, 50, 30, 70, 80, 40, 50, 80, 20, 30, 50};
hist.AbscissaData= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
hist.BaseWidth = 0.8;
hist.Filled = true;
hist.Pen.Color = System.Drawing.Color.White;
plot.Add(hist);窗口拖动、放缩好像也都可以实现,有空再更新。

水平线和垂直线
//水平线
lineX = dll.new("HorizontalLine", 3);
lineX.LengthScale = 1;
lineX.Pen.Color = System.Drawing.Color.Red;
//垂直线
lineY = dll.new("VerticalLine", 5);
lineY.LengthScale = 1;
lineY.Pen.Color = System.Drawing.Color.Green;
plot.Add(lineX);
plot.Add(lineY);
//水平线和垂直线跟随动态数据更新
index = 1;
winform.setInterval(
function(){
data[index] = math.random(10, 100);
line.OrdinateData= data;
lineX.OrdinateValue = data[index];
lineY.AbscissaValue = index;
plot.Refresh();
index ++;
if index>100 index=1;
},300
)//图例
plot.Legend = dll.new("Legend");
plot.Legend.NumberItemsHorizontally = 2;
plot.Legend.YOffset = 5;
plot.Legend.XOffset = 5;//网格
NPlot = dll.import("NPlot");
grid = dll.new("Grid");
grid.HorizontalGridType = NPlot.Grid.GridType.None;
grid.VerticalGridType = NPlot.Grid.GridType.Fine;
plot.Add(grid);//区间着色
fr = dll.new("FilledRegion", dll.new("VerticalLine", 50), dll.new("VerticalLine", 80));
plot.Add(fr);//画箭头
a = dll.new("ArrowItem", dll.new("PointD", 20, 10), 360, "Arrow");
a.HeadOffset = 5;
a.ArrowColor = System.Drawing.Color.Red;
a.TextColor = System.Drawing.Color.Purple;
plot.Add(a);回复#5 @jerryxjr1220 :
我测试

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add()
/*}}*/
import dotNet;
import System.Drawing;
import System.Windows.Forms;
dll = dotNet.loadFile("\NPlot.dll");
Nplot = dll.import("NPlot");
plot = dll.new("NPlot.Windows.PlotSurface2D");
System.Windows.Forms.CreateEmbed(plot, winform);
data = table.array(1000, 0);
xs = table.array(1000, 0);
for i=1;1000 {
xs[i] = i;
data[i] = 100*math.cos(i*0.05);
}
//垂直线
vline = dll.new("NPlot.VerticalLine",1);
vline.Pen = System.Drawing.Pen(System.Drawing.Color.Red, 3);
plot.Add(vline);
//折线图
line = dll.new("LinePlot");
line.OrdinateData= data;
line.AbscissaData= xs;
line.Pen.Color = System.Drawing.Color.Gray;
line.Pen.Width = 2;
line.Label = "Line";
plot.Add(line);
xAxis = plot.XAxis1;
yAxis = plot.YAxis1;
yAxis.Label = "Y Axis";
xAxis.Label = "X Axis";
plot.Refresh();
index = 1;
flag = false;
winform.setInterval(
function(){
if(!flag){
data[index] = 100*math.sin(index*0.05);
}else {
data[index] = 100*math.cos(index*0.05);
}
line.OrdinateData= data;
vline.AbscissaValue = index;
index ++;
plot.Refresh();
if index>1000 {index=1;flag=!flag;};
},10
)
winform.show();
win.loopMessage();Refresh()可以用 , 用的你动态数据那个代码......
如果你测试通过了把楼上那些代码改下. winform+-看着好别扭
回复#7 @admin :
HistogramPlot hist = new HistogramPlot();
hist.AbscissaData = new int[] { 1, 2, 3, 4, 5 };
hist.OrdinateData = new int[] { 10, 20, 13, 24, 15 };
hist.RectangleBrush = RectangleBrushes.Vertical.FaintBlueFade;
hist.Filled = true;
hist.BaseWidth = 0.9f;
plotSurface2D1.Add(hist);在C#中正常,但是在aardio中,这个RectangleBrushes一直引用不成功,我查了它也是在NPlot下的类,知道在aardio中应该怎么引用吗?

效果是这样的
回复#9 @jerryxjr1220 :
看了下代码, 发现它这里是用的lamda表达式new的实例, 我估摸着我们可能不能这么直接用这个方法,
本来想把nplot源码修改下重新编译, 貌似一直缺东西, 算了.
于是写了个扩展库nPlotEx.dll , 将这个函数单独实现扩展出来.

import dotNet;
import System.Drawing;
import System.Windows.Forms;
dll = dotNet.load("\NPlot.dll");
plot = dll.new("NPlot.Windows.PlotSurface2D");
System.Windows.Forms.CreateEmbed(plot, winform);
data = table.array(100, 0);
xs = table.array(100, 0);
for i=1;100 {
xs[i] = i;
data[i] = 100*math.cos(i*0.05);
}
hist = dll.new("HistogramPlot");
hist.OrdinateData= data;
hist.AbscissaData= xs;
//增加个渐变扩展
dllex = dotNet.load("\nPlotEx.dll");
RectangleBrushes = dllex.new("RectangleBrushes");
hist.RectangleBrush = RectangleBrushes.Vertical(System.Drawing.Color.Red,System.Drawing.Color.Green);
hist.BaseWidth = 0.8;
hist.Filled = true;
plot.Add(hist);
plot.Refresh();附上C#扩展文件所用代码:
using NPlot;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace nPlotEx
{
public class RectangleBrushes
{
public static IRectangleBrush Vertical(Color p1, Color p2)
{
var Vertical = new NPlot.RectangleBrushes.Vertical(p1, p2);
return Vertical;
}
public static IRectangleBrush Horizontal(Color p1, Color p2)
{
var Vertical = new NPlot.RectangleBrushes.Horizontal(p1, p2);
return Vertical;
}
public static IRectangleBrush HorizontalCenterFade(Color p1, Color p2)
{
var Vertical = new NPlot.RectangleBrushes.HorizontalCenterFade(p1, p2);
return Vertical;
}
public static IRectangleBrush VerticalCenterFade(Color p1, Color p2)
{
var Vertical = new NPlot.RectangleBrushes.VerticalCenterFade(p1, p2);
return Vertical;
}
}
}另外, 我发现nplot这个柱状图, 如果界面非常小的时候, 恰巧数据量比较大, 那么就会显示白板, 没有数据图出现, 当你全屏显示会发现其实图形是在的.
所以, 针对柱状图来说(其他图我并没有测试)当数据量小平面也足够大小的时候, 是可以使用的. 数据量大的时候需要斟酌下.
登录后方可回帖
回复

动态数据
import win.ui; /*DSG{{*/ var winform = win.form(text="aardio form";right=759;bottom=469) winform.add() /*}}*/ import dotNet; import System.Drawing; import System.Windows.Forms; dll = dotNet.loadFile("NPlot.dll"); Nplot = dll.import("NPlot"); plot = dll.new("NPlot.Windows.PlotSurface2D"); System.Windows.Forms.CreateEmbed(plot, winform); data = table.array(100, 0); xs = table.array(100, 0); for i=1;100 { xs[i] = i; data[i] = math.random(10, 100); } //折线图 line = dll.new("LinePlot"); line.OrdinateData= data; line.AbscissaData= xs; line.Pen.Color = System.Drawing.Color.Blue; line.Pen.Width = 2; line.Label = "Line"; plot.Add(line); xAxis = plot.XAxis1; yAxis = plot.YAxis1; yAxis.Label = "Y Axis"; xAxis.Label = "X Axis"; index = 1; winform.setInterval( function(){ data[index] = math.random(10, 100); line.OrdinateData= data; plot.Refresh(); index ++; if index>100 index=1; },300 ) winform.show(); win.loopMessage();