public Chart GenerateChart(string title, List<AssetWeighting> model) { Chart chart1 = new Chart(); Title title1 = GenerateTitle(title); View3D view3D1 = new View3D(); RotateX rotateX1 = new RotateX() { Val = 30 }; Perspective perspective1 = new Perspective() { Val = 30 }; view3D1.Append(rotateX1); view3D1.Append(perspective1); PlotArea plotArea1 = new PlotArea(); Layout layout1 = new Layout(); Pie3DChart pie3DChart1 = new Pie3DChart(); VaryColors varyColors1 = new VaryColors() { Val = true }; PieChartSeries pieChartSeries1 = new PieChartSeries(); Index index1 = new Index() { Val = (UInt32Value)0U }; Order order1 = new Order() { Val = (UInt32Value)0U }; // c:tx series text SeriesText seriesText1 = GenerateSeriesText(title, GraphData.DataColumn); // c:cat category axis data var categoryData = model.OrderByDescending(m => m.Weighting).Select(n => n.AssetClass); GraphData.AddTextColumn(categoryName, categoryData); CategoryAxisData categoryAxisData1 = GenerateCategoryAxisData(categoryData, GraphData.TextColumn); // c:val values var valuesData = model.OrderByDescending(m => m.Weighting).Select(n => n.Weighting ?? 0).ToArray(); string valuesColumn = GraphData.AddDataColumn(seriesName, valuesData); Values values1 = GenerateValues(valueFormat, valuesData, valuesColumn); //var series = from m in model // orderby m.Weighting descending // select new TextSeries { Name = m.AssetClass, Values = (new List<double>() { m.Weighting ?? 0 }) }; //string[] headers = { categoryName, seriesName }; //GraphData.AddTextSeries(headers, series); pieChartSeries1.Append(index1); pieChartSeries1.Append(order1); pieChartSeries1.Append(seriesText1); pieChartSeries1.Append(categoryAxisData1); pieChartSeries1.Append(values1); pie3DChart1.Append(varyColors1); pie3DChart1.Append(pieChartSeries1); plotArea1.Append(layout1); plotArea1.Append(pie3DChart1); Legend legend1 = GenerateLegend(LegendPositionValues.Right); PlotVisibleOnly plotVisibleOnly1 = new PlotVisibleOnly() { Val = true }; chart1.Append(title1); chart1.Append(view3D1); chart1.Append(plotArea1); chart1.Append(legend1); chart1.Append(plotVisibleOnly1); return chart1; }
// Create Chart in Word document public void CreateChart(List <ChartSubArea> chartList) { // Get MainDocumentPart of Document MainDocumentPart mainPart = document.AddMainDocumentPart(); mainPart.Document = new Document(new Body()); // Create ChartPart object in Word Document ChartPart chartPart = mainPart.AddNewPart <ChartPart>("rId110"); // the root element of chartPart dc.ChartSpace chartSpace = new dc.ChartSpace(); chartSpace.Append(new dc.EditingLanguage() { Val = "en-us" }); // Create Chart dc.Chart chart = new dc.Chart(); chart.Append(new dc.AutoTitleDeleted() { Val = true }); // Define the 3D view dc.View3D view3D = new dc.View3D(); view3D.Append(new dc.RotateX() { Val = 30 }); view3D.Append(new dc.RotateY() { Val = 0 }); // Intiliazes a new instance of the PlotArea class dc.PlotArea plotArea = new dc.PlotArea(); plotArea.Append(new dc.Layout()); // the type of Chart dc.Pie3DChart pie3DChart = new dc.Pie3DChart(); pie3DChart.Append(new dc.VaryColors() { Val = true }); dc.PieChartSeries pieChartSers = new dc.PieChartSeries(); pieChartSers.Append(new dc.Index() { Val = 0U }); pieChartSers.Append(new dc.Order() { Val = 0U }); dc.SeriesText seriesText = new dc.SeriesText(); seriesText.Append(new dc.NumericValue() { Text = "Series" }); uint rowcount = 0; uint count = UInt32.Parse(chartList.Count.ToString()); string endCell = (count + 1).ToString(); dc.ChartShapeProperties chartShapePros = new dc.ChartShapeProperties(); // Define cell for lable information dc.CategoryAxisData cateAxisData = new dc.CategoryAxisData(); dc.StringReference stringRef = new dc.StringReference(); stringRef.Append(new dc.Formula() { Text = "Main!$A$2:$A$" + endCell }); dc.StringCache stringCache = new dc.StringCache(); stringCache.Append(new dc.PointCount() { Val = count }); // Define cells for value information dc.Values values = new dc.Values(); dc.NumberReference numRef = new dc.NumberReference(); numRef.Append(new dc.Formula() { Text = "Main!$B$2:$B$" + endCell }); dc.NumberingCache numCache = new dc.NumberingCache(); numCache.Append(new dc.FormatCode() { Text = "General" }); numCache.Append(new dc.PointCount() { Val = count }); // Fill data for chart foreach (var item in chartList) { if (count == 0) { chartShapePros.Append(new d.SolidFill(new d.SchemeColor() { Val = item.Color })); pieChartSers.Append(chartShapePros); } else { dc.DataPoint dataPoint = new dc.DataPoint(); dataPoint.Append(new dc.Index() { Val = rowcount }); chartShapePros = new dc.ChartShapeProperties(); chartShapePros.Append(new d.SolidFill(new d.SchemeColor() { Val = item.Color })); dataPoint.Append(chartShapePros); pieChartSers.Append(dataPoint); } dc.StringPoint stringPoint = new dc.StringPoint() { Index = rowcount }; stringPoint.Append(new dc.NumericValue() { Text = item.Label }); stringCache.Append(stringPoint); dc.NumericPoint numericPoint = new dc.NumericPoint() { Index = rowcount }; numericPoint.Append(new dc.NumericValue() { Text = item.Value }); numCache.Append(numericPoint); rowcount++; } // Create c:cat and c:val element stringRef.Append(stringCache); cateAxisData.Append(stringRef); numRef.Append(numCache); values.Append(numRef); // Append c:cat and c:val to the end of c:ser element pieChartSers.Append(cateAxisData); pieChartSers.Append(values); // Append c:ser to the end of c:pie3DChart element pie3DChart.Append(pieChartSers); // Append c:pie3DChart to the end of s:plotArea element plotArea.Append(pie3DChart); // create child elements of the c:legend element dc.Legend legend = new dc.Legend(); legend.Append(new dc.LegendPosition() { Val = LegendPositionValues.Right }); dc.Overlay overlay = new dc.Overlay() { Val = false }; legend.Append(overlay); dc.TextProperties textPros = new TextProperties(); textPros.Append(new d.BodyProperties()); textPros.Append(new d.ListStyle()); d.Paragraph paragraph = new d.Paragraph(); d.ParagraphProperties paraPros = new d.ParagraphProperties(); d.DefaultParagraphProperties defaultParaPros = new d.DefaultParagraphProperties(); defaultParaPros.Append(new d.LatinFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 }); defaultParaPros.Append(new d.ComplexScriptFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 }); paraPros.Append(defaultParaPros); paragraph.Append(paraPros); paragraph.Append(new d.EndParagraphRunProperties() { Language = "en-Us" }); textPros.Append(paragraph); legend.Append(textPros); // Append c:view3D, c:plotArea and c:legend elements to the end of c:chart element chart.Append(view3D); chart.Append(plotArea); chart.Append(legend); // Append the c:chart element to the end of c:chartSpace element chartSpace.Append(chart); // Create c:spPr Elements and fill the child elements of it chartShapePros = new dc.ChartShapeProperties(); d.Outline outline = new d.Outline(); outline.Append(new d.NoFill()); chartShapePros.Append(outline); // Append c:spPr element to the end of c:chartSpace element chartSpace.Append(chartShapePros); chartPart.ChartSpace = chartSpace; // Generate content of the MainDocumentPart GeneratePartContent(mainPart); }