Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor examples and readme
  • Loading branch information
mfreeborn committed Nov 1, 2022
commit 6fdac4f93b781f1a2220c05ab8a2ac9d6fac3bd7
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ gh-pages/
Untitled*
.ipynb_checkpoints/
.DS_Store
.vscode
.vscode
dist/
out.*
16 changes: 15 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
[workspace]
members = [
# Crates
"plotly",
"plotly_derive",
"plotly_kaleido"
"plotly_kaleido",

# Examples
"examples/3d_charts",
"examples/custom_controls",
"examples/basic_charts",
"examples/financial_charts",
"examples/kaleido",
"examples/ndarray",
"examples/scientific_charts",
"examples/shapes",
"examples/statistical_charts",
"examples/subplots",
"examples/wasm-yew-minimal",
]
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Any figure can be saved as an HTML file using the `Plot.write_html()` method. Th
```rust
use plotly::{Plot, Scatter};

let mut plot = Plot::new()
let mut plot = Plot::new();
let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
plot.add_trace(trace);

Expand Down Expand Up @@ -111,7 +111,7 @@ Exporting a simple plot looks like this:
```rust
use plotly::{ImageFormat, Plot};

let mut plot = Plot::new()
let mut plot = Plot::new();
let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
plot.add_trace(trace);

Expand Down Expand Up @@ -155,7 +155,7 @@ use yew::prelude::*;
pub fn plot_component() -> Html {
let p = yew_hooks::use_async::<_, _, ()>({
let id = "plot-div";
let mut plot = Plot::new()
let mut plot = Plot::new();
let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
plot.add_trace(trace);

Expand All @@ -165,12 +165,13 @@ pub fn plot_component() -> Html {
}
});

{
use_effect(move || {

use_effect_with_deps(move |_| {
p.run();
|| ()
});
}
}, (),
);


html! {
<div id="plot-div"></div>
Expand Down
9 changes: 9 additions & 0 deletions examples/3d_charts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "three-d-charts"
version = "0.1.0"
authors = ["Michael Freeborn <michaelfreeborn1@gmail.com>"]
edition = "2021"

[dependencies]
ndarray = "0.15.6"
plotly = { path = "../../plotly" }
6 changes: 6 additions & 0 deletions examples/3d_charts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 3D Charts

## How to Run

1. Configure which example(s) you want to run by commenting/uncommenting lines in the `main` function, located in `src/main.rs`.
2. Run `cargo run`.
51 changes: 23 additions & 28 deletions plotly/examples/plot3d.rs → examples/3d_charts/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use itertools_num::linspace;
#![allow(dead_code)]

use ndarray::Array;
use plotly::{
common::{ColorScale, ColorScalePalette, Marker, MarkerSymbol, Mode, Title},
layout::{Axis, Layout},
Plot, Scatter3D, Surface,
};

// 3D Scatter Plots
fn simple_scatter3d_plot(show: bool) {
fn simple_scatter3d_plot() {
let n: usize = 100;
let t: Vec<f64> = linspace(0., 10., n).collect();
let t: Vec<f64> = Array::linspace(0., 10., n).into_raw_vec();
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
let z: Vec<f64> = t.iter().map(|x| x.cos()).collect();

let trace = Scatter3D::new(t, y, z).mode(Mode::Markers);
let mut plot = Plot::new();
plot.add_trace(trace);

if show {
plot.show();
}
plot.show();
}

fn customized_scatter3d_plot(show: bool) {
fn customized_scatter3d_plot() {
let n: usize = 100;
let t: Vec<f64> = linspace::<f64>(0., 10., n).collect();
let t: Vec<f64> = Array::linspace(0., 10., n).into_raw_vec();
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
let z: Vec<f64> = t.iter().map(|x| x.cos()).collect();
let sizelookup = z.clone();
Expand Down Expand Up @@ -63,32 +63,28 @@ fn customized_scatter3d_plot(show: bool) {
.z_axis(Axis::new().title("z Axis".into()));
plot.set_layout(layout);

if show {
plot.show();
}
plot.show();
}

// 3D Line Plots
fn simple_line3d_plot(show: bool) {
fn simple_line3d_plot() {
let n: usize = 100;
let t: Vec<f64> = linspace(0., 10., n).collect();
let t: Vec<f64> = Array::linspace(0., 10., n).into_raw_vec();
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
let z: Vec<f64> = t.iter().map(|x| x.cos()).collect();

let trace = Scatter3D::new(t, y, z).mode(Mode::Lines);
let mut plot = Plot::new();
plot.add_trace(trace);

if show {
plot.show();
}
plot.show();
}

// 3D Surface Plot
fn surface_plot(show: bool) {
fn surface_plot() {
let n: usize = 100;
let x: Vec<f64> = linspace(-10., 10., n).collect();
let y: Vec<f64> = linspace(-10., 10., n).collect();
let x: Vec<f64> = Array::linspace(-10., 10., n).into_raw_vec();
let y: Vec<f64> = Array::linspace(-10., 10., n).into_raw_vec();
let z: Vec<Vec<f64>> = x
.iter()
.map(|i| {
Expand All @@ -102,16 +98,15 @@ fn surface_plot(show: bool) {
let mut plot = Plot::new();
plot.add_trace(trace);

if show {
plot.show();
}
plot.show();
}

fn main() -> std::io::Result<()> {
fn main() {
// Uncomment any of these lines to display the example.

// Scatter3D Plots
simple_scatter3d_plot(true);
simple_line3d_plot(true);
customized_scatter3d_plot(true);
surface_plot(true);
Ok(())
simple_scatter3d_plot();
simple_line3d_plot();
customized_scatter3d_plot();
surface_plot();
}
5 changes: 5 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Examples

This folder contains a multitude of usage examples covering as many features of the library as possible. Instructions on how to run each example can be found in each example's subdirectory.

Pull requests with more examples of different behaviour are always welcome.
11 changes: 11 additions & 0 deletions examples/basic_charts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "basic-charts"
version = "0.1.0"
authors = ["Michael Freeborn <michaelfreeborn1@gmail.com>"]
edition = "2021"

[dependencies]
ndarray = "0.15.6"
plotly = { path = "../../plotly" }
rand = "0.8.5"
rand_distr = "0.4.3"
6 changes: 6 additions & 0 deletions examples/basic_charts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Basic Charts

## How to Run

1. Configure which example(s) you want to run by commenting/uncommenting lines in the `main` function, located in `src/main.rs`.
2. Run `cargo run`.
Loading