Ejemplo n.º 1
0
        private void FillCategoryAxisData(CategoryAxisData categoryAxisData, ChartSeriesElement newSeriesItem, Column dataColumn)
        {
            int         dataCount  = dataColumn.Data.Count;
            UInt32Value pointCount = new UInt32Value((uint)dataCount);

            if (categoryAxisData != null && categoryAxisData.StringReference != null)
            {
                categoryAxisData.StringReference.StringCache.RemoveAllChildren <StringPoint>();
                categoryAxisData.StringReference.StringCache.PointCount.Val = pointCount;
                categoryAxisData.StringReference.Formula.Text = newSeriesItem.CategoryAxisDataAddress;
                for (int rowNo = 0; rowNo < element.Data.Rows.Count; rowNo++)
                {
                    StringPoint categoryAxisDataStringPoint = new StringPoint()
                    {
                        Index = (UInt32Value)((uint)rowNo)
                    };
                    NumericValue categoryAxisDataNumericValue = new NumericValue()
                    {
                        Text = element.Data.Rows[rowNo].GetHeader()
                    };
                    categoryAxisDataStringPoint.Append(categoryAxisDataNumericValue);
                    categoryAxisData.StringReference.StringCache.Append(categoryAxisDataStringPoint);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the string point.
        /// </summary>
        /// <param name="stringCache">The string cache.</param>
        /// <param name="index">The index.</param>
        /// <param name="stringPointNumericVal">The string point numeric val.</param>
        protected static void AddStringPoint(StringCache stringCache, int index, string stringPointNumericVal)
        {
            StringPoint stringPoint = new StringPoint();

            stringPoint.Index        = new UInt32Value((uint)index);
            stringPoint.NumericValue = new NumericValue(stringPointNumericVal);
            stringCache.Append(stringPoint);
        }
Ejemplo n.º 3
0
        /// <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 });
                 */
            }
        }
Ejemplo n.º 4
0
        private void SetSeriesText(OpenXmlCompositeElement seriesItem, ChartSeriesElement newSeriesItem, string seriesHeader)
        {
            SeriesText seriesText = seriesItem.Elements <SeriesText>().First();

            seriesText.StringReference.Formula.Text = newSeriesItem.SeriesTextAddress;
            seriesText.StringReference.StringCache.RemoveAllChildren <StringPoint>();
            StringPoint stringReferencePoint = new StringPoint()
            {
                Index = (UInt32Value)((uint)0)
            };
            NumericValue stringReferenceNumericValue = new NumericValue()
            {
                Text = seriesHeader
            };

            stringReferencePoint.Append(stringReferenceNumericValue);
            seriesText.StringReference.StringCache.Append(stringReferencePoint);
        }
Ejemplo n.º 5
0
        protected static void SetStringCache(StringReference stringReference, out StringCache stringCache, out StringPoint stringPoint)
        {
            stringCache = stringReference.Descendants <StringCache>().FirstOrDefault();

            if (stringCache == null)
            {
                stringCache = new StringCache();
                stringReference.AppendChild <StringCache>(stringCache);
            }

            stringPoint = stringCache.Descendants <StringPoint>().FirstOrDefault();

            if (stringPoint == null)
            {
                stringPoint = new StringPoint();
                stringCache.AppendChild <StringPoint>(stringPoint);
            }

            stringPoint.Index = new UInt32Value((uint)0);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Modify/Add categories 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_Categories(string column_index, uint row_index, string new_value)
        {
            foreach (LineChartSeries linechart_series in chart_part.ChartSpace.Descendants <LineChartSeries>().ToList())
            {
                CategoryAxisData category_axis_data = linechart_series.Descendants <CategoryAxisData>().FirstOrDefault();
                if (category_axis_data == null)
                {
                    // If no StringReference --> Clone one from the 1st (usually we go in this when we create a new BarChartSeries)
                    BarChartSeries   template_barchartseries   = chart_part.ChartSpace.Descendants <BarChartSeries>().FirstOrDefault();
                    CategoryAxisData template_categoryaxisdata = template_barchartseries.Descendants <CategoryAxisData>().FirstOrDefault();
                    CategoryAxisData new_categoryaxisdata      = new CategoryAxisData(template_categoryaxisdata.OuterXml);
                    linechart_series.Append(new_categoryaxisdata);
                }
                else
                {
                    StringReference sr = category_axis_data.Descendants <StringReference>().FirstOrDefault();
                    // If there is a StringReference --> Update its values
                    StringCache sc = sr.Descendants <StringCache>().First();
                    try
                    {
                        StringPoint  sp = sc.Descendants <StringPoint>().ElementAt((int)row_index - 2);
                        NumericValue nv = sp.Descendants <NumericValue>().First();
                        nv.Text = new_value;
                    }
                    catch (Exception)
                    {
                        // Create new data and append to previous XML
                        sc.PointCount.Val = sc.PointCount.Val + 1;
                        NumericValue nv = new NumericValue(new_value);
                        StringPoint  sp = new StringPoint(nv);
                        sp.Index = (uint)sc.Descendants <StringPoint>().ToList().Count;
                        sc.Append(sp);

                        // Change fomula range
                        DocumentFormat.OpenXml.Drawing.Charts.Formula f = sr.Descendants <DocumentFormat.OpenXml.Drawing.Charts.Formula>().FirstOrDefault();
                        f.Text = worksheet_name + "!$A$2:$A$" + GetRowIndexByNum((int)row_index - 2).ToString();
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public PieChartSeries GeneratePieChartSeries(string[] labels, double[] data)
        {
            PieChartSeries pieChartSeries1 = new PieChartSeries();
            Index          index1          = new Index()
            {
                Val = (UInt32Value)0U
            };
            Order order1 = new Order()
            {
                Val = (UInt32Value)0U
            };

            SeriesText   seriesText1   = new SeriesText();
            NumericValue numericValue1 = new NumericValue();

            numericValue1.Text = "sreie 1";

            seriesText1.Append(numericValue1);

            CategoryAxisData categoryAxisData1 = new CategoryAxisData();

            StringLiteral stringLiteral1 = new StringLiteral();
            PointCount    pointCount1    = new PointCount()
            {
                Val = (uint)labels.Length
            };

            //StringPoint stringPoint1 = new StringPoint() { Index = (UInt32Value)0U };
            //NumericValue numericValue2 = new NumericValue();
            //numericValue2.Text = "a";

            //stringPoint1.Append(numericValue2);

            //StringPoint stringPoint2 = new StringPoint() { Index = (UInt32Value)1U };
            //NumericValue numericValue3 = new NumericValue();
            //numericValue3.Text = "n";

            //stringPoint2.Append(numericValue3);

            //StringPoint stringPoint3 = new StringPoint() { Index = (UInt32Value)2U };
            //NumericValue numericValue4 = new NumericValue();
            //numericValue4.Text = "c";

            //stringPoint3.Append(numericValue4);

            //StringPoint stringPoint4 = new StringPoint() { Index = (UInt32Value)3U };
            //NumericValue numericValue5 = new NumericValue();
            //numericValue5.Text = "d";

            //stringPoint4.Append(numericValue5);

            //Ajout des etiquette de legendes
            for (int i = 0; i < labels.Length; i++)
            {
                StringPoint stringPoint = new StringPoint()
                {
                    Index = (uint)i
                };
                NumericValue numericValue = new NumericValue();
                numericValue.Text = labels[i];

                stringPoint.Append(numericValue);
                stringLiteral1.Append(stringPoint);
            }

            stringLiteral1.Append(pointCount1);
            //stringLiteral1.Append(stringPoint1);
            //stringLiteral1.Append(stringPoint2);
            //stringLiteral1.Append(stringPoint3);
            //stringLiteral1.Append(stringPoint4);

            categoryAxisData1.Append(stringLiteral1);

            DocumentFormat.OpenXml.Drawing.Charts.Values values1 = new DocumentFormat.OpenXml.Drawing.Charts.Values();

            NumberLiteral numberLiteral1 = new NumberLiteral();
            FormatCode    formatCode1    = new FormatCode();

            formatCode1.Text = "General";
            PointCount pointCount2 = new PointCount()
            {
                Val = (uint)data.Length
            };

            //NumericPoint numericPoint1 = new NumericPoint() { Index = (UInt32Value)0U };
            //NumericValue numericValue6 = new NumericValue();
            //numericValue6.Text = "1";

            //numericPoint1.Append(numericValue6);

            //NumericPoint numericPoint2 = new NumericPoint() { Index = (UInt32Value)1U };
            //NumericValue numericValue7 = new NumericValue();
            //numericValue7.Text = "2";

            //numericPoint2.Append(numericValue7);

            //NumericPoint numericPoint3 = new NumericPoint() { Index = (UInt32Value)2U };
            //NumericValue numericValue8 = new NumericValue();
            //numericValue8.Text = "3";

            //numericPoint3.Append(numericValue8);

            //NumericPoint numericPoint4 = new NumericPoint() { Index = (UInt32Value)3U };
            //NumericValue numericValue9 = new NumericValue();
            //numericValue9.Text = "5";

            //numericPoint4.Append(numericValue9);


            for (int i = 0; i < data.Length; i++)
            {
                NumericPoint numericPoint = new NumericPoint()
                {
                    Index = (uint)i
                };
                NumericValue numericValue = new NumericValue();
                numericValue.Text = data[i].ToString();

                numericPoint.Append(numericValue);
                numberLiteral1.Append(numericPoint);
            }



            numberLiteral1.Append(formatCode1);
            numberLiteral1.Append(pointCount2);
            //numberLiteral1.Append(numericPoint1);
            //numberLiteral1.Append(numericPoint2);
            //numberLiteral1.Append(numericPoint3);
            //numberLiteral1.Append(numericPoint4);

            values1.Append(numberLiteral1);

            pieChartSeries1.Append(index1);
            pieChartSeries1.Append(order1);
            pieChartSeries1.Append(seriesText1);
            pieChartSeries1.Append(categoryAxisData1);
            pieChartSeries1.Append(values1);
            return(pieChartSeries1);
        }
Ejemplo n.º 8
0
        protected void modificaChartData(string FormatoValori, string titoloSerie, out SeriesText seriesText1, out CategoryAxisData categoryAxisData1, out Values values1)
        {
            seriesText1 = new SeriesText();

            StringReference stringReference1 = new StringReference();
            Formula         formula1         = new Formula();

            formula1.Text = "Foglio1!$B$1";

            StringCache stringCache1 = new StringCache();
            PointCount  pointCount1  = new PointCount()
            {
                Val = (UInt32Value)1U
            };

            StringPoint stringPoint1 = new StringPoint()
            {
                Index = (UInt32Value)0U
            };
            NumericValue numericValue1 = new NumericValue();

            numericValue1.Text = titoloSerie;

            stringPoint1.Append(numericValue1);

            stringCache1.Append(pointCount1);
            stringCache1.Append(stringPoint1);

            stringReference1.Append(formula1);
            stringReference1.Append(stringCache1);

            seriesText1.Append(stringReference1);

            DataPoint dataPoint1 = new DataPoint();
            Index     index2     = new Index()
            {
                Val = (UInt32Value)2U
            };


            dataPoint1.Append(index2);

            //################################i testi ####################################
            categoryAxisData1 = new CategoryAxisData();

            StringReference stringReference2 = new StringReference();
            Formula         formula2         = new Formula();

            formula2.Text = string.Format("Foglio1!$A$2:$A${0}", valori.Count + 2);

            StringCache stringCache2 = new StringCache();
            UInt32Value nValori      = Convert.ToUInt32(valori.Count);

            PointCount pointCount2 = new PointCount()
            {
                Val = nValori
            };

            StringPoint[] stringPoints = new StringPoint[nValori];
            UInt32Value   n            = 0;

            foreach (KeyValuePair <string, double> item in valori)
            {
                stringPoints[n] = new StringPoint()
                {
                    Index = (UInt32Value)n
                };
                NumericValue numericValue2 = new NumericValue();
                numericValue2.Text = item.Key;
                stringPoints[n].Append(numericValue2);
                n += 1;
            }



            stringCache2.Append(pointCount2);
            for (int i = 0; i < n; i++)
            {
                stringCache2.Append(stringPoints[i]);
            }

            stringReference2.Append(formula2);
            stringReference2.Append(stringCache2);

            categoryAxisData1.Append(stringReference2);


            //################################i valori####################################

            values1 = new Values();

            NumberReference numberReference1 = new NumberReference();
            Formula         formula3         = new Formula();

            formula3.Text = string.Format("Foglio1!$B$2:$B${0}", valori.Count + 2);

            NumberingCache numberingCache1 = new NumberingCache();
            FormatCode     formatCode1     = new FormatCode();

            formatCode1.Text = FormatoValori; //<-----------------------------------------------------------
            PointCount pointCount3 = new PointCount()
            {
                Val = nValori
            };

            NumericPoint[] numericPoints = new NumericPoint[nValori];
            n = 0;
            foreach (KeyValuePair <string, double> item in valori)
            {
                numericPoints[n] = new NumericPoint()
                {
                    Index = (UInt32Value)n
                };
                NumericValue numericValue = new NumericValue();
                numericValue.Text = item.Value.ToString();
                numericValue.Text = numericValue.Text.Replace(",", "."); // devo forzare il formato americano
                numericPoints[n].Append(numericValue);
                n += 1;
            }



            numberingCache1.Append(formatCode1);
            numberingCache1.Append(pointCount3);
            for (int i = 0; i < n; i++)
            {
                numberingCache1.Append(numericPoints[i]);
            }



            numberReference1.Append(formula3);
            numberReference1.Append(numberingCache1);

            values1.Append(numberReference1);
        }