C# created Excel graph - Should be ScatterLine but

2019-06-28 08:14发布

I am having a problem making the Excel graph-type appear as a ScatterLine. Oddly, after compiled, on my computer it appears as ScatterLine, but on everyone else's it appears as a Line type that has no X axis!

    private void showExcelXY_Open(ArrayList al)
    {
        ArrayList alX = new ArrayList();
        ArrayList alY = new ArrayList();
        alX.Clear();
        alY.Clear();
        for (int i = 0; i < al.Count; i++)
        {
            string[] val = al[i].ToString().Split('^');
            alX.Add(val[0]);
            alY.Add(val[1]);
        }

        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] = "Pressure";
        ws.Cells[1, 2] = "Compression";

        ws.Cells[2, 1] = alX[0];
        ws.Cells[2, 2] = alY[0];

        ws.Cells[3, 1] = alX[1];
        ws.Cells[3, 2] = alY[1];

        ws.Cells[4, 1] = alX[2];
        ws.Cells[4, 2] = alY[2];

        ws.Cells[5, 1] = alX[3];
        ws.Cells[5, 2] = alY[3];

        ws.Cells[6, 1] = alX[4];
        ws.Cells[6, 2] = alY[4];

        ws.Cells[7, 1] = alX[5];
        ws.Cells[7, 2] = alY[5];

        ws.Cells[8, 1] = alX[6];
        ws.Cells[8, 2] = alY[6];

        //set the source data and the chart type.
        Range chartRange = ws.get_Range("A2", "B8");
        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.xlCategory, XlAxisGroup.xlPrimary);
        xAxis.HasMajorGridlines = true;
        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 code clearly shows that it is setting the chart-type as:

xlChart.ChartType = XlChartType.xlXYScatterLines;

Any ideas would be greatly appreciated!

0条回答
登录 后发表回答