Skip to content

Commit 41d1e49

Browse files
committed
Add setting of fixed box_width
1 parent 4c0fe28 commit 41d1e49

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

gnuplot/examples/variable_color.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,34 @@ fn example(c: Common)
8787
ax.set_title(
8888
&format!("variable color boxerror, points, xyerrorbars, and boxxyerror.\nColor used is a {label}"),
8989
&[],
90-
);
91-
ax.set_y_range(Fix(-4.0), Fix(11.5));
92-
ax.box_error_low_high_set_width(
90+
)
91+
.set_y_range(Fix(-4.0), Fix(11.5))
92+
.set_box_width(0.5, true)
93+
.box_error_low_high(
9394
&d1,
9495
&d5,
9596
&d2,
9697
&d6,
97-
iter::repeat(0.2),
9898
&[
9999
color.clone(),
100100
FillAlpha(0.5),
101101
BorderColor(RGBString("black")),
102102
],
103-
);
104-
ax.points(&d1, iter::repeat(1), &[color.clone(), PointSymbol('D')]);
105-
ax.xy_error_bars(
103+
)
104+
.points(&d1, iter::repeat(1), &[color.clone(), PointSymbol('D')])
105+
.xy_error_bars(
106106
&d1,
107107
iter::repeat(8),
108108
d1.iter().map(by3),
109109
d1.iter().map(by4),
110110
&[color.clone()],
111-
);
112-
ax.points(
111+
)
112+
.points(
113113
&d1,
114114
d2.iter().map(|v| -v / 2.0),
115115
&[color.clone(), PointSymbol('O'), PointSize(3.0)],
116-
);
117-
ax.box_xy_error_delta(
116+
)
117+
.box_xy_error_delta(
118118
&d1,
119119
iter::repeat(10),
120120
d1.iter().map(by3),
@@ -153,21 +153,21 @@ fn example(c: Common)
153153
ax.set_title(
154154
&format!("variable color boxerror, points, xyerrorbars, and boxxyerror.\nColor used is a SavedColormap"),
155155
&[],
156-
);
157-
ax.set_y_range(Fix(-4.0), Fix(11.5));
158-
ax.box_error_low_high_set_width(
156+
)
157+
.set_y_range(Fix(-4.0), Fix(11.5))
158+
.set_box_width(0.8, false)
159+
.box_error_low_high(
159160
&d1,
160161
&d5,
161162
&d2,
162163
&d6,
163-
iter::repeat(0.2),
164164
&[
165165
Color(SavedColorMap("magma", color_values.clone())),
166166
FillAlpha(0.5),
167167
BorderColor(RGBString("black")),
168168
],
169-
);
170-
ax.points(
169+
)
170+
.points(
171171
&d1,
172172
iter::repeat(1),
173173
&[
@@ -177,24 +177,24 @@ fn example(c: Common)
177177
)),
178178
PointSymbol('D'),
179179
],
180-
);
181-
ax.xy_error_bars(
180+
)
181+
.xy_error_bars(
182182
&d1,
183183
iter::repeat(8),
184184
d1.iter().map(by3),
185185
d1.iter().map(by4),
186186
&[Color(SavedColorMap("ocean", color_values.clone()))],
187-
);
188-
ax.points(
187+
)
188+
.points(
189189
&d1,
190190
d2.iter().map(|v| -v / 2.0),
191191
&[
192192
Color(SavedColorMap("magma", color_values.clone())),
193193
PointSymbol('O'),
194194
PointSize(3.0),
195195
],
196-
);
197-
ax.box_xy_error_delta(
196+
)
197+
.box_xy_error_delta(
198198
&d1,
199199
iter::repeat(10),
200200
d1.iter().map(by3),

gnuplot/src/axes_common.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@ pub struct AxesCommonData
11071107
pub margins: Margins,
11081108
pub palette: PaletteType<Vec<(f32, f32, f32, f32)>>,
11091109
pub colormaps: Vec<(String, PaletteType<Vec<(f32, f32, f32, f32)>>)>,
1110+
pub box_width: Option<(f64, bool)>,
11101111
}
11111112

11121113
impl AxesCommonData
@@ -1131,6 +1132,7 @@ impl AxesCommonData
11311132
margins: Margins::new(),
11321133
palette: COLOR.to_one_way_owned(),
11331134
colormaps: Vec::new(),
1135+
box_width: None,
11341136
};
11351137
ret.x2_axis.tick_type = TickType::None;
11361138
ret.y2_axis.tick_type = TickType::None;
@@ -1277,6 +1279,12 @@ impl AxesCommonData
12771279
}
12781280
self.palette.write_out_commands(w);
12791281

1282+
if let Some((width, is_relative)) = self.box_width
1283+
{
1284+
let scale = if is_relative { "relative" } else { "absolute" };
1285+
writeln!(w, "set boxwidth {width} {scale}");
1286+
}
1287+
12801288
self.x_axis.write_out_commands(w, version);
12811289
self.y_axis.write_out_commands(w, version);
12821290
self.x2_axis.write_out_commands(w, version);
@@ -1408,6 +1416,18 @@ pub trait AxesCommon: AxesCommonPrivate
14081416
self
14091417
}
14101418

1419+
/// Set the width of boxes in any box plots on the axes (for example [boxes()](crate::Axes2D::boxes),
1420+
/// [box_and_whisker()](crate::Axes2D::box_and_whisker))
1421+
/// # Arguments
1422+
/// * `width` - Width of boxes.
1423+
/// * `is_relative` - if `true`, `width` is interpreted as a fraction of the default box width.
1424+
/// if `false` width is an absolute value in the units of the x axis
1425+
fn set_box_width(&mut self, width: f64, is_relative: bool) -> &mut Self
1426+
{
1427+
self.get_common_data_mut().box_width = Some((width, is_relative));
1428+
self
1429+
}
1430+
14111431
/// Set the aspect ratio of the axes
14121432
/// # Arguments
14131433
/// * `ratio` - The aspect ratio. Set to Auto to return the ratio to default

0 commit comments

Comments
 (0)