1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
From 57ae1a7f39c8351a155d71c1365f585d21a066d2 Mon Sep 17 00:00:00 2001
From: Xavier Gillard <xavier.gillard@uclouvain.be>
Date: Tue, 7 Apr 2020 16:26:15 +0200
Subject: [PATCH 02/19] display the markers in the svg lengend of scatter plots
---
CHANGELOG | 2 ++
src/repr/plot.rs | 8 ++++-
src/svg_render.rs | 81 +++++++++++++++++++++++++----------------------
3 files changed, 53 insertions(+), 38 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
index a095456..b996ac8 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,8 @@
# Changelog
## Unreleased
+- Show markers in the line/scatter plots legend.
+- Minor refactor in `svg_render` to factor out the logic to draw point styles.
## 0.5.1 - 2020-03-28
### Fixed
diff --git a/src/repr/plot.rs b/src/repr/plot.rs
index d8af721..a6039c2 100644
--- a/src/repr/plot.rs
+++ b/src/repr/plot.rs
@@ -21,6 +21,7 @@ use crate::repr::ContinuousRepresentation;
use crate::style::*;
use crate::svg_render;
use crate::text_render;
+use crate::svg_render::draw_marker;
/// Representation of any plot with points in the XY plane, visualized as points and/or with lines
/// in-between.
@@ -159,7 +160,7 @@ impl ContinuousRepresentation for Plot {
if let Some(ref style) = self.line_style {
let line = node::element::Line::new()
- .set("x1", -10)
+ .set("x1", -23)
.set("y1", -FONT_SIZE / 2. + 2.)
.set("x2", -3)
.set("y2", -FONT_SIZE / 2. + 2.)
@@ -168,6 +169,11 @@ impl ContinuousRepresentation for Plot {
group.append(line);
}
+ if let Some(ref style) = self.point_style {
+ let mark = draw_marker(-13., (-FONT_SIZE / 2. + 2.) as f64, style);
+ group.append(mark);
+ }
+
group
})
}
diff --git a/src/svg_render.rs b/src/svg_render.rs
index ce6e377..799f9a6 100644
--- a/src/svg_render.rs
+++ b/src/svg_render.rs
@@ -199,48 +199,55 @@ pub fn draw_face_points(
for &(x, y) in s {
let x_pos = value_to_face_offset(x, x_axis, face_width);
let y_pos = -value_to_face_offset(y, y_axis, face_height);
- let radius = f64::from(style.get_size());
- match style.get_marker() {
- style::PointMarker::Circle => {
- group.append(
- node::element::Circle::new()
- .set("cx", x_pos)
- .set("cy", y_pos)
- .set("r", radius)
- .set("fill", style.get_colour()),
- );
- }
- style::PointMarker::Square => {
- group.append(
- node::element::Rectangle::new()
- .set("x", x_pos - radius)
- .set("y", y_pos - radius)
- .set("width", 2. * radius)
- .set("height", 2. * radius)
- .set("fill", style.get_colour()),
- );
- }
- style::PointMarker::Cross => {
- let path = node::element::path::Data::new()
- .move_to((x_pos - radius, y_pos - radius))
- .line_by((radius * 2., radius * 2.))
- .move_by((-radius * 2., 0))
- .line_by((radius * 2., -radius * 2.))
- .close();
- group.append(
- node::element::Path::new()
- .set("fill", "none")
- .set("stroke", style.get_colour())
- .set("stroke-width", 2)
- .set("d", path),
- );
- }
- };
+ let mark = draw_marker(x_pos, y_pos, style);
+ group.append(mark);
}
group
}
+pub fn draw_marker(x_pos: f64, y_pos: f64, style: &style::PointStyle) -> node::element::Group {
+ let radius = f64::from(style.get_size());
+ let mut group = node::element::Group::new();
+ match style.get_marker() {
+ style::PointMarker::Circle => {
+ group.append(
+ node::element::Circle::new()
+ .set("cx", x_pos)
+ .set("cy", y_pos)
+ .set("r", radius)
+ .set("fill", style.get_colour()),
+ );
+ }
+ style::PointMarker::Square => {
+ group.append(
+ node::element::Rectangle::new()
+ .set("x", x_pos - radius)
+ .set("y", y_pos - radius)
+ .set("width", 2. * radius)
+ .set("height", 2. * radius)
+ .set("fill", style.get_colour()),
+ );
+ }
+ style::PointMarker::Cross => {
+ let path = node::element::path::Data::new()
+ .move_to((x_pos - radius, y_pos - radius))
+ .line_by((radius * 2., radius * 2.))
+ .move_by((-radius * 2., 0))
+ .line_by((radius * 2., -radius * 2.))
+ .close();
+ group.append(
+ node::element::Path::new()
+ .set("fill", "none")
+ .set("stroke", style.get_colour())
+ .set("stroke-width", 2)
+ .set("d", path),
+ );
+ }
+ };
+ group
+}
+
pub fn draw_face_bars(
h: &repr::Histogram,
x_axis: &axis::ContinuousAxis,
--
2.47.2
|