Skip to content

Commit

Permalink
gnuplot allow axis rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pardoxa committed Dec 6, 2023
1 parent 5da4e34 commit 1f271e3
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/heatmap/gnuplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::{Serialize, Deserialize};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
/// For labeling the gnuplot plots axis
pub enum GnuplotAxis{
pub enum Labels{
/// construct the labels
FromValues{
/// minimum value for axis labels
Expand All @@ -23,17 +23,31 @@ pub enum GnuplotAxis{
tics: usize,
},
/// use labels
Labels{
FromStrings{
/// this are the labels
labels: Vec<String>
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
/// For labeling the gnuplot plots axis
pub struct GnuplotAxis{
labels: Labels,
rotation: f32
}

impl GnuplotAxis{
/// Set the rotation value.
/// Tics will be displayed rotaded to the right by the requested amount
pub fn set_rotation(&mut self, rotation_degrees: f32)
{
self.rotation = rotation_degrees;
}

pub(crate) fn write_tics<W: Write>(&self, mut w: W, num_bins: usize, axis: &str) -> std::io::Result<()>
{
match self {
Self::FromValues{min, max, tics} => {
match &self.labels {
Labels::FromValues{min, max, tics} => {
if min.is_nan() || max.is_nan() || *tics < 2 || num_bins < 2 {
Ok(())
} else {
Expand All @@ -48,10 +62,10 @@ impl GnuplotAxis{
let pos = i as f64 * bin_dif;
write!(w, "\"{:#}\" {:e}, ", val, pos)?;
}
writeln!(w, "\"{:#}\" {:e} )", max, num_bins - 1)
writeln!(w, "\"{:#}\" {:e} ) rotate by {} right", max, num_bins - 1, self.rotation)
}
},
Self::Labels{labels} => {
Labels::FromStrings{labels} => {
let tics = labels.len();
match tics {
0 => Ok(()),
Expand All @@ -66,7 +80,7 @@ impl GnuplotAxis{
let pos = i as f64 * bin_dif;
write!(w, "\"{}\" {:e}, ", lab, pos)?;
}
writeln!(w, " )")
writeln!(w, " ) rotate by {} right", self.rotation)
}
}
}
Expand All @@ -76,20 +90,20 @@ impl GnuplotAxis{

/// Create new GnuplotAxis::FromValues
pub fn new(min: f64, max: f64, tics: usize) -> Self {
Self::FromValues{
let labels = Labels::FromValues{
min,
max,
tics
}
};
Self { labels, rotation: 0.0 }
}

/// Create new GnuplotAxis::Labels
/// - Vector contains labels used for axis
pub fn from_labels(labels: Vec<String>) -> Self
{
Self::Labels{
labels
}
let labels = Labels::FromStrings { labels };
Self{labels, rotation: 0.0}
}

/// Similar to `from_labels`
Expand Down

0 comments on commit 1f271e3

Please sign in to comment.