-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmain.rs
More file actions
144 lines (120 loc) · 3.4 KB
/
main.rs
File metadata and controls
144 lines (120 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// plotters-iced
//
// Iced backend for Plotters
// Copyright: 2022, Joylei <leingliu@gmail.com>
// License: MIT
/*!
- run the native version
```sh
cargo run --release --example split-chart
```
- run the web version with [trunk](https://trunkrs.dev/)
```sh
cd examples
trunk serve
```
*/
extern crate iced;
extern crate plotters;
use iced::{
widget::{Column, Container, Text},
window, Alignment, Element, Length,
};
use plotters::{coord::Shift, prelude::*};
use plotters_backend::DrawingBackend;
use plotters_iced::{plotters_backend, Chart, ChartWidget, DrawingArea};
const TITLE_FONT_SIZE: u16 = 22;
// antialiasing issue: https://github.com/iced-rs/iced/issues/1159
fn main() {
#[cfg(target_arch = "wasm32")]
{
console_log::init().expect("Initialize logger");
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
}
let app = iced::application(State::new, State::update, State::view)
.title("Split Chart Example")
.antialiasing(cfg!(not(target_arch = "wasm32")))
.subscription(|_| window::frames().map(|_| Message::Tick));
app.run().unwrap();
}
#[allow(unused)]
#[derive(Debug)]
enum Message {
Tick,
}
#[derive(Default)]
struct State {
chart: MyChart,
}
impl State {
fn new() -> Self {
Self::default()
}
fn update(&mut self, _message: Message) {}
fn view(&self) -> Element<'_, Message> {
let content = Column::new()
.spacing(20)
.align_x(Alignment::Start)
.width(Length::Fill)
.height(Length::Fill)
.push(Text::new("Iced test chart").size(TITLE_FONT_SIZE))
.push(self.chart.view());
Container::new(content)
.padding(5)
.center_x(Length::Fill)
.center_y(Length::Fill)
.into()
}
}
#[allow(unused)]
#[derive(Default)]
struct MyChart;
impl MyChart {
fn view(&self) -> Element<Message> {
let chart = ChartWidget::new(self)
.width(Length::Fill)
.height(Length::Fill);
chart.into()
}
}
impl Chart<Message> for MyChart {
type State = ();
// leave it empty
fn build_chart<DB: DrawingBackend>(&self, _state: &Self::State, _builder: ChartBuilder<DB>) {}
fn draw_chart<DB: DrawingBackend>(&self, _state: &Self::State, root: DrawingArea<DB, Shift>) {
let children = root.split_evenly((2, 2));
for (i, area) in children.iter().enumerate() {
let builder = ChartBuilder::on(area);
draw_chart(builder, i + 1);
}
}
}
fn draw_chart<DB: DrawingBackend>(mut chart: ChartBuilder<DB>, power: usize) {
let mut chart = chart
.margin(30)
.caption(format!("y=x^{}", power), ("sans-serif", 22))
.x_label_area_size(30)
.y_label_area_size(30)
.build_cartesian_2d(-1f32..1f32, -1.2f32..1.2f32)
.unwrap();
chart
.configure_mesh()
.x_labels(3)
.y_labels(3)
// .y_label_style(
// ("sans-serif", 15)
// .into_font()
// .color(&plotters::style::colors::BLACK.mix(0.8))
// .transform(FontTransform::RotateAngle(30.0)),
// )
.draw()
.unwrap();
chart
.draw_series(LineSeries::new(
(-50..=50)
.map(|x| x as f32 / 50.0)
.map(|x| (x, x.powf(power as f32))),
&RED,
))
.unwrap();
}