TeeChart 的使用从入门到精通

发布时间 2024-01-07 16:28:29作者: LowKeyC

1.首先nutGet 进行使用

2.如果需要使用管方的Key 进行激活

3.直接上写的Demo代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using Steema.TeeChart;
 11 using Steema.TeeChart.Styles;
 12 
 13 namespace TeechartForm
 14 {
 15     public partial class FormTeeChart : Form
 16     {
 17         private TChart tChart;
 18         private Label labelValue = new Label();
 19         public FormTeeChart()
 20         {
 21             InitializeComponent();
 22             // 在构造函数中初始化TChart  
 23             tChart = new TChart();
 24             //tChart.Parent = this.panelCharts;
 25             tChart.Dock = DockStyle.Fill;
 26             this.panelCharts.Controls.Add(tChart);
 27 
 28             // 初始化 labelValue
 29             //labelValue.AutoSize = true;
 30             //labelValue.BackColor = Color.Transparent;
 31             //labelValue.Visible = false; // 默认情况下不可见
 32             //this.Controls.Add(labelValue);
 33 
 34             //tChart.MouseMove += TChart_MouseMove;
 35         }
 36 
 37         private void FormTeeChart_Load(object sender, EventArgs e)
 38         {
 39 
 40         }
 41 
 42         // 添加一个标志来跟踪鼠标是否被按下
 43         private bool isMouseDown = false;
 44 
 45 
 46 
 47         private bool mOnBoxing = false;
 48         Point mPoint1 = new Point();
 49         Point mPoint2 = new Point();
 50         private void TChart_MouseHover(object sender, MouseEventArgs e)
 51         {
 52             MessageBox.Show("1");
 53             //labelValue.Text = "111";
 54             //labelValue.Visible = true; 
 55 
 56             TChart chart = (TChart)sender;
 57             if (Control.ModifierKeys == Keys.None)
 58             {
 59                 mPoint2 = e.Location;
 60 
 61                 chart.Invalidate();
 62             }
 63         }
 64 
 65 
 66         private void buttonLine_Click(object sender, EventArgs e)
 67         {
 68             //条形图
 69             BarChart();
 70 
 71         }
 72 
 73         private void buttonBoxPlot_Click(object sender, EventArgs e)
 74         {
 75             BoxPlot();
 76             //tChart.MouseMove += TChart_MouseMove;
 77         }
 78 
 79 
 80         /// <summary>
 81         /// 进行获取Tcharts 鼠标移动的位置
 82         /// </summary>
 83         /// <param name="sender"></param>
 84         /// <param name="e"></param>
 85         private void TChart_MouseMove(object sender, MouseEventArgs e)
 86         {
 87             // 获取鼠标在图表上的位置
 88             int x = e.X;
 89             int y = e.Y;
 90 
 91             Series s = tChart.Series[0];
 92             int pointIndex = s.Clicked(e.X, e.Y);
 93             if (pointIndex != -1)
 94             {
 95                 // 获取数据点的值
 96                 double value = s.YValues[pointIndex];
 97 
 98                 // 设置标签文本
 99                 labelValue.Text = "Value: " + value.ToString();
100 
101                 // 移动标签位置并显示
102                 labelValue.Left = x + 10; // 鼠标位置的偏移量
103                 labelValue.Top = y;
104                 labelValue.Visible = true;
105             }
106             else
107             {
108                 // 如果不在任何数据点上,则不显示标签 
109                 //labelValue.Visible = false;
110             }
111         }
112 
113         private void GantMap_Click(object sender, EventArgs e)
114         {
115             Gant();
116         }
117 
118         private void LineMap_Click(object sender, EventArgs e)
119         {
120             LineChart();
121         }
122 
123 
124         /// <summary>
125         /// 折线图
126         /// </summary>
127         public void LineChart()
128         {
129             // 清除现有的系列
130             tChart.Series.Clear();
131 
132             // 创建一个折线图系列
133             Line lineSeries = new Line(tChart.Chart);
134 
135             // 添加数据到图表
136             lineSeries.Add(0, "Apples");
137             lineSeries.Add(5, "Pears");
138             lineSeries.Add(2, "Oranges");
139             lineSeries.Add(7, "Bananas");
140             lineSeries.Add(4, "Pineapples");
141 
142             // 将系列添加到TChart
143             tChart.Series.Add(lineSeries);
144 
145             // 鼠标移动事件
146             tChart.MouseMove += (s, e) =>
147             {
148                 int pointIndex = -1;
149                 // 遍历所有系列
150                 foreach (Series series in tChart.Series)
151                 {
152                     // 尝试找到鼠标下的数据点
153                     pointIndex = series.Clicked(e.X, e.Y);
154                     if (pointIndex != -1)
155                     {
156                         // 如果找到数据点,执行所需的操作
157                         string pointLabel = series.Labels[pointIndex];
158                         double pointValue = series.YValues[pointIndex];
159                         tChart.ClickTitle += TChart_ClickTitle;
160                         //tChart.ClickTitle + = "Value: " + pointValue.ToString() + " for " + pointLabel;
161 
162                         break; // 跳出循环,因为我们已经找到了数据点
163                     }
164                     else
165                     {
166                         tChart.Header.Text = "";
167                     }
168                 }
169             };
170 
171             // 其他代码保持不变...
172 
173             // 设置图表标题
174             tChart.Header.Text = "Fruit Consumption";
175             tChart.Header.Visible = true;
176             tChart.Refresh();
177             tChart.Invalidate(); // 可能不需要手动调用 
178         }
179 
180         private void TChart_ClickTitle(object sender, MouseEventArgs e)
181         {
182             MessageBox.Show("1111");
183         }
184 
185 
186         /// <summary>
187         /// 条形图
188         /// </summary>
189         public void BarChart()
190         {
191             // 清除现有的系列
192             tChart.Series.Clear();
193             // 创建一个条形图系列
194             Bar barSeries = new Bar();
195             // 添加数据到图表
196             barSeries.Add(0, "Apples");
197             barSeries.Add(5, "Pears");
198             barSeries.Add(2, "Oranges");
199             barSeries.Add(7, "Bananas");
200             barSeries.Add(4, "Pineapples");
201 
202             // 将系列添加到TChart
203             tChart.Series.Add(barSeries);
204 
205 
206             // 鼠标按下事件
207             //tChart.MouseMove += (s, e) =>
208             //{
209             //    if (e.Button == MouseButtons.Left)
210             //    {
211             //        isMouseDown = true;  // 设置标志为true
212             //        MessageBox.Show("11");
213             //    }
214             //};
215 
216             // 在类的成员区域初始化一个工具提示
217             System.Windows.Forms.ToolTip tooltip = new System.Windows.Forms.ToolTip();
218 
219             // 鼠标释放事件
220             tChart.MouseMove += (s, e) =>
221             {
222                 if (e.Button == MouseButtons.Left)
223                 {
224                     int pointIndex = -1;
225                     // 遍历所有系列
226                     foreach (Series series in tChart.Series)
227                     {
228                         // 尝试找到点击的数据点
229                         pointIndex = series.Clicked(e.X, e.Y);
230 
231                         if (pointIndex != -1)
232                         {
233                             // 如果找到数据点,执行所需的操作
234                             string pointLabel = series.Labels[pointIndex];
235                             double pointValue = series.YValues[pointIndex];
236                             //MessageBox.Show("Clicked on: " + pointLabel + " Value: " + pointValue.ToString());
237 
238                             // 显示工具提示
239                             tooltip.Show("Value: " + pointValue.ToString() + " for " + pointLabel, tChart, e.Location.X + 15, e.Location.Y - 15, 2000); // 显示2秒
240 
241                             break; // 跳出循环,因为我们已经找到了数据点
242                         }
243                     }
244                 }
245             };
246 
247 
248             // 绑定鼠标点击事件处理程序到 tChart 控件
249             //tChart.MouseClick += (s, e) =>
250             //{
251             //    if (e.Button == MouseButtons.Left)
252             //    {
253             //        int pointIndex = -1;
254             //        // 遍历所有系列
255             //        foreach (Series series in tChart.Series)
256             //        {
257             //            // 尝试找到点击的数据点
258             //            pointIndex = series.Clicked(e.X, e.Y);
259 
260             //            if (pointIndex != -1)
261             //            {
262             //                // 如果找到数据点,执行所需的操作
263             //                string pointLabel = series.Labels[pointIndex];
264             //                double pointValue = series.YValues[pointIndex];
265             //                MessageBox.Show("Clicked on: " + pointLabel + " Value: " + pointValue.ToString());
266             //                break; // 跳出循环,因为我们已经找到了数据点
267             //            }
268             //        }
269             //    }
270             //};
271 
272             // 其他代码保持不变... 
273             // 其他代码保持不变...
274             // 在类的成员区域初始化一个工具提示
275             /*System.Windows.Forms.ToolTip tooltip = new System.Windows.Forms.ToolTip();
276             // 鼠标移动事件
277             chart.MouseMove += (s, e) =>
278             {
279                 int pointIndex = -1;
280                 // 遍历所有系列
281                 foreach (Series series in chart.Series)
282                 {
283                     Point mPointss = new Point();
284                     // 尝试找到鼠标下的数据点
285                     pointIndex = series.Clicked(e.X, e.Y);
286 
287                     if (pointIndex != -1)
288                     {
289                         // 如果找到数据点,执行所需的操作
290                         string pointLabel = series.Labels[pointIndex];
291                         double pointValue = series.YValues[pointIndex];
292                         // 显示工具提示
293                         tooltip.Show("Value: " + pointValue.ToString() + " for " + pointLabel, chart, e.Location.X + 15, e.Location.Y - 15, 2000); // 显示2秒
294                         // chart.Invalidate();
295                         break; // 跳出循环,因为我们已经找到了数据点
296                     }
297 
298                 }
299             };
300             */
301 
302             // 设置图表标题
303             tChart.Header.Text = "Fruit Consumption";
304             tChart.Header.Visible = true;
305             tChart.Refresh();
306             tChart.Invalidate();
307 
308 
309         }
310 
311         /// <summary>
312         /// BoxPlot
313         /// </summary>
314         public void BoxPlot()
315         {
316 
317             // 清除现有的系列
318             tChart.Series.Clear();
319 
320             // 创建一个 BoxPlot 系列
321             Box boxSeries = new Box(tChart.Chart);
322 
323             // 填充 BoxPlot 数据
324             boxSeries.Add(new double[] { 3, 5, 7, 2, 6 });
325             boxSeries.Add(new double[] { 4, 6, 8, 3, 7 });
326             boxSeries.Add(new double[] { 5, 7, 9, 4, 8 });
327             boxSeries.Add(new double[] { 5, 7, 9, 4, 8 });
328             //boxSeries.Add(new double[] { 15, 17, 19, 14, 18 });
329             // ... 添加更多的数据点 ...
330 
331             // 设置标题
332             tChart.Header.Text = "Sample BoxPlot";
333             tChart.Header.Visible = true;
334 
335             // 将 BoxPlot 系列添加到 TChart
336             tChart.Series.Add(boxSeries);
337             tChart.Refresh();
338             tChart.Invalidate();
339         }
340 
341 
342         /// <summary>
343         /// Gant
344         /// </summary>
345         public void Gant()
346         {
347             // 清除现有的系列
348             tChart.Series.Clear();
349 
350             // 创建一个甘特图系列
351             Gantt ganttSeries = new Gantt();
352 
353             // 添加数据到图表,每个点的格式为 Add(DateTime start, DateTime end, string text, Color color)
354             ganttSeries.Add(DateTime.Today.AddDays(1), DateTime.Today.AddDays(12), 100, "Project B", Color.Green);
355             ganttSeries.Add(DateTime.Today.AddDays(3), DateTime.Today.AddDays(15), 200, "Project C", Color.Red);
356             // ... 添加更多的数据 ...
357 
358             // 将系列添加到TChart
359             tChart.Series.Add(ganttSeries);
360 
361             // 设置X轴为日期时间类型
362             tChart.Axes.Bottom.Labels.Angle = 90;
363             tChart.Axes.Bottom.Labels.DateTimeFormat = "dd-MMM";
364             tChart.Axes.Bottom.Labels.Style = AxisLabelStyle.Value;
365 
366             // 设置Y轴为分类类型,便于显示任务名称
367             tChart.Axes.Left.Labels.Angle = 0;
368             tChart.Axes.Left.Labels.Style = AxisLabelStyle.Text;
369 
370             // 设置图表标题
371             tChart.Header.Text = "Project Timeline";
372             tChart.Header.Visible = true;
373 
374             // 刷新图表显示
375             tChart.Refresh();
376             tChart.Invalidate();
377 
378         }
379 
380     }
381 }