c#?chart縮放,局部放大問題
更新時(shí)間:2023年03月01日 09:34:54 作者:幻世頑靈
這篇文章主要介紹了c#?chart縮放,局部放大問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
c# chart縮放,局部放大
效果:
左鍵劃選放大區(qū)域,右鍵恢復(fù)
? ? ? ? /// <summary>
? ? ? ? /// 初始化,傳入要進(jìn)行初始化的chart
? ? ? ? /// </summary>
? ? ? ? /// <param name="chart1"></param>
? ? ? ? public static void InitChart (System.Windows.Forms.DataVisualization.Charting.Chart chart1)
? ? ? ? {
? ? ? ? ? ? //開啟縮放功能
? ? ? ? ? ? chart1.ChartAreas[0].CursorX.Interval = 0;
? ? ? ? ? ? chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
? ? ? ? ? ? chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
? ? ? ? ? ? chart1.MouseClick += new System.Windows.Forms.MouseEventHandler(chart_MouseClick);
? ? ? ? }
?
? ? ? ? //右鍵恢復(fù)縮放
? ? ? ? static void chart_MouseClick(object sender, MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? Chart chart1 = sender as Chart;
? ? ? ? ? ? //右鍵恢復(fù)事件
? ? ? ? ? ? if (e.Button == MouseButtons.Right)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(0);
? ? ? ? ? ? }
? ? ? ? }放大
僅針對x軸(y軸同理)
chartArea1.CursorX.IsUserEnabled = true; chartArea1.CursorX.IsUserSelectionEnabled = true;
縮小
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
ZoomReset(0);—— 撤銷所有放大動作ZoomReset(1);—— 撤銷上一次放大動作
設(shè)置滾動條寬度
chart1.ChartAreas[0].AxisX.ScrollBar.Size = 5;
以上所有方法也可以在chart屬性里直接進(jìn)行設(shè)置
獲取選區(qū)坐標(biāo)
Console.WriteLine(chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum);//當(dāng)前顯示范圍最小坐標(biāo) Console.WriteLine(chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum);//當(dāng)前顯示范圍最大坐標(biāo)
c# chart表格
- series1屬性中XAxisType屬性值設(shè)置為:Primary
- series2屬性中XAxisType屬性值設(shè)置為:Secondary
添加series會導(dǎo)致圖表預(yù)覽不可用

設(shè)置間隔與小數(shù)點(diǎn)

網(wǎng)格刻度
#region 表格參數(shù)設(shè)置
//ChartArea chartArea = chart1.ChartAreas[0];
//表格標(biāo)題內(nèi)容
Title title = new Title();
title.Font = new System.Drawing.Font("宋體", 12F);
title.Text = "壓機(jī)曲線波動分析";
chart1.Titles.Add(title);
//設(shè)置坐標(biāo)軸標(biāo)題
chart1.ChartAreas[0].AxisX.Title = "位 移 / mm ";
chart1.ChartAreas[0].AxisY.Title = "壓 力 / Kg ";
//X,Y軸的最大值最小值
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 100;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 200;
// 設(shè)置X,Y的坐標(biāo)間距
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisY.Interval = 5;
//設(shè)置坐標(biāo)軸標(biāo)題的字體
chart1.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("宋體", 12F);
chart1.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("宋體", 12F);
//設(shè)置坐標(biāo)軸柵格是否可見
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
//設(shè)置網(wǎng)格線
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Gainsboro; //顏色
chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 10; //網(wǎng)格間隔
chart1.ChartAreas[0].AxisX.MinorGrid.Interval = 10;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Gainsboro;
chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 20;
chart1.ChartAreas[0].AxisY.MinorGrid.Interval = 20;
放大表格
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(2, 3);
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false;
chart1.ChartAreas[0].AxisX.ScaleView.Size = 20;
Zoom into the X axis
Enable range selection and zooming end user interface
chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
//將滾動內(nèi)嵌到坐標(biāo)軸中
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].AxisY.ScrollBar.IsPositionedInside = true;
#endregion
#region 各個(gè)曲線參數(shù)設(shè)置
//曲線類型
chart1.Series[0].ChartType = SeriesChartType.Spline;
chart1.Series[1].ChartType = SeriesChartType.Spline;
chart1.Series[10].ChartType = SeriesChartType.Spline;
// 曲線 線寬像素
chart1.Series[0].BorderWidth = 1;
chart1.Series[1].BorderWidth = 1;
chart1.Series[10].BorderWidth = 3;
// 曲線的顏色
chart1.Series[10].Color = System.Drawing.Color.Red;
chart1.Series[4].Color = System.Drawing.Color.Green;
chart1.Series[5].Color = System.Drawing.Color.RoyalBlue;
//右上角的曲線名稱是否顯示
chart1.Series[0].IsVisibleInLegend = true;
chart1.Series[1].IsVisibleInLegend = true;
chart1.Series[10].IsVisibleInLegend = true;
//右上角的曲線名稱
chart1.Series[0].LegendText = "隨機(jī)抽取曲線1";
chart1.Series[1].LegendText = "隨機(jī)抽取曲線2";
chart1.Series[2].LegendText = "隨機(jī)抽取曲線3";
chart1.Series[3].LegendText = "隨機(jī)抽取曲線4";
chart1.Series[4].LegendText = "隨機(jī)抽取曲線5";
chart1.Series[5].LegendText = "隨機(jī)抽取曲線6";
chart1.Series[6].LegendText = "隨機(jī)抽取曲線7";
chart1.Series[7].LegendText = "隨機(jī)抽取曲線8";
chart1.Series[8].LegendText = "隨機(jī)抽取曲線9";
chart1.Series[9].LegendText = "隨機(jī)抽取曲線10";
chart1.Series[10].LegendText = "Average";
//名稱的懸浮備注
chart1.Series[10].LegendToolTip = "此乃10個(gè)隨機(jī)曲線的平均值曲線";
chart1.Series[0].LegendToolTip = "隨機(jī)抽取曲線1備注";
chart1.Series[1].LegendToolTip = "備注2";
//坐標(biāo)Y值是否顯示在圖表中
//chart1.Series[0].IsValueShownAsLabel = true;
//chart1.Series[1].IsValueShownAsLabel = true;
//chart1.Series[2].IsValueShownAsLabel = true;
//chart1.Series[3].IsValueShownAsLabel = true;
//chart1.Series[4].IsValueShownAsLabel = true;
chart1.Series[10].IsValueShownAsLabel = true;
//chart1.Series.Clear(); 使表格GGG
//chart1.Series.Dispose();
#endregion //設(shè)置網(wǎng)格線
chartDemo1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Gray;
chartDemo1.ChartAreas[0].AxisX.MajorGrid.Interval = 500;//網(wǎng)格間隔
chartDemo1.ChartAreas[0].AxisX.MinorGrid.Interval = 500;
chartDemo1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash; //設(shè)置網(wǎng)格類型為虛線
chartDemo1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;
chartDemo1.ChartAreas[0].AxisY.MajorGrid.Interval = 4;
chartDemo1.ChartAreas[0].AxisY.MinorGrid.Interval = 4;
chartDemo1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;//網(wǎng)格Y軸線類型
this.chartDemo1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Transparent;
// this.chartDemo1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Transparent;//滑輪設(shè)置
chartDemo1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
chartDemo1.ChartAreas[0].AxisX.ScaleView.Size = 8;
chartDemo1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chartDemo1.ChartAreas[0].AxisX.ScrollBar.Enabled = true; private void chart1_MouseEnter(object sender, MouseEventArgs e)
{
if (e.Delta > 0)//鼠標(biāo)向上
{
if (chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Size < 100)//判斷顯示的最大數(shù)值
{
// chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Size += 2;//+=5---滾動一次顯示5個(gè)
chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Position += 2;
}
}
else//鼠標(biāo)向下滾動
{
if (chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Size > 1)
{
// chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Size -= 2;// - = 5---滾動一次減小顯示5個(gè)
chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Position -= 2;
}
}
}
private void chart1_MouseEnter(object sender, EventArgs e)//當(dāng)鼠標(biāo)移動到控件上-發(fā)生的事件
{
MouseWheel += new MouseEventHandler(chart1_MouseEnter);//調(diào)用滾輪事件
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C# Winform實(shí)現(xiàn)自定義漂亮的通知效果
這篇文章主要介紹了C# Winform實(shí)現(xiàn)自定義漂亮的通知效果,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法
AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法,需要的朋友可以參考一下2013-03-03
c# winform 關(guān)閉窗體時(shí)同時(shí)結(jié)束線程實(shí)現(xiàn)思路
th.IsBackground = true解決線程問題,意思就是把線程設(shè)置為后臺線程,感興趣的朋友可以多了解下,如何有什么妙招還請多多指導(dǎo)哈2013-02-02
c#高效率導(dǎo)出多維表頭excel的實(shí)例代碼
這篇文章介紹了c#高效率導(dǎo)出多維表頭excel的實(shí)例代碼,有需要的朋友可以參考一下2013-11-11

