Beispiel #1
0
        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);
                            }
                        }
                    }
                }
            }
        }