Skip to content

Commit

Permalink
chart: add intial support for secondary axes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed May 28, 2024
1 parent 0b3c9da commit a1d967c
Show file tree
Hide file tree
Showing 17 changed files with 838 additions and 241 deletions.
677 changes: 437 additions & 240 deletions src/chart.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12239,7 +12239,7 @@ impl Worksheet {
pub(crate) fn prepare_worksheet_charts(&mut self, mut chart_id: u32, drawing_id: u32) -> u32 {
for chart in self.charts.values_mut() {
chart.id = chart_id;
chart.add_axis_ids();
chart.add_axis_ids(chart_id);
chart_id += 1;
}

Expand Down
Binary file added tests/input/bootstrap71.xlsx
Binary file not shown.
Binary file added tests/input/chart_area04.xlsx
Binary file not shown.
Binary file added tests/input/chart_bar24.xlsx
Binary file not shown.
Binary file added tests/input/chart_column04.xlsx
Binary file not shown.
Binary file added tests/input/chart_line02.xlsx
Binary file not shown.
Binary file added tests/input/chart_scatter07.xlsx
Binary file not shown.
Binary file added tests/input/chart_stock02.xlsx
Binary file not shown.
48 changes: 48 additions & 0 deletions tests/integration/bootstrap71.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Test case that compares a file generated by rust_xlsxwriter with a file
// created by Excel.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, [email protected]

use crate::common;
use rust_xlsxwriter::{Chart, ChartType, Workbook, XlsxError};

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

let worksheet = workbook.add_worksheet();

// Add some test data for the chart(s).
worksheet.write_column(0, 0, [1, 2, 3, 4, 5])?;
worksheet.write_column(0, 1, [6, 8, 6, 4, 2])?;

let mut chart = Chart::new(ChartType::Area);
// Test without chart ids

chart.add_series().set_values(("Sheet1", 0, 0, 4, 0));

chart
.add_series()
.set_values(("Sheet1", 0, 1, 4, 1))
.set_y2_axis(true);

worksheet.insert_chart(8, 4, &chart)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_bootstrap71() {
let test_runner = common::TestRunner::new()
.set_name("bootstrap71")
.set_function(create_new_xlsx_file)
.ignore_elements("xl/workbook.xml", "<fileVersion")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
49 changes: 49 additions & 0 deletions tests/integration/chart_area04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Test case that compares a file generated by rust_xlsxwriter with a file
// created by Excel.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, [email protected]

use crate::common;
use rust_xlsxwriter::{Chart, ChartType, Workbook, XlsxError};

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

let worksheet = workbook.add_worksheet();

// Add some test data for the chart(s).
worksheet.write_column(0, 0, [1, 2, 3, 4, 5])?;
worksheet.write_column(0, 1, [6, 8, 6, 4, 2])?;

let mut chart = Chart::new(ChartType::Area);
chart.set_axis_ids(63591168, 63592704);
chart.set_axis2_ids(74921856, 73764224);

chart.add_series().set_values(("Sheet1", 0, 0, 4, 0));

chart
.add_series()
.set_values(("Sheet1", 0, 1, 4, 1))
.set_y2_axis(true);

worksheet.insert_chart(8, 4, &chart)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_chart_area04() {
let test_runner = common::TestRunner::new()
.set_name("chart_area04")
.set_function(create_new_xlsx_file)
.ignore_elements("xl/workbook.xml", "<fileVersion")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
49 changes: 49 additions & 0 deletions tests/integration/chart_bar24.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Test case that compares a file generated by rust_xlsxwriter with a file
// created by Excel.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, [email protected]

use crate::common;
use rust_xlsxwriter::{Chart, ChartType, Workbook, XlsxError};

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

let worksheet = workbook.add_worksheet();

// Add some test data for the chart(s).
worksheet.write_column(0, 0, [27, 33, 44, 12, 1])?;
worksheet.write_column(0, 1, [6, 8, 6, 4, 2])?;

let mut chart = Chart::new(ChartType::Bar);
chart.set_axis_ids(63591168, 63592704);
chart.set_axis2_ids(65934464, 72628864);

chart.add_series().set_values(("Sheet1", 0, 0, 4, 0));

chart
.add_series()
.set_values(("Sheet1", 0, 1, 4, 1))
.set_y2_axis(true);

worksheet.insert_chart(8, 4, &chart)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_chart_bar24() {
let test_runner = common::TestRunner::new()
.set_name("chart_bar24")
.ignore_elements("xl/workbook.xml", "<fileVersion")
.set_function(create_new_xlsx_file)
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
49 changes: 49 additions & 0 deletions tests/integration/chart_column04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Test case that compares a file generated by rust_xlsxwriter with a file
// created by Excel.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, [email protected]

use crate::common;
use rust_xlsxwriter::{Chart, ChartType, Workbook, XlsxError};

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

let worksheet = workbook.add_worksheet();

// Add some test data for the chart(s).
worksheet.write_column(0, 0, [1, 2, 3, 4, 5])?;
worksheet.write_column(0, 1, [6, 8, 6, 4, 2])?;

let mut chart = Chart::new(ChartType::Column);
chart.set_axis_ids(63591936, 63593856);
chart.set_axis2_ids(63613568, 63612032);

chart.add_series().set_values(("Sheet1", 0, 0, 4, 0));

chart
.add_series()
.set_values(("Sheet1", 0, 1, 4, 1))
.set_y2_axis(true);

worksheet.insert_chart(8, 4, &chart)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_chart_column04() {
let test_runner = common::TestRunner::new()
.set_name("chart_column04")
.set_function(create_new_xlsx_file)
.ignore_elements("xl/workbook.xml", "<fileVersion")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
49 changes: 49 additions & 0 deletions tests/integration/chart_line02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Test case that compares a file generated by rust_xlsxwriter with a file
// created by Excel.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, [email protected]

use crate::common;
use rust_xlsxwriter::{Chart, ChartType, Workbook, XlsxError};

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

let worksheet = workbook.add_worksheet();

// Add some test data for the chart(s).
worksheet.write_column(0, 0, [1, 2, 3, 4, 5])?;
worksheet.write_column(0, 1, [6, 8, 6, 4, 2])?;

let mut chart = Chart::new(ChartType::Line);
chart.set_axis_ids(63593856, 63612032);
chart.set_axis2_ids(63615360, 63613568);

chart.add_series().set_values(("Sheet1", 0, 0, 4, 0));

chart
.add_series()
.set_values(("Sheet1", 0, 1, 4, 1))
.set_y2_axis(true);

worksheet.insert_chart(8, 4, &chart)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_chart_line02() {
let test_runner = common::TestRunner::new()
.set_name("chart_line02")
.set_function(create_new_xlsx_file)
.ignore_elements("xl/workbook.xml", "<fileVersion")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
55 changes: 55 additions & 0 deletions tests/integration/chart_scatter07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Test case that compares a file generated by rust_xlsxwriter with a file
// created by Excel.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, [email protected]

use crate::common;
use rust_xlsxwriter::{Chart, ChartType, Workbook, XlsxError};

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

let worksheet = workbook.add_worksheet();

// Add some test data for the chart(s).
worksheet.write_column(0, 0, [27, 33, 44, 12, 1])?;
worksheet.write_column(0, 1, [6, 8, 6, 4, 2])?;
worksheet.write_column(0, 2, [20, 10, 30, 50, 40])?;
worksheet.write_column(0, 3, [0, 27, 23, 30, 40])?;

let mut chart = Chart::new(ChartType::Scatter);
chart.set_axis_ids(63597952, 63616128);
chart.set_axis2_ids(63617664, 63619456);

chart
.add_series()
.set_categories(("Sheet1", 0, 0, 4, 0))
.set_values(("Sheet1", 0, 1, 4, 1));

chart
.add_series()
.set_categories(("Sheet1", 0, 2, 4, 2))
.set_values(("Sheet1", 0, 3, 4, 3))
.set_y2_axis(true);

worksheet.insert_chart(8, 4, &chart)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_chart_scatter07() {
let test_runner = common::TestRunner::new()
.set_name("chart_scatter07")
.set_function(create_new_xlsx_file)
.ignore_elements("xl/workbook.xml", "<fileVersion")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
Loading

0 comments on commit a1d967c

Please sign in to comment.