Skip to content

Commit 4fa99a0

Browse files
committed
Initial commit.
1 parent 68c2c1c commit 4fa99a0

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
# gnuplot-cpp
1+
# gnuplot-cpp
2+
3+
A trivial gnuplot interface for c++.
4+
5+
There are [many](https://github.com/search?l=C%2B%2B&q=gnuplot&type=Repositories&utf8=%E2%9C%93) libraries providing the same solution, such as [gnuplot-iostream](https://github.com/dstahlke/gnuplot-iostream). _gnuplot-cpp_ aims at being lightweight and super easy to use. It functions by piping data to a gnuplot subprocess.
6+
7+
## Features
8+
* Header only
9+
* Lightweight (~50 lines of code)
10+
* Easy to use
11+
12+
## Example usage
13+
14+
```
15+
#include "gnuplot.h"
16+
17+
int main(){
18+
GnuplotPipe gp;
19+
gp.sendLine("plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))");
20+
return 0;
21+
}
22+
```
23+
24+
Result:
25+
26+
![Example plot](example/example.jpg)

example/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Debug/
2+
Default/
3+
Release/
4+
build/
5+
CMakeLists.txt.user

example/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
project (gnuplot_cpp_example)
3+
set (CMAKE_CXX_STANDARD 11)
4+
5+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
6+
add_executable(gnuplot_cpp_example example.cpp ../include/gnuplot.h)

example/example.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "gnuplot.h"
2+
3+
int main(){
4+
GnuplotPipe gp;
5+
gp.sendLine("plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))");
6+
return 0;
7+
}

example/example.jpg

38.3 KB
Loading

include/gnuplot.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU Lesser General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU Lesser General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU Lesser General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*
15+
* URL: https://github.com/martinruenz/gnuplot-cpp
16+
* AUTHOR: Martin Rünz, 2015
17+
*/
18+
19+
#pragma once
20+
21+
#include <stdio.h>
22+
#include <string>
23+
#include <vector>
24+
#include <iostream>
25+
#include <fstream>
26+
27+
class GnuplotPipe {
28+
public:
29+
inline GnuplotPipe(bool persist = true) {
30+
std::cout << "Opening gnuplot... ";
31+
pipe = popen(persist ? "gnuplot -persist" : "gnuplot", "w");
32+
if (!pipe)
33+
std::cout << "failed!" << std::endl;
34+
else
35+
std::cout << "succeded." << std::endl;
36+
}
37+
inline virtual ~GnuplotPipe(){
38+
if (pipe) pclose(pipe);
39+
}
40+
41+
void sendLine(const std::string& text, bool useBuffer = false){
42+
if (!pipe) return;
43+
if (useBuffer)
44+
buffer.push_back(text + "\n");
45+
else
46+
fputs((text + "\n").c_str(), pipe);
47+
}
48+
void sendEndOfData(unsigned repeatBuffer = 1){
49+
if (!pipe) return;
50+
for (unsigned i = 0; i < repeatBuffer; i++) {
51+
for (auto& line : buffer) fputs(line.c_str(), pipe);
52+
fputs("e\n", pipe);
53+
}
54+
fflush(pipe);
55+
buffer.clear();
56+
}
57+
void sendNewDataBlock(){
58+
sendLine("\n", !buffer.empty());
59+
}
60+
61+
void writeBufferToFile(const std::string& fileName){
62+
std::ofstream fileOut(fileName);
63+
for (auto& line : buffer) fileOut << line;
64+
fileOut.close();
65+
}
66+
67+
private:
68+
GnuplotPipe(GnuplotPipe const&) = delete;
69+
void operator=(GnuplotPipe const&) = delete;
70+
71+
FILE* pipe;
72+
std::vector<std::string> buffer;
73+
};

0 commit comments

Comments
 (0)