I am using Plotters library (GitHub - plotters-rs/plotters: A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely π¦ ππ) to draw chart in web browser using rust compiled to webassembly. Recently I ran into a problem when I was trying to pass the chart to websocket set_on_message event and doing something with the graph there. This is the code where I encountered a problem:
let canvas_backend = CanvasBackend::new("canvas").expect("cannot find canvas");
let root_drawing_area = canvas_backend.into_drawing_area();
let mut chart = ChartBuilder::on(&root_drawing_area)
.set_label_area_size(LabelAreaPosition::Left, 50)
.set_label_area_size(LabelAreaPosition::Bottom, 50)
.build_cartesian_2d((1..1000).log_scale(), (1..1000).log_scale())
.unwrap();
let mut client = wasm_sockets::EventClient::new("ws://localhost:8000/ws")?;
client.set_on_message(Some(Box::new(
move |client: &wasm_sockets::EventClient, message: wasm_sockets::Message| {
let range = chart.x_range();
},
)));
Compiler is telling me an error: error[E0597]: root_drawing_area
DOES NOT LIVE LONG ENOUGH.
I guess I need to change root_drawing_area ownership to the closure, which i am passing to the set_on_message event. But I still cannot figure out how.
I have also tried to make the variables static, but also without success.
Could someone help me please with this problem? Thank you.