Skip to main content

ChartJSImage

Struct ChartJSImage 

Source
pub struct ChartJSImage { /* private fields */ }
Expand description

Builder for Chart.JS to Image API requests

Use the fluent API to configure chart parameters, then call one of the output methods (to_url, to_buffer, to_file, to_data_uri) to generate the chart.

§Example

use chartjs_image::ChartJSImage;

let chart = ChartJSImage::new()
    .chart(r#"{"type":"pie","data":{"datasets":[{"data":[1,2,3]}]}}"#)
    .width("400")
    .height("300")
    .to_url();

Implementations§

Source§

impl ChartJSImage

Source

pub fn new() -> Self

Create a new ChartJSImage instance with default configuration

§Example
use chartjs_image::ChartJSImage;

let chart = ChartJSImage::new();
Source

pub fn with_config(config: ChartJSImageConfig) -> Self

Create a new ChartJSImage instance with custom configuration

§Example
use chartjs_image::{ ChartJSImage, ChartJSImageConfig };
use std::time::Duration;

let config = ChartJSImageConfig {
    timeout: Duration::from_secs(10),
    ..Default::default()
};
let chart = ChartJSImage::with_config(config);
Source

pub fn with_secret(secret: impl Into<String>) -> Self

Create a new ChartJSImage instance for Enterprise usage with a secret key

§Example
use chartjs_image::ChartJSImage;

let chart = ChartJSImage::with_secret("my-secret-key");
Source

pub fn builder() -> ChartJSImageBuilder

Create a new ChartJSImage builder for advanced configuration

§Example
use chartjs_image::ChartJSImage;
use std::time::Duration;

let chart = ChartJSImage::builder()
    .secret("my-secret")
    .timeout(Duration::from_secs(30))
    .build();
Source

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

Javascript/JSON definition of the chart. Use a Chart.js configuration object.

§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().c("{type:'bar',data:{labels:['Q1','Q2','Q3','Q4'],datasets:[{label:'Users',data:[50,60,70,180]},{label:'Revenue',data:[100,200,300,400]}]}}");
Source

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

Javascript/JSON definition of the chart. Use a Chart.js configuration object.

§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().chart("{type:'bar',data:{labels:['Q1','Q2','Q3','Q4'],datasets:[{label:'Users',data:[50,60,70,180]},{label:'Revenue',data:[100,200,300,400]}]}}");
Source

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

Width of the chart

§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().width("400");

Default: "500"

Source

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

Height of the chart

§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().height("300");

Default: "300"

Source

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

Background of the chart canvas. Accepts rgb (rgb(255,255,120)), colors (red), and url-encoded hex values (%23ff00ff). Abbreviated as “bkg”

§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().backgroundColor("black");
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().backgroundColor("rgb(255,255,120)");
Source

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

Background of the chart canvas. Accepts rgb (rgb(255,255,120)), colors (red), and url-encoded hex values (%23ff00ff). Abbreviated as “bkg”

§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().bkg("black");
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().bkg("rgb(255,255,120)");
Source

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

Encoding of your “chart” parameter. Accepted values are url and base64.

§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().encoding("url");
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().encoding("base64");

Default: "url"

Source

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

image-charts enterprise account_id

Reference documentation

§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().icac("accountId");
Source

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

HMAC-SHA256 signature required to activate paid features

Reference documentation

§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().ichm("0785cf22a0381c2e0239e27c126de4181f501d117c2c81745611e9db928b0376");
Source

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

Retina is a marketing term coined by Apple that refers to devices and monitors that have a resolution and pixel density so high — roughly 300 or more pixels per inch – that a person is unable to discern the individual pixels at a normal viewing distance. In order to generate beautiful charts for these Retina displays, Image-Charts supports a retina mode that can be activated through the icretina=1 parameter

Reference documentation

Source

pub fn to_url(&self) -> String

Get the full Image-Charts API URL (signed and encoded if necessary)

This method returns the complete URL that can be used to fetch the chart image. If an enterprise account ID (icac) is set and a secret is configured, the URL will be automatically signed with HMAC-SHA256.

§Example
use chartjs_image::ChartJSImage;

let url = ChartJSImage::new()
    .chart(r#"{"type":"pie","data":{"datasets":[{"data":[1,2]}]}}"#)
    .width("100")
    .height("100")
    .to_url();

assert!(url.starts_with("https://image-charts.com/chart.js/2.8.0?"));
Source§

impl ChartJSImage

Source

pub async fn to_buffer(&self) -> Result<Vec<u8>, ChartJSImageError>

Do an async request to Image-Charts API and return the image as bytes

§Example
use chartjs_image::ChartJSImage;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let buffer = ChartJSImage::new()
        .chart(r#"{"type":"pie","data":{"datasets":[{"data":[1,2]}]}}"#)
        .width("100")
        .height("100")
        .to_buffer()
        .await?;

    println!("Image size: {} bytes", buffer.len());
    Ok(())
}
Source

pub async fn to_file( &self, path: impl AsRef<Path>, ) -> Result<(), ChartJSImageError>

Do an async request and write the image to a file

§Example
use chartjs_image::ChartJSImage;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ChartJSImage::new()
        .chart(r#"{"type":"pie","data":{"datasets":[{"data":[1,2]}]}}"#)
        .width("100")
        .height("100")
        .to_file("chart.png")
        .await?;

    println!("Chart saved to chart.png");
    Ok(())
}
Source

pub async fn to_data_uri(&self) -> Result<String, ChartJSImageError>

Do an async request and return a base64-encoded data URI

The returned string can be used directly in HTML <img> tags or CSS.

§Example
use chartjs_image::ChartJSImage;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let data_uri = ChartJSImage::new()
        .chart(r#"{"type":"pie","data":{"datasets":[{"data":[1,2]}]}}"#)
        .width("100")
        .height("100")
        .to_data_uri()
        .await?;

    println!("<img src=\"{}\" />", data_uri);
    Ok(())
}

Trait Implementations§

Source§

impl Clone for ChartJSImage

Source§

fn clone(&self) -> ChartJSImage

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ChartJSImage

Source§

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

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

impl Default for ChartJSImage

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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> 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> Same for T

Source§

type Output = T

Should always be Self
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 = 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.
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