vba 如何在二维图中标记 Excel X 和 Y 轴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15638830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:16:02  来源:igfitidea点击:

How to label Excel X and Y axes in 2d graph

c#visual-studio-2010visual-studioexcelexcel-vbavba

提问by Bob T

I am stumped on a problem that seems easy, labeling X and Y axes!

我被一个看起来很简单的问题难住了,标记 X 和 Y 轴!

I am creating an Excel graph inside VS2010 with C#. It is a 2D XY scatter chart. But I can't control the label of the X axis! I can only control the label of the Y axis! I have spent too much time trying to figure this one out and have given up. So I'm looking for someone's help! Here's my code:

我正在用 C# 在 VS2010 中创建一个 Excel 图表。它是一个二维 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;
    }

The result of this code, as you step through it, shows the X axis label goes to the Y axis, then it is overwritten by the Y axis label. Nothing goes to the X axis! Any ideas would be greatly appreciated.

此代码的结果,当您逐步执行时,显示 X 轴标签转到 Y 轴,然后被 Y 轴标签覆盖。X 轴什么也没有!任何想法将不胜感激。

回答by David Zemens

You have both axes defined the same way:

您以相同的方式定义了两个轴:

xAxis = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary)

yAxis = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary)

The x-Axis is the "Category" axis and the y-Axis is the "Value" axis. Try this for the x-Axis (revised):

x 轴是“类别”轴,y 轴是“值”轴。试试这个 x 轴(修改):

xAxis = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary)