Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl Color for f64 (+ f32, i64, i32, ...) #155

Merged
merged 7 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/3d_charts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2021"

[dependencies]
ndarray = "0.15.6"
rand = "0.8.5"
plotly = { path = "../../plotly" }
77 changes: 73 additions & 4 deletions examples/3d_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
use ndarray::Array;
use plotly::{
color::Rgb,
common::{ColorScale, ColorScalePalette, Font, Marker, MarkerSymbol, Mode, Title},
common::{ColorBar, ColorScale, ColorScalePalette, Font, Marker, MarkerSymbol, Mode, Title},
layout::{Axis, Camera, Layout, LayoutScene, Legend, Margin, ProjectionType},
Mesh3D, Plot, Scatter3D, Surface,
};
use rand::Rng;

// 3D Scatter Plots
fn simple_scatter3d_plot() {
Expand Down Expand Up @@ -41,10 +42,11 @@ fn customized_scatter3d_plot() {
.map(|i| (i.abs() * 25f64) as usize)
.collect(),
)
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis)),
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis))
.color_array(z.clone()),
);

let trace2 = Scatter3D::new(t, z, y)
let trace2 = Scatter3D::new(t, z.clone(), y)
.name("Helix 2")
.mode(Mode::Markers)
.marker(
Expand All @@ -55,7 +57,8 @@ fn customized_scatter3d_plot() {
.map(|i| (i.abs() * 25f64) as usize)
.collect(),
)
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis)),
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis))
.color_array(z),
);

let mut plot = Plot::new();
Expand Down Expand Up @@ -161,13 +164,79 @@ fn mesh_3d_plot() {
plot.show();
}

fn colorscale_plot() {
let mut plot = Plot::new();

let x = (0..100)
.map(|x| ((x - 50) as f64) / 100f64)
.collect::<Vec<f64>>();

let y = x.clone();

let iproduct = |x: &[f64], y: &[f64]| -> Vec<(f64, f64)> {
let mut result = Vec::new();
for x in x {
for y in y {
result.push((*x, *y));
}
}
result
};

let ((x, y), z): ((Vec<f64>, Vec<f64>), Vec<f64>) = iproduct(&x, &y)
.into_iter()
.map(|(x, y)| ((x, y), -(x.powi(2) + y.powi(2)) + 0.5))
.unzip();

let color: Vec<f32> = z.clone().into_iter().rev().map(|x| x as f32).collect();
let _color: Vec<usize> = (0..z.len()).collect();
let _color: Vec<u8> = (0..z.len()).map(|x| x as u8).collect();
let _color: Vec<i16> = {
let mut rng = rand::thread_rng();
(0..z.len()).map(|_| rng.gen_range(0..100)).collect()
};

let color_max = color.iter().fold(f64::MIN, |acc, x| acc.max(*x as f64));

let colorscale = ColorScalePalette::YlGnBu;

let marker = Marker::new()
.color_array(color)
.color_scale(plotly::common::ColorScale::Palette(colorscale.clone()))
.cauto(false)
.cmax(color_max * 1.5)
.color_bar(ColorBar::new());

let scatter = Scatter3D::new(x, y, z).mode(Mode::Markers).marker(marker);

plot.add_trace(scatter);

let layout = Layout::new()
.font(Font::new().size(18).family("Palatino-Linotype"))
.title(format!("Colorscale: {colorscale:?}").as_str().into())
.width(1200)
.height(1000)
.scene(
LayoutScene::new()
.aspect_mode(plotly::layout::AspectMode::Data)
.x_axis(Axis::new().tick_format(".1f"))
.y_axis(Axis::new().tick_format(".1f"))
.z_axis(Axis::new().tick_format(".1f")),
);

plot.set_layout(layout);

plot.show();
}

fn main() {
// Uncomment any of these lines to display the example.

// Scatter3D Plots
// simple_scatter3d_plot();
// simple_line3d_plot();
// customized_scatter3d_plot();
// colorscale_plot();

// Surface Plots
// surface_plot();
Expand Down
25 changes: 25 additions & 0 deletions plotly/src/common/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ impl Color for &'static str {}
impl Color for String {}
impl Color for Rgb {}
impl Color for Rgba {}
impl Color for f64 {}
impl Color for f32 {}
impl Color for u64 {}
impl Color for u32 {}
impl Color for u16 {}
impl Color for u8 {}
impl Color for i64 {}
impl Color for i32 {}
impl Color for i16 {}
impl Color for i8 {}
impl Color for usize {}

/// ColorArray is only used internally to provide a helper method for converting
/// Vec<impl Color> to Vec<Box<dyn Color>>, as we would otherwise fall foul of
Expand Down Expand Up @@ -290,6 +301,20 @@ mod tests {
assert_eq!(to_value(color).unwrap(), json!("any_arbitrary_string"));
}

#[test]
fn test_serialize_numbers() {
assert_eq!(to_value(1f64).unwrap(), json!(1f64));
assert_eq!(to_value(1f32).unwrap(), json!(1f32));
assert_eq!(to_value(1i64).unwrap(), json!(1i64));
assert_eq!(to_value(1i32).unwrap(), json!(1i32));
assert_eq!(to_value(1i16).unwrap(), json!(1i16));
assert_eq!(to_value(1i8).unwrap(), json!(1i8));
assert_eq!(to_value(1u64).unwrap(), json!(1u64));
assert_eq!(to_value(1u32).unwrap(), json!(1u32));
assert_eq!(to_value(1u16).unwrap(), json!(1u16));
assert_eq!(to_value(1u8).unwrap(), json!(1u8));
}

#[test]
#[rustfmt::skip]
fn test_serialize_named_color() {
Expand Down
Loading