Fix for ignored position in chart custom data labels.

Fix issue where custom chart data labels didn't inherit the position
for the data labels in the series.`
This commit is contained in:
John McNamara 2020-10-02 20:44:38 +01:00
parent dfbc07c973
commit 110af3ff4d
8 changed files with 176 additions and 0 deletions

View File

@ -2317,6 +2317,10 @@ _chart_write_custom_label_formula(lxw_chart *self, lxw_chart_series *series,
_chart_write_custom_label_format_only(self, data_label);
/* Write the c:dLblPos element. */
if (series->label_position)
_chart_write_d_lbl_pos(self, series->label_position);
/* Write the c:showVal element. */
if (series->show_labels_value)
_chart_write_show_val(self);
@ -2356,6 +2360,10 @@ _chart_write_custom_label_str(lxw_chart *self, lxw_chart_series *series,
_chart_write_sp_pr(self, data_label->line, data_label->fill,
data_label->pattern);
/* Write the c:dLblPos element. */
if (series->label_position)
_chart_write_d_lbl_pos(self, series->label_position);
/* Write the c:showVal element. */
if (series->show_labels_value)
_chart_write_show_val(self);

View File

@ -0,0 +1,53 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_chart_data_labels48.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 = 61160832;
chart->axis_id_2 = 61167104;
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);
lxw_chart_data_label data_label1 = {.value = "31"};
lxw_chart_data_label data_label2 = {.value = "32"};
lxw_chart_data_label *data_labels1[] = {&data_label1, NULL};
lxw_chart_data_label *data_labels2[] = {&data_label2, NULL};
lxw_chart_series *series1 = chart_add_series(chart, NULL, "=Sheet1!$A$1:$A$5");
lxw_chart_series *series2 = chart_add_series(chart, NULL, "=Sheet1!$B$1:$B$5");
chart_add_series(chart, NULL, "=Sheet1!$C$1:$C$5");
chart_series_set_labels(series1);
chart_series_set_labels(series2);
chart_series_set_labels_position(series2, LXW_CHART_LABEL_POSITION_INSIDE_BASE);
chart_series_set_labels_custom(series1, data_labels1);
chart_series_set_labels_custom(series2, data_labels2);
worksheet_insert_chart(worksheet, CELL("E9"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,53 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_chart_data_labels49.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 = 59202176;
chart->axis_id_2 = 60966784;
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);
lxw_chart_data_label data_label1 = {.value = "=Sheet1!$B$1"};
lxw_chart_data_label data_label2 = {.value = "=Sheet1!$B$2"};
lxw_chart_data_label *data_labels1[] = {&data_label1, NULL};
lxw_chart_data_label *data_labels2[] = {&data_label2, NULL};
lxw_chart_series *series1 = chart_add_series(chart, NULL, "=Sheet1!$A$1:$A$5");
lxw_chart_series *series2 = chart_add_series(chart, NULL, "=Sheet1!$B$1:$B$5");
chart_add_series(chart, NULL, "=Sheet1!$C$1:$C$5");
chart_series_set_labels(series1);
chart_series_set_labels(series2);
chart_series_set_labels_position(series2, LXW_CHART_LABEL_POSITION_INSIDE_BASE);
chart_series_set_labels_custom(series1, data_labels1);
chart_series_set_labels_custom(series2, data_labels2);
worksheet_insert_chart(worksheet, CELL("E9"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,53 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_chart_data_labels50.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 = 84605184;
chart->axis_id_2 = 84639744;
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);
lxw_chart_line line = {.color = LXW_COLOR_RED};
lxw_chart_font font = {.bold = LXW_TRUE, .italic = LXW_TRUE, .color = LXW_COLOR_RED, .baseline = -1};
lxw_chart_data_label data_label1 = {.line = &line, .font = &font};
lxw_chart_data_label *data_labels[] = {&data_label1, NULL};
lxw_chart_series *series = chart_add_series(chart, NULL, "=Sheet1!$A$1:$A$5");
chart_series_set_labels_custom(series, data_labels);
chart_series_set_labels_position(series, LXW_CHART_LABEL_POSITION_CENTER);
chart_add_series(chart, NULL, "=Sheet1!$B$1:$B$5");
chart_add_series(chart, NULL, "=Sheet1!$C$1:$C$5");
worksheet_insert_chart(worksheet, CELL("E9"), chart);
return workbook_close(workbook);
}

View File

@ -149,3 +149,12 @@ class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
def test_chart_data_labels47(self):
self.run_exe_test('test_chart_data_labels47')
def test_chart_data_labels48(self):
self.run_exe_test('test_chart_data_labels48')
def test_chart_data_labels49(self):
self.run_exe_test('test_chart_data_labels49')
def test_chart_data_labels50(self):
self.run_exe_test('test_chart_data_labels50')

Binary file not shown.

Binary file not shown.

Binary file not shown.