use crate::canvas;
use crate::chart::plotchart;
use crate::wish;
#[derive(Clone, Debug, PartialEq)]
pub struct TkTXPlot {
pub id: String,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TkTXDefinition {
canvas_id: String,
time_axis: (String, String, u64),
y_axis: (f64, f64, f64),
time_format: Option<String>,
gmt: Option<bool>,
axes_at_zero: Option<bool>,
isometric: Option<bool>,
}
impl TkTXDefinition {
pub fn time_format(&mut self, format: &str) -> &mut Self {
self.time_format = Some(String::from(format));
self
}
pub fn gmt(&mut self, value: bool) -> &mut Self {
self.gmt = Some(value);
self
}
pub fn axes_at_zero(&mut self, value: bool) -> &mut Self {
self.axes_at_zero = Some(value);
self
}
pub fn isometric(&mut self, value: bool) -> &mut Self {
self.isometric = Some(value);
self
}
pub fn plot(&self) -> TkTXPlot {
let id = wish::next_var();
let mut msg = format!(
"global {}; set {} [::Plotchart::createTXPlot {} {{{} {} {}}} {{{} {} {}}}",
id, id, &self.canvas_id,
self.time_axis.0, self.time_axis.1, self.time_axis.2,
self.y_axis.0, self.y_axis.1, self.y_axis.2
);
if let Some(value) = &self.time_format {
msg.push_str(&format!("-timeformat {{{}}} ", value));
}
if let Some(value) = self.gmt {
msg.push_str(&format!("-gmt {} ", if value { "1" } else { "0" }));
}
if let Some(value) = self.axes_at_zero {
msg.push_str(&format!("-axesatzero {} ", if value { "1" } else { "0" }));
}
if let Some(value) = self.isometric {
msg.push_str(&format!("-isometric {} ", if value { "1" } else { "0" }));
}
msg.push_str("]");
wish::tell_wish(&msg);
TkTXPlot { id }
}
}
pub fn make_tx(
canvas: &canvas::TkCanvas,
(min, max, step): (&str, &str, u64),
y_axis: (f64, f64, f64),
) -> TkTXDefinition {
TkTXDefinition {
canvas_id: String::from(&canvas.id),
time_axis: (String::from(min), String::from(max), step),
y_axis,
time_format: None,
gmt: None,
axes_at_zero: None,
isometric: None,
}
}
impl plotchart::TkPlotchart for TkTXPlot {
fn id(&self) -> &str {
&self.id
}
}
impl plotchart::TkChartSeries for TkTXPlot {}
impl TkTXPlot {
pub fn draw_interval(&self, series: &str, time_coord: &str, y_min: f64, y_max: f64) {
let msg = format!(
"global {}; ${} interval {} {} {} {}",
&self.id, &self.id, series, time_coord, y_min, y_max
);
wish::tell_wish(&msg);
}
pub fn draw_interval_with_symbol(
&self,
series: &str,
time_coord: &str,
y_min: f64,
y_max: f64,
y_centre: f64,
) {
let msg = format!(
"global {}; ${} interval {} {} {} {} {}",
&self.id, &self.id, series, time_coord, y_min, y_max, y_centre
);
wish::tell_wish(&msg);
}
pub fn draw_labelled_dot(
&self,
(x, y): (f64, f64),
label: &str,
location: plotchart::Location,
) {
let msg = format!(
"global {}; ${} labeldot {} {} {} {}",
&self.id, &self.id, x, y, label, location
);
wish::tell_wish(&msg);
}
pub fn plot(&self, series: &str, (time, y): (&str, f64)) {
let msg = format!(
"global {}; ${} plot {} {} {}",
&self.id, &self.id, series, time, y
);
wish::tell_wish(&msg);
}
}