Skip to content

Commit 2107563

Browse files
committed
add scatter_geo example in examples/maps
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 5e9d82f commit 2107563

File tree

3 files changed

+95
-37
lines changed

3 files changed

+95
-37
lines changed

examples/maps/src/main.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![allow(dead_code)]
22

33
use plotly::{
4+
color::Rgb,
45
common::Marker,
5-
layout::{Center, DragMode, Mapbox, MapboxStyle, Margin},
6-
DensityMapbox, Layout, Plot, ScatterMapbox,
6+
layout::{Axis, Center, DragMode, LayoutGeo, Mapbox, MapboxStyle, Margin, Projection},
7+
DensityMapbox, Layout, Plot, ScatterGeo, ScatterMapbox,
78
};
89

910
fn scatter_mapbox(show: bool, file_name: &str) {
@@ -30,6 +31,48 @@ fn scatter_mapbox(show: bool, file_name: &str) {
3031
}
3132
}
3233

34+
fn scatter_geo(show: bool, file_name: &str) {
35+
// Reproduce the Earth from https://plotly.com/javascript/lines-on-maps/#lines-on-an-orthographic-map
36+
let trace =
37+
ScatterGeo::new(vec![45.5017], vec![-73.5673]).marker(Marker::new().size(25).opacity(0.9));
38+
39+
let layout = Layout::new()
40+
.drag_mode(DragMode::Zoom)
41+
.margin(Margin::new().top(0).left(0).bottom(0).right(0))
42+
.geo(
43+
LayoutGeo::new()
44+
.showocean(true)
45+
.showlakes(true)
46+
.showcountries(true)
47+
.showland(true)
48+
.oceancolor(Rgb::new(0, 255, 255))
49+
.lakecolor(Rgb::new(0, 255, 255))
50+
.landcolor(Rgb::new(230, 145, 56))
51+
.lataxis(
52+
Axis::new()
53+
.show_grid(true)
54+
.grid_color(Rgb::new(102, 102, 102)),
55+
)
56+
.lonaxis(
57+
Axis::new()
58+
.show_grid(true)
59+
.grid_color(Rgb::new(102, 102, 102)),
60+
)
61+
.projection(
62+
Projection::new().projection_type(plotly::layout::ProjectionType::Orthographic),
63+
),
64+
);
65+
66+
let mut plot = Plot::new();
67+
plot.add_trace(trace);
68+
plot.set_layout(layout);
69+
70+
let path = write_example_to_html(&plot, file_name);
71+
if show {
72+
plot.show_html(path);
73+
}
74+
}
75+
3376
fn density_mapbox(show: bool, file_name: &str) {
3477
let trace = DensityMapbox::new(vec![45.5017], vec![-73.5673], vec![0.75]).zauto(true);
3578

@@ -63,5 +106,6 @@ fn write_example_to_html(plot: &Plot, name: &str) -> String {
63106
fn main() {
64107
// Change false to true on any of these lines to display the example.
65108
scatter_mapbox(false, "scatter_mapbox");
109+
scatter_geo(false, "scatter_geo");
66110
density_mapbox(false, "density_mapbox");
67111
}

plotly/src/layout/geo.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use plotly_derive::FieldSetter;
2+
use serde::Serialize;
3+
4+
use crate::color::Color;
5+
use crate::layout::{Axis, Center, Projection};
6+
7+
#[derive(Serialize, Clone, Debug, FieldSetter)]
8+
9+
pub struct LayoutGeo {
10+
/// Sets the latitude and longitude of the center of the map.
11+
center: Option<Center>,
12+
/// Sets the domain within which the mapbox will be drawn.
13+
/// Sets the zoom level of the map.
14+
zoom: Option<u8>,
15+
/// Sets the projection of the map
16+
#[field_setter(default = "Projection::new().projection_type(ProjectionType::Orthographic)")]
17+
projection: Option<Projection>,
18+
/// If to show the ocean or not
19+
#[field_setter(default = "Some(true)")]
20+
showocean: Option<bool>,
21+
/// Sets the color of the ocean
22+
#[field_setter(default = "'rgb(0, 255, 255)'")]
23+
oceancolor: Option<Box<dyn Color>>,
24+
/// If to show the land or not
25+
showland: Option<bool>,
26+
/// Sets the color of the land
27+
landcolor: Option<Box<dyn Color>>,
28+
/// If to show lakes or not
29+
showlakes: Option<bool>,
30+
/// Sets the color of the lakes
31+
lakecolor: Option<Box<dyn Color>>,
32+
/// If to show countries (borders) or not
33+
showcountries: Option<bool>,
34+
/// Configures the longitude axis
35+
lonaxis: Option<Axis>,
36+
/// Configures the latitude axis
37+
lataxis: Option<Axis>,
38+
}
39+
40+
impl LayoutGeo {
41+
pub fn new() -> Self {
42+
Default::default()
43+
}
44+
}

plotly/src/layout/mod.rs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod update_menu;
1313

1414
mod annotation;
1515
mod axis;
16+
mod geo;
1617
mod grid;
1718
mod legend;
1819
mod mapbox;
@@ -27,6 +28,7 @@ pub use self::axis::{
2728
RangeMode, RangeSelector, RangeSlider, RangeSliderYAxis, SelectorButton, SelectorStep,
2829
SliderRangeMode, StepMode, TicksDirection, TicksPosition,
2930
};
31+
pub use self::geo::LayoutGeo;
3032
pub use self::grid::{GridDomain, GridPattern, GridXSide, GridYSide, LayoutGrid, RowOrder};
3133
pub use self::legend::{Legend, TraceOrder};
3234
pub use self::mapbox::{Center, Mapbox, MapboxStyle};
@@ -137,41 +139,6 @@ pub enum SelectDirection {
137139
Any,
138140
}
139141

140-
#[derive(Serialize, Clone, Debug, FieldSetter)]
141-
pub struct Geo {
142-
/// Sets the zoom level of the map.
143-
zoom: Option<u8>,
144-
/// Sets the projection of the map
145-
#[field_setter(default = "Projection::new().projection_type(ProjectionType::Orthographic)")]
146-
projection: Option<Projection>,
147-
/// If to show the ocean or not
148-
#[field_setter(default = "Some(true)")]
149-
showocean: Option<bool>,
150-
/// Sets the color of the ocean
151-
#[field_setter(default = "'rgb(0, 255, 255)'")]
152-
oceancolor: Option<Box<dyn Color>>,
153-
/// If to show the land or not
154-
showland: Option<bool>,
155-
/// Sets the color of the land
156-
landcolor: Option<Box<dyn Color>>,
157-
/// If to show lakes or not
158-
showlakes: Option<bool>,
159-
/// Sets the color of the lakes
160-
lakecolor: Option<Box<dyn Color>>,
161-
/// If to show countries (borders) or not
162-
showcountries: Option<bool>,
163-
/// Configures the longitude axis
164-
lonaxis: Option<Axis>,
165-
/// Configures the latitude axis
166-
lataxis: Option<Axis>,
167-
}
168-
169-
impl Geo {
170-
pub fn new() -> Self {
171-
Default::default()
172-
}
173-
}
174-
175142
#[serde_with::skip_serializing_none]
176143
#[derive(Serialize, Debug, Clone, FieldSetter)]
177144
pub struct Template {
@@ -309,7 +276,10 @@ pub struct LayoutFields {
309276
y_axis8: Option<Box<Axis>>,
310277
#[serde(rename = "zaxis8")]
311278
z_axis8: Option<Box<Axis>>,
279+
// ternary: Option<LayoutTernary>,
312280
scene: Option<LayoutScene>,
281+
geo: Option<LayoutGeo>,
282+
// polar: Option<LayoutPolar>,
313283
annotations: Option<Vec<Annotation>>,
314284
shapes: Option<Vec<Shape>>,
315285
#[serde(rename = "newshape")]

0 commit comments

Comments
 (0)