其他分享
首页 > 其他分享> > CodeGo.net>如何在二维图中标注Excel X和Y轴

CodeGo.net>如何在二维图中标注Excel X和Y轴

作者:互联网

我为似乎很简单的问题而烦恼,标记了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轴什么也没有!任何想法将不胜感激.

解决方法:

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

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

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

x轴是“类别”轴,y轴是“值”轴.对于x轴,请尝试以下操作(修订):

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

标签:visual-studio-2010,c,excel,visual-studio,vba,excel-vba
来源: https://codeday.me/bug/20191031/1973029.html