Skip to content

Commit

Permalink
Add polygons to draw filled polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
SiegeLordEx authored and SiegeLord committed Jun 30, 2024
1 parent bcb592a commit 8ca2b8b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
5 changes: 5 additions & 0 deletions gnuplot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ path = "examples/multiple_axes.rs"
name = "text"
path = "examples/text.rs"

[[example]]

name = "polygons"
path = "examples/polygons.rs"

[dependencies]
byteorder = "1.4.3"
tempfile = "3.9"
1 change: 1 addition & 0 deletions gnuplot/examples/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file is released into Public Domain.
#![allow(dead_code)]
use argparse_rs::*;
use gnuplot::*;
use std::env;
Expand Down
48 changes: 48 additions & 0 deletions gnuplot/examples/polygons.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// This file is released into Public Domain.
use crate::common::*;
use gnuplot::*;

mod common;

fn example(c: Common)
{
let coords = [[0., 0.], [1., 1.], [2., 0.5], [2., -0.5], [1., -1.]];

let mut fg = Figure::new();
fg.set_title("Polygons");

let ax = fg.axes2d();
ax.polygon(
coords.iter().map(|x| x[0]),
coords.iter().map(|x| x[1]),
&[],
);

ax.polygon(
coords.iter().map(|x| x[0] + 2.),
coords.iter().map(|x| x[1]),
&[FillAlpha(0.), BorderColor("black"), LineWidth(4.)],
);
ax.polygon(
coords.iter().map(|x| x[0]),
coords.iter().map(|x| x[1] + 2.),
&[Color("#FF0000"), BorderColor("black"), LineWidth(4.)],
);
ax.polygon(
coords.iter().map(|x| x[0] + 2.),
coords.iter().map(|x| x[1] + 2.),
&[
FillPattern(Fix(BigCrosses)),
Color("#FF0000"),
BorderColor("black"),
LineWidth(4.),
LineStyle(Dash),
],
);
c.show(&mut fg, "polygons");
}

fn main()
{
Common::new().map(|c| example(c));
}
30 changes: 30 additions & 0 deletions gnuplot/src/axes2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,36 @@ impl Axes2D
self
}

// Plot a polygon given coordinates of its vertices.
//
// # Arguments
// * `x` - x coordinates of the vertices
// * `y` - y coordinates of the vertices
// * `options` - Array of PlotOption<&str> controlling the appearance of the plot element. The relevant options are:
// * `Caption` - Specifies the caption for this dataset. Use an empty string to hide it (default).
// * `FillAlpha` - Sets the transparency of the filled region
// * `Color` - Sets the color of the filled region (and the border, unless `BorderColor` is set)
// * `BorderColor` - Sets the color of the border
// * `FillPattern` - Sets the fill pattern
pub fn polygon<
'l,
Tx: DataType,
X: IntoIterator<Item = Tx>,
Ty: DataType,
Y: IntoIterator<Item = Ty>,
>(
&'l mut self, x: X, y: Y, options: &[PlotOption<&str>],
) -> &'l mut Self
{
self.common.elems.push(PlotElement::new_plot2(
Polygons,
x,
y,
options.to_one_way_owned(),
));
self
}

/// 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).
/// Boxes start at the x-axis and go towards the y value of the datapoint.
/// # Arguments
Expand Down
13 changes: 8 additions & 5 deletions gnuplot/src/axes_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use self::DataSourceType::*;

pub use self::LabelType::*;
pub use self::PlotType::*;
pub use self::TickAxis::*;
use crate::coordinates::*;

use crate::datatype::*;
Expand Down Expand Up @@ -305,6 +304,7 @@ impl PlotElement
XErrorBars => "xerrorbars",
YErrorBars => "yerrorbars",
FillBetween => "filledcurves",
Polygons => "polygons",
Boxes => "boxes",
BoxAndWhisker => "candlestick",
Pm3D => "pm3d",
Expand Down Expand Up @@ -352,13 +352,15 @@ impl PlotElement

if !is_pattern
{
writer.write_str("transparent solid ");
writer.write_str("transparent solid");
let mut alpha = 1.;
first_opt! {self.options,
FillAlpha(a) =>
{
write!(writer, "{:.12e}", a);
alpha = a;
}
}
write!(writer, " {:.12e}", alpha);
}

if self.plot_type.is_line()
Expand Down Expand Up @@ -725,6 +727,7 @@ pub enum PlotType
XErrorBars,
YErrorBars,
FillBetween,
Polygons,
Boxes,
BoxAndWhisker,
Pm3D,
Expand All @@ -737,7 +740,7 @@ impl PlotType
{
matches!(
*self,
Lines | LinesPoints | XErrorLines | Boxes | YErrorLines | BoxAndWhisker
Lines | LinesPoints | XErrorLines | Boxes | YErrorLines | BoxAndWhisker | Polygons
)
}

Expand All @@ -751,7 +754,7 @@ impl PlotType

fn is_fill(&self) -> bool
{
matches!(*self, Boxes | FillBetween | BoxAndWhisker)
matches!(*self, Boxes | FillBetween | BoxAndWhisker | Polygons)
}
}

Expand Down

0 comments on commit 8ca2b8b

Please sign in to comment.