Skip to content

Commit 644d78f

Browse files
authored
Merge pull request #79 from estie-inc/chart_solid_fill
feat: add chart_solid_fill
2 parents b8043b2 + 5657ce9 commit 644d78f

File tree

7 files changed

+83
-4
lines changed

7 files changed

+83
-4
lines changed

rust/src/wrapper/chart/chart_format.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rust_xlsxwriter::{self as xlsx};
22
use wasm_bindgen::prelude::*;
33

44
use crate::wrapper::color::Color;
5+
use crate::wrapper::chart::chart_solid_fill::ChartSolidFill;
56

67
/// The `ChartFormat` struct represents formatting for various chart objects.
78
///
@@ -79,7 +80,12 @@ impl ChartFormat {
7980
self
8081
}
8182

82-
// TODO: set_gradient_fill, set_pattern_fill, set_solid_fill
83+
// TODO: set_gradient_fill, set_pattern_fill
84+
#[wasm_bindgen(js_name = "setSolidFill")]
85+
pub fn set_solid_fill(mut self, fill: &ChartSolidFill) -> ChartFormat {
86+
self.inner.set_solid_fill(&fill.inner);
87+
self
88+
}
8389
}
8490

8591
/// The `ChartLine` struct represents a chart line/border.

rust/src/wrapper/chart/chart_point.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl ChartPoint {
3838
}
3939
}
4040

41+
#[wasm_bindgen(js_name = "setFormat")]
4142
pub fn set_format(&self, format: &mut ChartFormat) -> ChartPoint {
4243
ChartPoint {
4344
inner: self.clone().inner.set_format(&mut format.inner),

rust/src/wrapper/chart/chart_series.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex};
33
use rust_xlsxwriter as xlsx;
44
use wasm_bindgen::prelude::*;
55

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

88
#[wasm_bindgen]
99
pub struct ChartSeries {
@@ -151,4 +151,13 @@ impl ChartSeries {
151151
inner: Arc::clone(&self.inner),
152152
}
153153
}
154+
155+
#[wasm_bindgen(js_name = "setFormat", skip_jsdoc)]
156+
pub fn set_format(&self, format: &mut ChartFormat) -> ChartSeries {
157+
let mut series = self.inner.lock().unwrap();
158+
series.set_format(&mut format.inner);
159+
ChartSeries {
160+
inner: Arc::clone(&self.inner),
161+
}
162+
}
154163
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use rust_xlsxwriter as xlsx;
2+
use wasm_bindgen::prelude::*;
3+
4+
use crate::wrapper::color::Color;
5+
6+
/// The `ChartSolidFill` struct represents a solid fill for chart elements.
7+
///
8+
/// The `ChartSolidFill` struct is used to define the solid fill properties
9+
/// for chart elements such as plot areas, chart areas, data series, and other
10+
/// fillable elements in a chart.
11+
///
12+
/// It is used in conjunction with the {@link Chart} struct.
13+
#[wasm_bindgen]
14+
pub struct ChartSolidFill {
15+
pub(crate) inner: xlsx::ChartSolidFill,
16+
}
17+
18+
#[wasm_bindgen]
19+
impl ChartSolidFill {
20+
/// Create a new `ChartSolidFill` object.
21+
#[wasm_bindgen(constructor)]
22+
pub fn new() -> ChartSolidFill {
23+
ChartSolidFill {
24+
inner: xlsx::ChartSolidFill::new(),
25+
}
26+
}
27+
28+
/// Set the color of a solid fill.
29+
///
30+
/// @param {Color} color - The color property.
31+
/// @return {ChartSolidFill} - The ChartSolidFill instance.
32+
#[wasm_bindgen(js_name = "setColor")]
33+
pub fn set_color(mut self, color: &Color) -> ChartSolidFill {
34+
self.inner.set_color(color.inner);
35+
self
36+
}
37+
38+
/// Set the transparency of a solid fill.
39+
///
40+
/// Set the transparency of a solid fill color for a Chart element.
41+
/// You must also specify a fill color in order for the transparency to be applied.
42+
///
43+
/// @param {number} transparency - The color transparency in the range 0-100.
44+
/// @return {ChartSolidFill} - The ChartSolidFill instance.
45+
#[wasm_bindgen(js_name = "setTransparency")]
46+
pub fn set_transparency(mut self, transparency: u8) -> ChartSolidFill {
47+
self.inner.set_transparency(transparency);
48+
self
49+
}
50+
}

rust/src/wrapper/chart/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod chart_marker_type;
1010
mod chart_point;
1111
mod chart_range;
1212
mod chart_series;
13+
mod chart_solid_fill;
1314
mod chart_title;
1415
mod chart_type;
1516

test/chart.test.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {
1010
ChartDataLabelPosition,
1111
ChartMarker,
1212
ChartMarkerType,
13+
ChartSolidFill,
14+
ChartFormat,
15+
Color,
16+
ChartLine,
1317
} from "../web/wasm_xlsxwriter";
1418
import { describe, test, beforeAll, expect } from "vitest";
1519
import { initWasModule, readXlsx, readXlsxFile } from "./common";
@@ -46,23 +50,31 @@ describe("xlsx-wasm test", () => {
4650
const chartDataLabel = new ChartDataLabel().setFont(chartFont).showValue().setPosition(ChartDataLabelPosition.Left);
4751

4852
const chartSeries1 = new ChartSeries();
49-
const chartMarker1 = new ChartMarker().setType(ChartMarkerType.Circle).setSize(10);
53+
const chartLine1 = new ChartLine().setColor(Color.green());
54+
const chartSolidFill1 = new ChartSolidFill().setColor(Color.green());
55+
const chartFormat1 = new ChartFormat().setLine(chartLine1).setSolidFill(chartSolidFill1);
56+
const chartMarker1 = new ChartMarker().setType(ChartMarkerType.Circle).setSize(10).setFormat(chartFormat1);
5057
const categoriesRange1 = new ChartRange("Sheet1", 1, 0, 6, 0);
5158
const valuesRange1 = new ChartRange("Sheet1", 1, 1, 6, 1);
5259
chartSeries1
5360
.setName("Score 1")
5461
.setCategories(categoriesRange1)
5562
.setValues(valuesRange1)
63+
.setFormat(chartFormat1)
5664
.setDataLabel(chartDataLabel)
5765
.setMarker(chartMarker1);
5866
const chartSeries2 = new ChartSeries();
59-
const chartMarker2 = new ChartMarker().setType(ChartMarkerType.Diamond).setSize(10);
67+
const chartLine2 = new ChartLine().setColor(Color.purple());
68+
const chartSolidFill2 = new ChartSolidFill().setColor(Color.purple());
69+
const chartFormat2 = new ChartFormat().setLine(chartLine2).setSolidFill(chartSolidFill2);
70+
const chartMarker2 = new ChartMarker().setType(ChartMarkerType.Diamond).setSize(10).setFormat(chartFormat2);
6071
const categoriesRange2 = new ChartRange("Sheet1", 1, 0, 6, 0);
6172
const valuesRange2 = new ChartRange("Sheet1", 1, 2, 6, 2);
6273
chartSeries2
6374
.setName("Score 2")
6475
.setCategories(categoriesRange2)
6576
.setValues(valuesRange2)
77+
.setFormat(chartFormat2)
6678
.setDataLabel(chartDataLabel)
6779
.setMarker(chartMarker2);
6880
chart

test/expected/insert_chart.xlsx

57 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)