/// <summary>
        /// Задать заголовок графику
        /// </summary>
        /// <param name="chart">график</param>
        /// <param name="newTitleStr">новый заголовок графика</param>
        /// <returns>true - при удачной замене заголовка, false - в обратном случае</returns>
        public static bool Title(this DrawingCharts.Chart chart, string newTitleStr)
        {
            if (chart == null)
            {
                return(false);
            }

            var newTitle = new DrawingCharts.Title().From("Drawing\\ChartTitle");

            if (newTitle == null)
            {
                return(false);
            }

            var run = newTitle.FirstDescendant <Drawing.Run>();

            if (run == null)
            {
                return(false);
            }

            run.Text = newTitleStr.ToDrawingText();

            var oldTitle = chart.GetFirstChild <DrawingCharts.Title>();

            var pPr = oldTitle == null ? null : oldTitle.FirstDescendant <Drawing.ParagraphProperties>();
            var rPr = oldTitle == null ? null : oldTitle.FirstDescendant <Drawing.RunProperties>();

            var defPPr = newTitle.FirstDescendant <Drawing.ParagraphProperties>();
            var defRPr = newTitle.FirstDescendant <Drawing.RunProperties>();

            if (pPr != null && defPPr != null)
            {
                defPPr.Parent.ReplaceChild(pPr.CloneNode(true), defPPr);
            }

            if (rPr != null && defRPr != null)
            {
                defRPr.Parent.ReplaceChild(rPr.CloneNode(true), defRPr);
            }

            if (oldTitle == null)
            {
                chart.PrependChild(newTitle);
                return(true);
            }

            var resultTitle = chart.ReplaceChild(newTitle, oldTitle);

            if (resultTitle.Equals(oldTitle))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        void setChartData(ChartPart chartPart1, ChartDataHolder[] ChartDataSlide)
        {
            ChartSpace chartSpace = chartPart1.ChartSpace;

            DocumentFormat.OpenXml.Drawing.Charts.Chart chart1 = chartSpace.GetFirstChild <DocumentFormat.OpenXml.Drawing.Charts.Chart>();
            PlotArea plotArea1 = chart1.GetFirstChild <PlotArea>();
            BarChart barChart1 = plotArea1.GetFirstChild <BarChart>();

            //BarChartSeries barChartSeries1 = barChart1.Elements<BarChartSeries>().ElementAtOrDefault(2);
            for (int i = 0; i < barChart1.Elements <BarChartSeries>().Count(); i++)
            {
                BarChartSeries barChartSeries = barChart1.Elements <BarChartSeries>().ElementAtOrDefault(i);
                ChartDataSlide dataModel      = ChartDataSlide.ElementAtOrDefault(i).chartData;

                SeriesText seriesText = barChartSeries.Elements <SeriesText>().ElementAtOrDefault(0);
                if (seriesText != null)
                {
                    var stringReference = seriesText.Descendants <StringReference>().FirstOrDefault();
                    var stringCache     = stringReference.Descendants <StringCache>().FirstOrDefault();
                    var stringPoint     = stringCache.Descendants <StringPoint>().FirstOrDefault();
                    var barLabel        = stringPoint.GetFirstChild <NumericValue>();
                    barLabel.Text = ChartDataSlide.ElementAtOrDefault(i).seriesText;
                }

                if (barChartSeries != null)
                {
                    Values          values1          = barChartSeries.GetFirstChild <Values>();
                    NumberReference numberReference1 = values1.GetFirstChild <NumberReference>();
                    NumberingCache  numberingCache1  = numberReference1.GetFirstChild <NumberingCache>();

                    NumericPoint numericPoint1 = numberingCache1.GetFirstChild <NumericPoint>();
                    NumericPoint numericPoint2 = numberingCache1.Elements <NumericPoint>().ElementAt(1);
                    NumericPoint numericPoint3 = numberingCache1.Elements <NumericPoint>().ElementAt(2);

                    NumericValue numericValue1 = numericPoint1.GetFirstChild <NumericValue>();
                    //numericValue1.Text = ".50";
                    if (numericValue1 != null)
                    {
                        numericValue1.Text = dataModel.Interaction.ToString();
                    }


                    NumericValue numericValue2 = numericPoint2.GetFirstChild <NumericValue>();
                    //numericValue2.Text = ".10";
                    if (numericValue2 != null)
                    {
                        numericValue2.Text = dataModel.Knowlegde.ToString();
                    }


                    NumericValue numericValue3 = numericPoint3.GetFirstChild <NumericValue>();
                    //numericValue3.Text = ".40";
                    if (numericValue3 != null)
                    {
                        numericValue3.Text = dataModel.Image.ToString();
                    }
                }
            }

            chartSpace.Save();
        }
 /// <summary>
 /// Получить заголовок
 /// </summary>
 /// <param name="chart">График заголовок которогу нужно получить</param>
 /// <returns>Заголовок выбранного графика</returns>
 public static string Title(this DrawingCharts.Chart chart)
 {
     return(chart.GetFirstChild <DrawingCharts.Title>().InnerText);
 }