Added test for charts with sparse data.

This commit is contained in:
John McNamara 2016-05-20 23:40:23 +01:00
parent 7745394365
commit dd8d0599a2
6 changed files with 125 additions and 42 deletions

View File

@ -131,6 +131,7 @@ typedef struct lxw_series_data_point {
uint8_t is_string;
double number;
char *string;
uint8_t no_data;
STAILQ_ENTRY (lxw_series_data_point) list_pointers;

View File

@ -779,6 +779,10 @@ _chart_write_pt(lxw_chart *self, uint16_t index,
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
/* Ignore chart points that have no data. */
if (data_point->no_data)
return;
LXW_INIT_ATTRIBUTES();
LXW_PUSH_ATTRIBUTES_INT("idx", index);
@ -804,6 +808,10 @@ _chart_write_num_pt(lxw_chart *self, uint16_t index,
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
/* Ignore chart points that have no data. */
if (data_point->no_data)
return;
LXW_INIT_ATTRIBUTES();
LXW_PUSH_ATTRIBUTES_INT("idx", index);

View File

@ -658,7 +658,7 @@ _populate_range_data_cache(lxw_workbook *self, lxw_series_range *range)
}
}
else {
data_point->number = 0;
data_point->no_data = LXW_TRUE;
}
STAILQ_INSERT_TAIL(range->data_cache, data_point, list_pointers);
@ -678,7 +678,7 @@ STATIC void
_populate_range_dimensions(lxw_workbook *self, lxw_series_range *range)
{
char formula[128] = { 0 };
char formula[LXW_MAX_FORMULA_RANGE_LENGTH] = { 0 };
char *tmp_str;
char *sheetname;
@ -690,51 +690,54 @@ _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range)
return;
}
if (!range->sheetname) {
/* The sheetname hasn't been defined but formula has. */
/* If the sheetname is already defined it was already set via
* chart_series_set_categories() or chart_series_set_values().
*/
if (range->sheetname)
return;
/* Ignore non-contiguous like (Sheet1!$A$1:$A$2,Sheet1!$A$4:$A$5) */
if (range->formula[0] == '(') {
/* Ignore non-contiguous range like (Sheet1!$A$1:$A$2,Sheet1!$A$4:$A$5) */
if (range->formula[0] == '(') {
range->ignore_cache = LXW_TRUE;
return;
}
/* Create a copy of the formula to modify and parse into parts. */
lxw_snprintf(formula, LXW_MAX_FORMULA_RANGE_LENGTH, "%s", range->formula);
/* Check for valid formula. TODO. This needs stronger validation. */
tmp_str = strchr(formula, '!');
if (tmp_str == NULL) {
range->ignore_cache = LXW_TRUE;
return;
}
else {
/* Split the formulas into sheetname and row-col data. */
*tmp_str = '\0';
tmp_str++;
sheetname = formula;
/* Remove any worksheet quoting. */
if (sheetname[0] == '\'')
sheetname++;
if (sheetname[strlen(sheetname) - 1] == '\'')
sheetname[strlen(sheetname) - 1] = '\0';
/* Check that the sheetname exists. */
if (!workbook_get_worksheet_by_name(self, sheetname)) {
LXW_WARN_FORMAT2("workbook_add_chart(): worksheet name '%s' "
"in chart formula '%s' doesn't exist.",
sheetname, range->formula);
range->ignore_cache = LXW_TRUE;
return;
}
lxw_snprintf(formula, 128, "%s", range->formula);
/* Check for valid formula. TODO. This needs stronger validation. */
tmp_str = strchr(formula, '!');
if (tmp_str == NULL) {
range->ignore_cache = LXW_TRUE;
return;
}
else {
/* Split the formulas into sheetname and row-col data. */
*tmp_str = '\0';
tmp_str++;
sheetname = formula;
/* Remove any worksheet quoting. */
if (sheetname[0] == '\'')
sheetname++;
if (sheetname[strlen(sheetname) - 1] == '\'')
sheetname[strlen(sheetname) - 1] = '\0';
/* Check that the sheetname exists. */
if (!workbook_get_worksheet_by_name(self, sheetname)) {
LXW_WARN_FORMAT2("workbook_add_chart(): worksheet name '%s' "
"in chart formula '%s' doesn't exist.",
sheetname, range->formula);
range->ignore_cache = LXW_TRUE;
return;
}
range->sheetname = lxw_strdup(sheetname);
range->first_row = lxw_name_to_row(tmp_str);
range->first_col = lxw_name_to_col(tmp_str);
range->last_row = lxw_name_to_row_2(tmp_str);
range->last_col = lxw_name_to_col_2(tmp_str);
}
range->sheetname = lxw_strdup(sheetname);
range->first_row = lxw_name_to_row(tmp_str);
range->first_col = lxw_name_to_col(tmp_str);
range->last_row = lxw_name_to_row_2(tmp_str);
range->last_col = lxw_name_to_col_2(tmp_str);
}
}

View File

@ -0,0 +1,54 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2015, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = new_workbook("test_chart_sparse01.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_BAR);
/* For testing, copy the randomly generated axis ids in the target file. */
chart->axis_id_1 = 46202880;
chart->axis_id_2 = 46204416;
worksheet_write_number(worksheet, 0, 0, 1, NULL);
worksheet_write_number(worksheet, 1, 0, 2, NULL);
worksheet_write_number(worksheet, 2, 0, 3, NULL);
worksheet_write_number(worksheet, 3, 0, 4, NULL);
worksheet_write_number(worksheet, 4, 0, 5, NULL);
worksheet_write_number(worksheet, 0, 1, 2, NULL);
/* Omit the next data point to test how it is handled. */
/* worksheet_write_number(worksheet, 1, 1, 4, NULL); */
worksheet_write_number(worksheet, 2, 1, 6, NULL);
worksheet_write_number(worksheet, 3, 1, 8, NULL);
worksheet_write_number(worksheet, 4, 1, 10, NULL);
worksheet_write_number(worksheet, 0, 2, 3, NULL);
worksheet_write_number(worksheet, 1, 2, 6, NULL);
worksheet_write_number(worksheet, 2, 2, 9, NULL);
worksheet_write_number(worksheet, 3, 2, 12, NULL);
worksheet_write_number(worksheet, 4, 2, 15, NULL);
chart_add_series(chart,
"=Sheet1!$A$1:$A$5",
"=Sheet1!$B$1:$B$5"
);
chart_add_series(chart,
"=Sheet1!$A$1:$A$6", /* Ranges exceeds the data. */
"=Sheet1!$C$1:$C$6"
);
worksheet_insert_chart(worksheet, CELL("E9"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,17 @@
###############################################################################
#
# Tests for libxlsxwriter.
#
# Copyright 2014-2016, John McNamara, jmcnamara@cpan.org
#
import base_test_class
class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
"""
Test file created with libxlsxwriter against a file created by Excel.
"""
def test_chart_sparse01(self):
self.run_exe_test('test_chart_sparse01')

Binary file not shown.