Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chart options #75

Merged
merged 3 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions rust/src/wrapper/chart/chart_axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::sync::{Arc, Mutex};
use rust_xlsxwriter as xlsx;
use wasm_bindgen::prelude::*;

use crate::wrapper::chart::chart_font::ChartFont;

#[derive(Copy, Clone)]
pub enum AxisType {
X,
Expand Down Expand Up @@ -66,4 +68,18 @@ impl ChartAxis {
axis.set_max(max);
})
}

#[wasm_bindgen(js_name = "setFont", skip_jsdoc)]
pub fn set_font(&self, font: &ChartFont) -> ChartAxis {
self.with_chart(|axis| {
axis.set_font(&font.inner);
})
}

#[wasm_bindgen(js_name = "setNameFont", skip_jsdoc)]
pub fn set_name_font(&self, font: &ChartFont) -> ChartAxis {
self.with_chart(|axis| {
axis.set_name_font(&font.inner);
})
}
}
150 changes: 150 additions & 0 deletions rust/src/wrapper/chart/chart_data_label.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use rust_xlsxwriter::{self as xlsx};
use wasm_bindgen::prelude::*;

use crate::wrapper::chart::chart_data_label_position::ChartDataLabelPosition;
use crate::wrapper::chart::chart_font::ChartFont;
use crate::wrapper::chart::chart_format::ChartFormat;

#[wasm_bindgen]
pub struct ChartDataLabel {
pub(crate) inner: xlsx::ChartDataLabel,
}

#[wasm_bindgen]
impl ChartDataLabel {
#[wasm_bindgen(constructor)]
pub fn new() -> ChartDataLabel {
ChartDataLabel {
inner: xlsx::ChartDataLabel::new(),
}
}

#[wasm_bindgen(js_name = "showValue")]
pub fn show_value(&mut self) -> ChartDataLabel {
self.inner.show_value();
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "showCategoryName")]
pub fn show_category_name(&mut self) -> ChartDataLabel {
self.inner.show_category_name();
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "showSeriesName")]
pub fn show_series_name(&mut self) -> ChartDataLabel {
self.inner.show_series_name();
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "showLeaderLines")]
pub fn show_leader_lines(&mut self) -> ChartDataLabel {
self.inner.show_leader_lines();
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "showLegendKey")]
pub fn show_legend_key(&mut self) -> ChartDataLabel {
self.inner.show_legend_key();
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "showPercentage")]
pub fn show_percentage(&mut self) -> ChartDataLabel {
self.inner.show_percentage();
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "setPosition")]
pub fn set_position(&mut self, position: ChartDataLabelPosition) -> ChartDataLabel {
self.inner.set_position(position.into());
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "setFont")]
pub fn set_font(&mut self, font: &ChartFont) -> ChartDataLabel {
self.inner.set_font(&font.inner);
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "setFormat")]
pub fn set_format(&mut self, format: &mut ChartFormat) -> ChartDataLabel {
self.inner.set_format(&mut format.inner);
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "setNumFormat")]
pub fn set_num_format(&mut self, num_format: &str) -> ChartDataLabel {
self.inner.set_num_format(num_format);
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "setSeparator")]
pub fn set_separator(&mut self, separator: char) -> ChartDataLabel {
self.inner.set_separator(separator);
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "showYValue")]
pub fn show_y_value(&mut self) -> ChartDataLabel {
self.inner.show_y_value();
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "showXValue")]
pub fn show_x_value(&mut self) -> ChartDataLabel {
self.inner.show_x_value();
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "setHidden")]
pub fn set_hidden(&mut self) -> ChartDataLabel {
self.inner.set_hidden();
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "setValue")]
pub fn set_value(&mut self, value: &str) -> ChartDataLabel {
self.inner.set_value(value);
ChartDataLabel {
inner: self.inner.clone(),
}
}

#[wasm_bindgen(js_name = "toCustom")]
pub fn to_custom(&mut self) -> ChartDataLabel {
self.inner.to_custom();
ChartDataLabel {
inner: self.inner.clone(),
}
}
}

34 changes: 34 additions & 0 deletions rust/src/wrapper/chart/chart_data_label_position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use rust_xlsxwriter as xlsx;
use wasm_bindgen::prelude::*;

#[derive(Debug, Clone, Copy)]
#[wasm_bindgen]
pub enum ChartDataLabelPosition {
Default,
Center,
Right,
Left,
Above,
Below,
InsideBase,
InsideEnd,
OutsideEnd,
BestFit,
}

impl From<ChartDataLabelPosition> for xlsx::ChartDataLabelPosition {
fn from(position: ChartDataLabelPosition) -> Self {
match position {
ChartDataLabelPosition::Default => xlsx::ChartDataLabelPosition::Default,
ChartDataLabelPosition::Center => xlsx::ChartDataLabelPosition::Center,
ChartDataLabelPosition::Right => xlsx::ChartDataLabelPosition::Right,
ChartDataLabelPosition::Left => xlsx::ChartDataLabelPosition::Left,
ChartDataLabelPosition::Above => xlsx::ChartDataLabelPosition::Above,
ChartDataLabelPosition::Below => xlsx::ChartDataLabelPosition::Below,
ChartDataLabelPosition::InsideBase => xlsx::ChartDataLabelPosition::InsideBase,
ChartDataLabelPosition::InsideEnd => xlsx::ChartDataLabelPosition::InsideEnd,
ChartDataLabelPosition::OutsideEnd => xlsx::ChartDataLabelPosition::OutsideEnd,
ChartDataLabelPosition::BestFit => xlsx::ChartDataLabelPosition::BestFit,
}
}
}
168 changes: 168 additions & 0 deletions rust/src/wrapper/chart/chart_font.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
use rust_xlsxwriter as xlsx;
use wasm_bindgen::prelude::*;

use crate::wrapper::color::Color;

/// The `ChartFont` struct represents a chart font.
///
/// The `ChartFont` struct is used to define the font properties for chart elements
/// such as chart titles, axis labels, data labels and other text elements in a chart.
///
/// It is used in conjunction with the {@link Chart} struct.
#[wasm_bindgen]
pub struct ChartFont {
pub(crate) inner: xlsx::ChartFont,
}

#[wasm_bindgen]
impl ChartFont {
/// Create a new `ChartFont` object.
#[wasm_bindgen(constructor)]
pub fn new() -> ChartFont {
ChartFont {
inner: xlsx::ChartFont::new(),
}
}

/// Set the font bold property.
///
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setBold")]
pub fn set_bold(mut self) -> ChartFont {
self.inner.set_bold();
self
}

/// Set the font character set value.
///
/// Set the font character set value using the standard Windows values.
/// This is generally only required when using non-standard fonts.
///
/// @param {number} charset - The font character set value.
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setCharacterSet")]
pub fn set_character_set(mut self, character_set: u8) -> ChartFont {
self.inner.set_character_set(character_set);
self
}

/// Set the font color property.
///
/// @param {Color} color - The font color property.
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setColor")]
pub fn set_color(mut self, color: &Color) -> ChartFont {
self.inner.set_color(color.inner);
self
}

/// Set the font italic property.
///
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setItalic")]
pub fn set_italic(mut self) -> ChartFont {
self.inner.set_italic();
self
}

/// Set the font name/family property.
///
/// Set the font name for the chart element. Excel can only display fonts that
/// are installed on the system that it is running on. Therefore it is generally
/// best to use standard Excel fonts.
///
/// @param {string} name - The font name property.
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setName")]
pub fn set_name(mut self, name: &str) -> ChartFont {
self.inner.set_name(name);
self
}

/// Set the font pitch and family value.
///
/// Set the font pitch and family value using the standard Windows values.
/// This is generally only required when using non-standard fonts.
///
/// @param {number} pitch_family - The font pitch and family value.
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setPitchFamily")]
pub fn set_pitch_family(mut self, pitch_family: u8) -> ChartFont {
self.inner.set_pitch_family(pitch_family);
self
}

/// Set the right to left property.
///
/// Set the right to left property. This is generally only required when using
/// non-standard fonts.
///
/// @param {boolean} enable - Turn the property on/off. Defaults to true.
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setRightToLeft")]
pub fn set_right_to_left(mut self, enable: bool) -> ChartFont {
self.inner.set_right_to_left(enable);
self
}

/// Set the font rotation angle.
///
/// Set the rotation angle of the font text in the range -90 to 90 degrees.
///
/// @param {number} rotation - The rotation angle in degrees.
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setRotation")]
pub fn set_rotation(mut self, rotation: i16) -> ChartFont {
self.inner.set_rotation(rotation);
self
}

/// Set the font size property.
///
/// Set the font size for the chart element.
///
/// @param {number} size - The font size property.
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setSize")]
pub fn set_size(mut self, size: f64) -> ChartFont {
self.inner.set_size(size);
self
}

/// Set the font strikethrough property.
///
/// Set the strikethrough property. This is generally only required when using
/// non-standard fonts.
///
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setStrikethrough")]
pub fn set_strikethrough(mut self) -> ChartFont {
self.inner.set_strikethrough();
self
}

/// Set the font underline property.
///
/// Set the font underline. This is generally only required when using
/// non-standard fonts.
///
/// @param {number} underline - The font underline value.
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "setUnderline")]
pub fn set_underline(mut self) -> ChartFont {
self.inner.set_underline();
self
}

/// Unset bold property.
///
/// Unset the bold property. This is generally only required when using
/// non-standard fonts.
///
/// @return {ChartFont} - The ChartFont instance.
#[wasm_bindgen(js_name = "unsetBold")]
pub fn unset_bold(mut self) -> ChartFont {
self.inner.unset_bold();
self
}
}
Loading