mirror of
https://github.com/jmcnamara/libxlsxwriter
synced 2025-03-28 21:13:14 +00:00
Add handling for chart axis label position.
This commit is contained in:
parent
062ddc5ee6
commit
5b6d2584bb
1
.indent.pro
vendored
1
.indent.pro
vendored
@ -49,6 +49,7 @@
|
||||
-T lxw_cell
|
||||
-T lxw_chart
|
||||
-T lxw_chart_axis
|
||||
-T lxw_chart_axis_label_position
|
||||
-T lxw_chart_axis_tick_position
|
||||
-T lxw_chart_fill
|
||||
-T lxw_chart_font
|
||||
|
BIN
docs/images/chart_label_position1.png
Normal file
BIN
docs/images/chart_label_position1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 62 KiB |
BIN
docs/images/chart_label_position2.png
Normal file
BIN
docs/images/chart_label_position2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 54 KiB |
@ -436,6 +436,26 @@ typedef enum lxw_chart_axis_tick_position {
|
||||
LXW_CHART_AXIS_POSITION_BETWEEN
|
||||
} lxw_chart_axis_tick_position;
|
||||
|
||||
/**
|
||||
* @brief Axis label positions.
|
||||
*/
|
||||
typedef enum lxw_chart_axis_label_position {
|
||||
|
||||
/** Position the axis labels next to the axis. The default. */
|
||||
LXW_CHART_AXIS_LABEL_POSITION_NEXT_TO,
|
||||
|
||||
/** Position the axis labels at the top of the chart, for horizontal
|
||||
* axes, or to the right for vertical axes.*/
|
||||
LXW_CHART_AXIS_LABEL_POSITION_HIGH,
|
||||
|
||||
/** Position the axis labels at the bottom of the chart, for horizontal
|
||||
* axes, or to the left for vertical axes.*/
|
||||
LXW_CHART_AXIS_LABEL_POSITION_LOW,
|
||||
|
||||
/** Turn off the the axis labels. */
|
||||
LXW_CHART_AXIS_LABEL_POSITION_NONE
|
||||
} lxw_chart_axis_label_position;
|
||||
|
||||
enum lxw_chart_position {
|
||||
LXW_CHART_AXIS_RIGHT,
|
||||
LXW_CHART_AXIS_LEFT,
|
||||
@ -666,6 +686,7 @@ typedef struct lxw_chart_axis {
|
||||
uint8_t is_date;
|
||||
uint8_t is_value;
|
||||
uint8_t position_axis;
|
||||
uint8_t label_position;
|
||||
uint8_t hidden;
|
||||
uint8_t reverse;
|
||||
uint8_t has_min;
|
||||
@ -1402,6 +1423,43 @@ void chart_axis_off(lxw_chart_axis *axis);
|
||||
*/
|
||||
void chart_axis_set_position(lxw_chart_axis *axis, uint8_t position);
|
||||
|
||||
/**
|
||||
* @brief Position the axis labels.
|
||||
*
|
||||
* @param axis A pointer to a chart #lxw_chart_axis object.
|
||||
* @param position A #lxw_chart_axis_label_position 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.
|
||||
*
|
||||
* For example:
|
||||
*
|
||||
* @code
|
||||
* chart_axis_set_label_position(chart->x_axis, LXW_CHART_AXIS_LABEL_POSITION_HIGH);
|
||||
chart_axis_set_label_position(chart->y_axis, LXW_CHART_AXIS_LABEL_POSITION_HIGH);
|
||||
* @endcode
|
||||
*
|
||||
* @image html chart_label_position2.png
|
||||
*
|
||||
* The allowable values:
|
||||
*
|
||||
* - #LXW_CHART_AXIS_LABEL_POSITION_NEXT_TO - The default.
|
||||
* - #LXW_CHART_AXIS_LABEL_POSITION_HIGH - Also right for vertical axes.
|
||||
* - #LXW_CHART_AXIS_LABEL_POSITION_LOW - Also left for vertical axes.
|
||||
* - #LXW_CHART_AXIS_LABEL_POSITION_NONE
|
||||
*
|
||||
* @image html chart_label_position1.png
|
||||
*
|
||||
* The #LXW_CHART_AXIS_LABEL_POSITION_NONE turns off the axis labels. This
|
||||
* is slightly different from `chart_axis_off()` which also turns off the
|
||||
* labels but also turns off tick marks.
|
||||
*
|
||||
* **Axis types**: This function is applicable to to all axes types.
|
||||
* See @ref ww_charts_axes.
|
||||
*/
|
||||
void chart_axis_set_label_position(lxw_chart_axis *axis, uint8_t position);
|
||||
|
||||
/**
|
||||
* @brief Set the minimum value for a chart axis.
|
||||
*
|
||||
|
28
src/chart.c
28
src/chart.c
@ -2242,14 +2242,21 @@ _chart_write_axis_pos(lxw_chart *self, uint8_t position, uint8_t reverse)
|
||||
* Write the <c:tickLblPos> element.
|
||||
*/
|
||||
STATIC void
|
||||
_chart_write_tick_lbl_pos(lxw_chart *self)
|
||||
_chart_write_tick_label_pos(lxw_chart *self, lxw_chart_axis *axis)
|
||||
{
|
||||
struct xml_attribute_list attributes;
|
||||
struct xml_attribute *attribute;
|
||||
char val[] = "nextTo";
|
||||
|
||||
LXW_INIT_ATTRIBUTES();
|
||||
LXW_PUSH_ATTRIBUTES_STR("val", val);
|
||||
|
||||
if (axis->label_position == LXW_CHART_AXIS_LABEL_POSITION_HIGH)
|
||||
LXW_PUSH_ATTRIBUTES_STR("val", "high");
|
||||
else if (axis->label_position == LXW_CHART_AXIS_LABEL_POSITION_LOW)
|
||||
LXW_PUSH_ATTRIBUTES_STR("val", "low");
|
||||
else if (axis->label_position == LXW_CHART_AXIS_LABEL_POSITION_NONE)
|
||||
LXW_PUSH_ATTRIBUTES_STR("val", "none");
|
||||
else
|
||||
LXW_PUSH_ATTRIBUTES_STR("val", "nextTo");
|
||||
|
||||
lxw_xml_empty_tag(self->file, "c:tickLblPos", &attributes);
|
||||
|
||||
@ -2727,7 +2734,7 @@ _chart_write_cat_axis(lxw_chart *self)
|
||||
_chart_write_major_tick_mark(self, self->x_axis);
|
||||
|
||||
/* Write the c:tickLblPos element. */
|
||||
_chart_write_tick_lbl_pos(self);
|
||||
_chart_write_tick_label_pos(self, self->x_axis);
|
||||
|
||||
/* Write the c:spPr element for the axis line. */
|
||||
_chart_write_sp_pr(self, self->x_axis->line, self->x_axis->fill,
|
||||
@ -2797,7 +2804,7 @@ _chart_write_val_axis(lxw_chart *self)
|
||||
_chart_write_major_tick_mark(self, self->y_axis);
|
||||
|
||||
/* Write the c:tickLblPos element. */
|
||||
_chart_write_tick_lbl_pos(self);
|
||||
_chart_write_tick_label_pos(self, self->y_axis);
|
||||
|
||||
/* Write the c:spPr element for the axis line. */
|
||||
_chart_write_sp_pr(self, self->y_axis->line, self->y_axis->fill,
|
||||
@ -2861,7 +2868,7 @@ _chart_write_cat_val_axis(lxw_chart *self)
|
||||
_chart_write_major_tick_mark(self, self->x_axis);
|
||||
|
||||
/* Write the c:tickLblPos element. */
|
||||
_chart_write_tick_lbl_pos(self);
|
||||
_chart_write_tick_label_pos(self, self->x_axis);
|
||||
|
||||
/* Write the c:spPr element for the axis line. */
|
||||
_chart_write_sp_pr(self, self->x_axis->line, self->x_axis->fill,
|
||||
@ -3944,6 +3951,15 @@ chart_axis_set_position(lxw_chart_axis *axis, uint8_t position)
|
||||
axis->position_axis = position;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the axis label position.
|
||||
*/
|
||||
void
|
||||
chart_axis_set_label_position(lxw_chart_axis *axis, uint8_t position)
|
||||
{
|
||||
axis->label_position = position;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the minimum value for an axis.
|
||||
*/
|
||||
|
45
test/functional/src/test_chart_axis19.c
Normal file
45
test/functional/src/test_chart_axis19.c
Normal file
@ -0,0 +1,45 @@
|
||||
/*****************************************************************************
|
||||
* Test cases for libxlsxwriter.
|
||||
*
|
||||
* Test to compare output against Excel files.
|
||||
*
|
||||
* Copyright 2014-2017, John McNamara, jmcnamara@cpan.org
|
||||
*
|
||||
*/
|
||||
|
||||
#include "xlsxwriter.h"
|
||||
|
||||
int main() {
|
||||
|
||||
lxw_workbook *workbook = new_workbook("test_chart_axis19.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 = 43813504;
|
||||
chart->axis_id_2 = 45705472;
|
||||
|
||||
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_label_position(chart->x_axis, LXW_CHART_AXIS_LABEL_POSITION_HIGH);
|
||||
chart_axis_set_label_position(chart->y_axis, LXW_CHART_AXIS_LABEL_POSITION_LOW);
|
||||
|
||||
worksheet_insert_chart(worksheet, CELL("E9"), chart);
|
||||
|
||||
return workbook_close(workbook);
|
||||
}
|
45
test/functional/src/test_chart_axis20.c
Normal file
45
test/functional/src/test_chart_axis20.c
Normal file
@ -0,0 +1,45 @@
|
||||
/*****************************************************************************
|
||||
* Test cases for libxlsxwriter.
|
||||
*
|
||||
* Test to compare output against Excel files.
|
||||
*
|
||||
* Copyright 2014-2017, John McNamara, jmcnamara@cpan.org
|
||||
*
|
||||
*/
|
||||
|
||||
#include "xlsxwriter.h"
|
||||
|
||||
int main() {
|
||||
|
||||
lxw_workbook *workbook = new_workbook("test_chart_axis20.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 = 43572224;
|
||||
chart->axis_id_2 = 43812352;
|
||||
|
||||
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_label_position(chart->x_axis, LXW_CHART_AXIS_LABEL_POSITION_NEXT_TO);
|
||||
chart_axis_set_label_position(chart->y_axis, LXW_CHART_AXIS_LABEL_POSITION_NONE);
|
||||
|
||||
worksheet_insert_chart(worksheet, CELL("E9"), chart);
|
||||
|
||||
return workbook_close(workbook);
|
||||
}
|
@ -56,7 +56,11 @@ class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
|
||||
def test_chart_axis17(self):
|
||||
self.run_exe_test('test_chart_axis17')
|
||||
|
||||
def test_chart_axis19(self):
|
||||
self.run_exe_test('test_chart_axis19')
|
||||
|
||||
def test_chart_axis20(self):
|
||||
self.run_exe_test('test_chart_axis20')
|
||||
|
||||
def test_chart_axis21(self):
|
||||
self.run_exe_test('test_chart_axis21')
|
||||
|
BIN
test/functional/xlsx_files/chart_axis19.xlsx
Normal file
BIN
test/functional/xlsx_files/chart_axis19.xlsx
Normal file
Binary file not shown.
BIN
test/functional/xlsx_files/chart_axis20.xlsx
Normal file
BIN
test/functional/xlsx_files/chart_axis20.xlsx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user