Skip to content

Commit

Permalink
Added support for chart display units.
Browse files Browse the repository at this point in the history
Feature request: #185
  • Loading branch information
jmcnamara committed Mar 21, 2015
1 parent 4a4fd8e commit 7645173
Show file tree
Hide file tree
Showing 27 changed files with 710 additions and 3 deletions.
56 changes: 56 additions & 0 deletions xlsxwriter/chart.py
Expand Up @@ -623,6 +623,8 @@ def _convert_axis_args(self, axis, user_options):
'major_unit': options.get('major_unit'),
'minor_unit_type': options.get('minor_unit_type'),
'major_unit_type': options.get('major_unit_type'),
'display_units': options.get('display_units'),
'display_units_visible': options.get('display_units_visible'),
'log_base': options.get('log_base'),
'crossing': options.get('crossing'),
'position_axis': options.get('position_axis'),
Expand All @@ -639,6 +641,9 @@ def _convert_axis_args(self, axis, user_options):
else:
axis['visible'] = 1

# Convert the display units.
axis['display_units'] = self._get_display_units(axis['display_units'])

# Map major_gridlines properties.
if (options.get('major_gridlines')
and options['major_gridlines']['visible']):
Expand Down Expand Up @@ -1180,6 +1185,31 @@ def _get_points_properties(self, user_points):

return points

def _get_display_units(self, display_units):
# Convert user defined display units to internal units.
if not display_units:
return

types = {
'hundreds': 'hundreds',
'thousands': 'thousands',
'ten_thousands': 'tenThousands',
'hundred_thousands': 'hundredThousands',
'millions': 'millions',
'ten_millions': 'tenMillions',
'hundred_millions': 'hundredMillions',
'billions': 'billions',
'trillions': 'trillions',
}

if display_units in types:
display_units = types[display_units]
else:
warn("Unknown display_units type '%s'" % display_units)
return

return display_units

def _get_primary_axes_series(self):
# Returns series which use the primary axes.
primary_axes_series = []
Expand Down Expand Up @@ -1905,6 +1935,10 @@ def _write_val_axis(self, args):
# Write the c:minorUnit element.
self._write_c_minor_unit(y_axis.get('minor_unit'))

# Write the c:dispUnits element.
self._write_disp_units(y_axis.get('display_units'),
y_axis.get('display_units_visible'))

self._xml_end_tag('c:valAx')

def _write_cat_val_axis(self, args):
Expand Down Expand Up @@ -1995,6 +2029,10 @@ def _write_cat_val_axis(self, args):
# Write the c:minorUnit element.
self._write_c_minor_unit(x_axis.get('minor_unit'))

# Write the c:dispUnits element.
self._write_disp_units(x_axis.get('display_units'),
x_axis.get('display_units_visible'))

self._xml_end_tag('c:valAx')

def _write_date_axis(self, args):
Expand Down Expand Up @@ -3571,3 +3609,21 @@ def _write_down_bars(self, bar_format):
self._xml_end_tag('c:downBars')
else:
self._xml_empty_tag('c:downBars')

def _write_disp_units(self, units, display):
# Write the <c:dispUnits> element.

if not units:
return

attributes = [('val', units)]

self._xml_start_tag('c:dispUnits')
self._xml_empty_tag('c:builtInUnit', attributes)

if display:
self._xml_start_tag('c:dispUnitsLbl')
self._xml_empty_tag('c:layout')
self._xml_end_tag('c:dispUnitsLbl')

self._xml_end_tag('c:dispUnits')
52 changes: 52 additions & 0 deletions xlsxwriter/test/comparison/test_chart_display_units01.py
@@ -0,0 +1,52 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2015, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparsion_test import ExcelComparisonTest
from ...workbook import Workbook


class TestCompareXLSXFiles(ExcelComparisonTest):
"""
Test file created by XlsxWriter against a file created by Excel.
"""

def setUp(self):
self.maxDiff = None

filename = 'chart_display_units01.xlsx'

test_dir = 'xlsxwriter/test/comparison/'
self.got_filename = test_dir + '_test_' + filename
self.exp_filename = test_dir + 'xlsx_files/' + filename

self.ignore_files = []
self.ignore_elements = {}

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'column'})

chart.axis_ids = [69572096, 93549312]

data = [
[10000000, 20000000, 30000000, 20000000, 10000000],
]

worksheet.write_column(0, 0, data[0])

chart.add_series({'values': '=Sheet1!$A$1:$A$5'})

worksheet.insert_chart('E9', chart)

workbook.close()

self.assertExcelEqual()
54 changes: 54 additions & 0 deletions xlsxwriter/test/comparison/test_chart_display_units02.py
@@ -0,0 +1,54 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2015, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparsion_test import ExcelComparisonTest
from ...workbook import Workbook


class TestCompareXLSXFiles(ExcelComparisonTest):
"""
Test file created by XlsxWriter against a file created by Excel.
"""

def setUp(self):
self.maxDiff = None

filename = 'chart_display_units02.xlsx'

test_dir = 'xlsxwriter/test/comparison/'
self.got_filename = test_dir + '_test_' + filename
self.exp_filename = test_dir + 'xlsx_files/' + filename

self.ignore_files = []
self.ignore_elements = {}

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'column'})

chart.axis_ids = [56159232, 61364096]

data = [
[10000000, 20000000, 30000000, 20000000, 10000000],
]

worksheet.write_column(0, 0, data[0])

chart.add_series({'values': '=Sheet1!$A$1:$A$5'})

chart.set_y_axis({'display_units': 'hundreds'})

worksheet.insert_chart('E9', chart)

workbook.close()

self.assertExcelEqual()
54 changes: 54 additions & 0 deletions xlsxwriter/test/comparison/test_chart_display_units03.py
@@ -0,0 +1,54 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2015, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparsion_test import ExcelComparisonTest
from ...workbook import Workbook


class TestCompareXLSXFiles(ExcelComparisonTest):
"""
Test file created by XlsxWriter against a file created by Excel.
"""

def setUp(self):
self.maxDiff = None

filename = 'chart_display_units03.xlsx'

test_dir = 'xlsxwriter/test/comparison/'
self.got_filename = test_dir + '_test_' + filename
self.exp_filename = test_dir + 'xlsx_files/' + filename

self.ignore_files = []
self.ignore_elements = {}

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'column'})

chart.axis_ids = [56159232, 61364096]

data = [
[10000000, 20000000, 30000000, 20000000, 10000000],
]

worksheet.write_column(0, 0, data[0])

chart.add_series({'values': '=Sheet1!$A$1:$A$5'})

chart.set_y_axis({'display_units': 'thousands'})

worksheet.insert_chart('E9', chart)

workbook.close()

self.assertExcelEqual()
54 changes: 54 additions & 0 deletions xlsxwriter/test/comparison/test_chart_display_units04.py
@@ -0,0 +1,54 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2015, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparsion_test import ExcelComparisonTest
from ...workbook import Workbook


class TestCompareXLSXFiles(ExcelComparisonTest):
"""
Test file created by XlsxWriter against a file created by Excel.
"""

def setUp(self):
self.maxDiff = None

filename = 'chart_display_units04.xlsx'

test_dir = 'xlsxwriter/test/comparison/'
self.got_filename = test_dir + '_test_' + filename
self.exp_filename = test_dir + 'xlsx_files/' + filename

self.ignore_files = []
self.ignore_elements = {}

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'column'})

chart.axis_ids = [56159232, 61364096]

data = [
[10000000, 20000000, 30000000, 20000000, 10000000],
]

worksheet.write_column(0, 0, data[0])

chart.add_series({'values': '=Sheet1!$A$1:$A$5'})

chart.set_y_axis({'display_units': 'ten_thousands'})

worksheet.insert_chart('E9', chart)

workbook.close()

self.assertExcelEqual()
54 changes: 54 additions & 0 deletions xlsxwriter/test/comparison/test_chart_display_units05.py
@@ -0,0 +1,54 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2015, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparsion_test import ExcelComparisonTest
from ...workbook import Workbook


class TestCompareXLSXFiles(ExcelComparisonTest):
"""
Test file created by XlsxWriter against a file created by Excel.
"""

def setUp(self):
self.maxDiff = None

filename = 'chart_display_units05.xlsx'

test_dir = 'xlsxwriter/test/comparison/'
self.got_filename = test_dir + '_test_' + filename
self.exp_filename = test_dir + 'xlsx_files/' + filename

self.ignore_files = []
self.ignore_elements = {}

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'column'})

chart.axis_ids = [56159232, 61364096]

data = [
[10000000, 20000000, 30000000, 20000000, 10000000],
]

worksheet.write_column(0, 0, data[0])

chart.add_series({'values': '=Sheet1!$A$1:$A$5'})

chart.set_y_axis({'display_units': 'hundred_thousands'})

worksheet.insert_chart('E9', chart)

workbook.close()

self.assertExcelEqual()

0 comments on commit 7645173

Please sign in to comment.