Skip to content

Commit eb3e168

Browse files
committed
Changed API of geometry primitives
1 parent 554527a commit eb3e168

File tree

10 files changed

+87
-61
lines changed

10 files changed

+87
-61
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[*.toml]
2+
indent_size = 2
3+
indent_style = space
4+
[*.{rs,md}]
5+
indent_style = space
6+
indent_size = 4

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ cairo = ["cairo-rs", "png", "gio", "gtk", "ux-animate", "ux-animate/cairo"]
2222
[dependencies]
2323
log = "0.4"
2424
lazy_static = "1.4"
25-
ux-primitives = { version = "0.1" }
2625
ux-animate = { version = "0.1", default-features = false, optional = true }
2726

2827
png = { version = "0.16", optional = true }
@@ -37,11 +36,15 @@ gloo = { version = "0.2", optional = true }
3736
js-sys = { version = "0.3", optional = true }
3837
wasm-bindgen-test = { version = "0.3", optional = true }
3938

39+
[dependencies.ux-primitives]
40+
version = "0.1"
41+
features = [ "canvas" ]
42+
4043
[dependencies.web-sys]
4144
version = "0.3"
4245
optional = true
4346
features = [
44-
'KeyboardEvent',
47+
"KeyboardEvent",
4548
"ValidityState",
4649
"CustomEvent",
4750
"Node",

src/bar.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
#![allow(dead_code)]
44

55
use std::{collections::HashMap, fmt, cell::RefCell, rc::Rc};
6-
use ux_primitives::{canvas::*, math::*};
6+
use ux_primitives::{
7+
canvas::CanvasContext,
8+
geom::{Point, Size, Rect}
9+
};
710

811
use crate::*;
912

@@ -77,8 +80,8 @@ struct BarChartProperties {
7780
x_label_hop: f64,
7881
/// Distance between two consecutive x-axis labels.
7982
y_label_hop: f64,
80-
x_title_box: Rectangle<f64>,
81-
y_title_box: Rectangle<f64>,
83+
x_title_box: Rect<f64>,
84+
y_title_box: Rect<f64>,
8285
x_title_center: Point<f64>,
8386
y_title_center: Point<f64>,
8487
x_labels: Vec<String>,

src/basechart.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::{borrow::Borrow, cell::RefCell, collections::HashMap, fmt, rc::Rc};
2-
use ux_primitives::{canvas::*, math::*};
2+
use ux_primitives::{
3+
canvas::CanvasContext,
4+
geom::{Point, Size, Rect}
5+
};
36

47
use super::*;
58

@@ -49,10 +52,10 @@ pub struct BaseChartProperties {
4952
pub tooltip_value_formatter: Option<ValueFormatter>,
5053

5154
/// Bounding box of the series and axes.
52-
pub series_and_axes_box: Rectangle<f64>,
55+
pub series_and_axes_box: Rect<f64>,
5356

5457
/// Bounding box of the chart title.
55-
pub title_box: Rectangle<f64>,
58+
pub title_box: Rect<f64>,
5659

5760
/// A list used to keep track of the visibility of the series.
5861
pub series_states: Vec<Visibility>,
@@ -216,11 +219,12 @@ where
216219
}
217220

218221
let mut props = self.props.borrow_mut();
219-
props.series_and_axes_box = Rectangle {
220-
left: CHART_PADDING,
221-
top: CHART_PADDING,
222-
width: props.width - 2.0 * CHART_PADDING,
223-
height: props.height - 2.0 * CHART_PADDING,
222+
props.series_and_axes_box = Rect {
223+
origin: Point::new(CHART_PADDING, CHART_PADDING),
224+
size: Size::new(
225+
props.width - 2.0 * CHART_PADDING,
226+
props.height - 2.0 * CHART_PADDING
227+
),
224228
};
225229

226230
// // Consider the title.
@@ -229,18 +233,15 @@ where
229233
match title.position {
230234
"above" => {
231235
title_y = CHART_PADDING;
232-
props.series_and_axes_box.top =
233-
props.series_and_axes_box.top + title_h + CHART_TITLE_MARGIN;
234-
props.series_and_axes_box.height =
235-
props.series_and_axes_box.height - title_h + CHART_TITLE_MARGIN;
236+
props.series_and_axes_box.origin.x += title_h + CHART_TITLE_MARGIN;
237+
props.series_and_axes_box.size.height -= title_h + CHART_TITLE_MARGIN;
236238
}
237239
"middle" => {
238240
title_y = f64::floor((props.height - title_h) / 2.0);
239241
}
240242
"below" => {
241243
title_y = props.height - title_h - CHART_PADDING;
242-
props.series_and_axes_box.height =
243-
props.series_and_axes_box.height - title_h + CHART_TITLE_MARGIN;
244+
props.series_and_axes_box.size.height -= title_h + CHART_TITLE_MARGIN;
244245
}
245246
_ => {}
246247
}
@@ -253,11 +254,9 @@ where
253254
// title_x = (width - titleW - 2 * TITLE_PADDING) ~/ 2;
254255
}
255256

256-
let title_box = Rectangle {
257-
left: title_x,
258-
top: title_y,
259-
width: title_w,
260-
height: title_h,
257+
let title_box = Rect {
258+
origin: Point::new(title_x, title_y),
259+
size: Size::new(title_w, title_h),
261260
};
262261

263262
// Consider the legend.

src/gauge.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
#![allow(dead_code)]
44

55
use std::{collections::HashMap, fmt, cell::RefCell, rc::Rc};
6-
use ux_primitives::{canvas::*, math::*};
6+
use ux_primitives::{
7+
canvas::CanvasContext,
8+
geom::Point
9+
};
710

811
use crate::*;
912

@@ -40,7 +43,7 @@ impl GaugeEntity {
4043

4144
pub fn contains_point(&self, p: Point<f64>) -> bool {
4245
// let p = p - center;
43-
let mag = p.magnitude();
46+
let mag = p.distance_to(Point::default()); //p.magnitude()
4447
if mag > self.outer_radius || mag < self.inner_radius {
4548
return false;
4649
}

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
#![allow(unused_variables)]
33

44
use std::{collections::HashMap, fmt, rc::Rc, cell::RefCell};
5-
use ux_primitives::{canvas::*, math::*};
5+
use ux_primitives::{
6+
canvas::CanvasContext,
7+
geom::Point
8+
};
69

710
#[macro_use]
811
extern crate lazy_static;
@@ -43,10 +46,10 @@ pub use utils::*;
4346

4447
pub const PI: f64 = std::f64::consts::PI;
4548
/// The 2*pi constant - TAU
46-
pub const TAU: f64 = 6.28318530717958647692528676655900577_f64;
49+
pub const TAU: f64 = std::f64::consts::TAU;
4750

4851
/// The pi/2 constant.
49-
pub const PI_2: f64 = 1.57079632679489661923132169163975144_f64;
52+
pub const PI_2: f64 = std::f64::consts::FRAC_PI_2;
5053

5154
pub const FONT_FAMILY: &str = r#""Segoe UI", "Open Sans", Verdana, Arial"#;
5255

@@ -86,7 +89,7 @@ pub fn default_value_formatter(value: f64) -> String {
8689
#[derive(Debug, Clone)]
8790
pub enum Visibility {
8891
Hidden,
89-
Hidding,
92+
Hiding,
9093
Showing,
9194
Shown,
9295
}

src/line.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
#![allow(dead_code)]
44

55
use std::{collections::HashMap, fmt, cell::RefCell, rc::Rc};
6-
use ux_primitives::{canvas::*, math::*};
6+
use ux_primitives::{
7+
canvas::CanvasContext,
8+
geom::{Point, Rect},
9+
};
710

811
use crate::*;
912

1013
#[derive(Default, Clone)]
11-
struct PointEntity {
14+
struct LinePoint {
1215
// Chart chart,
1316
color: String,
1417
highlight_color: String,
@@ -35,17 +38,14 @@ struct PointEntity {
3538
point_radius: f64,
3639
}
3740

38-
impl PointEntity {
41+
impl LinePoint {
3942
fn as_point(&self) -> Point<f64> {
40-
Point {
41-
x: self.x,
42-
y: self.y,
43-
}
43+
Point::new(self.x, self.y)
4444
}
4545
}
4646

4747
/// A point in a line chart.
48-
impl<C> Drawable<C> for PointEntity
48+
impl<C> Drawable<C> for LinePoint
4949
where
5050
C: CanvasContext,
5151
{
@@ -67,7 +67,7 @@ where
6767
}
6868
}
6969

70-
impl Entity for PointEntity {
70+
impl Entity for LinePoint {
7171
fn free(&mut self) {
7272
// chart = null;
7373
}
@@ -94,8 +94,8 @@ struct LineChartProperties {
9494
x_label_step: i64,
9595
x_label_hop: f64, // Distance between two consecutive x-axis labels.
9696
y_label_hop: f64, // Distance between two consecutive x-axis labels.
97-
x_title_box: Rectangle<f64>,
98-
y_title_box: Rectangle<f64>,
97+
x_title_box: Rect<f64>,
98+
y_title_box: Rect<f64>,
9999
x_title_center: Point<f64>,
100100
y_title_center: Point<f64>,
101101
x_labels: Vec<String>,
@@ -122,7 +122,7 @@ where
122122
D: fmt::Display,
123123
{
124124
props: RefCell<LineChartProperties>,
125-
base: BaseChart<'a, C, PointEntity, M, D, LineChartOptions<'a>>,
125+
base: BaseChart<'a, C, LinePoint, M, D, LineChartOptions<'a>>,
126126
}
127127

128128
impl<'a, C, M, D> LineChart<'a, C, M, D>
@@ -551,7 +551,7 @@ where
551551
// }
552552
}
553553

554-
fn lerp_points(&self, points: Vec<PointEntity>, percent: f64) -> Vec<PointEntity> {
554+
fn lerp_points(&self, points: Vec<LinePoint>, percent: f64) -> Vec<LinePoint> {
555555
// return points.map((p) {
556556
// let x = lerp(p.oldX, p.x, percent);
557557
// let y = lerp(p.oldY, p.y, percent);
@@ -580,7 +580,7 @@ where
580580
}
581581
}
582582

583-
impl<'a, C, M, D> Chart<PointEntity> for LineChart<'a, C, M, D>
583+
impl<'a, C, M, D> Chart<LinePoint> for LineChart<'a, C, M, D>
584584
where
585585
C: CanvasContext,
586586
M: fmt::Display,
@@ -594,7 +594,7 @@ where
594594
}
595595

596596
fn draw_series(&self, percent: f64) -> bool {
597-
fn curve_to(cp1: Point<f64>, cp2: Point<f64>, p: PointEntity) {
597+
fn curve_to(cp1: Point<f64>, cp2: Point<f64>, p: LinePoint) {
598598
// if cp2 == null && cp1 == null {
599599
// series_context.lineTo(p.x, p.y);
600600
// } else if cp2 == null {
@@ -786,7 +786,7 @@ where
786786
value: String,
787787
color: String,
788788
highlight_color: String,
789-
) -> PointEntity {
789+
) -> LinePoint {
790790
// let x = x_label_x(entityIndex);
791791
// let oldY = x_axis_top;
792792
// // oldCp1 and oldCp2 are calculated in [update_series].

src/pie.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
#![allow(dead_code)]
55

66
use std::{collections::HashMap, fmt, cell::RefCell, rc::Rc};
7-
use ux_primitives::{canvas::*, math::*};
7+
use ux_primitives::{
8+
canvas::CanvasContext,
9+
geom::Point
10+
};
811

912
use crate::*;
1013

@@ -43,7 +46,7 @@ impl PieEntity {
4346

4447
fn contains_point(&self, p: Point<f64>) -> bool {
4548
// p -= center;
46-
let mag = p.magnitude();
49+
let mag = p.distance_to(Point::default()); //p.magnitude();
4750
if mag > self.outer_radius || mag < self.inner_radius {
4851
return false;
4952
}
@@ -169,7 +172,7 @@ where
169172
}
170173

171174
fn get_entity_group_index(&self, x: f64, y: f64) -> i64 {
172-
let p = Point { x, y };
175+
let p = Point::new(x, y);
173176
// let entities = series_list.first.entities;
174177
// for (let i = entities.length - 1; i >= 0; i--) {
175178
// let pie = entities[i] as Pie;
@@ -209,8 +212,8 @@ where
209212
fn calculate_drawing_sizes(&self) {
210213
self.base.calculate_drawing_sizes();
211214
let rect = &self.base.props.borrow().series_and_axes_box;
212-
let half_w = rect.width as i64 >> 1;
213-
let half_h = rect.height as i64 >> 1;
215+
let half_w = rect.size.width as i64 >> 1;
216+
let half_h = rect.size.height as i64 >> 1;
214217

215218
// self.center = Point {
216219
// x: (rect.left + half_w) as f64,

0 commit comments

Comments
 (0)