class Filters(Serialisable): tagname = "filters" blank = Bool(allow_none=True) calendarType = NoneSet(values=[ "gregorian", "gregorianUs", "gregorianMeFrench", "gregorianArabic", "hijri", "hebrew", "taiwan", "japan", "thai", "korea", "saka", "gregorianXlitEnglish", "gregorianXlitFrench" ]) filter = ValueSequence(expected_type=unicode) dateGroupItem = Sequence(expected_type=DateGroupItem, allow_none=True) __elements__ = ('filter', 'dateGroupItem') def __init__( self, blank=None, calendarType=None, filter=(), dateGroupItem=(), ): self.blank = blank self.calendarType = calendarType self.filter = filter self.dateGroupItem = dateGroupItem
class ExternalSheetNames(Serialisable): sheetName = ValueSequence(expected_type=unicode) __elements__ = ('sheetName',) def __init__(self, sheetName=(), ): self.sheetName = sheetName
class CustomSplit(Serialisable): tagname = "custSplit" secondPiePt = ValueSequence(expected_type=int) __elements__ = ('secondPiePt',) def __init__(self, secondPiePt=(), ): self.secondPiePt = secondPiePt
class GradientFill(Fill): tagname = "gradientFill" __fields__ = ('type', 'degree', 'left', 'right', 'top', 'bottom', 'stop') type = Set(values=('linear', 'path')) fill_type = Alias("type") degree = Float() left = Float() right = Float() top = Float() bottom = Float() stop = ValueSequence(expected_type=Color, to_tree=_serialise_stop) def __init__(self, type="linear", degree=0, left=0, right=0, top=0, bottom=0, stop=(), fill_type=None): self.degree = degree self.left = left self.right = right self.top = top self.bottom = bottom self.stop = stop if fill_type is not None: type = fill_type self.type = type def __iter__(self): for attr in self.__attrs__: value = getattr(self, attr) if value: yield attr, safe_string(value) @classmethod def _from_tree(cls, node): colors = [] for color in safe_iterator(node, "{%s}color" % SHEET_MAIN_NS): colors.append(Color.from_tree(color)) return cls(stop=colors, **node.attrib) def to_tree(self, tagname=None, namespace=None): parent = Element("fill") el = super(GradientFill, self).to_tree() parent.append(el) return parent
class ChartBase(Serialisable): """ Base class for all charts """ legend = Typed(expected_type=Legend, allow_none=True) layout = Typed(expected_type=Layout, allow_none=True) roundedCorners = Bool(allow_none=True) axId = ValueSequence(expected_type=int) visible_cells_only = Bool(allow_none=True) display_blanks = Set(values=['span', 'gap', 'zero']) _series_type = "" ser = () series = Alias('ser') title = TitleDescriptor() anchor = "E15" # default anchor position width = 15 # in cm, approx 5 rows height = 7.5 # in cm, approx 14 rows _id = 1 _path = "/xl/charts/chart{0}.xml" style = MinMax(allow_none=True, min=1, max=48) mime_type = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml" graphical_properties = Typed(expected_type=GraphicalProperties, allow_none=True) __elements__ = () def __init__(self, axId=(), **kw): self._charts = [self] self.title = None self.layout = None self.roundedCorners = None self.legend = Legend() self.graphical_properties = None self.style = None self.plot_area = PlotArea() self.axId = axId self.display_blanks = 'gap' self.pivotSource = None self.pivotFormats = () self.visible_cells_only = True self.idx_base = 0 super(ChartBase, self).__init__() def __hash__(self): """ Just need to check for identity """ return id(self) def __iadd__(self, other): """ Combine the chart with another one """ if not isinstance(other, ChartBase): raise TypeError("Only other charts can be added") self._charts.append(other) return self def to_tree(self, namespace=None, tagname=None, idx=None): self.axId = [id for id in self._axes] if self.ser is not None: for s in self.ser: s.__elements__ = attribute_mapping[self._series_type] return super(ChartBase, self).to_tree(tagname, idx) def _reindex(self): """ Normalise and rebase series: sort by order and then rebase order """ # sort data series in order and rebase ds = sorted(self.series, key=attrgetter("order")) for idx, s in enumerate(ds): s.order = idx self.series = ds def _write(self): from .chartspace import ChartSpace, ChartContainer self.plot_area.layout = self.layout idx_base = self.idx_base for chart in self._charts: if chart not in self.plot_area._charts: chart.idx_base = idx_base idx_base += len(chart.series) self.plot_area._charts = self._charts container = ChartContainer(plotArea=self.plot_area, legend=self.legend, title=self.title) if isinstance(chart, _3DBase): container.view3D = chart.view3D container.floor = chart.floor container.sideWall = chart.sideWall container.backWall = chart.backWall container.plotVisOnly = self.visible_cells_only container.dispBlanksAs = self.display_blanks container.pivotFmts = self.pivotFormats cs = ChartSpace(chart=container) cs.style = self.style cs.roundedCorners = self.roundedCorners cs.pivotSource = self.pivotSource return cs.to_tree() @property def _axes(self): x = getattr(self, "x_axis", None) y = getattr(self, "y_axis", None) z = getattr(self, "z_axis", None) return OrderedDict([(axis.axId, axis) for axis in (x, y, z) if axis]) def set_categories(self, labels): """ Set the categories / x-axis values """ if not isinstance(labels, Reference): labels = Reference(range_string=labels) for s in self.ser: s.cat = AxDataSource(numRef=NumRef(f=labels)) def add_data(self, data, from_rows=False, titles_from_data=False): """ Add a range of data in a single pass. The default is to treat each column as a data series. """ if not isinstance(data, Reference): data = Reference(range_string=data) if from_rows: values = data.rows else: values = data.cols for ref in values: series = SeriesFactory(ref, title_from_data=titles_from_data) self.series.append(series) def append(self, value): """Append a data series to the chart""" l = self.series[:] l.append(value) self.series = l @property def path(self): return self._path.format(self._id)