/// <summary> /// Modify/Add series into chart XML /// </summary> /// <param name="column_index">Corresponds to the column index that needs to be modified in chart spreadsheet (Ex: A, B, C, ...)</param> /// <param name="row_index">Corresponds to the column index that needs to be modified in excel </param> /// <param name="new_value">Corresponds to the new value we need to insert to the cell </param> protected override void ModifyChartXML_Series(string column_index, uint row_index, string new_value) { LineChartSeries linechart_series = chart_part.ChartSpace.Descendants <LineChartSeries>().Where(s => string.Compare(s.InnerText, worksheet_name + "!$" + column_index + "$1", true) > 0).FirstOrDefault(); if (linechart_series != null) // There exists data on the series --> We only need to modify it { SeriesText st = linechart_series.Descendants <SeriesText>().FirstOrDefault(); StringReference sr = st.Descendants <StringReference>().First(); StringCache sc = sr.Descendants <StringCache>().First(); StringPoint sp = sc.Descendants <StringPoint>().First(); NumericValue nv = sp.Descendants <NumericValue>().First(); nv.Text = new_value; } else // No such series exists --> Consider create a new series { /* * // Find location in XML to append the BarChartSeries * Chart chart = chart_part.ChartSpace.Descendants<Chart>().FirstOrDefault(); * PlotArea plot = chart.PlotArea; * * // Create new BarChartSeries * barchart_series = new BarChartSeries(); * uint index = (uint)plot.Descendants<BarChartSeries>().ToList().Count; * * barchart_series.Append(new Index() { Val = index }); * barchart_series.Append(new Order() { Val = index }); * * SeriesText seriesText = new SeriesText(); * seriesText.Append(new NumericValue() { Text = new_value }); * * barchart_series.Append(seriesText); * * * // Append data * Bar3DChart bar_3dchart = plot.Descendants<Bar3DChart>().FirstOrDefault(); * if (bar_3dchart != null) // Chart is 3D * bar_3dchart.Append(barchart_series); * else // Chart is not 3d * { * BarChart barchart = plot.Descendants<BarChart>().FirstOrDefault(); * barchart.Append(barchart_series); * } * * // Append other settings * BarChartSeries barchart_series_template = chart_part.ChartSpace.Descendants<BarChartSeries>().LastOrDefault(); * * CategoryAxisData cateAxisData = new CategoryAxisData(); * StringReference string_ref = new StringReference(); * string_ref.Append(new DocumentFormat.OpenXml.Drawing.Charts.Formula() { Text = barchart_series.Descendants<DocumentFormat.OpenXml.Drawing.Charts.Formula>().FirstOrDefault().Text}); * StringCache string_cache = new StringCache(); * string_cache.Append(new PointCount() { Val = count }); */ } }
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(); }