This commit is contained in:
Simon Gardling 2022-02-11 13:38:51 -05:00
parent 4c072d3d96
commit 08c9b1b8e3
3 changed files with 19 additions and 19 deletions

View File

@ -36,14 +36,16 @@ pub fn draw(
chart.configure_mesh().x_labels(3).y_labels(3).draw()?; chart.configure_mesh().x_labels(3).y_labels(3).draw()?;
let data = (1..=resolution) let data: Vec<(f32, f32)> = (1..=resolution)
.map(|x| ((x as f32 / resolution as f32) * (&absrange)) + &min_x) .map(|x| ((x as f32 / resolution as f32) * (&absrange)) + &min_x)
.map(|x| (x, func(x as f64) as f32)) .map(|x| (x, func(x as f64) as f32))
.filter(|(_, y)| &min_y <= y && y <= &max_y); .filter(|(_, y)| &min_y <= y && y <= &max_y)
.collect();
chart.draw_series(LineSeries::new(data, &RED))?; chart.draw_series(LineSeries::new(data, &RED))?;
let (data2, area) = integral_rectangles(min_x, step, num_interval, &func); // Get rectangle coordinates and the total area let (data2, area): (Vec<(f32, f32, f32)>, f32) =
integral_rectangles(min_x, step, num_interval, &func); // Get rectangle coordinates and the total area
// Draw rectangles // Draw rectangles
chart.draw_series( chart.draw_series(
@ -54,7 +56,7 @@ pub fn draw(
root.present()?; root.present()?;
let output = chart.into_coord_trans(); let output = chart.into_coord_trans();
return Ok(((output), area)); return Ok((output, area));
} }
// Creates and does the math for creating all the rectangles under the graph // Creates and does the math for creating all the rectangles under the graph

View File

@ -25,7 +25,7 @@ pub struct Point {
impl Chart { impl Chart {
pub fn draw( pub fn draw(
canvas: HtmlCanvasElement, canvas: HtmlCanvasElement,
func: &str, func_str: &str,
min_x: f32, min_x: f32,
max_x: f32, max_x: f32,
min_y: f32, min_y: f32,
@ -33,9 +33,9 @@ impl Chart {
num_interval: usize, num_interval: usize,
resolution: i32, resolution: i32,
) -> Result<Chart, JsValue> { ) -> Result<Chart, JsValue> {
let output = func_plot::draw( let draw_output = func_plot::draw(
canvas, canvas,
func, func_str,
min_x, min_x,
max_x, max_x,
min_y, min_y,
@ -44,15 +44,16 @@ impl Chart {
resolution, resolution,
) )
.map_err(|err| err.to_string())?; .map_err(|err| err.to_string())?;
let map_coord = output.0; let map_coord = draw_output.0;
Ok(Chart {
return Ok(Chart {
convert: Box::new(move |coord| map_coord(coord).map(|(x, y)| (x.into(), y.into()))), convert: Box::new(move |coord| map_coord(coord).map(|(x, y)| (x.into(), y.into()))),
area: output.1, area: draw_output.1,
}) });
} }
pub fn get_area(&self) -> Result<f32, JsValue> { pub fn get_area(&self) -> f32 {
return Ok(self.area); return self.area;
} }
pub fn coord(&self, x: i32, y: i32) -> Option<Point> { pub fn coord(&self, x: i32, y: i32) -> Option<Point> {

View File

@ -1,5 +1,3 @@
// If you only use `npm` you can simply
// import { Chart } from "wasm-demo" and remove `setup` call from `bootstrap.js`.
class Chart {} class Chart {}
const canvas = document.getElementById("canvas"); const canvas = document.getElementById("canvas");
@ -57,7 +55,7 @@ function onMouseMove(event) {
if (chart) { if (chart) {
var text = "Mouse is outside Chart."; var text = "Mouse is outside Chart.";
if(event.target == canvas) { if (event.target == canvas) {
let actualRect = canvas.getBoundingClientRect(); let actualRect = canvas.getBoundingClientRect();
let logicX = event.offsetX * canvas.width / actualRect.width; let logicX = event.offsetX * canvas.width / actualRect.width;
let logicY = event.offsetY * canvas.height / actualRect.height; let logicY = event.offsetY * canvas.height / actualRect.height;
@ -72,10 +70,9 @@ function onMouseMove(event) {
function updatePlot() { function updatePlot() {
status.innerText = `Rendering y=${math_function.value}...`; status.innerText = `Rendering y=${math_function.value}...`;
chart = null;
const start = performance.now(); const start = performance.now();
chart = Chart.draw(canvas, math_function.value, Number(minX.value), Number(maxX.value), Number(minY.value), Number(maxY.value), Number(num_interval.value), Number(resolution.value));
chart = Chart.draw(canvas, math_function.value, Number(minX.value), Number(maxX.value), Number(minY.value), Number(maxY.value), Number(num_interval.value), Number(resolution.value));
const end = performance.now(); const end = performance.now();
area_msg.innerText = `Estimated Area: ${chart.get_area()}`; area_msg.innerText = `Estimated Area: ${chart.get_area()}`;