Fixed a data aliasing issue where LineStyle.solid and other constants like it were shared globally across all charts. Changed LineStyle.solid() and others into class methods, and changed DataSeries.style to automatically call these methods in order to correct legacy code.
git-svn-id: http://graphy.googlecode.com/svn/trunk@77 30582518-8026-11dd-8d1c-71c7e1663bfb
This commit is contained in:
parent
d2e474b2c9
commit
b0a8358821
|
@ -132,8 +132,24 @@ class DataSeries(object):
|
|||
'DataSeries.style.color instead.', DeprecationWarning,
|
||||
stacklevel=2)
|
||||
self.style.color = color
|
||||
|
||||
color = property(_GetColor, _SetColor)
|
||||
|
||||
color = property(_GetColor, _SetColor)
|
||||
|
||||
def _GetStyle(self):
|
||||
return self._style;
|
||||
|
||||
def _SetStyle(self, style):
|
||||
if style is not None and callable(style):
|
||||
warnings.warn('Your code may be broken ! LineStyle.solid and similar '
|
||||
'are no longer constants, but class methods that '
|
||||
'create LineStyle instances. Change your code to call '
|
||||
'LineStyle.solid() instead of passing it as a value.',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
self._style = style()
|
||||
else:
|
||||
self._style = style
|
||||
|
||||
style = property(_GetStyle, _SetStyle)
|
||||
|
||||
|
||||
class AxisPosition(object):
|
||||
|
|
|
@ -21,7 +21,6 @@ import warnings
|
|||
|
||||
from graphy import common
|
||||
|
||||
|
||||
class LineStyle(object):
|
||||
|
||||
"""Represents the style for a line on a line chart. Also provides some
|
||||
|
@ -36,12 +35,12 @@ class LineStyle(object):
|
|||
AutoColor will fill this in for you automatically if empty.
|
||||
|
||||
Some common styles, such as LineStyle.dashed, are available:
|
||||
solid
|
||||
dashed
|
||||
dotted
|
||||
thick_solid
|
||||
thick_dashed
|
||||
thick_dotted
|
||||
LineStyle.solid()
|
||||
LineStyle.dashed()
|
||||
LineStyle.dotted()
|
||||
LineStyle.thick_solid()
|
||||
LineStyle.thick_dashed()
|
||||
LineStyle.thick_dotted()
|
||||
"""
|
||||
|
||||
# Widths
|
||||
|
@ -61,13 +60,29 @@ class LineStyle(object):
|
|||
self.off = off
|
||||
self.color = color
|
||||
|
||||
@classmethod
|
||||
def solid(cls):
|
||||
return LineStyle(1, 1, 0)
|
||||
|
||||
@classmethod
|
||||
def dashed(cls):
|
||||
return LineStyle(1, 8, 4)
|
||||
|
||||
@classmethod
|
||||
def dotted(cls):
|
||||
return LineStyle(1, 2, 4)
|
||||
|
||||
@classmethod
|
||||
def thick_solid(cls):
|
||||
return LineStyle(2, 1, 0)
|
||||
|
||||
LineStyle.solid = LineStyle(1, 1, 0)
|
||||
LineStyle.dashed = LineStyle(1, 8, 4)
|
||||
LineStyle.dotted = LineStyle(1, 2, 4)
|
||||
LineStyle.thick_solid = LineStyle(2, 1, 0)
|
||||
LineStyle.thick_dashed = LineStyle(2, 8, 4)
|
||||
LineStyle.thick_dotted = LineStyle(2, 2, 4)
|
||||
@classmethod
|
||||
def thick_dashed(cls):
|
||||
return LineStyle(2, 8, 4)
|
||||
|
||||
@classmethod
|
||||
def thick_dotted(cls):
|
||||
return LineStyle(2, 2, 4)
|
||||
|
||||
|
||||
class LineChart(common.BaseChart):
|
||||
|
@ -110,7 +125,7 @@ class LineChart(common.BaseChart):
|
|||
label=None):
|
||||
"""DEPRECATED"""
|
||||
warnings.warn('LineChart.AddSeries is deprecated. Call AddLine instead. ',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
DeprecationWarning, stacklevel=2)
|
||||
return self.AddLine(points, color=color, width=style.width,
|
||||
pattern=(style.on, style.off), markers=markers,
|
||||
label=label)
|
||||
|
|
|
@ -36,13 +36,13 @@ class LineChartTest(graphy_test.GraphyTest):
|
|||
def testAddSeries(self):
|
||||
warnings.filterwarnings('ignore')
|
||||
chart = line_chart.LineChart()
|
||||
chart.AddSeries(points=[1, 2, 3], style=line_chart.LineStyle.solid,
|
||||
chart.AddSeries(points=[1, 2, 3], style=line_chart.LineStyle.solid(),
|
||||
markers='markers', label='label')
|
||||
series = chart.data[0]
|
||||
self.assertEqual(series.data, [1, 2, 3])
|
||||
self.assertEqual(series.style.width, line_chart.LineStyle.solid.width)
|
||||
self.assertEqual(series.style.on, line_chart.LineStyle.solid.on)
|
||||
self.assertEqual(series.style.off, line_chart.LineStyle.solid.off)
|
||||
self.assertEqual(series.style.width, line_chart.LineStyle.solid().width)
|
||||
self.assertEqual(series.style.on, line_chart.LineStyle.solid().on)
|
||||
self.assertEqual(series.style.off, line_chart.LineStyle.solid().off)
|
||||
self.assertEqual(series.markers, 'markers')
|
||||
self.assertEqual(series.label, 'label')
|
||||
|
||||
|
@ -62,16 +62,36 @@ class LineChartTest(graphy_test.GraphyTest):
|
|||
self.assertEqual('label', chart.data[0].label)
|
||||
self.assertEqual([x], chart.data[0].markers)
|
||||
self.assertEqual('color', chart.data[0].style.color)
|
||||
|
||||
|
||||
class LineStyleTest(graphy_test.GraphyTest):
|
||||
|
||||
def tearDown(self):
|
||||
warnings.resetwarnings()
|
||||
|
||||
def testPresets(self):
|
||||
"""Test selected traits from the preset line styles."""
|
||||
self.assertEqual(0, line_chart.LineStyle.solid.off)
|
||||
self.assert_(line_chart.LineStyle.dashed.off > 0)
|
||||
self.assert_(line_chart.LineStyle.solid.width <
|
||||
line_chart.LineStyle.thick_solid.width)
|
||||
|
||||
self.assertEqual(0, line_chart.LineStyle.solid().off)
|
||||
self.assert_(line_chart.LineStyle.dashed().off > 0)
|
||||
self.assert_(line_chart.LineStyle.solid().width <
|
||||
line_chart.LineStyle.thick_solid().width)
|
||||
|
||||
def testLineStyleByValueGivesWarning(self):
|
||||
"""Using LineStyle.foo as a value should throw a deprecation warning"""
|
||||
warnings.filterwarnings('error')
|
||||
self.assertRaises(DeprecationWarning, common.DataSeries, [],
|
||||
style=line_chart.LineStyle.solid)
|
||||
series = common.DataSeries([])
|
||||
def _TestAssignment():
|
||||
series.style = line_chart.LineStyle.solid
|
||||
self.assertRaises(DeprecationWarning, _TestAssignment)
|
||||
warnings.filterwarnings('ignore')
|
||||
series.style = line_chart.LineStyle.solid
|
||||
warnings.resetwarnings()
|
||||
self.assertEqual(1, series.style.width)
|
||||
self.assertEqual(1, series.style.on)
|
||||
self.assertEqual(0, series.style.off)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
graphy_test.main()
|
||||
|
|
Loading…
Reference in New Issue