Two years ago, I've written a post on how to call gnuplot from C++. It turns out the most viewed post of my posts. If you're missing it, click here.
Instead of using Foms of C#, I get used to using WPF. However, as we are talking about the C#, so it should work
Here we go -- How to call
I'll skip the basics of C# and assume that you all know how to start a project (here I use desktop WPF) or create a button click event.
1 At you design view, create a button click, all the coding we're going to do is just within this event. So it has the most flexibility for using elsewhere.
2 Preparation. Only two preparation we need:
b.
3 set up a new process and specify the path of gnuplot , using StreamWriter to communicate with gnuplot . I will give three examples here to show the usage.
3.1 Simple case: function plot:
Then debug it.private void Button_Click( object sender, RoutedEventArgs e)
{
ProcessplotProcess = new Process( );
plotProcess . StartInfo. FileName = @"D:\Project\VisualGnuplot\VisualGnuplot\bin\Debug\gnuplot\bin\gnuplot.exe";
plotProcess . StartInfo. UseShellExecute = false;
plotProcess . StartInfo. RedirectStandardInput = true;
plotProcess . Start( );
StreamWritersw =plotProcess . StandardInput;
StringstrInputText = "plot sin(x)\n";
sw . WriteLine( strInputText );
sw . Flush( );
MessageBox . Show( "Close thegnuplot Window? " );sw . Close( );}plotProcess . Close( );
3.2 normal case: plot external data file.
# X Y Z
0 1 2
0 2 4
0 3 8
0 4 16
0 5 32
1 1 5
1 2 3
1 3 5
1 4 3
1 5 5
2 1 32
2 2 16
2 3 8
2 4 4
2 5 2
3 1 3I
3 2 9
3 3 27
3 4 81
}private void Button_Click( object sender, RoutedEventArgs e)
{
ProcessplotProcess = new Process( );
plotProcess . StartInfo. FileName = @"D:\Project\VisualGnuplot\VisualGnuplot\bin\Debug\gnuplot\bin\gnuplot.exe";
plotProcess . StartInfo. UseShellExecute = false;
plotProcess . StartInfo. RedirectStandardInput = true;
plotProcess . Start( );
StreamWritersw =plotProcess . StandardInput;
//StringstrInputText = "plot sin(x)\n";
StringstrInputText = "splot \"3d.txt\" using 1:2:3 with lines\n";
sw . WriteLine( strInputText );
sw . Flush( );
MessageBox . Show( "Close thegnuplot Window? " );
sw . Close( );
plotProcess . Close( );
Now the result is:
I think now you
3.3 plot an external data file and with external plot setting file
set terminalpngset output "plot.png"
splot "3d.txt" using 1:2:3 with lines
private void Button_Click( object sender, RoutedEventArgs e)
{
ProcessplotProcess = new Process( );
plotProcess . StartInfo. FileName = @"D:\Project\VisualGnuplot\VisualGnuplot\bin\Debug\gnuplot\bin\gnuplot.exe";
plotProcess . StartInfo. UseShellExecute = false;
plotProcess . StartInfo. RedirectStandardInput = true;
plotProcess . Start( );
StreamWritersw =plotProcess . StandardInput;
//StringstrInputText = "plot sin(x)\n";
//StringstrInputText = "splot \"3d.txt\" using 1:2:3 with lines\n";
StringstrInputText ;
StreamReadersr = new StreamReader( "sp . plt ");
strInputText =sr . ReadToEnd( );
sr . Close( );
sw . WriteLine( strInputText );
sw . Flush( );
//MessageBox . Show( "Close thegnuplot Window? " );
sw . Close( );
plotProcess . Close( );
}
(
Summary:
Now we could use the
PS. I'm quite busy with my work so I didn't check the comments very often. But you can still leave your comments there that might be helpful for other readers or I will reply to it when I see it.
No comments:
Post a Comment