Skip to content

Commit 8ca2b8b

Browse files
SiegeLordExSiegeLord
authored andcommitted
Add polygons to draw filled polygons
1 parent bcb592a commit 8ca2b8b

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

gnuplot/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ path = "examples/multiple_axes.rs"
120120
name = "text"
121121
path = "examples/text.rs"
122122

123+
[[example]]
124+
125+
name = "polygons"
126+
path = "examples/polygons.rs"
127+
123128
[dependencies]
124129
byteorder = "1.4.3"
125130
tempfile = "3.9"

gnuplot/examples/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// This file is released into Public Domain.
2+
#![allow(dead_code)]
23
use argparse_rs::*;
34
use gnuplot::*;
45
use std::env;

gnuplot/examples/polygons.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 coords = [[0., 0.], [1., 1.], [2., 0.5], [2., -0.5], [1., -1.]];
10+
11+
let mut fg = Figure::new();
12+
fg.set_title("Polygons");
13+
14+
let ax = fg.axes2d();
15+
ax.polygon(
16+
coords.iter().map(|x| x[0]),
17+
coords.iter().map(|x| x[1]),
18+
&[],
19+
);
20+
21+
ax.polygon(
22+
coords.iter().map(|x| x[0] + 2.),
23+
coords.iter().map(|x| x[1]),
24+
&[FillAlpha(0.), BorderColor("black"), LineWidth(4.)],
25+
);
26+
ax.polygon(
27+
coords.iter().map(|x| x[0]),
28+
coords.iter().map(|x| x[1] + 2.),
29+
&[Color("#FF0000"), BorderColor("black"), LineWidth(4.)],
30+
);
31+
ax.polygon(
32+
coords.iter().map(|x| x[0] + 2.),
33+
coords.iter().map(|x| x[1] + 2.),
34+
&[
35+
FillPattern(Fix(BigCrosses)),
36+
Color("#FF0000"),
37+
BorderColor("black"),
38+
LineWidth(4.),
39+
LineStyle(Dash),
40+
],
41+
);
42+
c.show(&mut fg, "polygons");
43+
}
44+
45+
fn main()
46+
{
47+
Common::new().map(|c| example(c));
48+
}

gnuplot/src/axes2d.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,36 @@ impl Axes2D
607607
self
608608
}
609609

610+
// Plot a polygon given coordinates of its vertices.
611+
//
612+
// # Arguments
613+
// * `x` - x coordinates of the vertices
614+
// * `y` - y coordinates of the vertices
615+
// * `options` - Array of PlotOption<&str> controlling the appearance of the plot element. The relevant options are:
616+
// * `Caption` - Specifies the caption for this dataset. Use an empty string to hide it (default).
617+
// * `FillAlpha` - Sets the transparency of the filled region
618+
// * `Color` - Sets the color of the filled region (and the border, unless `BorderColor` is set)
619+
// * `BorderColor` - Sets the color of the border
620+
// * `FillPattern` - Sets the fill pattern
621+
pub fn polygon<
622+
'l,
623+
Tx: DataType,
624+
X: IntoIterator<Item = Tx>,
625+
Ty: DataType,
626+
Y: IntoIterator<Item = Ty>,
627+
>(
628+
&'l mut self, x: X, y: Y, options: &[PlotOption<&str>],
629+
) -> &'l mut Self
630+
{
631+
self.common.elems.push(PlotElement::new_plot2(
632+
Polygons,
633+
x,
634+
y,
635+
options.to_one_way_owned(),
636+
));
637+
self
638+
}
639+
610640
/// Plot a 2D scatter-plot using boxes of automatic width. Box widths are set so that there are no gaps between successive boxes (i.e. each box may have a different width).
611641
/// Boxes start at the x-axis and go towards the y value of the datapoint.
612642
/// # Arguments

gnuplot/src/axes_common.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use self::DataSourceType::*;
66

77
pub use self::LabelType::*;
88
pub use self::PlotType::*;
9-
pub use self::TickAxis::*;
109
use crate::coordinates::*;
1110

1211
use crate::datatype::*;
@@ -305,6 +304,7 @@ impl PlotElement
305304
XErrorBars => "xerrorbars",
306305
YErrorBars => "yerrorbars",
307306
FillBetween => "filledcurves",
307+
Polygons => "polygons",
308308
Boxes => "boxes",
309309
BoxAndWhisker => "candlestick",
310310
Pm3D => "pm3d",
@@ -352,13 +352,15 @@ impl PlotElement
352352

353353
if !is_pattern
354354
{
355-
writer.write_str("transparent solid ");
355+
writer.write_str("transparent solid");
356+
let mut alpha = 1.;
356357
first_opt! {self.options,
357358
FillAlpha(a) =>
358359
{
359-
write!(writer, "{:.12e}", a);
360+
alpha = a;
360361
}
361362
}
363+
write!(writer, " {:.12e}", alpha);
362364
}
363365

364366
if self.plot_type.is_line()
@@ -725,6 +727,7 @@ pub enum PlotType
725727
XErrorBars,
726728
YErrorBars,
727729
FillBetween,
730+
Polygons,
728731
Boxes,
729732
BoxAndWhisker,
730733
Pm3D,
@@ -737,7 +740,7 @@ impl PlotType
737740
{
738741
matches!(
739742
*self,
740-
Lines | LinesPoints | XErrorLines | Boxes | YErrorLines | BoxAndWhisker
743+
Lines | LinesPoints | XErrorLines | Boxes | YErrorLines | BoxAndWhisker | Polygons
741744
)
742745
}
743746

@@ -751,7 +754,7 @@ impl PlotType
751754

752755
fn is_fill(&self) -> bool
753756
{
754-
matches!(*self, Boxes | FillBetween | BoxAndWhisker)
757+
matches!(*self, Boxes | FillBetween | BoxAndWhisker | Polygons)
755758
}
756759
}
757760

0 commit comments

Comments
 (0)