3
我很難看到一個問題,標註X軸和Y軸!如何在2d圖中標記Excel X軸和Y軸
我使用C#在VS2010中創建Excel圖。這是一個2D XY散點圖。但我無法控制X軸的標籤!我只能控制Y軸的標籤!我花了太多時間試圖找出這個問題並放棄了。所以我正在尋找別人的幫助!這裏是我的代碼:
private void showExcelXY_v2()
{
xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;
// Now create the chart.
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject chartObj = chartObjs.Add(100, 20, 500, 300);
Microsoft.Office.Interop.Excel.Chart xlChart = chartObj.Chart;
//add some data from your datasource like a dataset or like below.
ws.Cells[1, 1] = "Value of n";
ws.Cells[1, 2] = "Term";
ws.Cells[2, 1] = "0";
ws.Cells[2, 2] = "0";
ws.Cells[3, 1] = "9181";
ws.Cells[3, 2] = "0";
ws.Cells[4, 1] = "9181";
ws.Cells[4, 2] = "-377094";
ws.Cells[5, 1] = "0";
ws.Cells[5, 2] = "-377094";
ws.Cells[6, 1] = "-9554";
ws.Cells[6, 2] = "-329688";
ws.Cells[7, 1] = "-9554";
ws.Cells[7, 2] = "0";
ws.Cells[8, 1] = "0";
ws.Cells[8, 2] = "0";
//set the source data and the chart type.
Range chartRange = ws.get_Range("A2", "B" + "8");
xlChart.SetSourceData(chartRange, Type.Missing);
xlChart.ChartType = XlChartType.xlXYScatterLines;
// Customize axes:
Microsoft.Office.Interop.Excel.Axis xAxis = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
xAxis.HasTitle = true;
xAxis.AxisTitle.Text = "Above Pressure (psi) Below";
Microsoft.Office.Interop.Excel.Axis yAxis = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
yAxis.MajorTickMark = XlTickMark.xlTickMarkCross;
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = "Compression (lbf)";
// Add title:
xlChart.HasTitle = true;
xlChart.ChartTitle.Text = "XPak Performance Envelope" + '\n' + "(Open Top & Bottom)";
// Remove legend:
xlChart.HasLegend = false;
}
這段代碼的結果,你通過它一步,顯示了X軸標籤一直到Y軸,那麼它是由Y軸標籤覆蓋。什麼都沒有去X軸!任何想法將不勝感激。
大衛,謝謝。你讓我度過這個噩夢!我不得不重新排列你的建議的順序,把它(XlAxisType.xlCategory,XlGroup.xlValue)接受,但這解決了我的問題! – 2013-03-26 14:08:22