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

feat: add chart_solid_fill #79

Merged
merged 2 commits into from
Mar 13, 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
8 changes: 7 additions & 1 deletion rust/src/wrapper/chart/chart_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rust_xlsxwriter::{self as xlsx};
use wasm_bindgen::prelude::*;

use crate::wrapper::color::Color;
use crate::wrapper::chart::chart_solid_fill::ChartSolidFill;

/// The `ChartFormat` struct represents formatting for various chart objects.
///
Expand Down Expand Up @@ -79,7 +80,12 @@ impl ChartFormat {
self
}

// TODO: set_gradient_fill, set_pattern_fill, set_solid_fill
// TODO: set_gradient_fill, set_pattern_fill
#[wasm_bindgen(js_name = "setSolidFill")]
pub fn set_solid_fill(mut self, fill: &ChartSolidFill) -> ChartFormat {
self.inner.set_solid_fill(&fill.inner);
self
}
}

/// The `ChartLine` struct represents a chart line/border.
Expand Down
1 change: 1 addition & 0 deletions rust/src/wrapper/chart/chart_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl ChartPoint {
}
}

#[wasm_bindgen(js_name = "setFormat")]
pub fn set_format(&self, format: &mut ChartFormat) -> ChartPoint {
ChartPoint {
inner: self.clone().inner.set_format(&mut format.inner),
Expand Down
11 changes: 10 additions & 1 deletion rust/src/wrapper/chart/chart_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex};
use rust_xlsxwriter as xlsx;
use wasm_bindgen::prelude::*;

use super::{chart_data_label::ChartDataLabel, chart_marker::ChartMarker, chart_point::ChartPoint, chart_range::ChartRange};
use super::{chart_data_label::ChartDataLabel, chart_format::ChartFormat, chart_marker::ChartMarker, chart_point::ChartPoint, chart_range::ChartRange};

#[wasm_bindgen]
pub struct ChartSeries {
Expand Down Expand Up @@ -151,4 +151,13 @@ impl ChartSeries {
inner: Arc::clone(&self.inner),
}
}

#[wasm_bindgen(js_name = "setFormat", skip_jsdoc)]
pub fn set_format(&self, format: &mut ChartFormat) -> ChartSeries {
let mut series = self.inner.lock().unwrap();
series.set_format(&mut format.inner);
ChartSeries {
inner: Arc::clone(&self.inner),
}
}
}
50 changes: 50 additions & 0 deletions rust/src/wrapper/chart/chart_solid_fill.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use rust_xlsxwriter as xlsx;
use wasm_bindgen::prelude::*;

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

/// The `ChartSolidFill` struct represents a solid fill for chart elements.
///
/// The `ChartSolidFill` struct is used to define the solid fill properties
/// for chart elements such as plot areas, chart areas, data series, and other
/// fillable elements in a chart.
///
/// It is used in conjunction with the {@link Chart} struct.
#[wasm_bindgen]
pub struct ChartSolidFill {
pub(crate) inner: xlsx::ChartSolidFill,
}

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

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

/// Set the transparency of a solid fill.
///
/// Set the transparency of a solid fill color for a Chart element.
/// You must also specify a fill color in order for the transparency to be applied.
///
/// @param {number} transparency - The color transparency in the range 0-100.
/// @return {ChartSolidFill} - The ChartSolidFill instance.
#[wasm_bindgen(js_name = "setTransparency")]
pub fn set_transparency(mut self, transparency: u8) -> ChartSolidFill {
self.inner.set_transparency(transparency);
self
}
}
1 change: 1 addition & 0 deletions rust/src/wrapper/chart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod chart_marker_type;
mod chart_point;
mod chart_range;
mod chart_series;
mod chart_solid_fill;
mod chart_title;
mod chart_type;

Expand Down
16 changes: 14 additions & 2 deletions test/chart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
ChartDataLabelPosition,
ChartMarker,
ChartMarkerType,
ChartSolidFill,
ChartFormat,
Color,
ChartLine,
} from "../web/wasm_xlsxwriter";
import { describe, test, beforeAll, expect } from "vitest";
import { initWasModule, readXlsx, readXlsxFile } from "./common";
Expand Down Expand Up @@ -46,23 +50,31 @@ describe("xlsx-wasm test", () => {
const chartDataLabel = new ChartDataLabel().setFont(chartFont).showValue().setPosition(ChartDataLabelPosition.Left);

const chartSeries1 = new ChartSeries();
const chartMarker1 = new ChartMarker().setType(ChartMarkerType.Circle).setSize(10);
const chartLine1 = new ChartLine().setColor(Color.green());
const chartSolidFill1 = new ChartSolidFill().setColor(Color.green());
const chartFormat1 = new ChartFormat().setLine(chartLine1).setSolidFill(chartSolidFill1);
const chartMarker1 = new ChartMarker().setType(ChartMarkerType.Circle).setSize(10).setFormat(chartFormat1);
const categoriesRange1 = new ChartRange("Sheet1", 1, 0, 6, 0);
const valuesRange1 = new ChartRange("Sheet1", 1, 1, 6, 1);
chartSeries1
.setName("Score 1")
.setCategories(categoriesRange1)
.setValues(valuesRange1)
.setFormat(chartFormat1)
.setDataLabel(chartDataLabel)
.setMarker(chartMarker1);
const chartSeries2 = new ChartSeries();
const chartMarker2 = new ChartMarker().setType(ChartMarkerType.Diamond).setSize(10);
const chartLine2 = new ChartLine().setColor(Color.purple());
const chartSolidFill2 = new ChartSolidFill().setColor(Color.purple());
const chartFormat2 = new ChartFormat().setLine(chartLine2).setSolidFill(chartSolidFill2);
const chartMarker2 = new ChartMarker().setType(ChartMarkerType.Diamond).setSize(10).setFormat(chartFormat2);
const categoriesRange2 = new ChartRange("Sheet1", 1, 0, 6, 0);
const valuesRange2 = new ChartRange("Sheet1", 1, 2, 6, 2);
chartSeries2
.setName("Score 2")
.setCategories(categoriesRange2)
.setValues(valuesRange2)
.setFormat(chartFormat2)
.setDataLabel(chartDataLabel)
.setMarker(chartMarker2);
chart
Expand Down
Binary file modified test/expected/insert_chart.xlsx
Binary file not shown.