1.前言:
NPlot為一個開放原始碼程式,是一種在.NET上使用的元件,其最新版軟體版權宣告如下:
NPlot since release 0.9.10.0 has been relicensed and now is distributed under the terms of a 3-clause-BSD license.
雖然NPlot稱不上成熟的繪圖元件,官方發布的版本只到.NET2.0,也無更新版本釋出,某些功能尚不完整,需要自己修改原始碼內容,但是對於一般的繪圖應用基本足夠,在實際應用上也容易操作。

2.說明:
NPlot說明文件
http://netcontrols.org/nplot/wiki/
NPlot軟體在sorceforge下載
http://sourceforge.net/projects/nplot/files/nplot/0.9.10.0/

軟體下載後解壓縮,將bin下的NPlot.dll複製到專案下的bin目錄,加入參考NPlot.dll,加入命名空間using NPlot;
在工具箱加入NPlot元件,完成後在工具箱看到PlotSurface2D的選項,可用拖曳的方式將元件布局在表單中,預設圖表名稱為plotSurface2D1

繪圖資料來源是由R語言中的mtcars導出dataset。
write.table(mtcars,'d:/tmp/mtcars.csv',row.names=FALSE,sep=',')

讀取數值資料

public DataTable TxtConvertToDataTable(string File, string TableName, string delimiter)
{
	DataTable dt = new DataTable();
	DataSet ds = new DataSet();
	StreamReader s = new StreamReader(File, System.Text.Encoding.Default);
	//string ss = s.ReadLine();//skip the first line
	string[] columns = s.ReadLine().Split(delimiter.ToCharArray());
	ds.Tables.Add(TableName);
	foreach (string col in columns)
	{
		bool added = false;
		string next = "";
		int i = 0;
		while (!added)
		{
			string columnname = col + next;
			columnname = columnname.Replace("#", "");
			columnname = columnname.Replace("'", "");
			columnname = columnname.Replace("&", "");

			if (!ds.Tables[TableName].Columns.Contains(columnname))
			{
				ds.Tables[TableName].Columns.Add(columnname.ToUpper());
				added = true;
			}
			else
			{
				i++;
				next = "_" + i.ToString();
			}
		}
	}

	string AllData = s.ReadToEnd();
	string[] rows = AllData.Split("\n".ToCharArray());

	foreach (string r in rows)
	{
		string[] items = r.Split(delimiter.ToCharArray());
		ds.Tables[TableName].Rows.Add(items);
	}

	s.Close();

	dt = ds.Tables[0];

	return dt;
}
繪圖
private void PointsPlot(DataTable dt)
{
	//圖表容器基本屬性設定
	plotSurface2D1.Clear();            
	
	//定義PointPlot Series與資料來源,為主要顯示的資料點
	PointPlot points = new PointPlot();
	points.DataSource = dt;
	points.AbscissaData = "WT";
	points.OrdinateData = "MPG";

	//定義LabelPointPlot Series與資料來源,做為顯示標記符號數值之用
	LabelPointPlot lp = new LabelPointPlot();
	lp.DataSource = dt;
	lp.AbscissaData = "WT";
	lp.OrdinateData = "MPG";
	lp.TextData = "MPG";            

	//設定標記符號屬性
	points.Marker.Type = Marker.MarkerType.Circle;//標記符號型態
	points.Marker.Color = Color.Yellow;//標記符號顏色
	points.Marker.Size = 8;//標記符號大小
	points.Marker.Filled = true;//是否填滿標記符號
	points.Marker.Pen.Color = Color.Red;//標記符號border顏色
	points.Marker.Pen.Width = 5;//標記符號border寬度

	lp.LabelTextPosition = LabelPointPlot.LabelPositions.Above;//標記符號數值位置
	lp.Marker.Type = Marker.MarkerType.None;//將此Series的標記符號設為None
	lp.Marker.Size = 10;//數值與標記符號距離
	lp.Font = new Font("Verdana", 8);//標記符號數值字形與大小            

	//加入Series到圖表容器
	plotSurface2D1.Add(points);
	plotSurface2D1.Add(lp);

	//圖表容器基本屬性設定
	plotSurface2D1.Title = "Points Chart Example";//圖表標題
	plotSurface2D1.BackColor = Color.Azure;//圖表背景顏色
	plotSurface2D1.XAxis1.Label = "Weight (lb/1000)";//X軸標題
	plotSurface2D1.YAxis1.Label = "Miles/Gallon";//Y軸標題
	plotSurface2D1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//圖表品質設定
	plotSurface2D1.PlotBackColor = Color.Azure;//繪圖區背景顏色
	
	//加入繪圖區網格線
	Grid grd = new Grid();//網格線實例化
	grd.MajorGridPen.Color = Color.Silver;//主網格線顏色
	grd.MajorGridPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;//主網格線風格            
	plotSurface2D1.Add(grd);//網格線加入到圖表容器

	plotSurface2D1.Refresh();
}
3.應用:
private void btPlot_Click(object sender, EventArgs e)
{
	//讀取數值資料
	DataTable dt = TxtConvertToDataTable(@"D:\TMP\mtcars.csv", "tmp", ",");

	//繪圖
	PointsPlot(dt);
}
圖例
nplotdemo_20130925  
arrow
arrow
    文章標籤
    C# NPlot Chart
    全站熱搜

    西夏普 發表在 痞客邦 留言(0) 人氣()