-
I'd like to use common code for drawing a diagram in logarithmic coords as well as in linear, depending on a seting passed at runtime. I tried to use |
Beta Was this translation helpful? Give feedback.
Answered by
pkolaczk
Feb 2, 2023
Replies: 1 comment
-
I think I've found a solution: enum YSpec {
Linear(RangedCoordf32),
Log(LogCoord<f32>)
}
impl Ranged for YSpec {
type FormatOption = DefaultFormatting;
type ValueType = f32;
fn map(&self, value: &Self::ValueType, limit: (i32, i32)) -> i32 {
match self {
YSpec::Linear(range) => range.map(value, limit),
YSpec::Log(range) => range.map(value, limit),
}
}
fn key_points<Hint: KeyPointHint>(&self, hint: Hint) -> Vec<Self::ValueType> {
match self {
YSpec::Linear(range) => range.key_points(hint),
YSpec::Log(range) => range.key_points(hint)
}
}
fn range(&self) -> Range<Self::ValueType> {
match self {
YSpec::Linear(range) => range.range(),
YSpec::Log(range) => range.range()
}
}
} Then I can create the coord spec like this: let primary_y_spec: YSpec = match is_logarithmic {
true => YSpec::Log((0.01f32..max_value).log_scale().into()),
false => YSpec::Linear((0f32..max_value).into()),
}
let mut chart = ChartBuilder::on(&root)
.margin(10)
.x_label_area_size(60)
.y_label_area_size(150)
.build_cartesian_2d(0f32..max_time, primary_y_spec)
.unwrap(); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
pkolaczk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I've found a solution: