簡體   English   中英

在 C# 中設置 Excel 散點 plot 上的 x 和 y 軸

[英]Set x and y axis on an Excel scatter plot in C#

我在 Excel 表中有這樣的數據:

15 0.5   25 0.36
20 0.32  37 0.54
35 0.33  19 0.24

數據由我的代碼生成,並且可以通過添加更多系列的方式進行擴展。 我想用 x 和 y 坐標繪制數據的散點圖。

using Excel = Microsoft.Office.Interop.Excel;

    public void OpenExcel(string folder, string filename)
    {
        this.folder = folder;
        this.filename = filename;
        path = folder + @"\" + filename;

        if (File.Exists(path) != true) //if the output file does not exists, create it
        {
            var app = new Microsoft.Office.Interop.Excel.Application();
            var temp = app.Workbooks.Add();
            temp.SaveAs(path);
            temp.Close();
        }

        wb = excel.Workbooks.Open(path);
    }

    public void MakeScatterPlot(int numberofseries, int rowlenght) //we already have the data in the sheet at this point
    {
        Excel.Shape chart_shape = workSheet.Shapes.AddChart(Excel.XlChartType.xlXYScatter, 5, 5, 900, 450);
        Excel.Chart chart = chart_shape.Chart;
        Excel.Range chart_range = (Excel.Range)workSheet.Range[workSheet.Cells[1, 1], workSheet.Cells[rowlenght, (numberofseries * 2)]].Cells;
        chart.SetSourceData(chart_range, Excel.XlRowCol.xlColumns);

        for (int i = 1; i <= numberofseries; i++)
        {
           chart.SeriesCollection(i).XValues = (Excel.Range)workSheet.Range[workSheet.Cells[1, 1 + 2 * (i-1)], workSheet.Cells[rowlenght, 1]].Cells; //1, 3, 5...
           chart.SeriesCollection(i).Values = (Excel.Range)workSheet.Range[workSheet.Cells[1, 2 + 2 * (i-1)], workSheet.Cells[rowlenght, 1]].Cells; //2, 4, 6...
        }

    }

折線chart.SeriesCollection(i).XValues似乎工作正常,但我收到錯誤消息,當我嘗試設置 y 軸時程序停止我收到錯誤消息: System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC' 如何設置 y 軸值?

我似乎只是弄亂了列和行:

        int xcol = 1 + 2 * (i- 1); //1, 3, 5, 6
        int ycol = xcol++; //2, 5, 7
        chart.SeriesCollection(currentarm).XValues = (Excel.Range)workSheet.Range[workSheet.Cells[1, xcol], workSheet.Cells[rowlenght, **xcol**]].Cells;
        chart.SeriesCollection(currentarm).Values = (Excel.Range)workSheet.Range[workSheet.Cells[1, ycol], workSheet.Cells[rowlenght, **ycol**]].Cells;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM