pub struct Chart { /* private fields */ }Expand description
A chart, bound to one canvas element.
The chart owns its option, its layout, and its display list. Setting an option invalidates the layout; rendering rebuilds both and hands the resulting display list to the backend.
The backend is chosen when the chart is created: WebGPU if the host has it, WebGL2 if it does not, Canvas2D otherwise. From the outside the three behave identically — the same option produces the same picture, and the same API draws it.
Implementations§
Source§impl Chart
Inherent implementation of Chart.
impl Chart
Inherent implementation of Chart.
Sourcepub async fn create(canvas: HtmlCanvasElement) -> Option<Self>
pub async fn create(canvas: HtmlCanvasElement) -> Option<Self>
Creates a chart on the given canvas.
The backend is chosen automatically: WebGPU when the host has it, WebGL2 when it does not, and Canvas2D everywhere else. The chart looks the same whichever one it gets.
Returns None when the host has no DOM, or when the canvas
cannot give out a rendering context at all.
§Arguments
HtmlCanvasElement- TheHtmlCanvasElementvalue.
§Returns
Option<Self>- TheOption<Self>value.
Sourcepub async fn create_with_backend(
canvas: HtmlCanvasElement,
preferred: BackendKind,
) -> Option<Self>
pub async fn create_with_backend( canvas: HtmlCanvasElement, preferred: BackendKind, ) -> Option<Self>
Creates a chart on the given canvas, preferring a particular backend.
The preference is honoured when the host can provide it; the factory falls back to the next backend down when it cannot.
§Arguments
HtmlCanvasElement- TheHtmlCanvasElementvalue.BackendKind- TheBackendKindvalue.
§Returns
Option<Self>- TheOption<Self>value.
Sourcepub fn set_option(&mut self, option: ChartOption) -> &mut Self
pub fn set_option(&mut self, option: ChartOption) -> &mut Self
Sets the configuration of the chart.
The chart is re-laid out and re-rendered. When the chart
animates, the new configuration is the target of the animation and
the chart moves towards it; call Chart::advance to move it
along.
§Arguments
ChartOption- AChartOptionparameter.
§Returns
&mut Self- Amut Selfvalue.
Sourcepub fn option(&self) -> &ChartOption
pub fn option(&self) -> &ChartOption
Sourcepub fn option_mut(&mut self) -> &mut ChartOption
pub fn option_mut(&mut self) -> &mut ChartOption
Returns the configuration of the chart, mutably.
Changing the configuration marks the chart as needing a new
layout; call Chart::render to apply the change.
§Returns
&mut ChartOption- Amut ChartOptionvalue.
Sourcepub fn render(&mut self) -> &mut Self
pub fn render(&mut self) -> &mut Self
Lays the chart out and draws it.
The call is cheap when nothing changed: the layout and the display list are only rebuilt when the option or the size changed.
§Returns
&mut Self- Amut Selfvalue.
Sourcepub fn handle(&self) -> ChartHandle
pub fn handle(&self) -> ChartHandle
Returns a handle to the chart.
The handle is how an application that does not hold the chart talks to it: the commands it sends travel through a signal, and the chart performs them on its next frame.
§Returns
ChartHandle- AChartHandlevalue.
Sourcepub fn backend_kind(&self) -> BackendKind
pub fn backend_kind(&self) -> BackendKind
Sourcepub fn layout(&self) -> &ChartLayout
pub fn layout(&self) -> &ChartLayout
Sourcepub fn display_list(&self) -> &DisplayList
pub fn display_list(&self) -> &DisplayList
Sourcepub fn canvas(&self) -> &HtmlCanvasElement
pub fn canvas(&self) -> &HtmlCanvasElement
Sourcepub fn on(
&mut self,
event_type: ChartEventType,
handler: EventHandler,
) -> &mut Self
pub fn on( &mut self, event_type: ChartEventType, handler: EventHandler, ) -> &mut Self
Sourcepub fn off(&mut self, event_type: ChartEventType) -> &mut Self
pub fn off(&mut self, event_type: ChartEventType) -> &mut Self
Sourcepub fn emit(&mut self, event: ChartEvent) -> &mut Self
pub fn emit(&mut self, event: ChartEvent) -> &mut Self
Sourcepub fn dispatch_action(&mut self, action: ChartAction) -> &mut Self
pub fn dispatch_action(&mut self, action: ChartAction) -> &mut Self
Sourcepub fn show_tip(
&mut self,
series_index: usize,
data_index: usize,
x: f64,
y: f64,
) -> &mut Self
pub fn show_tip( &mut self, series_index: usize, data_index: usize, x: f64, y: f64, ) -> &mut Self
Sourcepub fn tooltip(&self) -> Option<&TooltipState>
pub fn tooltip(&self) -> Option<&TooltipState>
Returns the tooltip the chart currently shows.
§Returns
Option<&TooltipState>-Some(...)on success,Noneotherwise.
Sourcepub fn hit_test(&self, x: f64, y: f64) -> Option<ChartEvent>
pub fn hit_test(&self, x: f64, y: f64) -> Option<ChartEvent>
Finds the data point under the given position.
The search walks the chart’s series from the last one to the first, so that the series drawn on top wins — which is what the user expects to click on.
§Arguments
f64- A 64-bit float (f64).
§Returns
Option<ChartEvent>-Some(...)on success,Noneotherwise.
Sourcepub fn register_map(
&mut self,
name: &str,
geojson: &DataValue,
) -> Result<&mut Self, GeoError>
pub fn register_map( &mut self, name: &str, geojson: &DataValue, ) -> Result<&mut Self, GeoError>
Registers a map, so that a series can draw it.
The map is given as a GeoJSON document: a collection of features, each with a geometry and a name. The name of the map is the one a series asks for.
The geometry is kept out of the option on purpose: a map of a country is megabytes of coordinates, and every chart of the application draws the very same one.
§Arguments
&str- Shared reference to astr.&DataValue- Shared reference to aDataValue.
§Returns
Result<&mut Self, GeoError>- Result of the operation; anErrvariant on failure.
Sourcepub fn register_map_data(&mut self, data: MapData) -> &mut Self
pub fn register_map_data(&mut self, data: MapData) -> &mut Self
Sourcepub fn maps(&self) -> &MapRegistry
pub fn maps(&self) -> &MapRegistry
Sourcepub fn to_data_url(&self) -> Option<String>
pub fn to_data_url(&self) -> Option<String>
Returns the chart as a data URL.
The PNG is produced by the browser, so it is only available in a
browser; a native build returns None.
§Returns
Option<String>-Some(...)on success,Noneotherwise.