aboutsummaryrefslogtreecommitdiff
path: root/src/filledcurve.rs
diff options
context:
space:
mode:
authorJakub Kotur <qtr@google.com>2020-12-21 17:28:14 +0100
committerJakub Kotur <qtr@google.com>2021-03-05 15:07:06 +0100
commit835fea47b902cc9ae1f6283bc6e107f0cd83734e (patch)
treee0f621ccc416ebc1c69588afba361fef31b1ae2e /src/filledcurve.rs
parent790fbb964d669b370a0017b6747f3b9f13a04af9 (diff)
downloadcriterion-plot-835fea47b902cc9ae1f6283bc6e107f0cd83734e.tar.gz
Initial import of criterion-plot-0.4.3.
Bug: 155309706 Change-Id: Ia9cfbc3f7d52994d45a3113e5bdfefa9733a80c8
Diffstat (limited to 'src/filledcurve.rs')
-rwxr-xr-xsrc/filledcurve.rs140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/filledcurve.rs b/src/filledcurve.rs
new file mode 100755
index 0000000..f79dbdd
--- /dev/null
+++ b/src/filledcurve.rs
@@ -0,0 +1,140 @@
+//! Filled curve plots
+
+use std::borrow::Cow;
+use std::iter::IntoIterator;
+
+use crate::data::Matrix;
+use crate::traits::{self, Data, Set};
+use crate::{Axes, Color, Default, Display, Figure, Label, Opacity, Plot, Script};
+
+/// Properties common to filled curve plots
+pub struct Properties {
+ axes: Option<Axes>,
+ color: Option<Color>,
+ label: Option<Cow<'static, str>>,
+ opacity: Option<f64>,
+}
+
+impl Default for Properties {
+ fn default() -> Properties {
+ Properties {
+ axes: None,
+ color: None,
+ label: None,
+ opacity: None,
+ }
+ }
+}
+
+impl Script for Properties {
+ fn script(&self) -> String {
+ let mut script = if let Some(axes) = self.axes {
+ format!("axes {} ", axes.display())
+ } else {
+ String::new()
+ };
+ script.push_str("with filledcurves ");
+
+ script.push_str("fillstyle ");
+
+ if let Some(opacity) = self.opacity {
+ script.push_str(&format!("solid {} ", opacity))
+ }
+
+ // TODO border shoulde be configurable
+ script.push_str("noborder ");
+
+ if let Some(color) = self.color {
+ script.push_str(&format!("lc rgb '{}' ", color.display()));
+ }
+
+ if let Some(ref label) = self.label {
+ script.push_str("title '");
+ script.push_str(label);
+ script.push('\'')
+ } else {
+ script.push_str("notitle")
+ }
+
+ script
+ }
+}
+
+impl Set<Axes> for Properties {
+ /// Select axes to plot against
+ ///
+ /// **Note** By default, the `BottomXLeftY` axes are used
+ fn set(&mut self, axes: Axes) -> &mut Properties {
+ self.axes = Some(axes);
+ self
+ }
+}
+
+impl Set<Color> for Properties {
+ /// Sets the fill color
+ fn set(&mut self, color: Color) -> &mut Properties {
+ self.color = Some(color);
+ self
+ }
+}
+
+impl Set<Label> for Properties {
+ /// Sets the legend label
+ fn set(&mut self, label: Label) -> &mut Properties {
+ self.label = Some(label.0);
+ self
+ }
+}
+
+impl Set<Opacity> for Properties {
+ /// Changes the opacity of the fill color
+ ///
+ /// **Note** By default, the fill color is totally opaque (`opacity = 1.0`)
+ ///
+ /// # Panics
+ ///
+ /// Panics if `opacity` is outside the range `[0, 1]`
+ fn set(&mut self, opacity: Opacity) -> &mut Properties {
+ self.opacity = Some(opacity.0);
+ self
+ }
+}
+
+/// Fills the area between two curves
+pub struct FilledCurve<X, Y1, Y2> {
+ /// X coordinate of the data points of both curves
+ pub x: X,
+ /// Y coordinate of the data points of the first curve
+ pub y1: Y1,
+ /// Y coordinate of the data points of the second curve
+ pub y2: Y2,
+}
+
+impl<X, Y1, Y2> traits::Plot<FilledCurve<X, Y1, Y2>> for Figure
+where
+ X: IntoIterator,
+ X::Item: Data,
+ Y1: IntoIterator,
+ Y1::Item: Data,
+ Y2: IntoIterator,
+ Y2::Item: Data,
+{
+ type Properties = Properties;
+
+ fn plot<F>(&mut self, fc: FilledCurve<X, Y1, Y2>, configure: F) -> &mut Figure
+ where
+ F: FnOnce(&mut Properties) -> &mut Properties,
+ {
+ let FilledCurve { x, y1, y2 } = fc;
+
+ let mut props = Default::default();
+ configure(&mut props);
+
+ let (x_factor, y_factor) =
+ crate::scale_factor(&self.axes, props.axes.unwrap_or(crate::Axes::BottomXLeftY));
+
+ let data = Matrix::new(izip!(x, y1, y2), (x_factor, y_factor, y_factor));
+ self.plots.push(Plot::new(data, &props));
+ self
+ }
+}