Skip to content

Commit 73e0821

Browse files
etfrogersSiegeLord
authored andcommitted
Add boxxyerror interface
1 parent d156b04 commit 73e0821

File tree

4 files changed

+187
-2
lines changed

4 files changed

+187
-2
lines changed

gnuplot/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ path = "examples/box_and_whisker.rs"
5757

5858
[[example]]
5959

60+
name = "box_xy_error"
61+
path = "examples/box_xy_error.rs"
62+
63+
[[example]]
64+
6065
name = "lines_3d"
6166
path = "examples/lines_3d.rs"
6267

gnuplot/examples/box_xy_error.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// This file is released into Public Domain.
2+
use crate::common::*;
3+
use gnuplot::*;
4+
5+
mod common;
6+
7+
fn example(c: Common)
8+
{
9+
let mut fg = Figure::new();
10+
11+
fg.axes2d()
12+
.set_title("Box XY Error", &[])
13+
.box_xy_error_delta(
14+
[0.0f32, 1.0, 2.0].iter(),
15+
[-1.0f32, 0.0, 1.0].iter(),
16+
[0.25f32, 0.375, 0.15].iter(),
17+
[2.0f32, 3.0, 4.0].iter(),
18+
&[],
19+
)
20+
.box_xy_error_low_high(
21+
[-0.6f32, 1.5, 2.5].iter(),
22+
[-1.0f32, 0.0, 1.0].iter(),
23+
[-0.9f32, -1.0, 2.2].iter(),
24+
[-0.45f32, 3.0, 2.95].iter(),
25+
[-1.5f32, 4.5, 3.0].iter(),
26+
[0.5f32, 4.75, 0.125].iter(),
27+
&[
28+
Color("blue"),
29+
LineWidth(2.0),
30+
LineStyle(SmallDot),
31+
FillAlpha(0.5),
32+
],
33+
)
34+
.set_x_range(Fix(-1.0), Fix(3.0))
35+
.set_y_range(Fix(-3.0), Fix(5.0));
36+
37+
c.show(&mut fg, "box_xy_error");
38+
}
39+
40+
fn main()
41+
{
42+
Common::new().map(|c| example(c));
43+
}

gnuplot/src/axes2d.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,93 @@ impl Axes2D
797797
self
798798
}
799799

800+
/// Plot 2D rectangular boxes - usually used for error bars - using specified by width (x_delta) and height (y_delta).
801+
///
802+
/// # Arguments
803+
/// * `x` - x values (horizontal center of the box)
804+
/// * `y` - y values (vertical center of the box)
805+
/// * `x_delta` - Error in x (horizontal half-width of the box)
806+
/// * `y_delta` - Error in y (vertical half-width of the box)
807+
/// * `options` - Array of PlotOption<&str> controlling the appearance of the plot element. The relevant options are:
808+
/// * `Caption` - Specifies the caption for this dataset. Use an empty string to hide it (default).
809+
/// * `LineWidth` - Sets the width of the border
810+
/// * `LineStyle` - Sets the style of the border
811+
/// * `BorderColor` - Sets the color of the border
812+
/// * `Color` - Sets the color of the box fill
813+
/// * `FillAlpha` - Sets the transparency of the box fill
814+
pub fn box_xy_error_delta<
815+
'l,
816+
Tx: DataType,
817+
X: IntoIterator<Item = Tx>,
818+
Ty: DataType,
819+
Y: IntoIterator<Item = Ty>,
820+
TXDelta: DataType,
821+
XDelta: IntoIterator<Item = TXDelta>,
822+
TYDelta: DataType,
823+
YDelta: IntoIterator<Item = TYDelta>,
824+
>(
825+
&'l mut self, x: X, y: Y, x_delta: XDelta, y_delta: YDelta, options: &[PlotOption<&str>],
826+
) -> &'l mut Self
827+
{
828+
self.common.elems.push(PlotElement::new_plot4(
829+
BoxXYError,
830+
x,
831+
y,
832+
x_delta,
833+
y_delta,
834+
options.to_one_way_owned(),
835+
));
836+
self
837+
}
838+
839+
/// Plot 2D rectangular boxes - usually used for error bars - using specified low and high limits for x and y.
840+
///
841+
/// # Arguments
842+
/// * `x` - x values (horizontal center of the box)
843+
/// * `y` - y values (vertical center of the box)
844+
/// * `x_low` - Horizontal lower limit of the box
845+
/// * `x_high` - Horizontal upper limit of the box
846+
/// * `y_low` - Vertical lower limit of the box
847+
/// * `y_high` - Vertical upper limit of the box
848+
/// * `options` - Array of PlotOption<&str> controlling the appearance of the plot element. The relevant options are:
849+
/// * `Caption` - Specifies the caption for this dataset. Use an empty string to hide it (default).
850+
/// * `LineWidth` - Sets the width of the border
851+
/// * `LineStyle` - Sets the style of the border
852+
/// * `BorderColor` - Sets the color of the border
853+
/// * `Color` - Sets the color of the box fill
854+
/// * `FillAlpha` - Sets the transparency of the box fill
855+
pub fn box_xy_error_low_high<
856+
'l,
857+
Tx: DataType,
858+
X: IntoIterator<Item = Tx>,
859+
Ty: DataType,
860+
Y: IntoIterator<Item = Ty>,
861+
TXLow: DataType,
862+
XLow: IntoIterator<Item = TXLow>,
863+
TXHigh: DataType,
864+
XHigh: IntoIterator<Item = TXHigh>,
865+
TYLow: DataType,
866+
YLow: IntoIterator<Item = TYLow>,
867+
TYHigh: DataType,
868+
YHigh: IntoIterator<Item = TYHigh>,
869+
>(
870+
&'l mut self, x: X, y: Y, x_low: XLow, x_high: XHigh, y_low: YLow, y_high: YHigh,
871+
options: &[PlotOption<&str>],
872+
) -> &'l mut Self
873+
{
874+
self.common.elems.push(PlotElement::new_plot6(
875+
BoxXYError,
876+
x,
877+
y,
878+
x_low,
879+
x_high,
880+
y_low,
881+
y_high,
882+
options.to_one_way_owned(),
883+
));
884+
self
885+
}
886+
800887
/// Draws an image from a rectangular array of data by connecting the individual datapoints with polygons.
801888
///
802889
/// #Arguments:

gnuplot/src/axes_common.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,46 @@ impl PlotElement
9292
}
9393
}
9494

95+
pub fn new_plot4<T1, X1, T2, X2, T3, X3, T4, X4>(
96+
plot_type: PlotType, x1: X1, x2: X2, x3: X3, x4: X4, options: Vec<PlotOption<String>>,
97+
) -> PlotElement
98+
where
99+
T1: DataType,
100+
X1: IntoIterator<Item = T1>,
101+
T2: DataType,
102+
X2: IntoIterator<Item = T2>,
103+
T3: DataType,
104+
X3: IntoIterator<Item = T3>,
105+
T4: DataType,
106+
X4: IntoIterator<Item = T4>,
107+
{
108+
let mut num_rows = 0;
109+
let mut data = vec![];
110+
// TODO: Reserve.
111+
for (((x1, x2), x3), x4) in x1
112+
.into_iter()
113+
.zip(x2.into_iter())
114+
.zip(x3.into_iter())
115+
.zip(x4.into_iter())
116+
{
117+
data.push(x1.get());
118+
data.push(x2.get());
119+
data.push(x3.get());
120+
data.push(x4.get());
121+
num_rows += 1;
122+
}
123+
124+
PlotElement {
125+
data,
126+
num_rows,
127+
num_cols: 4,
128+
plot_type,
129+
source_type: Record,
130+
is_3d: false,
131+
options,
132+
}
133+
}
134+
95135
pub fn new_plot5<T1, X1, T2, X2, T3, X3, T4, X4, T5, X5>(
96136
plot_type: PlotType, x1: X1, x2: X2, x3: X3, x4: X4, x5: X5,
97137
options: Vec<PlotOption<String>>,
@@ -307,6 +347,7 @@ impl PlotElement
307347
Polygons => "polygons",
308348
Boxes => "boxes",
309349
BoxAndWhisker => "candlestick",
350+
BoxXYError => "boxxyerror",
310351
Pm3D => "pm3d",
311352
Image => "image",
312353
};
@@ -724,6 +765,7 @@ pub enum PlotType
724765
Polygons,
725766
Boxes,
726767
BoxAndWhisker,
768+
BoxXYError,
727769
Pm3D,
728770
Image,
729771
}
@@ -734,7 +776,12 @@ impl PlotType
734776
{
735777
matches!(
736778
*self,
737-
Lines | LinesPoints | XErrorLines | Boxes | YErrorLines | BoxAndWhisker | Polygons
779+
Lines
780+
| LinesPoints
781+
| XErrorLines
782+
| Boxes | YErrorLines
783+
| BoxAndWhisker
784+
| BoxXYError | Polygons
738785
)
739786
}
740787

@@ -748,7 +795,10 @@ impl PlotType
748795

749796
fn is_fill(&self) -> bool
750797
{
751-
matches!(*self, Boxes | FillBetween | BoxAndWhisker | Polygons)
798+
matches!(
799+
*self,
800+
Boxes | FillBetween | BoxAndWhisker | BoxXYError | Polygons
801+
)
752802
}
753803
}
754804

0 commit comments

Comments
 (0)