1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2026, John McNamara, jmcnamara@cpan.org
//! A chart example demonstrating setting the axes bounds for chart axes.
use rust_xlsxwriter::{Chart, ChartType, Workbook, XlsxError};
fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
// Add some data for the chart.
worksheet.write(0, 0, 10)?;
worksheet.write(1, 0, -30)?;
worksheet.write(2, 0, 40)?;
worksheet.write(3, 0, -30)?;
worksheet.write(4, 0, 10)?;
// Create a new chart.
let mut chart = Chart::new(ChartType::Column);
// Add a data series using Excel formula syntax to describe the range.
chart.add_series().set_values("Sheet1!$A$1:$A$5");
// Set the value axes bounds.
chart.y_axis().set_min(-60);
chart.y_axis().set_max(60);
// Hide legend for clarity.
chart.legend().set_hidden();
// Add the chart to the worksheet.
worksheet.insert_chart(0, 2, &chart)?;
// Save the file.
workbook.save("chart.xlsx")?;
Ok(())
}