图表隐藏与显示方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked==true)

// chart1.Series[0].IsVisibleInLegend = true;
// chart2.Series[0].IsVisibleInLegend = true;
chart1.Series[0].Enabled = true;
//by txwtech

}
else
{
// chart1.Series[0].IsVisibleInLegend = false;
chart1.Series[0].Enabled = false;
// chart1.Series[0].Points.Clear();
// chart2.Series[0].IsVisibleInLegend = false;
}
}

完整代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace draw_chart_practice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ToolTip toolTip = new ToolTip();
string postion11 = "";
string[] xx = new string []{"1月","2月", "3月","4月", "5月", "6月","7月", "8月", "9月", "10月", "11月", "12月" };

int[] yy = new int[] { 88,99,100, 88, 99, 100 , 88, 99, 100,5,3,5 };
int[] yy2 = new int[] { 8, 9, 10, 8, 9, 10, 18, 95, 105,2,5,7 };
// string[] yy3 = new string[] { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" };

private void modify()
{
// static void Main(string[] args)
{
DirectoryInfo directoryInfo = new DirectoryInfo(Application.StartupPath);
string path = directoryInfo.Parent.Parent.FullName + @"\Properties\AssemblyInfo.cs"; //上上级目录

// if (args.Length != 1 || !File.Exists(args[0])) return;
var lines = File.ReadAllLines(path);
for (int i = lines.Length - 1; i >= 0; i--)
{
if (lines[i].Contains("assembly: AssemblyFileVersion"))
{
UpdateVersion(ref lines[i]);
}
if (lines[i].Contains("assembly: AssemblyVersion") && !lines[i].Contains("*"))
{
UpdateVersion(ref lines[i]);
break;
}
}
File.WriteAllLines(path, lines);
}
}
public static void AddLogInfo_IP(string log_text)
{
try
{


// lock (Obj)
{


string data = log_text;

string strNow, strDate;

System.DateTime currentTime = new System.DateTime();
currentTime = System.DateTime.Now;
strNow = currentTime.ToString();//记录具体时间
strDate = currentTime.ToString("yyyyMMdd");
strDate = "\\\\172.17.50.89\\txw\\box_verification_ip\\" + strDate + "box_verification.txt"; //创建文件名
// strDate = "\
if (true)//
{
data = strNow + " " + data + "\r\n";
File.AppendAllText(strDate, data, Encoding.UTF8);

}
}
}
catch (IOException e)
{

MessageBox.Show("日志记录写入错误\r\n" + e.StackTrace + "\r\n" + e.Message);
}
catch (Exception ex)
{
MessageBox.Show("日志记录写入错误\r\n" + ex.StackTrace + "\r\n" + ex.Message);

}
}
public static string GetLocalIp()
{
///获取本地的IP地址
string AddressIP = string.Empty;
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
//if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
{
// AddressIP = _IPAddress.ToString();
}
AddLogInfo_IP(_IPAddress.ToString());
}
return AddressIP;
}
private void button1_Click(object sender, EventArgs e)
{
//GetLocalIp();
// modify();
//chart1.Series[0].Points.AddXY(1, 168);
//chart1.Series[0].Points.AddXY(3, 122);
//chart1.Series[0].Points.AddXY(4, 153);
//chart1.Series[0].Points.AddXY(5, 172);
//chart1.Series[0].Points.AddXY(6, 100);
//chart1.Series[0].Points.AddXY(100, 120);
//chart1.Series[1].Points.AddXY(2, 2);
//chart1.Series[1].Points.AddXY(4, 6);
xx[0] = System.DateTime.Now.ToString();
xx[1] = System.DateTime.Now.ToString();
xx[2] = System.DateTime.Now.ToString();
xx[3] = System.DateTime.Now.ToString();
xx[4] = System.DateTime.Now.ToString();
xx[5] = System.DateTime.Now.ToString();
chart1.Series[0].Points.DataBindXY(xx,yy);
chart1.Series[1].Points.DataBindXY(xx, yy2);
chart2.Series[0].Points.DataBindXY(xx, yy);
// chart1.Series[0].Label = "#VAL";//每个点的值显示
chart1.Series[0].ToolTip = "#VALX:#VAL";
chart1.Series[1].ToolTip = "#VALX:#VAL";

if (checkBox1.Checked == true)
{
chart1.Series[0].IsVisibleInLegend = true;
chart1.Series[0].Enabled = true;
}
else
{
chart1.Series[0].IsVisibleInLegend = false;
chart1.Series[0].Enabled = false;
// chart1.Series[0].Points.Clear();
}
if (checkBox2.Checked == true)
{
chart1.Series[1].IsVisibleInLegend = true;
}
else
{
chart1.Series[1].IsVisibleInLegend = false;
//chart1.Series[1].Points.Clear();
}
InitChart(chart1);


}
static void UpdateVersion(ref string line)
{
var first = line.IndexOf('"');
var second = line.LastIndexOf('"');
var sVersion = line.Substring(first + 1, second - first - 1);
var arrVersion = sVersion.Split('.');
if (arrVersion.Length < 4) return;
var major = Convert.ToInt32(arrVersion[0]);
var minor = Convert.ToInt32(arrVersion[1]);
var build = Convert.ToInt32(arrVersion[2]);
var amendment = Convert.ToInt32(arrVersion[3]);
if (++amendment > 999) ++build;
if (build > 99) ++minor;
if (minor > 9) ++major;
var sNewVersion = $"{major}.{minor}.{build}.{amendment}";
line = line.Replace(sVersion, sNewVersion);
}


private 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(chart1_MouseClick);
}

private void chart1_MouseMove(object sender, MouseEventArgs e)
{
var area = chart1.ChartAreas[0];
// var area = chart1.Series[0];

// double xvalue = area.AxisX.PixelPositionToValue(e.X);

// double yvalue = area.AxisY.PixelPositionToValue(e.Y);

// textbox1.text = string.Format("{0:f0},{1:f0}", xvalue, yvalue);
// postion11=(string.Format("{0:f0},{1:f0}", xvalue, yvalue));


}

private void chart1_MouseHover(object sender, EventArgs e)
{
var area = chart1.ChartAreas[0];

// double xvalue = area.AxisX.PixelPositionToValue(e.X);

// double yvalue = area.AxisY.PixelPositionToValue(e.Y);

// textbox1.text = string.Format("{0:f0},{1:f0}", xvalue, yvalue);
// MessageBox.Show(string.Format("{0:f0},{1:f0}", xvalue, yvalue));
// toolTip.SetToolTip(chart1, postion11);
}

// private void chart1_Click(object sender, EventArgs e)
// {

// }

private void chart1_MouseClick(object sender, MouseEventArgs e)
{
Chart chart1 = sender as Chart;

// string aa = chart1.Series[0].ToolTip = "#VALX:#VAL";
// MessageBox.Show(aa);
if (e.Button == MouseButtons.Right)
{
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(0);
}
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked==true)
{
// chart1.Series[0].IsVisibleInLegend = true;
// chart2.Series[0].IsVisibleInLegend = true;

chart1.Series[0].Enabled = true;

//by txwtech
}
else
{
// chart1.Series[0].IsVisibleInLegend = false;
chart1.Series[0].Enabled = false;
// chart1.Series[0].Points.Clear();
// chart2.Series[0].IsVisibleInLegend = false;
}
}

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == true)
{
chart1.Series[1].IsVisibleInLegend = true;
}
else
{
chart1.Series[1].IsVisibleInLegend = false;
//chart1.Series[1].Points.Clear();
}
}
}

}

相关链接(侵删)

  1. c#ms chart图表隐藏与显示某一系列曲线方法vs2019

=================我是分割线=================

欢迎到公众号来唠嗑: