Form1.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using System.Windows.Forms.DataVisualization.Charting;
  9. namespace ChartTest
  10. {
  11. public partial class Form1 : Form
  12. {
  13. private static bool isover = false;
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. isover = false;
  21. Task.Factory.StartNew(() =>
  22. {
  23. while (!isover)
  24. {
  25. this.Invoke(new Action(() =>
  26. {
  27. List<string> origindata = ReadLog();
  28. System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  29. sw.Start();
  30. List<double> listx = new List<double>();
  31. List<double> listy = new List<double>();
  32. int startDegree = (Convert.ToInt32(this.textBox1.Text) + 45) * 3;
  33. int endDegree = (Convert.ToInt32(this.textBox2.Text) + 45) * 3 + 1;
  34. List<int> origindataTransformed = new List<int>();
  35. origindataTransformed = origindata.ConvertAll<int>(x => Convert.ToInt32(x));
  36. for (int i = startDegree; i < endDegree; i++)
  37. {
  38. double x = origindataTransformed[i] * Math.Cos((-45 + 0.333 * i) * Math.PI / 180);
  39. double y = origindataTransformed[i] * Math.Sin((-45 + 0.333 * i) * Math.PI / 180);
  40. listx.Add(x);
  41. listy.Add(y);
  42. }
  43. this.chart1.Series[0].Points.Clear();
  44. InitChart(listx,listy);
  45. sw.Stop();
  46. Console.WriteLine(sw.ElapsedMilliseconds + "ms");
  47. }));
  48. Thread.Sleep(5000);
  49. }
  50. });
  51. //List<string> origindata = ReadLog();
  52. //List<int> origindataTransformed = new List<int>();
  53. //origindataTransformed = origindata.ConvertAll<int>(x => Convert.ToInt32(x));
  54. //for(int i = 0; i < origindataTransformed.Count; i++)
  55. //{
  56. // double x = origindataTransformed[i] * Math.Cos((-45 + 0.333 * i) * Math.PI / 180);
  57. // double y = origindataTransformed[i] * Math.Sin((-45 + 0.333 * i) * Math.PI / 180);
  58. // listx.Add(x);
  59. // listy.Add(y);
  60. //}
  61. //InitChart();
  62. }
  63. /// <summary>
  64. /// 初始化图表
  65. /// </summary>
  66. private void InitChart(List<double> listx, List<double> listy)
  67. {
  68. //定义图表区域
  69. this.chart1.ChartAreas.Clear();
  70. ChartArea chartArea1 = new ChartArea("C1");
  71. this.chart1.ChartAreas.Add(chartArea1);
  72. //定义存储和显示点的容器
  73. this.chart1.Series.Clear();
  74. Series series1 = new Series("S1");
  75. series1.ChartArea = "C1";
  76. this.chart1.Series.Add(series1);
  77. //设置图表显示样式
  78. //this.chart1.ChartAreas[0].AxisY.Minimum = 0;
  79. //this.chart1.ChartAreas[0].AxisY.Maximum = 100;
  80. //this.chart1.ChartAreas[0].AxisX.Interval = 5;
  81. this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
  82. this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
  83. //设置标题
  84. this.chart1.Titles.Clear();
  85. this.chart1.Titles.Add("S01");
  86. this.chart1.Titles[0].Text = "雷达显示";
  87. this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
  88. this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
  89. //设置图表显示样式
  90. this.chart1.Series[0].Color = Color.Red;
  91. this.chart1.Series[0].ChartType = SeriesChartType.Line;
  92. for (int i = 0; i < listx.Count; i++)
  93. {
  94. this.chart1.Series[0].Points.AddXY(listx[i], listy[i]);
  95. }
  96. }
  97. private static List<string> ReadLog()
  98. {
  99. List<string> digitList = new List<string>();
  100. string filePath = "log//data0.txt";
  101. try
  102. {
  103. List<string> logLines = null;
  104. if (File.Exists(filePath))
  105. {
  106. logLines = new List<string>(File.ReadAllLines(filePath));
  107. }
  108. if (logLines != null)
  109. {
  110. for (int i = 0; i < logLines.Count; i++)
  111. {
  112. string logLine = logLines[i];
  113. digitList.Add(logLine);
  114. }
  115. }
  116. }
  117. catch (Exception e) { Console.WriteLine(e.Message); }
  118. return digitList;
  119. }
  120. private void button2_Click(object sender, EventArgs e)
  121. {
  122. isover = true;
  123. }
  124. }
  125. }