Winform中实现ZedGraph中曲线右键显示为中文
生活随笔
收集整理的這篇文章主要介紹了
Winform中实现ZedGraph中曲线右键显示为中文
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
場(chǎng)景
Winforn中設(shè)置ZedGraph曲線圖的屬性、坐標(biāo)軸屬性、刻度屬性:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100112573
在上面實(shí)現(xiàn)右鍵的基礎(chǔ)上,效果如下:
?
實(shí)現(xiàn)
添加如下代碼
?this.zedGraphControl1.ContextMenuBuilder += MyContextMenuBuilder;方法中
?
效果
?
完整示例代碼
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 ZedGraph;namespace ZedGraphTest {public partial class Form1 : Form{GraphPane myPane = new GraphPane();public Form1(){InitializeComponent();//Form1初始化后創(chuàng)建設(shè)置控件的方法并將當(dāng)前ZedGraph控件傳遞createPane(zedGraphControl1);}//需要引入命名空間--using ZedGraph;public void createPane(ZedGraphControl zgc){myPane = zgc.GraphPane;//設(shè)置圖表標(biāo)題 和 x y 軸標(biāo)題myPane.Title.Text = "霸道測(cè)試標(biāo)題";myPane.XAxis.Title.Text = "X軸標(biāo)題";myPane.YAxis.Title.Text = "Y軸標(biāo)題";//更改標(biāo)題的字體FontSpec myFont = new FontSpec("Arial",16,Color.Black,false,false,false);myPane.XAxis.Title.FontSpec = myFont;myPane.YAxis.Title.FontSpec = myFont;// 造一些數(shù)據(jù),PointPairList里有數(shù)據(jù)對(duì)x,y的數(shù)組Random y = new Random();PointPairList list1 = new PointPairList();for (int i = 0; i < 36; i++){double x = i;double y1 = y.NextDouble() * 1000;list1.Add(x, y1); //添加一組數(shù)據(jù)}// 用list1生產(chǎn)一條曲線,標(biāo)注是“曲線1”//SymbolType,枚舉代表曲線的樣式//Square = 0,//Diamond = 1,//Triangle = 2,//Circle = 3,//XCross = 4,//Plus = 5,//Star = 6,//TriangleDown = 7,//HDash = 8,//VDash = 9,//UserDefined = 10,//Default = 11,//None = 12,LineItem myCurve = myPane.AddCurve("曲線1", list1, Color.Red, SymbolType.None);//填充圖表顏色myPane.Fill = new Fill(Color.White, Color.LightGray, 45.0f);//以上生成的圖標(biāo)X軸為數(shù)字,下面將轉(zhuǎn)換為日期的文本string[] labels = new string[36];for (int i = 0; i < 36; i++){labels[i] = System.DateTime.Now.AddDays(i).ToShortDateString();}#region 坐標(biāo)軸屬性設(shè)置//X軸類(lèi)型myPane.XAxis.Type = AxisType.Text;??//顯示小刻度 是false則看不到效果myPane.XAxis.MinorGrid.IsVisible = true;//線的顏色myPane.XAxis.Color = Color.Black;//點(diǎn)線中點(diǎn)與點(diǎn)之間的間隔myPane.XAxis.MinorGrid.DashOff = 1f;//點(diǎn)線中點(diǎn)的長(zhǎng)度myPane.XAxis.MinorGrid.DashOn = 1f;//畫(huà)筆寬度myPane.XAxis.MinorGrid.PenWidth = 1f;#endregion#region 坐標(biāo)軸上刻度線設(shè)置//X軸文本取值myPane.XAxis.Scale.TextLabels = labels;//第一個(gè)刻度從哪里開(kāi)始myPane.XAxis.Scale.BaseTic = 1;//刻度值的字體屬性myPane.XAxis.Scale.FontSpec = myFont;#endregion//畫(huà)到zedGraphControl1控件中,此句必加zgc.AxisChange();//在數(shù)據(jù)變化時(shí)繪圖//更新圖表zedGraphControl1.Invalidate();//重繪控件Refresh();#region 屬性設(shè)置//是否允許橫向縮放this.zedGraphControl1.IsEnableHZoom = true;//是否允許縱向縮放this.zedGraphControl1.IsEnableVZoom = true;//是否允許縮放this.zedGraphControl1.IsEnableZoom = true;//是否顯示右鍵菜單this.zedGraphControl1.IsShowContextMenu = true;//復(fù)制圖像時(shí)是否顯示提示信息this.zedGraphControl1.IsShowCopyMessage = true;//鼠標(biāo)在圖表上移動(dòng)時(shí)是否顯示鼠標(biāo)所在點(diǎn)對(duì)應(yīng)的坐標(biāo) 默認(rèn)為falsethis.zedGraphControl1.IsShowCursorValues = true;//是否顯示橫向滾動(dòng)條this.zedGraphControl1.IsShowHScrollBar = true;//是否顯示縱向滾動(dòng)條this.zedGraphControl1.IsShowVScrollBar = true;//鼠標(biāo)經(jīng)過(guò)圖表上的點(diǎn)時(shí)是否顯示該點(diǎn)所對(duì)應(yīng)的值 默認(rèn)為falsethis.zedGraphControl1.IsShowPointValues = true;//使用滾輪時(shí)以鼠標(biāo)所在點(diǎn)為中心進(jìn)行縮放還是以圖形中心進(jìn)行縮放//this.zedGraphControl1.IsZoomOnMouseCenter = true;#endregionthis.zedGraphControl1.ContextMenuBuilder += MyContextMenuBuilder;}private void zedGraphControl1_Load(object sender, EventArgs e){}/// <summary>/// 打印預(yù)覽/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click(object sender, EventArgs e){this.zedGraphControl1.DoPrintPreview();}//復(fù)制到剪切板private void button2_Click(object sender, EventArgs e){//ture代表復(fù)制成功提示this.zedGraphControl1.Copy(true);}/// <summary>/// 獲取圖片并保存/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button3_Click(object sender, EventArgs e){//獲取圖像Image image = this.zedGraphControl1.GetImage();//保存照片嗎,指定保存路徑image.Save(@"C:\Users\HAOHAO\Desktop\1.png");//彈窗提示MessageBox.Show("保存成功");}/// <summary>/// 顯示另存為對(duì)話框/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button4_Click(object sender, EventArgs e){this.zedGraphControl1.SaveAs();}/// <summary>/// 另存為BMP文件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button5_Click(object sender, EventArgs e){this.zedGraphControl1.SaveAsBitmap();}/// <summary>/// 另存為EMF文件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button6_Click(object sender, EventArgs e){this.zedGraphControl1.SaveAsEmf();}/// <summary>/// 一鍵復(fù)原/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button7_Click(object sender, EventArgs e){//一鍵復(fù)原縮放this.zedGraphControl1.ZoomOutAll(myPane);}//右擊菜單變中文private static void MyContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip,Point mousePt, ZedGraphControl.ContextMenuObjectState objState){foreach (ToolStripMenuItem item in menuStrip.Items){switch (item.Name){case "copied_to_clip":item.Text = @"復(fù)制到剪貼板";break;case "copy":item.Text = @"復(fù)制";break;case "page_setup":item.Text = @"頁(yè)面設(shè)置...";break;case "print":item.Text = @"打印...";break;case "save_as":item.Text = @"另存圖表...";break;case "set_default":item.Text = @"恢復(fù)默認(rèn)大小";break;case "show_val":item.Text = @"顯示節(jié)點(diǎn)數(shù)值";break;case "title_def":item.Text = @"標(biāo)題";break;case "undo_all":item.Text = @"還原縮放/移動(dòng)";break;case "unpan":item.Text = @"還原移動(dòng)";break;case "unzoom":item.Text = @"還原縮放";break;case "x_title_def":item.Text = @"X 軸";break;case "y_title_def":item.Text = @"Y 軸";break;}}}} }?
總結(jié)
以上是生活随笔為你收集整理的Winform中实现ZedGraph中曲线右键显示为中文的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Winform中实现ZedGraph曲线
- 下一篇: Winform中实现ZedGraph曲线