mirror of
https://github.com/jmcnamara/libxlsxwriter.git
synced 2026-05-15 14:15:54 -06:00
Add function to set axis labels alignment
This commit is contained in:
parent
b74f48c6f1
commit
3d3bd12abe
6 changed files with 104 additions and 3 deletions
BIN
docs/images/chart_axis_label_alignment1.png
Executable file
BIN
docs/images/chart_axis_label_alignment1.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 7.9 KiB |
|
|
@ -529,6 +529,20 @@ typedef enum lxw_chart_axis_label_position {
|
|||
LXW_CHART_AXIS_LABEL_POSITION_NONE
|
||||
} lxw_chart_axis_label_position;
|
||||
|
||||
/**
|
||||
* @brief Axis label alignments.
|
||||
*/
|
||||
typedef enum lxw_chart_axis_label_alignment {
|
||||
/** Series data label alignment: center. */
|
||||
LXW_CHART_AXIS_LABEL_ALIGNMENT_CENTER,
|
||||
|
||||
/** Series data label alignment: left. */
|
||||
LXW_CHART_AXIS_LABEL_ALIGNMENT_LEFT,
|
||||
|
||||
/** Series data label alignment: right. */
|
||||
LXW_CHART_AXIS_LABEL_ALIGNMENT_RIGHT
|
||||
} lxw_chart_axis_label_alignment;
|
||||
|
||||
/**
|
||||
* @brief Display units for chart value axis.
|
||||
*/
|
||||
|
|
@ -986,6 +1000,7 @@ typedef struct lxw_chart_axis {
|
|||
uint8_t is_value;
|
||||
uint8_t axis_position;
|
||||
uint8_t position_axis;
|
||||
uint8_t label_alignment;
|
||||
uint8_t label_position;
|
||||
uint8_t hidden;
|
||||
uint8_t reverse;
|
||||
|
|
@ -2596,6 +2611,27 @@ void chart_axis_set_position(lxw_chart_axis *axis, uint8_t position);
|
|||
*/
|
||||
void chart_axis_set_label_position(lxw_chart_axis *axis, uint8_t position);
|
||||
|
||||
/**
|
||||
* @brief Position the alignment of axis labels.
|
||||
*
|
||||
* @param axis A pointer to a chart #lxw_chart_axis object.
|
||||
* @param alignment A #lxw_chart_label_alignment value.
|
||||
*
|
||||
* Position the axis labels for the chart. The labels are the numbers, or
|
||||
* strings or dates, on the axis that indicate the categories or values of
|
||||
* the axis. * The allowable values:
|
||||
*
|
||||
* - #LXW_CHART_AXIS_LABEL_ALIGNMENT_CENTER - Align label center (default).
|
||||
* - #LXW_CHART_AXIS_LABEL_ALIGNMENT_LEFT - Align label left.
|
||||
* - #LXW_CHART_AXIS_LABEL_ALIGNMENT_RIGHT - Align label right.
|
||||
*
|
||||
* @image html chart_label_alignment1.png
|
||||
*
|
||||
* **Axis types**: This function is applicable to to all axes types.
|
||||
* See @ref ww_charts_axes.
|
||||
*/
|
||||
void chart_axis_set_label_alignment(lxw_chart_axis *axis, uint8_t alignment);
|
||||
|
||||
/**
|
||||
* @brief Set the minimum value for a chart axis.
|
||||
*
|
||||
|
|
|
|||
21
src/chart.c
21
src/chart.c
|
|
@ -3201,13 +3201,19 @@ _chart_write_auto(lxw_chart *self)
|
|||
* Write the <c:lblAlgn> element.
|
||||
*/
|
||||
STATIC void
|
||||
_chart_write_label_align(lxw_chart *self)
|
||||
_chart_write_label_align(lxw_chart *self, lxw_chart_axis *axis)
|
||||
{
|
||||
struct xml_attribute_list attributes;
|
||||
struct xml_attribute *attribute;
|
||||
|
||||
LXW_INIT_ATTRIBUTES();
|
||||
LXW_PUSH_ATTRIBUTES_STR("val", "ctr");
|
||||
|
||||
if (axis->label_alignment == LXW_CHART_AXIS_LABEL_ALIGNMENT_LEFT)
|
||||
LXW_PUSH_ATTRIBUTES_STR("val", "l");
|
||||
else if (axis->label_alignment == LXW_CHART_AXIS_LABEL_ALIGNMENT_RIGHT)
|
||||
LXW_PUSH_ATTRIBUTES_STR("val", "r");
|
||||
else
|
||||
LXW_PUSH_ATTRIBUTES_STR("val", "ctr");
|
||||
|
||||
lxw_xml_empty_tag(self->file, "c:lblAlgn", &attributes);
|
||||
|
||||
|
|
@ -4098,7 +4104,7 @@ _chart_write_cat_axis(lxw_chart *self)
|
|||
_chart_write_auto(self);
|
||||
|
||||
/* Write the c:lblAlgn element. */
|
||||
_chart_write_label_align(self);
|
||||
_chart_write_label_align(self, self->x_axis);
|
||||
|
||||
/* Write the c:lblOffset element. */
|
||||
_chart_write_label_offset(self);
|
||||
|
|
@ -6047,6 +6053,15 @@ chart_axis_minor_gridlines_set_line(lxw_chart_axis *axis,
|
|||
axis->minor_gridlines.visible = LXW_TRUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the labels alignement.
|
||||
*/
|
||||
void
|
||||
chart_axis_set_label_alignment(lxw_chart_axis *axis, uint8_t alignment)
|
||||
{
|
||||
axis->label_alignment = alignment;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the chart title.
|
||||
*/
|
||||
|
|
|
|||
47
test/functional/src/test_chart_axis42.c
Normal file
47
test/functional/src/test_chart_axis42.c
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/*****************************************************************************
|
||||
* Test cases for libxlsxwriter.
|
||||
*
|
||||
* Test to compare output against Excel files.
|
||||
*
|
||||
* Copyright 2014-2018, John McNamara, jmcnamara@cpan.org
|
||||
*
|
||||
*/
|
||||
|
||||
#include "xlsxwriter.h"
|
||||
|
||||
int main() {
|
||||
|
||||
lxw_workbook *workbook = new_workbook("test_chart_axis42.xlsx");
|
||||
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
|
||||
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
|
||||
|
||||
/* For testing, copy the randomly generated axis ids in the target file. */
|
||||
chart->axis_id_1 = 43704320;
|
||||
chart->axis_id_2 = 43706624;
|
||||
|
||||
uint8_t data[5][3] = {
|
||||
{1, 2, 3},
|
||||
{2, 4, 6},
|
||||
{3, 6, 9},
|
||||
{4, 8, 12},
|
||||
{5, 10, 15}
|
||||
};
|
||||
|
||||
int row, col;
|
||||
for (row = 0; row < 5; row++)
|
||||
for (col = 0; col < 3; col++)
|
||||
worksheet_write_number(worksheet, row, col, data[row][col], NULL);
|
||||
|
||||
chart_add_series(chart, NULL, "=Sheet1!$A$1:$A$5");
|
||||
chart_add_series(chart, NULL, "=Sheet1!$B$1:$B$5");
|
||||
chart_add_series(chart, NULL, "=Sheet1!$C$1:$C$5");
|
||||
|
||||
chart_axis_set_name(chart->x_axis, "XXX");
|
||||
chart_axis_set_name(chart->y_axis, "YYY");
|
||||
|
||||
chart_axis_set_label_alignment(chart->x_axis, LXW_CHART_AXIS_LABEL_ALIGNMENT_RIGHT);
|
||||
|
||||
worksheet_insert_chart(worksheet, CELL("E9"), chart);
|
||||
|
||||
return workbook_close(workbook);
|
||||
}
|
||||
|
|
@ -130,3 +130,6 @@ class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
|
|||
|
||||
def test_chart_axis41(self):
|
||||
self.run_exe_test('test_chart_axis41')
|
||||
|
||||
def test_chart_axis42(self):
|
||||
self.run_exe_test('test_chart_axis42')
|
||||
|
|
|
|||
BIN
test/functional/xlsx_files/chart_axis42.xlsx
Normal file
BIN
test/functional/xlsx_files/chart_axis42.xlsx
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue