private void SetPropertiesFromLegend(OpenXmlCompositeElement seriesItem, Column dataColumn) { if (dataColumn.Legends.All(l => l == null)) { return; } var dataPoints = seriesItem.Elements <DataPoint>(); var labels = seriesItem.FirstElement <DataLabels>(); for (int rowNo = 0; rowNo < dataColumn.Data.Count; rowNo++) { if (dataColumn.Legends[rowNo] != null && dataColumn.Data[rowNo] != null) { ShapeElement legend = dataColumn.Legends[rowNo] as ShapeElement; DataPoint dataPoint = dataPoints.FirstOrDefault(p => p.Index != null && p.Index.Val != null && p.Index.Val.Value == rowNo); if (legend != null) //KAJ: Actually always true if you have reached this line { if (dataPoint != null) { dataPoint.RemoveAllChildren <ChartShapeProperties>(); A.SolidFill legendFill = legend.Element.GetFill(); A.Outline legendOutline = legend.Element.GetOutline(); ChartShapeProperties chartShapeProperties = new ChartShapeProperties(); chartShapeProperties.Append(legendFill); chartShapeProperties.Append(legendOutline); dataPoint.Append(chartShapeProperties); } if (labels != null) { DataLabel label = labels.Elements <DataLabel>().FirstOrDefault(p => p.Index != null && p.Index.Val != null && p.Index.Val.Value == rowNo); if (label != null) { TextProperties labelTextProperties = label.FirstElement <TextProperties>(); if (labelTextProperties == null) { labelTextProperties = labels.FirstElement <TextProperties>().CloneNode(true) as TextProperties; label.Append(labelTextProperties); } NumberingFormat labelNumberingFormat = label.FirstElement <NumberingFormat>(); if (labelNumberingFormat == null) { labelNumberingFormat = labels.FirstElement <NumberingFormat>().CloneNode(true) as NumberingFormat; label.Append(labelNumberingFormat); } A.Paragraph labelParagraph = labelTextProperties.FirstElement <A.Paragraph>(); var legendRunProperties = legend.Element.GetRunProperties(); labelParagraph.ParagraphProperties.RemoveAllChildren <A.DefaultRunProperties>(); List <OpenXmlElement> list = new List <OpenXmlElement>(); foreach (var item in legendRunProperties.ChildElements) { list.Add(item.CloneNode(true)); } var newLabelRunProperties = new A.DefaultRunProperties(list); labelParagraph.ParagraphProperties.Append(newLabelRunProperties); var labelShapeProperties = label.FirstElement <ChartShapeProperties>(); if (labelShapeProperties != null && labelShapeProperties.FirstElement <A.NoFill>() == null) { label.RemoveAllChildren <ChartShapeProperties>(); } A.SolidFill legendFill = legend.Element.GetFill(); ChartShapeProperties chartShapeProperties = new ChartShapeProperties(); chartShapeProperties.Append(legendFill); label.Append(chartShapeProperties); } } } } } }
private void FillSeriesLabels(OpenXmlCompositeElement seriesItem, Column dataColumn) { var labels = seriesItem.FirstElement <DataLabels>(); if (labels == null) { return; } TextProperties defaultTextProperties = labels.FirstElement <TextProperties>(); ChartShapeProperties defaultChartShapeProperties = labels.FirstElement <ChartShapeProperties>(); NumberingFormat defaultNumberingFormat = labels.FirstElement <NumberingFormat>(); ShowLegendKey defaultShowLegendKey = labels.FirstElement <ShowLegendKey>(); ShowValue defaultShowValue = labels.FirstElement <ShowValue>(); ShowCategoryName defaultShowCategoryName = labels.FirstElement <ShowCategoryName>(); ShowSeriesName defaultShowSeriesName = labels.FirstElement <ShowSeriesName>(); ShowPercent defaultShowPercent = labels.FirstElement <ShowPercent>(); ShowBubbleSize defaultShowBubbleSize = labels.FirstElement <ShowBubbleSize>(); ShowLeaderLines defaultShowLeaderLines = labels.FirstElement <ShowLeaderLines>(); DLblsExtension defaultDLblsExtension = labels.FirstElement <DLblsExtension>(); for (int rowNo = 0; rowNo < dataColumn.Data.Count; rowNo++) { if (dataColumn.Data[rowNo] != null) { DataLabel dl = labels.Elements <DataLabel>().FirstOrDefault(l => l.Index != null && l.Index.Val != null && l.Index.Val.Value == rowNo); //if (dl != null) //{ // Delete delete = dl.FirstElement<Delete>(); // if (delete == null || !delete.Val) // { // var tp = dl.FirstElement<TextProperties>(); // if (tp != null) // { // textProperties = tp.CloneNode(true) as TextProperties; // } // labels.RemoveChild<DataLabel>(dl); // dl = null; // } //} if (dl == null) { var newDataLabel = new DataLabel(); DocumentFormat.OpenXml.Drawing.Charts.Index index = new DocumentFormat.OpenXml.Drawing.Charts.Index() { Val = new UInt32Value((uint)rowNo) }; newDataLabel.Index = index; if (defaultNumberingFormat != null) { newDataLabel.Append(defaultNumberingFormat.CloneNode(true)); } if (defaultChartShapeProperties != null) { newDataLabel.Append(defaultChartShapeProperties.CloneNode(true)); } if (defaultTextProperties != null) { newDataLabel.Append(defaultTextProperties.CloneNode(true)); } newDataLabel.Append(defaultShowLegendKey.CloneNode(true)); newDataLabel.Append(defaultShowValue.CloneNode(true)); newDataLabel.Append(defaultShowCategoryName.CloneNode(true)); newDataLabel.Append(defaultShowSeriesName.CloneNode(true)); newDataLabel.Append(defaultShowPercent.CloneNode(true)); newDataLabel.Append(defaultShowBubbleSize.CloneNode(true)); if (defaultShowLeaderLines != null) { newDataLabel.Append(defaultShowLeaderLines.CloneNode(true)); } if (defaultDLblsExtension != null) { newDataLabel.Append(defaultDLblsExtension.CloneNode(true)); } DataLabel lastDataLabel = labels.Elements <DataLabel>().LastOrDefault(l => l.Index != null && l.Index.Val != null && l.Index.Val.Value < rowNo); if (lastDataLabel != null) { labels.InsertAfter(newDataLabel, lastDataLabel); } else { labels.InsertAt(newDataLabel, 0); } } } } }