public void add_chart1(ref List<Team> teams,ref List<Team> teams1 ) { objChart1 = objslide1.Shapes[1].OLEFormat.Object; objChart = objSlide.Shapes[1].OLEFormat.Object; Graph.SeriesCollection ss = objChart.SeriesCollection(); kj = ss.Item(1).Points(); // sc.Item(1).Values = 5; // questions[question_no].datasheet = objChart.Application.DataSheet; /* datasheet.Cells[1, 3] = "ali"; datasheet.Cells[1, 4] = "ali"; datasheet.Cells[1, 5] = "ali"; datasheet.Cells[2, 4] = kk.Count; */ //objChart.ApplyDataLabels(); datasheet= objChart.Application.DataSheet; //float total = 0; //for (int i = 2; i <= choices_no1 + 1; i++) //{ // total = (total + questions[question_no].values[i - 2]); //} objChart.ApplyDataLabels(); for (int i = 2; (16 - i) >= 0; i++) { datasheet.Cells[1, i] = teams[16 - i].KeyPad; // kj.Item(i - 1).Interior.Color = } for (int i = 2; (16-i) >=0 ; i++) { // Graph.DataLabel label = ss.Item(1).DataLabels(i - 1); datasheet.Cells[2, i] = teams[16-i].points; kj.Item(i - 1).Interior.Color = teams[16-i].bc; } Graph.Axes chart1 = objChart.Axes(); objchart2 = objSlide.Shapes[2].OLEFormat.Object; Graph.Axes chart2 = objchart2.Axes(); ss = objchart2.SeriesCollection(); kj = ss.Item(1).Points(); // sc.Item(1).Values = 5; // questions[question_no].datasheet = objChart.Application.DataSheet; /* datasheet.Cells[1, 3] = "ali"; datasheet.Cells[1, 4] = "ali"; datasheet.Cells[1, 5] = "ali"; datasheet.Cells[2, 4] = kk.Count; */ //objChart.ApplyDataLabels(); datasheet3 = objchart2.Application.DataSheet; //float total = 0; //for (int i = 2; i <= choices_no1 + 1; i++) //{ // total = (total + questions[question_no].values[i - 2]); //} objchart2.ApplyDataLabels(); for (int i = 2; (31-i) >14; i++) { datasheet3.Cells[1, i] = teams[31-i].KeyPad; // kj.Item(i - 1).Interior.Color = } for (int i = 2; (31 - i) >14; i++) { // Graph.DataLabel label = ss.Item(1).DataLabels(i - 1); datasheet3.Cells[2, i] = teams[31 - i].points; kj.Item(i - 1).Interior.Color = teams[31 - i].bc; } //second slide objChart1 =objslide1.Shapes[1].OLEFormat.Object; ss = objChart1.SeriesCollection(); // sc.Item(1).Values = 5; // questions[question_no].datasheet = objChart.Application.DataSheet; /* datasheet.Cells[1, 3] = "ali"; datasheet.Cells[1, 4] = "ali"; datasheet.Cells[1, 5] = "ali"; datasheet.Cells[2, 4] = kk.Count; */ //objChart.ApplyDataLabels(); datasheet1 = objChart1.Application.DataSheet; //float total = 0; //for (int i = 2; i <= choices_no1 + 1; i++) //{ // total = (total + questions[question_no].values[i - 2]); //} objChart1.ApplyDataLabels(); for (int i = 2; (i - 2) < teams1.Count; i++) { datasheet1.Cells[1, i] = teams1[i - 2].KeyPad; // kj.Item(i - 1).Interior.Color = } for (int i = 2; (i - 2) < teams1.Count; i++) { // Graph.DataLabel label = ss.Item(1).DataLabels(i - 1); datasheet1.Cells[2, i] = teams1[i - 2].time; kj = ss.Item(1).Points(); kj.Item(i - 1).Interior.Color = teams1[i - 2].bc; } }
public void add_chart(ref List<question> questions, ref int question_no, ref int choices_no1,ref ColorDialog colordialog1, ref int data_format, ref Color[] bar_colors, ref FontDialog fontdialog1) { if (questions[question_no].show_chart == true) { objChart = objSlide.Shapes[2].OLEFormat.Object; Graph.SeriesCollection ss = objChart.SeriesCollection(); Graph.Points kj; kj = ss.Item(1).Points(); Graph.Axes kk; kk = objChart.Axes(); questions[question_no].datasheet = objChart.Application.DataSheet; /* datasheet.Cells[1, 3] = "ali"; datasheet.Cells[1, 4] = "ali"; datasheet.Cells[1, 5] = "ali"; datasheet.Cells[2, 4] = kk.Count; */ objChart.ApplyDataLabels(); for (int i = 2; i <= choices_no1 + 1; i++) { questions[question_no].datasheet.Cells[1, i] = i - 1; // kj.Item(i - 1).Interior.Color = } float total = 0; for (int i = 2; i <= choices_no1 + 1; i++) { total = (total + questions[question_no].values[i - 2]); } if (data_format == 2) { for (int i = 2; i <= choices_no1 + 1; i++) { Graph.DataLabel label = ss.Item(1).DataLabels(i - 1); questions[question_no].datasheet.Cells[2, i] = questions[question_no].values[i - 2]; kj.Item(i - 1).Interior.Color = bar_colors[i - 2]; } objShape2 = objSlide.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 435, 480, 120, 70); objShape2.TextEffect.Text = "Total" + " " + total.ToString(); objShape2.TextEffect.FontName = "Comic Sans MS"; objShape2.TextEffect.FontSize = 15; objShape2.TextEffect.FontBold = MsoTriState.msoTrue; } else if (data_format == 1) { for (int i = 2; i <= choices_no1 + 1; i++) { Graph.DataLabel label = ss.Item(1).DataLabels(i - 1); questions[question_no].datasheet.Cells[2, i] = (((questions[question_no].values[i - 2]) / total) * 100) + "%"; kj.Item(i - 1).Interior.Color = bar_colors[i - 2]; } } else if (data_format == 3) { for (int i = 2; i <= choices_no1 + 1; i++) { questions[question_no].datasheet.Cells[2, i] = questions[question_no].values[i - 2]; Graph.DataLabel label = ss.Item(1).DataLabels(i - 1); label.Text = (((questions[question_no].values[i - 2]) / total) * 100).ToString("F") + "%" + "[" + questions[question_no].values[i - 2].ToString() + "]"; kj.Item(i - 1).Interior.Color = bar_colors[i - 2]; label.Font.Size = 15; } objShape2 = objSlide.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 435, 480, 120, 70); objShape2.TextEffect.Text = "Total" + " " + total.ToString(); objShape2.TextEffect.FontName = "Comic Sans MS"; objShape2.TextEffect.FontSize = 15; objShape2.TextEffect.FontBold = MsoTriState.msoTrue; } objChart.Refresh(); objChart.Application.Update(); } }
private void SetChart(string formatStr, string Name, object value, Bookmark tempBookmark) { try { if (value != null) { XlChartType xlChartType = XlChartType.xlColumnClustered; if (formatStr != null) { if (!(formatStr == "CHARTLINE")) { if (formatStr == "CHARTPIE") { xlChartType = XlChartType.xl3DPieExploded; } } else { xlChartType = XlChartType.xlLine; } } System.Data.DataTable dataTable = value as System.Data.DataTable; object obj = "MSGraph.Chart.8"; Microsoft.Office.Interop.Word.Range range = tempBookmark.Range; InlineShape inlineShape = range.InlineShapes.AddOLEObject(ref obj, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing); if (tempBookmark.Range.Cells.Count == 1) { inlineShape.Width = tempBookmark.Range.Cells[1].Width - 20f; inlineShape.Height = tempBookmark.Range.Cells[1].Height; } object @object = inlineShape.OLEFormat.Object; object obj2 = @object.GetType().InvokeMember("Application", System.Reflection.BindingFlags.GetProperty, null, @object, null); object[] args = new object[] { 4 }; @object.GetType().InvokeMember("ChartType", System.Reflection.BindingFlags.SetProperty, null, @object, args); Microsoft.Office.Interop.Graph.Chart chart = (Microsoft.Office.Interop.Graph.Chart)inlineShape.OLEFormat.Object; chart.ChartType = xlChartType; chart.HasTitle = true; chart.ChartTitle.Text = dataTable.TableName; chart.PlotArea.Interior.Color = Color.White; if (!string.IsNullOrEmpty(dataTable.TableName)) { chart.ChartTitle.Font.Size = 12; } DataSheet dataSheet = chart.Application.DataSheet; dataSheet.Columns.Clear(); dataSheet.Rows.Clear(); foreach (DataColumn dataColumn in dataTable.Columns) { dataSheet.Cells[1, dataTable.Columns.IndexOf(dataColumn) + 1] = dataColumn.ColumnName; } foreach (DataRow dataRow in dataTable.Rows) { foreach (DataColumn dataColumn in dataTable.Columns) { if (dataRow[dataColumn] == System.DBNull.Value) { dataRow[dataColumn] = ""; } dataSheet.Cells[dataTable.Rows.IndexOf(dataRow) + 2, dataTable.Columns.IndexOf(dataColumn) + 1] = dataRow[dataColumn]; } } if (xlChartType != XlChartType.xl3DPieExploded) { chart.Legend.Position = Microsoft.Office.Interop.Graph.XlLegendPosition.xlLegendPositionTop; for (int i = 1; i <= dataTable.Rows.Count; i++) { Microsoft.Office.Interop.Graph.Series series = (Microsoft.Office.Interop.Graph.Series)chart.SeriesCollection(i); series.HasDataLabels = true; Color chartColor = this.GetChartColor(i); if (chartColor != Color.Empty) { series.Interior.Color = chartColor; } else { series.Interior.ColorIndex = 15 + i; } } } else { chart.HasLegend = false; Microsoft.Office.Interop.Graph.Series series = (Microsoft.Office.Interop.Graph.Series)chart.SeriesCollection(1); series.HasDataLabels = true; series.HasLeaderLines = true; } chart.Application.Update(); obj2.GetType().InvokeMember("Update", System.Reflection.BindingFlags.InvokeMethod, null, obj2, null); obj2.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, obj2, null); } } catch (System.Exception ex) { throw new System.Exception("图表[" + tempBookmark.Name + "]数据加载出错:" + ex.Message); } }