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()?;
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, 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))?;
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
chart.draw_series(
@ -54,7 +56,7 @@ pub fn draw(
root.present()?;
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

View File

@ -25,7 +25,7 @@ pub struct Point {
impl Chart {
pub fn draw(
canvas: HtmlCanvasElement,
func: &str,
func_str: &str,
min_x: f32,
max_x: f32,
min_y: f32,
@ -33,9 +33,9 @@ impl Chart {
num_interval: usize,
resolution: i32,
) -> Result<Chart, JsValue> {
let output = func_plot::draw(
let draw_output = func_plot::draw(
canvas,
func,
func_str,
min_x,
max_x,
min_y,
@ -44,15 +44,16 @@ impl Chart {
resolution,
)
.map_err(|err| err.to_string())?;
let map_coord = output.0;
Ok(Chart {
let map_coord = draw_output.0;
return Ok(Chart {
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> {
return Ok(self.area);
pub fn get_area(&self) -> f32 {
return self.area;
}
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 {}
const canvas = document.getElementById("canvas");
@ -57,7 +55,7 @@ function onMouseMove(event) {
if (chart) {
var text = "Mouse is outside Chart.";
if(event.target == canvas) {
if (event.target == canvas) {
let actualRect = canvas.getBoundingClientRect();
let logicX = event.offsetX * canvas.width / actualRect.width;
let logicY = event.offsetY * canvas.height / actualRect.height;
@ -72,10 +70,9 @@ function onMouseMove(event) {
function updatePlot() {
status.innerText = `Rendering y=${math_function.value}...`;
chart = null;
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();
area_msg.innerText = `Estimated Area: ${chart.get_area()}`;