Load and process a dataset of BarDatum points.
(mut self, data: &Vec<impl PointDatum<T, U>>)
| 83 | |
| 84 | /// Load and process a dataset of BarDatum points. |
| 85 | pub fn load_data(mut self, data: &Vec<impl PointDatum<T, U>>) -> Result<Self, String> { |
| 86 | match self.x_scale { |
| 87 | Some(_) => {}, |
| 88 | _ => return Err("Please provide a scale for the X dimension before loading data".to_string()), |
| 89 | } |
| 90 | match self.y_scale { |
| 91 | Some(_) => {}, |
| 92 | _ => return Err("Please provide a scale for the Y dimension before loading data".to_string()), |
| 93 | } |
| 94 | |
| 95 | // If no keys were explicitly provided, extract the keys from the data. |
| 96 | if self.keys.len() == 0 { |
| 97 | self.keys = Self::extract_keys(&data); |
| 98 | } |
| 99 | |
| 100 | // Organize entries based on the order of the keys first, since displayed data |
| 101 | // should keep the order defined in the `keys` attribute. |
| 102 | for (i, key) in self.keys.iter_mut().enumerate() { |
| 103 | // Map the key to the corresponding color. |
| 104 | self.color_map.insert(key.clone(), self.colors[i].as_hex()); |
| 105 | } |
| 106 | |
| 107 | for datum in data.iter() { |
| 108 | let scaled_x = self.x_scale.unwrap().scale(&datum.get_x()); |
| 109 | let scaled_y = self.y_scale.unwrap().scale(&datum.get_y()); |
| 110 | let y_bandwidth_offset = { |
| 111 | if self.y_scale.unwrap().is_range_reversed() { |
| 112 | -self.y_scale.unwrap().bandwidth().unwrap() / 2_f32 |
| 113 | } else { |
| 114 | self.y_scale.unwrap().bandwidth().unwrap() / 2_f32 |
| 115 | } |
| 116 | }; |
| 117 | let x_bandwidth_offset = { |
| 118 | if self.x_scale.unwrap().is_range_reversed() { |
| 119 | -self.x_scale.unwrap().bandwidth().unwrap() / 2_f32 |
| 120 | } else { |
| 121 | self.x_scale.unwrap().bandwidth().unwrap() / 2_f32 |
| 122 | } |
| 123 | }; |
| 124 | self.entries.push(ScatterPoint::new(scaled_x + x_bandwidth_offset, scaled_y + y_bandwidth_offset, self.marker_type, 5, datum.get_x(), datum.get_y(), self.label_position, self.labels_visible, self.color_map.get(&datum.get_key()).unwrap().clone())); |
| 125 | } |
| 126 | |
| 127 | Ok(self) |
| 128 | } |
| 129 | |
| 130 | /// Extract the list of keys to use when stacking and coloring the bars. |
| 131 | fn extract_keys(data: &Vec<impl PointDatum<T, U>>) -> Vec<String> { |
no test coverage detected