• c#用Gnuplot画图源码


    直接调用这个类即可,需要下载个GnuPlot安装下。

    1. // Author: Leonardo Tazzini
    2. using System;
    3. using System.Diagnostics;
    4. using System.Drawing;
    5. using System.IO;
    6. using System.Windows.Forms;
    7. /// <summary>
    8. /// Tested with Gnuplot 5.2
    9. /// </summary>
    10. public class GnuPlot
    11. {
    12. public GnuPlot()
    13. {
    14. // Default path for gnuplot
    15. this.Path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "gnuplot\\bin\\gnuplot.exe");
    16. // TODO: Check if path is valid
    17. }
    18. public GnuPlot(string path)
    19. {
    20. this.Path = path;
    21. }
    22. /// <summary>
    23. /// Path of gnuplot.exe
    24. /// </summary>
    25. public string Path { get; set; }
    26. /// <summary>
    27. /// Graph input CSV with GnuPlot. NumWaves is the number of channels to draw. If imageOut a PNG image is saved into out.png and displayed.
    28. /// </summary>
    29. public void DrawGraph(string csvFileName, int numWaves, bool generatePngImage = true)
    30. {
    31. // gnuplot.exe not found
    32. if(!File.Exists(Path))
    33. {
    34. MessageBox.Show("gnuplot.exe not found. Please check the path.");
    35. return;
    36. }
    37. var gnuPlot = new Process();
    38. gnuPlot.StartInfo.FileName = Path;
    39. gnuPlot.StartInfo.CreateNoWindow = true;
    40. gnuPlot.StartInfo.UseShellExecute = false;
    41. gnuPlot.StartInfo.RedirectStandardInput = true;
    42. gnuPlot.Start();
    43. StreamWriter gnuPlotSw = gnuPlot.StandardInput;
    44. if (generatePngImage)
    45. {
    46. // TODO: Remove dependency from Windows Forms
    47. Rectangle bounds = Screen.PrimaryScreen.WorkingArea;
    48. gnuPlotSw.WriteLine("set terminal png size " + (bounds.Width * 0.9) + "," + (bounds.Height * 0.8));
    49. gnuPlotSw.WriteLine("set out 'out.png'");
    50. }
    51. gnuPlotSw.WriteLine("set style data lines"); //linespoints
    52. gnuPlotSw.WriteLine("set datafile separator ';'");
    53. //gnuPlotSw.WriteLine("set decimalsign locale; set decimalsign ','");
    54. gnuPlotSw.WriteLine("set xlabel 'time (s)'");
    55. gnuPlotSw.WriteLine("set ylabel 'value (v)'");
    56. gnuPlotSw.WriteLine("set grid");
    57. //gnuPlotSw.WriteLine("set xtics 0,0.5");
    58. //gnuPlotSw.WriteLine("set ytics 0,5");
    59. // TODO: evaluate add "every .." to gnuplot cmd line
    60. gnuPlotSw.Write("plot '" + System.IO.Path.GetFullPath(csvFileName) + "' using 1:2 title 'CH1'");
    61. if (numWaves == 2)
    62. gnuPlotSw.WriteLine(", '' using 1:3 title 'CH2'");
    63. else
    64. gnuPlotSw.Write("\n");
    65. gnuPlotSw.Flush();
    66. if (generatePngImage)
    67. {
    68. gnuPlotSw.WriteLine("exit");
    69. gnuPlotSw.Flush();
    70. gnuPlot.WaitForExit();
    71. Process.Start("out.png");
    72. }
    73. gnuPlot.Close();
    74. }
    75. }

    读吧数据存为csv,传入DrawGraph即可;数据如下:

    TIME(s);CH1(v);
    -600.99999949;1.5001;
    -599.99999949;-0.0598999999999998;
    -598.99999949;-0.0398999999999998;
    -597.99999949;-0.0398999999999998;
    -596.99999949;-0.0398999999999998;
    -595.99999949;-0.0398999999999998;
    -594.99999949;-0.0398999999999998;
    -593.99999949;-0.0398999999999998;
    -592.99999949;-0.0598999999999998;
    -591.99999949;-0.0398999999999998;
    -590.99999949;-0.0398999999999998;
    -589.99999949;-0.0598999999999998;
    -588.99999949;-0.0398999999999998;
    -587.99999949;-0.0398999999999998;
    -586.99999949;-0.0398999999999998;
    -585.99999949;-0.0598999999999998;

    显示示意:

  • 相关阅读:
    洛谷P2440 木材加工 —二分答案
    【Nginx】nginx | 微信小程序验证域名配置
    Linux命令之systemctl命令
    JavaScript基础 JavaScript第三天 1. for - 循环 && 2. 数组
    内网渗透-常用反弹shell方法总结
    java springboot 实现 对象或对象数组 转为 前端可解析的JSON字符串格式
    跨数据中心Multi-Fabric解决方案:L2和L3网络的高效连接和扩展
    ROS系统通过类定义实现数据处理并重新发布在另一话题
    【PG】PostgreSQL单机部署(简洁命令版)
    Python自动操作电脑|pywin32
  • 原文地址:https://blog.csdn.net/HETONGDE/article/details/133076489