Skip to content

Commit 5a2ae4a

Browse files
committed
Fix rendering of scatter plots in text mode
This had got lost while being ported to Plot
1 parent 71fdb5e commit 5a2ae4a

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

examples/scatter_text.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() {
1212
(6.4, 4.3),
1313
(8.5, 3.7),
1414
];
15-
let s1 = Plot::new(data);
15+
let s1 = Plot::new(data).point_style(PointStyle::new().marker(PointMarker::Circle));
1616
let s2 = Plot::new(vec![(-1.4, 2.5), (7.2, -0.3)])
1717
.point_style(PointStyle::new().marker(PointMarker::Square));
1818

src/page.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<'a> Page<'a> {
8585
*/
8686
pub fn to_text(&self) -> Result<String> {
8787
let (width, height) = self.dimensions;
88-
// TODO compose multiple views into a plot
88+
// TODO compose multiple views into a page
8989
let view = self.views[0];
9090
view.to_text(width, height)
9191
}

src/repr/plot.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::axis;
2020
use crate::repr::ContinuousRepresentation;
2121
use crate::style::*;
2222
use crate::svg_render;
23+
use crate::text_render;
2324

2425
/// Representation of any plot with points in the XY plane, visualized as points and/or with lines
2526
/// in-between.
@@ -173,11 +174,34 @@ impl ContinuousRepresentation for Plot {
173174

174175
fn to_text(
175176
&self,
176-
_x_axis: &axis::ContinuousAxis,
177-
_y_axis: &axis::ContinuousAxis,
178-
_face_width: u32,
179-
_face_height: u32,
177+
x_axis: &axis::ContinuousAxis,
178+
y_axis: &axis::ContinuousAxis,
179+
face_width: u32,
180+
face_height: u32,
180181
) -> String {
181-
"".into()
182+
let face_lines = if let Some(line_style) = &self.line_style {
183+
unimplemented!("Text rendering does not yet support line plots")
184+
} else {
185+
(0..face_height)
186+
.map(|_| " ".repeat(face_width as usize))
187+
.collect::<Vec<String>>()
188+
.join("\n")
189+
};
190+
let face_points = if let Some(point_style) = &self.point_style {
191+
text_render::render_face_points(
192+
&self.data,
193+
x_axis,
194+
y_axis,
195+
face_width,
196+
face_height,
197+
&point_style,
198+
)
199+
} else {
200+
(0..face_height)
201+
.map(|_| " ".repeat(face_width as usize))
202+
.collect::<Vec<String>>()
203+
.join("\n")
204+
};
205+
text_render::overlay(&face_lines, &face_points, 0, 0)
182206
}
183207
}

0 commit comments

Comments
 (0)