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
impl ChartJSImage
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new ChartJSImage instance with default configuration
§Example
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new();Sourcepub fn with_config(config: ChartJSImageConfig) -> Self
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);Sourcepub fn with_secret(secret: impl Into<String>) -> Self
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");Sourcepub fn builder() -> ChartJSImageBuilder
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();Sourcepub fn c(self, value: impl Into<String>) -> Self
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]}]}}");Sourcepub fn chart(self, value: impl Into<String>) -> Self
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]}]}}");Sourcepub fn width(self, value: impl Into<String>) -> Self
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"
Sourcepub fn height(self, value: impl Into<String>) -> Self
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"
Sourcepub fn backgroundColor(self, value: impl Into<String>) -> Self
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)");Sourcepub fn bkg(self, value: impl Into<String>) -> Self
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)");Sourcepub fn encoding(self, value: impl Into<String>) -> Self
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"
Sourcepub fn icac(self, value: impl Into<String>) -> Self
pub fn icac(self, value: impl Into<String>) -> Self
image-charts enterprise account_id
§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().icac("accountId");Sourcepub fn ichm(self, value: impl Into<String>) -> Self
pub fn ichm(self, value: impl Into<String>) -> Self
HMAC-SHA256 signature required to activate paid features
§Examples
use chartjs_image::ChartJSImage;
let chart = ChartJSImage::new().ichm("0785cf22a0381c2e0239e27c126de4181f501d117c2c81745611e9db928b0376");Sourcepub fn icretina(self, value: impl Into<String>) -> Self
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
Sourcepub fn to_url(&self) -> String
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
impl ChartJSImage
Sourcepub async fn to_buffer(&self) -> Result<Vec<u8>, ChartJSImageError>
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(())
}Sourcepub async fn to_file(
&self,
path: impl AsRef<Path>,
) -> Result<(), ChartJSImageError>
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(())
}Sourcepub async fn to_data_uri(&self) -> Result<String, ChartJSImageError>
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
impl Clone for ChartJSImage
Source§fn clone(&self) -> ChartJSImage
fn clone(&self) -> ChartJSImage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more