Skip to content

Commit

Permalink
Refactor chart axis label alignment.
Browse files Browse the repository at this point in the history
Issue #186
  • Loading branch information
jmcnamara committed Sep 1, 2018
1 parent 3d3bd12 commit f7f32de
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 31 deletions.
43 changes: 23 additions & 20 deletions include/xlsxwriter/chart.h
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,14 @@ typedef enum 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,
/** Chart axis label alignment: center. */
LXW_CHART_AXIS_LABEL_ALIGN_CENTER,

/** Series data label alignment: left. */
LXW_CHART_AXIS_LABEL_ALIGNMENT_LEFT,
/** Chart axis label alignment: left. */
LXW_CHART_AXIS_LABEL_ALIGN_LEFT,

/** Series data label alignment: right. */
LXW_CHART_AXIS_LABEL_ALIGNMENT_RIGHT
/** Chart axis label alignment: right. */
LXW_CHART_AXIS_LABEL_ALIGN_RIGHT
} lxw_chart_axis_label_alignment;

/**
Expand Down Expand Up @@ -1000,8 +1000,8 @@ 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 label_align;
uint8_t hidden;
uint8_t reverse;

Expand Down Expand Up @@ -2588,7 +2588,7 @@ void chart_axis_set_position(lxw_chart_axis *axis, uint8_t position);
*
* @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);
* chart_axis_set_label_position(chart->y_axis, LXW_CHART_AXIS_LABEL_POSITION_HIGH);
* @endcode
*
* @image html chart_label_position2.png
Expand All @@ -2612,25 +2612,28 @@ 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.
* @brief Set the alignment of the axis labels.
*
* @param axis A pointer to a chart #lxw_chart_axis object.
* @param alignment A #lxw_chart_label_alignment value.
* @param axis A pointer to a chart #lxw_chart_axis object.
* @param align A #lxw_chart_axis_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:
* Position the category axis labels for the chart. The labels are the numbers, or
* strings or dates, on the axis that indicate the categories 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.
* - #LXW_CHART_AXIS_LABEL_ALIGN_CENTER - Align label center (default).
* - #LXW_CHART_AXIS_LABEL_ALIGN_LEFT - Align label left.
* - #LXW_CHART_AXIS_LABEL_ALIGN_RIGHT - Align label right.
*
* @image html chart_label_alignment1.png
* @code
* chart_axis_set_label_align(chart->x_axis, LXW_CHART_AXIS_LABEL_ALIGN_RIGHT);
* @endcode
*
* **Axis types**: This function is applicable to to all axes types.
* **Axis types**: This function is applicable to category axes only.
* See @ref ww_charts_axes.
*/
void chart_axis_set_label_alignment(lxw_chart_axis *axis, uint8_t alignment);
void chart_axis_set_label_align(lxw_chart_axis *axis, uint8_t align);

/**
* @brief Set the minimum value for a chart axis.
Expand Down
10 changes: 5 additions & 5 deletions src/chart.c
Original file line number Diff line number Diff line change
Expand Up @@ -3208,9 +3208,9 @@ _chart_write_label_align(lxw_chart *self, lxw_chart_axis *axis)

LXW_INIT_ATTRIBUTES();

if (axis->label_alignment == LXW_CHART_AXIS_LABEL_ALIGNMENT_LEFT)
if (axis->label_align == LXW_CHART_AXIS_LABEL_ALIGN_LEFT)
LXW_PUSH_ATTRIBUTES_STR("val", "l");
else if (axis->label_alignment == LXW_CHART_AXIS_LABEL_ALIGNMENT_RIGHT)
else if (axis->label_align == LXW_CHART_AXIS_LABEL_ALIGN_RIGHT)
LXW_PUSH_ATTRIBUTES_STR("val", "r");
else
LXW_PUSH_ATTRIBUTES_STR("val", "ctr");
Expand Down Expand Up @@ -6054,12 +6054,12 @@ chart_axis_minor_gridlines_set_line(lxw_chart_axis *axis,
}

/*
* Set the labels alignement.
* Set the chart axis label alignment.
*/
void
chart_axis_set_label_alignment(lxw_chart_axis *axis, uint8_t alignment)
chart_axis_set_label_align(lxw_chart_axis *axis, uint8_t align)
{
axis->label_alignment = alignment;
axis->label_align = align;
}

/*
Expand Down
9 changes: 3 additions & 6 deletions test/functional/src/test_chart_axis42.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ int main() {
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;
chart->axis_id_1 = 61296640;
chart->axis_id_2 = 61298560;

uint8_t data[5][3] = {
{1, 2, 3},
Expand All @@ -36,10 +36,7 @@ int main() {
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);
chart_axis_set_label_align(chart->x_axis, LXW_CHART_AXIS_LABEL_ALIGN_RIGHT);

worksheet_insert_chart(worksheet, CELL("E9"), chart);

Expand Down
44 changes: 44 additions & 0 deletions test/functional/src/test_chart_axis43.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2018, John McNamara, [email protected]
*
*/

#include "xlsxwriter.h"

int main() {

lxw_workbook *workbook = new_workbook("test_chart_axis43.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 = 61296640;
chart->axis_id_2 = 61298560;

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_align(chart->x_axis, LXW_CHART_AXIS_LABEL_ALIGN_RIGHT);

worksheet_insert_chart(worksheet, CELL("E9"), chart);

return workbook_close(workbook);
}
Binary file modified test/functional/xlsx_files/chart_axis42.xlsx
Binary file not shown.
Binary file added test/functional/xlsx_files/chart_axis43.xlsx
Binary file not shown.

0 comments on commit f7f32de

Please sign in to comment.