Pārlūkot izejas kodu

fix panning accuracy on mouse move events of charts

Berke 1 gadu atpakaļ
vecāks
revīzija
3427b4981b
3 mainītis faili ar 16 papildinājumiem un 46 dzēšanām
  1. 3 1
      src/charts/candlestick.rs
  2. 1 1
      src/charts/footprint.rs
  3. 12 44
      src/charts/heatmap.rs

+ 3 - 1
src/charts/candlestick.rs

@@ -76,8 +76,10 @@ impl CandlestickChart {
 
     fn calculate_range(&self) -> (i64, i64, f32, f32) {
         let chart = self.get_common_data();
+
+        let timestamp_latest = self.data_points.keys().last().map_or(0, |time| *time);
     
-        let latest: i64 = self.data_points.keys().last().map_or(0, |time| time - ((chart.translation.x*10000.0)*(self.timeframe as f32)) as i64);
+        let latest: i64 = timestamp_latest - ((chart.translation.x*8000.0)*(self.timeframe as f32)) as i64;
         let earliest: i64 = latest - ((6400000.0*self.timeframe as f32) / (chart.scaling / (chart.bounds.width/800.0))) as i64;
     
         let visible_klines = self.data_points.range(earliest..=latest);

+ 1 - 1
src/charts/footprint.rs

@@ -181,7 +181,7 @@ impl FootprintChart {
 
         let timestamp_latest = self.data_points.keys().last().unwrap_or(&0);
 
-        let latest: i64 = *timestamp_latest - ((chart.translation.x*1000.0)*(self.timeframe as f32)) as i64;
+        let latest: i64 = *timestamp_latest - ((chart.translation.x*800.0)*(self.timeframe as f32)) as i64;
         let earliest: i64 = latest - ((640000.0*self.timeframe as f32) / (chart.scaling / (chart.bounds.width/800.0))) as i64;
     
         let mut highest: f32 = 0.0;

+ 12 - 44
src/charts/heatmap.rs

@@ -26,7 +26,6 @@ pub struct HeatmapChart {
     chart: CommonChartData,
     data_points: BTreeMap<i64, (GroupedDepth, Vec<GroupedTrade>)>,
     tick_size: f32,
-    y_scaling: f32,
     size_filter: f32,
 }
 
@@ -50,7 +49,6 @@ impl HeatmapChart {
             chart: CommonChartData::default(),
             data_points: BTreeMap::new(),
             tick_size,
-            y_scaling: 0.0001,
             size_filter: 0.0,
         }
     }
@@ -146,7 +144,7 @@ impl HeatmapChart {
     fn calculate_range(&self) -> (i64, i64, f32, f32) {
         let timestamp_latest: &i64 = self.data_points.keys().last().unwrap_or(&0);
 
-        let latest: i64 = *timestamp_latest - (self.chart.translation.x*80.0) as i64;
+        let latest: i64 = *timestamp_latest - (self.chart.translation.x*60.0) as i64;
         let earliest: i64 = latest - (48000.0 / (self.chart.scaling / (self.chart.bounds.width/800.0))) as i64;
     
         let mut max_ask_price = f32::MIN;
@@ -366,11 +364,11 @@ impl canvas::Program<Message> for HeatmapChart {
                         Interaction::Drawing => None,
                         Interaction::Erasing => None,
                         Interaction::Panning { translation, start } => {
-                            Some(Message::Translated(
-                                translation
-                                    + (cursor_position - start)
-                                        * (1.0 / chart_state.scaling),
-                            ))
+                            Some(
+                                Message::Translated(
+                                    translation + (cursor_position - start) * (1.0 / chart_state.scaling),
+                                )
+                            )
                         }
                         Interaction::None => 
                             if chart_state.crosshair && cursor.is_over(bounds) {
@@ -388,49 +386,19 @@ impl canvas::Program<Message> for HeatmapChart {
                     (event_status, message)
                 }
                 mouse::Event::WheelScrolled { delta } => match delta {
-                    mouse::ScrollDelta::Lines { y, .. }
-                    | mouse::ScrollDelta::Pixels { y, .. } => {
+                    mouse::ScrollDelta::Lines { y, .. } | mouse::ScrollDelta::Pixels { y, .. } => {                        
                         if y < 0.0 && chart_state.scaling > Self::MIN_SCALING
-                            || y > 0.0 && chart_state.scaling < Self::MAX_SCALING
+                            || y > 0.0 && chart_state.scaling < Self::MAX_SCALING 
                         {
-                            //let old_scaling = self.scaling;
-
                             let scaling = (chart_state.scaling * (1.0 + y / 30.0))
-                                .clamp(
-                                    Self::MIN_SCALING, 
-                                    Self::MAX_SCALING,  
-                                );
-
-                            //let translation =
-                            //    if let Some(cursor_to_center) =
-                            //        cursor.position_from(bounds.center())
-                            //    {
-                            //        let factor = scaling - old_scaling;
-
-                            //        Some(
-                            //            self.translation
-                            //                - Vector::new(
-                            //                    cursor_to_center.x * factor
-                            //                        / (old_scaling
-                            //                            * old_scaling),
-                            //                    cursor_to_center.y * factor
-                            //                        / (old_scaling
-                            //                            * old_scaling),
-                            //                ),
-                            //        )
-                            //    } else {
-                            //        None
-                            //    };
-
-                            (
-                                event::Status::Captured,
-                                Some(Message::Scaled(scaling, None)),
-                            )
+                                .clamp(Self::MIN_SCALING, Self::MAX_SCALING);
+
+                            (event::Status::Captured, Some(Message::Scaled(scaling, None)))
                         } else {
                             (event::Status::Captured, None)
                         }
                     }
-                },
+                }
                 _ => (event::Status::Ignored, None),
             },
             _ => (event::Status::Ignored, None),