Beispiel #1
0
        /// <summary>
        /// Gets the id of the chart reference for the <see cref="ChartPart"/>
        /// </summary>
        /// <param name="chartPart">The <see cref="ChartPart"/></param>
        /// <returns>The chart reference id</returns>
        private static string GetChartReferenceId(ChartPart chartPart)
        {
            // DrawingPart is parent of ChartPart, and WorksheetPart parent of DrawingParts
            var drawingsPart = chartPart.GetParentParts().FirstOrDefault();

            return(drawingsPart.GetIdOfPart(chartPart));
        }
        /// <summary>
        /// Получить информацию о местоположении области с графиками
        /// </summary>
        /// <param name="chartPart">Область информацию которой нужно получить</param>
        /// <returns>Сведения о положении области графиков на листе</returns>
        public static TwoCellAnchor Anchor(this ChartPart chartPart)
        {
            if (chartPart == null)
            {
                return(null);
            }

            var drawingsPart = chartPart.GetParentParts().FirstOrDefault() as DrawingsPart;

            if (drawingsPart == null)
            {
                return(null);
            }

            var partId  = drawingsPart.GetIdOfPart(chartPart);
            var anchors = drawingsPart.WorksheetDrawing.Elements <TwoCellAnchor>();

            var theAnchor = anchors.FirstOrDefault(anc =>
            {
                var chartRef = anc.FirstDescendant <DrawingCharts.ChartReference>();
                if (chartRef == null)
                {
                    return(false);
                }
                return(chartRef.Id.Value == partId);
            });

            return(theAnchor);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the <see cref="DrawingSpreadsheet.TwoCellAnchor"/> that is used to host the
        /// <see cref="DrawingSpreadsheet.GraphicFrame"/> in which the <see cref="ChartPart"/> resides.
        /// </summary>
        /// <param name="chartPart">The <see cref="ChartPart"/></param>
        /// <returns>The hosting <see cref="DrawingSpreadsheet.TwoCellAnchor"/></returns>
        private static DrawingSpreadsheet.TwoCellAnchor GetHostingTwoCellAnchor(ChartPart chartPart)
        {
            // Get the chart reference id for the chartPart
            string chartRefId = GetChartReferenceId(chartPart);

            // DrawingPart is parent of ChartPart, and WorksheetPart parent of DrawingParts
            var drawingsPart = (DrawingsPart)chartPart.GetParentParts().FirstOrDefault();

            // Get chart reference which matches reference id of supplied chart part.
            DrawingCharts.ChartReference chartRef = drawingsPart.WorksheetDrawing.Descendants <DrawingCharts.ChartReference>().FirstOrDefault(cr => cr.Id == chartRefId);

            // Work bak up to, and return, TwoCellAnchor
            DrawingSpreadsheet.TwoCellAnchor anchor = null;
            if (chartRef != null)
            {
                anchor = chartRef.Ancestors <DrawingSpreadsheet.TwoCellAnchor>().FirstOrDefault();
            }

            return(anchor);
        }