Skip to content

Commit

Permalink
Merge branch 'main' into update-license
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-ng authored Jun 25, 2024
2 parents eff2ed0 + 13a8971 commit 5ca0375
Show file tree
Hide file tree
Showing 36 changed files with 632 additions and 166 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [0.9.0] - 2024-xx-xx
### Added
- [[#207](https://github.com/plotly/plotly,rs/pull/207)] Add `Table` trace.
- [[#181](https://github.com/plotly/plotly,rs/pull/181)] Fix compilation error when mixing the crate with `askama/with-axum` by adding `with-axum` feature.
- [[#180](https://github.com/plotly/plotly.rs/pull/180)] Add setter for `Mapbox::domain`.
- [[#178](https://github.com/plotly/plotly.rs/pull/178)] Fix setter for `Axis::matches` to take string arg.
- [[#166](https://github.com/plotly/plotly.rs/pull/166)] Added subplot example with multiple titles.
- [[#163](https://github.com/plotly/plotly.rs/pull/163)] Added `DensityMapbox`.
- [[#161](https://github.com/plotly/plotly.rs/pull/161)] Added `Axis` `scaleanchor` settter.
- [[#159](https://github.com/plotly/plotly.rs/pull/159)] Make `heat_map` module public to expose `Smoothing enum`.
- [[#157](https://github.com/plotly/plotly.rs/pull/157)] Fix `HeatMap`'s setters for correctly setting `zmin`, `zmax` and `zmin` independent of `Z` input type.
- [[#154](https://github.com/plotly/plotly.rs/pull/154)] Improve ergonomics of `Title` and `LegendGroupTitle` structs: `new` method now takes no arguments as per other structs, whilst a new `with_text()` constructor is added for convenience. Where other structs contain a `Title` (and `LegendGroupTitle`), users can now call the `title()` (and `legend_group_title()`) method with anything that `impl`s `Into<Title>`, viz. `String`, `&String`, `&str` and `Title`.
- [[#153](https://github.com/plotly/plotly.rs/pull/153)] Added `LayoutScene`.
- [[#212](https://github.com/plotly/plotly.rs/pull/212)] Update LICENSE

Expand Down
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" }
85 changes: 77 additions & 8 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},
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 All @@ -66,27 +69,27 @@ fn customized_scatter3d_plot() {
let front_color: Rgb = Rgb::new(255, 255, 255);

let layout = Layout::new()
.title("Helix".into())
.title("Helix")
.legend(Legend::new().x(0.9).y(0.9))
.font(Font::new().color(front_color))
.paper_background_color(background_color)
.scene(
LayoutScene::new()
.x_axis(
Axis::new()
.title("x (A meaningful axis name goes here)".into())
.title("x (A meaningful axis name goes here)")
.tick_angle(0f64)
.grid_color(front_color)
.color(front_color),
)
.y_axis(
Axis::new()
.title(Title::new("This is the label of the Y axis"))
.title("This is the label of the Y axis")
.tick_format(".1f")
.grid_color(front_color)
.color(front_color),
)
.z_axis(Axis::new().title("".into()).tick_values(vec![]))
.z_axis(Axis::new().title("").tick_values(vec![]))
.aspect_mode(plotly::layout::AspectMode::Manual)
.aspect_ratio((3.0, 1.0, 1.0).into())
.camera(
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:?}"))
.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
38 changes: 25 additions & 13 deletions examples/basic_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use plotly::{
color::{NamedColor, Rgb, Rgba},
common::{
ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode,
Orientation, Title,
Orientation,
},
layout::{Axis, BarMode, Layout, Legend, TicksDirection, TraceOrder},
sankey::{Line as SankeyLine, Link, Node},
Bar, Plot, Sankey, Scatter, ScatterPolar,
traces::table::{Cells, Header},
Bar, Plot, Sankey, Scatter, ScatterPolar, Table,
};
use rand_distr::{Distribution, Normal, Uniform};

Expand Down Expand Up @@ -117,9 +118,9 @@ fn data_labels_hover() {
plot.add_trace(trace2);

let layout = Layout::new()
.title("Data Labels Hover".into())
.x_axis(Axis::new().title("x".into()).range(vec![0.75, 5.25]))
.y_axis(Axis::new().title("y".into()).range(vec![0., 8.]));
.title("Data Labels Hover")
.x_axis(Axis::new().title("x").range(vec![0.75, 5.25]))
.y_axis(Axis::new().title("y").range(vec![0., 8.]));
plot.set_layout(layout);

plot.show();
Expand All @@ -142,7 +143,7 @@ fn data_labels_on_the_plot() {
plot.add_trace(trace2);

let layout = Layout::new()
.title("Data Labels on the Plot".into())
.title("Data Labels on the Plot")
.x_axis(Axis::new().range(vec![0.75, 5.25]))
.y_axis(Axis::new().range(vec![0., 8.]));
plot.set_layout(layout);
Expand Down Expand Up @@ -215,14 +216,14 @@ fn colored_and_styled_scatter_plot() {
.marker(Marker::new().color(Rgb::new(142, 124, 195)).size(12));

let layout = Layout::new()
.title(Title::new("Quarter 1 Growth"))
.title("Quarter 1 Growth")
.x_axis(
Axis::new()
.title(Title::new("GDP per Capita"))
.title("GDP per Capita")
.show_grid(false)
.zero_line(false),
)
.y_axis(Axis::new().title(Title::new("Percent")).show_line(false));
.y_axis(Axis::new().title("Percent").show_line(false));
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
Expand Down Expand Up @@ -279,7 +280,7 @@ fn adding_names_to_line_and_scatter_plot() {
.mode(Mode::LinesMarkers)
.name("Scatter + Lines");

let layout = Layout::new().title(Title::new("Adding Names to Line and Scatter Plot"));
let layout = Layout::new().title("Adding Names to Line and Scatter Plot");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
Expand All @@ -304,7 +305,7 @@ fn line_and_scatter_styling() {
.marker(Marker::new().color(Rgb::new(128, 0, 128)).size(12))
.line(Line::new().color(Rgb::new(128, 0, 128)).width(1.0));

let layout = Layout::new().title(Title::new("Line and Scatter Styling"));
let layout = Layout::new().title("Line and Scatter Styling");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
Expand All @@ -325,7 +326,7 @@ fn styling_line_plot() {
.line(Line::new().color(Rgb::new(55, 128, 191)).width(1.0));

let layout = Layout::new()
.title(Title::new("Styling Line Plot"))
.title("Styling Line Plot")
.width(500)
.height(500);
let mut plot = Plot::new();
Expand Down Expand Up @@ -594,7 +595,7 @@ fn basic_sankey_diagram() {
);

let layout = Layout::new()
.title("Basic Sankey".into())
.title("Basic Sankey")
.font(Font::new().size(10));

let mut plot = Plot::new();
Expand All @@ -604,6 +605,16 @@ fn basic_sankey_diagram() {
plot.show();
}

fn table_chart() {
let trace = Table::new(
Header::new(vec![String::from("col1"), String::from("col2")]),
Cells::new(vec![vec![1, 2], vec![2, 3]]),
);
let mut plot = Plot::new();
plot.add_trace(trace);
plot.show();
}

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

Expand All @@ -629,6 +640,7 @@ fn main() {
// basic_bar_chart();
// grouped_bar_chart();
// stacked_bar_chart();
// table_chart();

// Sankey Diagrams
// basic_sankey_diagram();
Expand Down
6 changes: 3 additions & 3 deletions examples/financial_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::env;
use std::path::PathBuf;

use plotly::common::{TickFormatStop, Title};
use plotly::common::TickFormatStop;
use plotly::layout::{Axis, RangeSelector, RangeSlider, SelectorButton, SelectorStep, StepMode};
use plotly::{Candlestick, Layout, Ohlc, Plot, Scatter};
use serde::Deserialize;
Expand Down Expand Up @@ -50,7 +50,7 @@ fn time_series_plot_with_custom_date_range() {

let layout = Layout::new()
.x_axis(Axis::new().range(vec!["2016-07-01", "2016-12-31"]))
.title(Title::new("Manually Set Date Range"));
.title("Manually Set Date Range");
plot.set_layout(layout);

plot.show();
Expand All @@ -68,7 +68,7 @@ fn time_series_with_range_slider() {

let layout = Layout::new()
.x_axis(Axis::new().range_slider(RangeSlider::new().visible(true)))
.title(Title::new("Manually Set Date Range"));
.title("Manually Set Date Range");
plot.set_layout(layout);

plot.show();
Expand Down
Loading

0 comments on commit 5ca0375

Please sign in to comment.