/// <summary> /// Updates the chart. /// </summary> /// <param name="chart">The chart.</param> /// <param name="sheetName">Name of the sheet.</param> protected override void UpdateChart(OpenXmlCompositeElement chart, string sheetName) { if (chart != null) { chart.RemoveAllChildren <LineChartSeries>(); // Index 0 is for category axis data for (int index = 1; index < chartData.columnNameToSeries.Count(); index++) { string columnName = GetExcelColumnName(index); LineChartSeries lineChartSeries = chart.AppendChild <LineChartSeries>(new LineChartSeries()); UpdateSeriesText(sheetName, lineChartSeries, columnName, 1, chartData.columnNameToSeries.Skip(index).FirstOrDefault().Key); // Update Category Axis data CategoryAxisData catAxisData = new CategoryAxisData(); catAxisData.RemoveAllChildren <StringReference>(); catAxisData.RemoveAllChildren <NumberReference>(); StringReference catStringReference = GetStringReference(sheetName + "!$A$2:$A$" + (chartData.Count + 1).ToString(), chartData.Count); // Series 0 is for category axis data foreach (string cat in chartData.columnNameToSeries.First().Value) { AddStringPoint(catStringReference.StringCache, catStringReference.StringCache.Descendants <StringPoint>().Count(), cat); } catAxisData.Append(catStringReference); // Update Values NumberingCache numberingCache; PointCount pointCount; Values values = lineChartSeries.AppendChild <Values>(new Values()); NumberReference reference = CreateNumberReference(values, sheetName + "!$" + columnName + "$2:$" + columnName + "$" + (chartData.Count + 1).ToString()); SetNumberingCache(reference, out numberingCache, out pointCount); int rowIndex = 0; foreach (var point in chartData.columnNameToSeries.Skip(index).FirstOrDefault().Value) { AddNumericPoint(numberingCache, pointCount, rowIndex, point.ToString()); rowIndex += 1; } } } }
public ILineChart InitializeFromRange(IRange labelRange, IRange categoryRange) { uint orderStart = (uint)this.chartSpace.Charts.TakeWhile(c => c != this).Sum(c => c.SeriesCount); this.lineChart.RemoveAllChildren <LineChartSeries>(); IWorksheet worksheet = labelRange.Worksheet; if (labelRange.Width == 1 && labelRange.Height > 0 && categoryRange.Height == 1 && categoryRange.Width > 0) { for (uint labelIndex = 0; labelIndex < labelRange.Height; ++labelIndex) { LineChartSeries series = this.lineChart.AppendChild( new LineChartSeries() { Index = new Index() { Val = orderStart + labelIndex }, Order = new Order() { Val = orderStart + labelIndex } } ); series.AppendChild( new Marker() { Symbol = new Symbol() { Val = MarkerStyleValues.None } } ); series.AppendChild( new SeriesText().AppendChildFluent( new StringReference() { Formula = new Formula(labelRange[0, labelIndex].Reference), StringCache = new StringCache() .AppendChildFluent(new PointCount() { Val = 1 }) .AppendChildFluent(new StringPoint() { Index = 0, NumericValue = new NumericValue() { Text = labelRange[0, labelIndex].InnerValue } }) } ) ); series.AppendChild( new CategoryAxisData().AppendChildFluent( new StringReference() { Formula = new Formula(categoryRange.Formula), StringCache = new StringCache() .AppendChildFluent(new PointCount() { Val = (uint)categoryRange.Width.Value }) .AppendFluent(Enumerable.Range(0, categoryRange.Width.Value).Select(categoryIndex => new StringPoint() { Index = (uint)categoryIndex, NumericValue = new NumericValue() { Text = categoryRange[(uint)categoryIndex, 0].InnerValue } })) } ) ); string valuesFormula = ReferenceEncoder.EncodeRangeReference( worksheet.Name, categoryRange.StartColumn, false, labelRange.StartRow + labelIndex, false, categoryRange.EndColumn, false, labelRange.StartRow + labelIndex, false ); series.AppendChild( new Values().AppendChildFluent( new NumberReference() { Formula = new Formula(valuesFormula), NumberingCache = new NumberingCache() .AppendChildFluent(new PointCount() { Val = (uint)categoryRange.Width.Value }) .AppendFluent(Enumerable.Range(0, categoryRange.Width.Value).Select(categoryIndex => new NumericPoint() { Index = (uint)categoryIndex, NumericValue = new NumericValue() { Text = worksheet.Cells[categoryRange.StartColumn.Value + (uint)categoryIndex, labelRange.StartRow.Value + labelIndex].InnerValue } })) } ) ); series.AppendChild(new Smooth() { Val = false }); } } else if (labelRange.Height == 1 && labelRange.Width > 0 && categoryRange.Width == 1 && categoryRange.Height > 0) { for (uint labelIndex = 0; labelIndex < labelRange.Width; ++labelIndex) { LineChartSeries series = this.lineChart.AppendChild( new LineChartSeries() { Index = new Index() { Val = orderStart + labelIndex }, Order = new Order() { Val = orderStart + labelIndex } } ); series.AppendChild( new Marker() { Symbol = new Symbol() { Val = MarkerStyleValues.None } } ); series.AppendChild( new SeriesText().AppendChildFluent( new StringReference() { Formula = new Formula(labelRange[labelIndex, 0].Reference), StringCache = new StringCache() .AppendChildFluent(new PointCount() { Val = 1 }) .AppendChildFluent(new StringPoint() { Index = 0, NumericValue = new NumericValue() { Text = labelRange[labelIndex, 0].InnerValue } }) } ) ); series.AppendChild( new CategoryAxisData().AppendChildFluent( new StringReference() { Formula = new Formula(categoryRange.Formula), StringCache = new StringCache() .AppendChildFluent(new PointCount() { Val = (uint)categoryRange.Height.Value }) .AppendFluent(Enumerable.Range(0, categoryRange.Height.Value).Select(categoryIndex => new StringPoint() { Index = (uint)categoryIndex, NumericValue = new NumericValue() { Text = categoryRange[0, (uint)categoryIndex].InnerValue } })) } ) ); string valuesFormula = ReferenceEncoder.EncodeRangeReference( worksheet.Name, labelRange.StartColumn + labelIndex, false, categoryRange.StartRow, false, labelRange.StartColumn + labelIndex, false, categoryRange.EndRow, false ); series.AppendChild( new Values().AppendChildFluent( new NumberReference() { Formula = new Formula(valuesFormula), NumberingCache = new NumberingCache() .AppendChildFluent(new PointCount() { Val = (uint)categoryRange.Height.Value }) .AppendFluent(Enumerable.Range(0, categoryRange.Height.Value).Select(categoryIndex => new NumericPoint() { Index = (uint)categoryIndex, NumericValue = new NumericValue() { Text = worksheet.Cells[labelRange.StartColumn.Value + labelIndex, categoryRange.StartRow.Value + (uint)categoryIndex].InnerValue } })) } ) ); series.AppendChild(new Smooth() { Val = false }); } } else { throw new ArgumentException(); } return(this); }
protected IEnumerable <DataLabel> AddDataLabels(LineChartSeries series, int start, int end) { var returnList = new List <DataLabel>(); if (start > -1) { if (end > -1 && start > end) { var tmp = start; start = end; end = start; } else if (start == end) { start = end - 1; } var numberFormat = new NumberingFormat { FormatCode = "#,##0", SourceLinked = false }; var dataLabels = series.AppendChild( new DataLabels(numberFormat, new DataLabelPosition { Val = DataLabelPositionValues.Top }, new ShowLegendKey { Val = false }, new ShowValue { Val = false }, new ShowCategoryName { Val = false }, new ShowSeriesName { Val = false }, new ShowPercent { Val = false }, new ShowBubbleSize { Val = false }, new ShowLeaderLines { Val = false })); while (start < end) { var label = new DataLabel(new Index { Val = (uint)start }, new Layout(), new NumberingFormat { FormatCode = "#,##0", SourceLinked = false }, new Separator(), new TextProperties( new BodyProperties(), new ListStyle(), new Paragraph(new ParagraphProperties(new DefaultRunProperties { Bold = true }), new EndParagraphRunProperties { Language = "en-US" })), new DataLabelPosition { Val = DataLabelPositionValues.Top }, new ShowLegendKey { Val = false }, new ShowValue { Val = true }, new ShowCategoryName { Val = false }, new ShowSeriesName { Val = false }, new ShowPercent { Val = false }, new ShowBubbleSize { Val = false }, new ShowLeaderLines { Val = false }); dataLabels.InsertBefore(label, numberFormat); returnList.Add(label); start++; } } return(returnList); }
private void CreateCumulative(PlotArea plotArea, Statistics stat, uint index, uint categoryAxisId, uint valueAxisId) { LineChart lineChart = plotArea.AppendChild <LineChart>(new LineChart( new ShowMarker() { Val = true }, new Smooth() { Val = false }, new Grouping() { Val = GroupingValues.Standard }, new DataLabels(new ShowLegendKey() { Val = false }, new ShowValue() { Val = false }, new ShowCategoryName() { Val = false }, new ShowSeriesName() { Val = false }, new ShowPercent() { Val = false }, new ShowBubbleSize() { Val = false }))); LineChartSeries lineChartSeries = lineChart.AppendChild( new LineChartSeries(new Index() { Val = new UInt32Value(index), }, new Order() { Val = new UInt32Value(index) }, new SeriesText(new NumericValue() { Text = "Cumulative %" }))); StringLiteral strLit = lineChartSeries.AppendChild(new CategoryAxisData()).AppendChild(new StringLiteral()); strLit.Append(new PointCount() { Val = new UInt32Value((uint)stat.Frequencies.Count) }); NumberLiteral numLit = lineChartSeries.AppendChild(new DocumentFormat.OpenXml.Drawing.Charts.Values()) .AppendChild(new NumberLiteral()); numLit.Append(new FormatCode("0.00%")); numLit.Append(new PointCount() { Val = new UInt32Value((uint)stat.Frequencies.Count) }); for (uint i = 0; i < stat.Frequencies.Count; i++) { strLit.AppendChild <StringPoint>(new StringPoint() { Index = new UInt32Value(i) }) .Append(new NumericValue(stat.Frequencies[(int)i].Value.ToString())); numLit.AppendChild <NumericPoint>(new NumericPoint() { Index = new UInt32Value(i) }) .Append(new NumericValue((stat.Frequencies[(int)i].TotalCountPercent / 100).ToString())); } lineChart.Append(new AxisId() { Val = new UInt32Value(categoryAxisId) }); lineChart.Append(new AxisId() { Val = new UInt32Value(valueAxisId) }); AppendCategoryAxis(plotArea, categoryAxisId, "Time, ms", valueAxisId, false); AppendValueAxis(plotArea, valueAxisId, "", categoryAxisId, AxisPositionValues.Right, TickLabelPositionValues.High, false); }