1
votes

I want to generate a couple of hundreds .txt scripts in a C# app, launch GnuPlot and generate .png graphs for each of the scripts I have.

I can launch GnuPlot from C# with the following code:

Process gnuPlotProcess = new Process();
gnuPlotProcess.StartInfo = new ProcessStartInfo(@"C:\Program Files\gnuplot\bin\wgnuplot.exe");
gnuPlotProcess.Start();

The first problem appears when I'm trying to change the current directory, adding this line of code before starting the process:

gnuPlotProcess.StartInfo.Arguments = "cd '" + scriptsPath + "'";

Now GnuPlot doesn't start.

The second problem, which I was able to test working on GnuPlot's default current directory, is when attempting to pass the "load 'script_xxx.txt'" command. Here is the complete code (inspiration from Plotting graph from Gunplot in C#):

Process gnuPlotProcess = new Process();
gnuPlotProcess.StartInfo = new ProcessStartInfo(@"C:\Program Files\gnuplot\bin\wgnuplot.exe");
gnuPlotProcess.StartInfo.RedirectStandardInput = true;
gnuPlotProcess.StartInfo.UseShellExecute = false;
theProcess.Start();
StreamWriter sw = gnuPlotProcess.StandardInput;
sw.WriteLine("load '" + pathToScript + "'");
sw.Flush();

The script that is found on pathToScript should create a .png file, and it works when being launched directly from gnuPlot. But from code, nothing happens.

Any help would be appreciated.

1

1 Answers

1
votes

I hope this code will help you:

        int N = 1000;                                    
        string dataFile = "data.txt";                  // one data file
        string gnuplotScript = "gnuplotScript.plt";    // gnuplot script
        string pngFile = "trajectory.png";             // output png file

        // init values
        double x = 0, y = 0;

        // random values to plot
        Random rnd = new Random();
        StreamWriter sw = new StreamWriter(dataFile);

        // US output standard
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");

        // generate data for visualisation       
        for (int i = 0; i < N; i++)
        {
            x += rnd.NextDouble() - 0.5;
            y += rnd.NextDouble() - 0.5;
            sw.WriteLine(x.ToString("F3") + "\t" + y.ToString("F3"));
        }
        sw.Close();

        // you can download it from file
        string gnuplot_script = "set encoding utf8\n" +
                                "set title \"Random trajectory\"\n" +
                                "set xlabel \"Coordinate X\"\n" +
                                "set ylabel \"Coordinate Y\"\n" +
                                "set term pngcairo size 1024,768 font \"Arial,14\"\n" +
                                "set output \"pngFile\"\n" +
                                "plot 'dataFile' w l notitle\n" +
                                "end";

        // change filenames in script
        gnuplot_script = gnuplot_script.Replace("dataFile", dataFile);
        gnuplot_script = gnuplot_script.Replace("pngFile", pngFile);

        // write sccript to file
        sw = new StreamWriter(gnuplotScript, false, new System.Text.UTF8Encoding(false));
        sw.WriteLine(gnuplot_script);
        sw.Close();

        // launch script
        ProcessStartInfo PSI = new ProcessStartInfo();
        PSI.FileName = gnuplotScript;
        string dir = Directory.GetCurrentDirectory();
        PSI.WorkingDirectory = dir;
        using (Process exeProcess = Process.Start(PSI))
        {
            exeProcess.WaitForExit();
        }

        // OPTION: launch deafault program to see file
        PSI.FileName = pngFile;
        using (Process exeProcess = Process.Start(PSI))
        {
        }

You can repeat this example with your own data as many times as you want to make many png files