Skip to content

Commit c03b70a

Browse files
committed
Fix the formatting issue
1 parent 1bdc8b4 commit c03b70a

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

examples/3d-plot.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use plotters::prelude::*;
22
fn main() -> Result<(), Box<dyn std::error::Error>> {
3-
let area = SVGBackend::new("plotters-doc-data/3d-plot.svg", (600, 400)).into_drawing_area();
3+
let area = SVGBackend::new("plotters-doc-data/3d-plot.svg", (1024, 760)).into_drawing_area();
4+
45
area.fill(&WHITE)?;
6+
57
let x_axis = (-3.0..3.0).step(0.1);
68
let z_axis = (-3.0..3.0).step(0.1);
79

810
let mut chart = ChartBuilder::on(&area)
911
.caption(format!("3D Plot Test"), ("sans", 20))
10-
.build_cartesian_3d(-3.0..3.0, -3.0..3.0, -3.0..3.0)?;
12+
.build_cartesian_3d(x_axis.clone(), -3.0..3.0, z_axis.clone())?;
1113

1214
chart.with_projection(|mut pb| {
1315
pb.yaw = 0.5;
@@ -17,17 +19,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1719

1820
chart.configure_axes().draw()?;
1921

20-
let surface = SurfaceSeries::<f64, f64, f64>::new(
21-
x_axis.values(),
22-
z_axis.values(),
23-
|&x, &z| (x * x + z * z).cos(),
24-
&BLUE.mix(0.2),
25-
);
26-
2722
chart
28-
.draw_series(surface)?
23+
.draw_series(SurfaceSeries::<f64, _, f64>::new(
24+
x_axis.values(),
25+
z_axis.values(),
26+
|&x, &z| (x * x + z * z).cos(),
27+
&BLUE.mix(0.2),
28+
))?
2929
.label("Surface")
30-
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &BLUE));
30+
.legend(|(x, y)| Rectangle::new([(x + 5, y - 5), (x + 15, y + 5)], BLUE.mix(0.5).filled()));
3131

3232
chart
3333
.draw_series(LineSeries::new(

src/coord/ranged1d/types/numeric.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::convert::TryFrom;
22
use std::ops::Range;
33

44
use crate::coord::ranged1d::{
5-
AsRangedCoord, DefaultFormatting, DiscreteRanged, KeyPointHint, Ranged, ReversibleRanged,
5+
AsRangedCoord, DefaultFormatting, DiscreteRanged, KeyPointHint, NoDefaultFormatting, Ranged,
6+
ReversibleRanged, ValueFormatter,
67
};
78

89
macro_rules! impl_discrete_trait {
@@ -58,7 +59,7 @@ macro_rules! impl_reverse_mapping_trait {
5859
};
5960
}
6061
macro_rules! make_numeric_coord {
61-
($type:ty, $name:ident, $key_points:ident, $doc: expr) => {
62+
($type:ty, $name:ident, $key_points:ident, $doc: expr, $fmt: ident) => {
6263
#[doc = $doc]
6364
#[derive(Clone)]
6465
pub struct $name($type, $type);
@@ -68,7 +69,7 @@ macro_rules! make_numeric_coord {
6869
}
6970
}
7071
impl Ranged for $name {
71-
type FormatOption = DefaultFormatting;
72+
type FormatOption = $fmt;
7273
type ValueType = $type;
7374
#[allow(clippy::float_cmp)]
7475
fn map(&self, v: &$type, limit: (i32, i32)) -> i32 {
@@ -96,6 +97,9 @@ macro_rules! make_numeric_coord {
9697
}
9798
}
9899
};
100+
($type:ty, $name:ident, $key_points:ident, $doc: expr) => {
101+
make_numeric_coord!($type, $name, $key_points, $doc, DefaultFormatting);
102+
}
99103
}
100104

101105
macro_rules! gen_key_points_comp {
@@ -207,16 +211,28 @@ make_numeric_coord!(
207211
f32,
208212
RangedCoordf32,
209213
compute_f32_key_points,
210-
"The ranged coordinate for type f32"
214+
"The ranged coordinate for type f32",
215+
NoDefaultFormatting
211216
);
212217
impl_reverse_mapping_trait!(f32, RangedCoordf32);
218+
impl ValueFormatter<f32> for RangedCoordf32 {
219+
fn format(value: &f32) -> String {
220+
crate::data::float::pretty_print_float(*value as f64, false)
221+
}
222+
}
213223
make_numeric_coord!(
214224
f64,
215225
RangedCoordf64,
216226
compute_f64_key_points,
217-
"The ranged coordinate for type f64"
227+
"The ranged coordinate for type f64",
228+
NoDefaultFormatting
218229
);
219230
impl_reverse_mapping_trait!(f64, RangedCoordf64);
231+
impl ValueFormatter<f64> for RangedCoordf64 {
232+
fn format(value: &f64) -> String {
233+
crate::data::float::pretty_print_float(*value, false)
234+
}
235+
}
220236
make_numeric_coord!(
221237
u32,
222238
RangedCoordu32,

0 commit comments

Comments
 (0)