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 marker #77

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
96 changes: 32 additions & 64 deletions rust/src/wrapper/chart/chart_data_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,131 +20,99 @@ impl ChartDataLabel {
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2 changes: 1 addition & 1 deletion rust/src/wrapper/chart/chart_data_label_position.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rust_xlsxwriter as xlsx;
use wasm_bindgen::prelude::*;

#[derive(Debug, Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq)]
#[wasm_bindgen]
pub enum ChartDataLabelPosition {
Default,
Expand Down
73 changes: 73 additions & 0 deletions rust/src/wrapper/chart/chart_marker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use rust_xlsxwriter::{self as xlsx};
use wasm_bindgen::prelude::*;

use crate::wrapper::chart::chart_format::ChartFormat;
use crate::wrapper::chart::chart_marker_type::ChartMarkerType;

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

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

/// Set the automatic/default marker type.
///
/// Allow the marker type to be set automatically by Excel.
///
/// @return {ChartMarker} - The ChartMarker instance.
#[wasm_bindgen(js_name = "setAutomatic")]
pub fn set_automatic(mut self) -> ChartMarker {
self.inner.set_automatic();
self
}

/// Set the formatting properties for a chart marker.
///
/// @param {ChartFormat} format - The chart format properties.
/// @return {ChartMarker} - The ChartMarker instance.
#[wasm_bindgen(js_name = "setFormat")]
pub fn set_format(mut self, format: &mut ChartFormat) -> ChartMarker {
self.inner.set_format(&mut format.inner);
self
}

/// Turn off/hide a chart marker.
///
/// This method can be used to turn off markers for an individual data series
/// in a chart that has default markers for all series.
///
/// @return {ChartMarker} - The ChartMarker instance.
#[wasm_bindgen(js_name = "setNone")]
pub fn set_none(mut self) -> ChartMarker {
self.inner.set_none();
self
}

/// Set the marker size.
///
/// @param {number} size - The marker size.
/// @return {ChartMarker} - The ChartMarker instance.
#[wasm_bindgen(js_name = "setSize")]
pub fn set_size(mut self, size: u8) -> ChartMarker {
self.inner.set_size(size);
self
}

/// Set the marker type.
///
/// @param {number} marker_type - The marker type.
/// @return {ChartMarker} - The ChartMarker instance.
#[wasm_bindgen(js_name = "setType")]
pub fn set_type(mut self, marker_type: ChartMarkerType) -> ChartMarker {
self.inner.set_type(marker_type.into());
self
}
}
32 changes: 32 additions & 0 deletions rust/src/wrapper/chart/chart_marker_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use rust_xlsxwriter as xlsx;
use wasm_bindgen::prelude::*;

#[derive(Clone, Copy, PartialEq, Eq)]
#[wasm_bindgen]
pub enum ChartMarkerType {
Square,
Diamond,
Triangle,
X,
Star,
ShortDash,
LongDash,
Circle,
PlusSign,
}

impl From<ChartMarkerType> for xlsx::ChartMarkerType {
fn from(marker_type: ChartMarkerType) -> xlsx::ChartMarkerType {
match marker_type {
ChartMarkerType::Square => xlsx::ChartMarkerType::Square,
ChartMarkerType::Diamond => xlsx::ChartMarkerType::Diamond,
ChartMarkerType::Triangle => xlsx::ChartMarkerType::Triangle,
ChartMarkerType::X => xlsx::ChartMarkerType::X,
ChartMarkerType::Star => xlsx::ChartMarkerType::Star,
ChartMarkerType::ShortDash => xlsx::ChartMarkerType::ShortDash,
ChartMarkerType::LongDash => xlsx::ChartMarkerType::LongDash,
ChartMarkerType::Circle => xlsx::ChartMarkerType::Circle,
ChartMarkerType::PlusSign => xlsx::ChartMarkerType::PlusSign,
}
}
}
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_point::ChartPoint, chart_range::ChartRange};
use super::{chart_data_label::ChartDataLabel, chart_marker::ChartMarker, chart_point::ChartPoint, chart_range::ChartRange};

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

#[wasm_bindgen(js_name = "setMarker", skip_jsdoc)]
pub fn set_marker(&self, marker: &ChartMarker) -> ChartSeries {
let mut series = self.inner.lock().unwrap();
series.set_marker(&marker.inner);
ChartSeries {
inner: Arc::clone(&self.inner),
}
}
}
2 changes: 2 additions & 0 deletions rust/src/wrapper/chart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod chart_font;
mod chart_format;
mod chart_legend;
mod chart_legend_position;
mod chart_marker;
mod chart_marker_type;
mod chart_point;
mod chart_range;
mod chart_series;
Expand Down
12 changes: 9 additions & 3 deletions test/chart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
ChartFont,
ChartDataLabel,
ChartDataLabelPosition,
ChartMarker,
ChartMarkerType,
} from "../web/wasm_xlsxwriter";
import { describe, test, beforeAll, expect } from "vitest";
import { initWasModule, readXlsx, readXlsxFile } from "./common";
Expand Down Expand Up @@ -39,26 +41,30 @@ describe("xlsx-wasm test", () => {
});
});

const chart = new Chart(ChartType.Line);
const chart = new Chart(ChartType.Stock);
const chartFont = new ChartFont().setName("Meiryo UI");
const chartDataLabel = new ChartDataLabel().setFont(chartFont).showValue().setPosition(ChartDataLabelPosition.Left);

const chartSeries1 = new ChartSeries();
const chartMarker1 = new ChartMarker().setType(ChartMarkerType.Circle).setSize(10);
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)
.setDataLabel(chartDataLabel);
.setDataLabel(chartDataLabel)
.setMarker(chartMarker1);
const chartSeries2 = new ChartSeries();
const chartMarker2 = new ChartMarker().setType(ChartMarkerType.Diamond).setSize(10);
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)
.setDataLabel(chartDataLabel);
.setDataLabel(chartDataLabel)
.setMarker(chartMarker2);
chart
.pushSeries(chartSeries1)
.pushSeries(chartSeries2);
Expand Down
Binary file modified test/expected/insert_chart.xlsx
Binary file not shown.