Skip to content

Commit dc07948

Browse files
committed
Fix some value formatter issue
1 parent f42dd06 commit dc07948

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed

src/chart/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl<'a, DB: DrawingBackend, CT: ReverseCoordTranslate> ChartContext<'a, DB, CT>
274274
impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, Arc<RangedCoord<X, Y>>> {
275275
// TODO: All draw_series_impl is over strict on lifetime, because we don't have stable HKT,
276276
// what we can ensure is for all lifetime 'b the element reference &'b E is a iterator
277-
// of points reference with the same lifetime.
277+
// of points reference with the same lifetime.
278278
// However, this doesn't work if the coordinate doesn't live longer than the backend,
279279
// this is unneccessarily strct
280280
pub(super) fn draw_series_impl<E, R, S>(

src/coord/discrete.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use super::{AsRangedCoord, Ranged};
2-
use std::fmt::{Debug, Formatter, Result as FmtResult};
1+
use super::{AsRangedCoord, Ranged, ValueFormatter};
32
use std::ops::Range;
43

54
/// The trait indicates the coordinate is discrete
@@ -108,25 +107,28 @@ where
108107
impl<R: AsRangedCoord> IntoCentric for R where R::CoordDescType: DiscreteRanged {}
109108

110109
/// The value that used by the centric coordinate
111-
#[derive(Clone)]
110+
#[derive(Clone, Debug)]
112111
pub enum CentricValues<T> {
113112
Exact(T),
114113
CenterOf(T),
115114
Last,
116115
}
117116

118-
impl<D: Debug> Debug for CentricValues<D> {
119-
fn fmt(&self, formatter: &mut Formatter) -> FmtResult {
120-
match self {
121-
CentricValues::Exact(value) => write!(formatter, "{:?}", value),
122-
CentricValues::CenterOf(value) => write!(formatter, "{:?}", value),
123-
CentricValues::Last => Ok(()),
117+
impl <T, D: DiscreteRanged + Ranged<ValueType = T>> ValueFormatter<CentricValues<T>> for CentricDiscreteRange<D>
118+
where
119+
D: ValueFormatter<T>
120+
{
121+
fn format(value: &CentricValues<T>) -> String {
122+
match value {
123+
CentricValues::Exact(ref value) => D::format(value),
124+
CentricValues::CenterOf(ref value) => D::format(value),
125+
_ => "".to_string()
124126
}
125127
}
126128
}
127129

128130
impl<D: DiscreteRanged> Ranged for CentricDiscreteRange<D> {
129-
type FormatOption = crate::coord::ranged::DefaultFormatting;
131+
type FormatOption = crate::coord::ranged::NoDefaultFormatting;
130132
type ValueType = CentricValues<D::ValueType>;
131133

132134
fn map(&self, value: &Self::ValueType, limit: (i32, i32)) -> i32 {

src/coord/group_by.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::numeric::RangedCoordusize;
2-
use super::{AsRangedCoord, DiscreteRanged, Ranged};
2+
use super::{AsRangedCoord, DiscreteRanged, Ranged, ValueFormatter};
33
use std::ops::Range;
44

55
/// The ranged value spec that needs to be grouped.
@@ -37,8 +37,14 @@ impl<T: DiscreteRanged> DiscreteRanged for GroupBy<T> {
3737
}
3838
}
3939

40+
impl<T, R: DiscreteRanged<ValueType = T> + ValueFormatter<T>> ValueFormatter<T> for GroupBy<R> {
41+
fn format(value: &T) -> String {
42+
R::format(value)
43+
}
44+
}
45+
4046
impl<T: DiscreteRanged> Ranged for GroupBy<T> {
41-
type FormatOption = crate::coord::ranged::DefaultFormatting;
47+
type FormatOption = crate::coord::ranged::NoDefaultFormatting;
4248
type ValueType = T::ValueType;
4349
fn map(&self, value: &T::ValueType, limit: (i32, i32)) -> i32 {
4450
self.0.map(value, limit)

src/coord/linspace.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{numeric::RangedCoordusize, AsRangedCoord, DiscreteRanged, Ranged};
1+
use super::{numeric::RangedCoordusize, AsRangedCoord, DiscreteRanged, Ranged, ValueFormatter};
22
use std::cmp::{Ordering, PartialOrd};
33
use std::marker::PhantomData;
44
use std::ops::{Add, Range, Sub};
@@ -251,11 +251,23 @@ where
251251
}
252252
}
253253

254+
impl<T, R, S, RM> ValueFormatter<T> for Linspace<R, S, RM>
255+
where
256+
R: Ranged<ValueType = T> + ValueFormatter<T>,
257+
RM: LinspaceRoundingMethod<T>,
258+
T: Add<S, Output = T> + PartialOrd + Clone,
259+
S: Clone,
260+
{
261+
fn format(value: &T) -> String {
262+
R::format(value)
263+
}
264+
}
265+
254266
impl<T: Ranged, S: Clone, R: LinspaceRoundingMethod<T::ValueType>> Ranged for Linspace<T, S, R>
255267
where
256268
T::ValueType: Add<S, Output = T::ValueType> + PartialOrd + Clone,
257269
{
258-
type FormatOption = crate::coord::ranged::DefaultFormatting;
270+
type FormatOption = crate::coord::ranged::NoDefaultFormatting;
259271
type ValueType = T::ValueType;
260272

261273
fn range(&self) -> Range<T::ValueType> {

src/coord/nested.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use super::{AsRangedCoord, DiscreteRanged, Ranged};
2-
use std::fmt::{Debug, Formatter, Result as FmtResult};
1+
use super::{AsRangedCoord, DiscreteRanged, Ranged, ValueFormatter};
32
use std::ops::Range;
43

54
/// Describe a value for a nested croodinate
6-
#[derive(PartialEq, Eq, Clone)]
5+
#[derive(PartialEq, Eq, Clone, Debug)]
76
pub enum NestedValue<C, V> {
87
/// Category value
98
Category(C),
@@ -40,15 +39,6 @@ impl<C, V> From<C> for NestedValue<C, V> {
4039
}
4140
}
4241

43-
impl<C: Debug, V: Debug> Debug for NestedValue<C, V> {
44-
fn fmt(&self, formatter: &mut Formatter) -> FmtResult {
45-
match self {
46-
NestedValue::Category(cat) => write!(formatter, "{:?}", cat),
47-
NestedValue::Value(_, val) => write!(formatter, "{:?}", val),
48-
}
49-
}
50-
}
51-
5242
/// A nested coordinate spec which is a discrete coordinate on the top level and
5343
/// for each value in discrete value, there is a secondary coordinate system.
5444
/// And the value is defined as a tuple of primary coordinate value and secondary
@@ -58,8 +48,23 @@ pub struct NestedRange<Primary: DiscreteRanged, Secondary: Ranged> {
5848
secondary: Vec<Secondary>,
5949
}
6050

51+
impl <PT, ST, P, S> ValueFormatter<NestedValue<PT, ST>> for NestedRange<P, S>
52+
where
53+
P: Ranged<ValueType = PT> + DiscreteRanged,
54+
S: Ranged<ValueType = ST>,
55+
P: ValueFormatter<PT>,
56+
S: ValueFormatter<ST>,
57+
{
58+
fn format(value: &NestedValue<PT, ST>) -> String{
59+
match value {
60+
NestedValue::Category(cat) => P::format(cat),
61+
NestedValue::Value(_, val) => S::format(val),
62+
}
63+
}
64+
}
65+
6166
impl<P: DiscreteRanged, S: Ranged> Ranged for NestedRange<P, S> {
62-
type FormatOption = crate::coord::ranged::DefaultFormatting;
67+
type FormatOption = crate::coord::ranged::NoDefaultFormatting;
6368
type ValueType = NestedValue<P::ValueType, S::ValueType>;
6469

6570
fn range(&self) -> Range<Self::ValueType> {

0 commit comments

Comments
 (0)