gnuplot_wrapper/
figure.rs

1use std::{
2    fmt::format,
3    fs::{remove_file, File},
4    io::Write,
5};
6
7use crate::process::gnuplot_live_process::GnuplotLiveProcess;
8
9pub struct Figure {
10    index: i32,
11    file_path: String,
12    file: File,
13    commands: Vec<String>,
14    title: String,
15}
16
17impl Figure {
18    pub fn new(index: i32, temp_file: &str) -> Self {
19        return Figure {
20            index: index,
21            file_path: temp_file.to_string(),
22            file: File::create(temp_file).unwrap(),
23            commands: Vec::new(),
24            title: String::new()
25        };
26    }
27
28    pub fn plot(&mut self, x: &Vec<f64>, y: &Vec<f64>) {
29        for i in 0..x.len() {
30            self.file
31                .write_fmt(format_args!("{} {}\n", x[i], y[i]))
32                .unwrap();
33        }
34        self.file.write_fmt(format_args!("\n")).unwrap();
35
36        self.commands.push(format!("set term qt {}", self.index));
37        self.commands
38            .push(format!("plot '{}' with linespoints", "/tmp/index.dat"));
39    }
40
41    pub fn title(&mut self, title: &str) {
42        self.title = title.to_string()
43    }
44
45    pub fn compile(&self) -> Vec<String> {
46        let mut lines: Vec<String> = Vec::new();
47        lines.push(format!("set title '{}'", self.title));
48        for line in self.commands.clone() {
49            lines.push(line);
50        }
51        lines.push("pause mouse close".to_string());
52        return lines;
53    }
54
55    pub fn clear(&self) {
56        remove_file(self.file_path.clone()).unwrap()
57    }
58}