Skip to main content

DonutChart

Struct DonutChart 

Source
pub struct DonutChart<S, T> { /* private fields */ }
Expand description

A donut chart of the rows of a list signal, each an arc as long as its share of the total.

Each row is a slice labeled by the text field category, and its length is the number field value over the sum of value across the rows. The slices run clockwise from the top in the order of the rows, in the chart colors from token::chart_1 to token::chart_5, which repeat after the fifth row. A negative value counts as 0, since a share cannot be negative. The total is written in the middle of the ring, and the legend names each row with its value.

Each slice sums the rows before it, so a change to the rows costs time that grows with the square of their number.

The ring and the legend are hidden from assistive technology, and a screen reader reads a visually hidden table of the same rows instead, captioned with label.

use hypersig::{Page, Signals};
use hypersig_ui::DonutChart;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Browser {
    name: String,
    visitors: i32,
}

#[derive(Signals, Serialize, Deserialize)]
struct Traffic {
    browsers: Vec<Browser>,
}

let chart = DonutChart::new("Browsers", Traffic::browsers(), Browser::name(), Browser::visitors());
let browsers = vec![
    Browser { name: "Chrome".into(), visitors: 275 },
    Browser { name: "Safari".into(), visitors: 125 },
];
let html = Page::new("Traffic", Traffic { browsers }).child(chart).render();
assert!(html.contains(">400</span>"));
assert!(html.contains(r#"stroke-dasharray="68.750 31.250""#));

Implementations§

Source§

impl<S: 'static, T: 'static> DonutChart<S, T>

Source

pub fn new<U: Number + Textual + 'static>( label: impl Into<String>, rows: Sig<S, Vec<T>>, category: Sig<T, String>, value: Sig<T, U>, ) -> Self

A donut chart named label of rows, with slices of value labeled by category.

Source

pub fn format(self, format: NumberFormat) -> Self

Writes the values and the total in format.

Until set, they are written as JavaScript’s String writes a number.

Source

pub fn render(self) -> El<S>

The ring, its total, its legend, and the table a screen reader reads, in a <figure>.

Trait Implementations§

Source§

impl<S, T> Debug for DonutChart<S, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: 'static, T: 'static> From<DonutChart<S, T>> for El<S>

Source§

fn from(chart: DonutChart<S, T>) -> Self

Converts to this type from the input type.
Source§

impl<S: 'static, T: 'static> From<DonutChart<S, T>> for Node<S>

Source§

fn from(chart: DonutChart<S, T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<S, T> !RefUnwindSafe for DonutChart<S, T>

§

impl<S, T> !Send for DonutChart<S, T>

§

impl<S, T> !Sync for DonutChart<S, T>

§

impl<S, T> !UnwindSafe for DonutChart<S, T>

§

impl<S, T> Freeze for DonutChart<S, T>

§

impl<S, T> Unpin for DonutChart<S, T>

§

impl<S, T> UnsafeUnpin for DonutChart<S, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.