Skip to content

Commit 1e8dda8

Browse files
committed
Rename MeshStyle::line_style_1 to MeshStyle::bold_line_style and line_style_2 to light_line_style + make datetime coordinate follows the extra key points input feeded by hint
1 parent 0b6310a commit 1e8dda8

File tree

8 files changed

+114
-42
lines changed

8 files changed

+114
-42
lines changed

examples/boxplot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8080
.x_desc("Ping, ms")
8181
.y_desc("Host")
8282
.y_labels(host_list.len())
83-
.line_style_2(&WHITE)
83+
.light_line_style(&WHITE)
8484
.draw()?;
8585

8686
for (label, (values, style, offset)) in &series {
@@ -128,7 +128,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
128128
values_range.start - 10.0..values_range.end + 10.0,
129129
)?;
130130

131-
chart.configure_mesh().line_style_2(&WHITE).draw()?;
131+
chart.configure_mesh().light_line_style(&WHITE).draw()?;
132132
chart.draw_series(vec![
133133
Boxplot::new_vertical(SegmentValue::CenterOf(&"a"), &quartiles_a),
134134
Boxplot::new_vertical(SegmentValue::CenterOf(&"b"), &quartiles_b),
@@ -140,7 +140,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
140140
.caption("Horizontal Boxplot", ("sans-serif", 20).into_font())
141141
.build_ranged(-30f32..90f32, 0..3)?;
142142

143-
chart.configure_mesh().line_style_2(&WHITE).draw()?;
143+
chart.configure_mesh().light_line_style(&WHITE).draw()?;
144144
chart.draw_series(vec![
145145
Boxplot::new_horizontal(1, &quartiles_a),
146146
Boxplot::new_horizontal(2, &Quartiles::new(&[30])),

examples/histogram.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1515
chart
1616
.configure_mesh()
1717
.disable_x_mesh()
18-
.line_style_1(&WHITE.mix(0.3))
18+
.bold_line_style(&WHITE.mix(0.3))
1919
.y_desc("Count")
2020
.x_desc("Bucket")
2121
.axis_desc_style(("sans-serif", 15).into_font())

examples/stock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2323
.caption("MSFT Stock Price", ("sans-serif", 50.0).into_font())
2424
.build_ranged(from_date..to_date, 110f32..135f32)?;
2525

26-
chart.configure_mesh().line_style_2(&WHITE).draw()?;
26+
chart.configure_mesh().light_line_style(&WHITE).draw()?;
2727

2828
chart.draw_series(
2929
data.iter()

src/chart/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ impl<
248248
draw_y_axis: true,
249249
n_x_labels: 10,
250250
n_y_labels: 10,
251-
line_style_1: None,
252-
line_style_2: None,
251+
bold_line_style: None,
252+
light_line_style: None,
253253
x_label_style: None,
254254
y_label_style: None,
255255
format_x: &X::format,

src/chart/mesh.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::marker::PhantomData;
22

33
use super::builder::LabelAreaPosition;
44
use super::context::ChartContext;
5-
use crate::coord::{BoldPoints, MeshLine, Ranged, RangedCoord, ValueFormatter};
5+
use crate::coord::{BoldPoints, LightPoints, MeshLine, Ranged, RangedCoord, ValueFormatter};
66
use crate::drawing::DrawingAreaErrorKind;
77
use crate::style::{
88
AsRelative, Color, FontDesc, FontFamily, FontStyle, IntoTextStyle, RGBColor, ShapeStyle,
@@ -156,8 +156,8 @@ where
156156
pub(super) axis_desc_style: Option<TextStyle<'b>>,
157157
pub(super) x_desc: Option<String>,
158158
pub(super) y_desc: Option<String>,
159-
pub(super) line_style_1: Option<ShapeStyle>,
160-
pub(super) line_style_2: Option<ShapeStyle>,
159+
pub(super) bold_line_style: Option<ShapeStyle>,
160+
pub(super) light_line_style: Option<ShapeStyle>,
161161
pub(super) axis_style: Option<ShapeStyle>,
162162
pub(super) x_label_style: Option<TextStyle<'b>>,
163163
pub(super) y_label_style: Option<TextStyle<'b>>,
@@ -277,15 +277,15 @@ where
277277

278278
/// Set the style for the coarse grind grid
279279
/// - `style`: This is the coarse grind grid style
280-
pub fn line_style_1<T: Into<ShapeStyle>>(&mut self, style: T) -> &mut Self {
281-
self.line_style_1 = Some(style.into());
280+
pub fn bold_line_style<T: Into<ShapeStyle>>(&mut self, style: T) -> &mut Self {
281+
self.bold_line_style = Some(style.into());
282282
self
283283
}
284284

285285
/// Set the style for the fine grind grid
286286
/// - `style`: The fine grind grid style
287-
pub fn line_style_2<T: Into<ShapeStyle>>(&mut self, style: T) -> &mut Self {
288-
self.line_style_2 = Some(style.into());
287+
pub fn light_line_style<T: Into<ShapeStyle>>(&mut self, style: T) -> &mut Self {
288+
self.light_line_style = Some(style.into());
289289
self
290290
}
291291

@@ -362,12 +362,12 @@ where
362362
FontStyle::Normal,
363363
);
364364

365-
let mesh_style_1 = self
366-
.line_style_1
365+
let bold_style = self
366+
.bold_line_style
367367
.clone()
368368
.unwrap_or_else(|| (&default_mesh_color_1).into());
369-
let mesh_style_2 = self
370-
.line_style_2
369+
let light_style = self
370+
.light_line_style
371371
.clone()
372372
.unwrap_or_else(|| (&default_mesh_color_2).into());
373373
let axis_style = self
@@ -391,8 +391,11 @@ where
391391
.unwrap_or_else(|| x_label_style.clone());
392392

393393
target.draw_mesh(
394-
(self.n_y_labels * 10, self.n_x_labels * 10),
395-
&mesh_style_2,
394+
(
395+
LightPoints::new(self.n_y_labels, self.n_y_labels * 10),
396+
LightPoints::new(self.n_x_labels, self.n_x_labels * 10),
397+
),
398+
&light_style,
396399
&x_label_style,
397400
&y_label_style,
398401
|_| None,
@@ -412,7 +415,7 @@ where
412415

413416
target.draw_mesh(
414417
(BoldPoints(self.n_y_labels), BoldPoints(self.n_x_labels)),
415-
&mesh_style_1,
418+
&bold_style,
416419
&x_label_style,
417420
&y_label_style,
418421
|m| match m {

src/coord/datetime.rs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,8 @@ impl<T: TimeValue + Datelike + Clone> ValueFormatter<T> for Monthly<T> {
252252
}
253253
}
254254

255-
impl<T: TimeValue + Clone> Ranged for Monthly<T> {
256-
type FormatOption = crate::coord::ranged::NoDefaultFormatting;
257-
type ValueType = T;
258-
259-
fn range(&self) -> Range<T> {
260-
self.0.start.clone()..self.0.end.clone()
261-
}
262-
263-
fn map(&self, value: &Self::ValueType, limit: (i32, i32)) -> i32 {
264-
T::map_coord(value, &self.0.start, &self.0.end, limit)
265-
}
266-
267-
fn key_points<HintType: KeyPointHint>(&self, hint: HintType) -> Vec<Self::ValueType> {
255+
impl<T: TimeValue + Clone> Monthly<T> {
256+
fn bold_key_points<H: KeyPointHint>(&self, hint: &H) -> Vec<T> {
268257
let max_points = hint.max_num_points();
269258
let start_date = self.0.start.date_ceil();
270259
let end_date = self.0.end.date_floor();
@@ -356,7 +345,35 @@ impl<T: TimeValue + Clone> Ranged for Monthly<T> {
356345
}
357346
}
358347

359-
impl<T: TimeValue + Clone> DiscreteRanged for Monthly<T> {
348+
impl<T: TimeValue + Clone> Ranged for Monthly<T>
349+
where
350+
Range<T>: AsRangedCoord<Value = T>,
351+
{
352+
type FormatOption = crate::coord::ranged::NoDefaultFormatting;
353+
type ValueType = T;
354+
355+
fn range(&self) -> Range<T> {
356+
self.0.start.clone()..self.0.end.clone()
357+
}
358+
359+
fn map(&self, value: &Self::ValueType, limit: (i32, i32)) -> i32 {
360+
T::map_coord(value, &self.0.start, &self.0.end, limit)
361+
}
362+
363+
fn key_points<HintType: KeyPointHint>(&self, hint: HintType) -> Vec<Self::ValueType> {
364+
if hint.weight().allow_light_points() && self.size() <= hint.bold_points() * 2 {
365+
let coord: <Range<T> as AsRangedCoord>::CoordDescType = self.0.clone().into();
366+
let normal = coord.key_points(hint.max_num_points());
367+
return normal;
368+
}
369+
self.bold_key_points(&hint)
370+
}
371+
}
372+
373+
impl<T: TimeValue + Clone> DiscreteRanged for Monthly<T>
374+
where
375+
Range<T>: AsRangedCoord<Value = T>,
376+
{
360377
fn size(&self) -> usize {
361378
let (start_year, start_month) = {
362379
let ceil = self.0.start.date_ceil();
@@ -455,7 +472,10 @@ impl<T: TimeValue + Datelike + Clone> ValueFormatter<T> for Yearly<T> {
455472
}
456473
}
457474

458-
impl<T: TimeValue + Clone> Ranged for Yearly<T> {
475+
impl<T: TimeValue + Clone> Ranged for Yearly<T>
476+
where
477+
Range<T>: AsRangedCoord<Value = T>,
478+
{
459479
type FormatOption = crate::coord::ranged::NoDefaultFormatting;
460480
type ValueType = T;
461481

@@ -468,6 +488,9 @@ impl<T: TimeValue + Clone> Ranged for Yearly<T> {
468488
}
469489

470490
fn key_points<HintType: KeyPointHint>(&self, hint: HintType) -> Vec<Self::ValueType> {
491+
if hint.weight().allow_light_points() && self.size() <= hint.bold_points() * 2 {
492+
return Monthly(self.0.clone()).key_points(hint);
493+
}
471494
let max_points = hint.max_num_points();
472495
let start_date = self.0.start.date_ceil();
473496
let end_date = self.0.end.date_floor();
@@ -498,7 +521,10 @@ impl<T: TimeValue + Clone> Ranged for Yearly<T> {
498521
}
499522
}
500523

501-
impl<T: TimeValue + Clone> DiscreteRanged for Yearly<T> {
524+
impl<T: TimeValue + Clone> DiscreteRanged for Yearly<T>
525+
where
526+
Range<T>: AsRangedCoord<Value = T>,
527+
{
502528
fn size(&self) -> usize {
503529
let year_start = self.0.start.date_ceil().year();
504530
let year_end = self.0.end.date_floor().year();
@@ -883,6 +909,7 @@ mod test {
883909

884910
#[test]
885911
fn test_yearly_date_range() {
912+
use crate::coord::BoldPoints;
886913
let range = Utc.ymd(1000, 8, 5)..Utc.ymd(2999, 1, 1);
887914
let ranged_coord = range.yearly();
888915

@@ -910,7 +937,7 @@ mod test {
910937

911938
let range = Utc.ymd(2019, 8, 5)..Utc.ymd(2020, 1, 1);
912939
let ranged_coord = range.yearly();
913-
let kps = ranged_coord.key_points(23);
940+
let kps = ranged_coord.key_points(BoldPoints(23));
914941
assert!(kps.len() == 1);
915942
}
916943

@@ -919,13 +946,15 @@ mod test {
919946
let range = Utc.ymd(2019, 8, 5)..Utc.ymd(2020, 9, 1);
920947
let ranged_coord = range.monthly();
921948

922-
let kps = ranged_coord.key_points(15);
949+
use crate::coord::BoldPoints;
950+
951+
let kps = ranged_coord.key_points(BoldPoints(15));
923952

924953
assert!(kps.len() <= 15);
925954
assert!(kps.iter().all(|x| x.day() == 1));
926955
assert!(kps.into_iter().any(|x| x.month() != 9));
927956

928-
let kps = ranged_coord.key_points(5);
957+
let kps = ranged_coord.key_points(BoldPoints(5));
929958
assert!(kps.len() <= 5);
930959
assert!(kps.iter().all(|x| x.day() == 1));
931960
let kps: Vec<_> = kps.into_iter().map(|x| x.month()).collect();

src/coord/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub use numeric::{
4444
RangedCoordu128, RangedCoordu32, RangedCoordu64,
4545
};
4646
pub use ranged::{
47-
AsRangedCoord, BoldPoints, DefaultFormatting, KeyPointHint, KeyPointWeight, MeshLine,
48-
NoDefaultFormatting, Ranged, RangedCoord, ReversibleRanged, ValueFormatter,
47+
AsRangedCoord, BoldPoints, DefaultFormatting, KeyPointHint, KeyPointWeight, LightPoints,
48+
MeshLine, NoDefaultFormatting, Ranged, RangedCoord, ReversibleRanged, ValueFormatter,
4949
};
5050

5151
pub use partial_axis::{make_partial_axis, IntoPartialAxis};

src/coord/ranged.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,21 @@ pub enum KeyPointWeight {
4242
Any,
4343
}
4444

45+
impl KeyPointWeight {
46+
pub fn allow_light_points(&self) -> bool {
47+
match self {
48+
Self::Bold => false,
49+
Self::Any => true,
50+
}
51+
}
52+
}
53+
4554
pub trait KeyPointHint {
4655
fn max_num_points(&self) -> usize;
4756
fn weight(&self) -> KeyPointWeight;
57+
fn bold_points(&self) -> usize {
58+
self.max_num_points()
59+
}
4860
}
4961

5062
impl KeyPointHint for usize {
@@ -69,6 +81,34 @@ impl KeyPointHint for BoldPoints {
6981
}
7082
}
7183

84+
pub struct LightPoints {
85+
bold_points_num: usize,
86+
light_limit: usize,
87+
}
88+
89+
impl LightPoints {
90+
pub fn new(bold_count: usize, requested: usize) -> Self {
91+
Self {
92+
bold_points_num: bold_count,
93+
light_limit: requested,
94+
}
95+
}
96+
}
97+
98+
impl KeyPointHint for LightPoints {
99+
fn max_num_points(&self) -> usize {
100+
self.light_limit
101+
}
102+
103+
fn bold_points(&self) -> usize {
104+
self.bold_points_num
105+
}
106+
107+
fn weight(&self) -> KeyPointWeight {
108+
KeyPointWeight::Any
109+
}
110+
}
111+
72112
/// The trait that indicates we have a ordered and ranged value
73113
/// Which is used to describe the axis
74114
pub trait Ranged {

0 commit comments

Comments
 (0)