Skip to main content

Chart

Struct Chart 

Source
pub struct Chart<K: Kind> { /* private fields */ }
Expand description

A D3 chart. Render it in a Maud template.

use autumn_plugin_d3::{Chart, Format};

let chart = Chart::bar([("Apples", 3.0), ("Pears", 5.0)])
    .label("Fruit sold")
    .y_label("Units")
    .format(Format::integer());
let html = autumn_web::html! { (chart) }.into_string();
assert!(html.contains(r#"data-d3="bar""#), "{html}");
assert!(html.contains("<table"), "fallback table: {html}");

Implementations§

Source§

impl<K: Kind> Chart<K>

Source

pub fn attribute(&self, name: &str) -> Option<&str>

The value of an emitted data-d3-* option attribute, for example "data-d3-label". For data-d3 use kind; for data-d3-data use data_json.

Source

pub fn kind(&self) -> &str

The data-d3 kind name.

Source

pub fn id(self, id: impl Into<String>) -> Self

Sets the element id.

Source

pub fn class(self, class: impl Into<String>) -> Self

Adds CSS classes to the figure, after d3-chart.

Source

pub fn label(self, label: impl Into<String>) -> Self

Sets the accessible name of the chart. It is also the table caption. Without it, the runtime uses the caption text.

Source

pub fn caption(self, caption: impl Into<String>) -> Self

Adds a visible <figcaption>.

Source

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

Sets the value format (axis ticks and tooltips).

Source

pub fn x_label(self, label: impl Into<String>) -> Self

Sets the x title (bottom axis) and the first table header.

Source

pub fn y_label(self, label: impl Into<String>) -> Self

Sets the value title (value axis) and the value table header.

Source

pub fn colors(self, colors: impl IntoIterator<Item = Color>) -> Self

Sets the series colors, in order. They replace the palette. At most 16 colors are used.

Source

pub fn aspect(self, ratio: f64) -> Self

Sets the plot width / height ratio. The value is clamped to 0.2..=10. The default is 16 / 9 (from d3.css).

Source

pub fn duration(self, duration: Duration) -> Self

Sets the transition time. Zero turns transitions off. At most 10 s.

Source

pub fn animate_reduced_motion(self) -> Self

Animates also for users who ask for reduced motion.

Source

pub fn src(self, url: impl Into<String>) -> Self

Loads the data from a same-origin JSON URL. The JSON has the same shape as the inline data. Inline data, if any, shows first.

Source

pub fn refresh(self, every: Duration) -> Self

Loads src again at this interval. The interval is clamped to 1 s ..= 24 h. Without src, the runtime ignores it.

Source

pub const fn table(self, show: bool) -> Self

Includes the fallback data table (default true).

Source

pub fn data_json(&self) -> Option<String>

The JSON for data-d3-data, if the chart has inline data.

Source§

impl<K: BuiltIn> Chart<K>

Source

pub fn from_src(url: impl Into<String>) -> Self

Makes a chart with no inline data. It loads the data from url.

use autumn_plugin_d3::{Bar, Chart};

let chart = Chart::<Bar>::from_src("/api/sales.json");
assert_eq!(chart.attribute("data-d3-src"), Some("/api/sales.json"));
Source§

impl<K: Axes> Chart<K>

Source

pub fn y_min(self, min: f64) -> Self

Sets the lower end of the value axis. The data can extend it.

Source

pub fn y_max(self, max: f64) -> Self

Sets the upper end of the value axis. The data can extend it.

Source§

impl<K: Xy> Chart<K>

Source

pub fn x_scale(self, scale: Scale) -> Self

Sets the x scale type. With Scale::Time, x is Unix milliseconds.

Source

pub fn y_scale(self, scale: Scale) -> Self

Sets the y scale type. Scale::Time is not a y scale; it means Scale::Linear.

Source

pub fn x_format(self, format: Format) -> Self

Sets the x tick format. Time scales ignore it.

Source§

impl<K: Curved> Chart<K>

Source

pub fn curve(self, curve: Curve) -> Self

Sets the line interpolation.

Source

pub fn dots(self) -> Self

Draws a dot on each point.

Source§

impl<K: Legend> Chart<K>

Source

pub fn legend(self, show: bool) -> Self

Shows or hides the legend. The default shows it for two or more series or slices.

Source§

impl Chart<Bar>

Source

pub fn bar<D: Into<Datum>>(data: impl IntoIterator<Item = D>) -> Self

Makes a bar chart. Repeated labels draw as separate bars.

Source

pub fn horizontal(self) -> Self

Draws horizontal bars.

Source§

impl Chart<Pie>

Source

pub fn pie<D: Into<Datum>>(data: impl IntoIterator<Item = D>) -> Self

Makes a pie chart. Repeated labels draw as separate slices.

Source

pub fn donut(self, inner: f64) -> Self

Makes a donut. inner is the hole radius as a fraction of the outer radius, clamped to 0..=0.95.

Source§

impl Chart<Line>

Source

pub fn line(series: impl IntoIterator<Item = Series>) -> Self

Makes a line chart.

Source§

impl Chart<Area>

Source

pub fn area(series: impl IntoIterator<Item = Series>) -> Self

Makes an area chart. Each series fills down to zero (or to the lower end of the value axis).

Source§

impl Chart<Scatter>

Source

pub fn scatter(series: impl IntoIterator<Item = Series>) -> Self

Makes a scatter chart.

Source

pub fn radius(self, px: f64) -> Self

Sets the dot radius in pixels, clamped to 1..=50. Default 4.

Source§

impl Chart<Custom>

Source

pub fn custom(name: impl Into<String>) -> Result<Self, Error>

Makes a custom chart. name is the kind name that AutumnD3.register uses in JavaScript.

§Errors

Error::InvalidKind when name is not 1–64 characters of a-z, 0-9, and -, starting with a letter, or when it is a built-in kind name.

use autumn_plugin_d3::Chart;

let chart = Chart::custom("network").unwrap().json(&[1, 2, 3]).unwrap();
assert_eq!(chart.data_json().as_deref(), Some("[1,2,3]"));
assert!(Chart::custom("Bad Name").is_err());
Source

pub fn json<T: Serialize + ?Sized>(self, data: &T) -> Result<Self, Error>

Sets the inline data from any serializable value.

§Errors

Error::Json when the value does not serialize (for example, a map with non-string keys).

Trait Implementations§

Source§

impl<K: Clone + Kind> Clone for Chart<K>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug + Kind> Debug for Chart<K>

Source§

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

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

impl<K: PartialEq + Kind> PartialEq for Chart<K>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<K: Kind> Render for Chart<K>

Source§

fn render(&self) -> Markup

Renders self as a block of Markup.
Source§

fn render_to(&self, buffer: &mut String)

Appends a representation of self to the given buffer. Read more
Source§

impl<K: PartialEq + Kind> StructuralPartialEq for Chart<K>

Auto Trait Implementations§

§

impl<K> Forget for Chart<K>

§

impl<K> Freeze for Chart<K>
where PhantomData<K>: Freeze,

§

impl<K> RefUnwindSafe for Chart<K>

§

impl<K> Send for Chart<K>
where PhantomData<K>: Send,

§

impl<K> Sync for Chart<K>
where PhantomData<K>: Sync,

§

impl<K> Unpin for Chart<K>
where PhantomData<K>: Unpin,

§

impl<K> UnsafeUnpin for Chart<K>

§

impl<K> UnwindSafe for Chart<K>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scoped for T
where T: Send + Sync + 'static,

Source§

fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>

Open a deferred ScopeQuery for this type. Resolves the registered scope at .load() time, not here.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more