locked
How to position excel chart x axis text position c# RRS feed

  • Question

  • i am developing a chart programmatically using c# and excel interop dll.

    i have to develop this kind of char. here is the screen shot

    i almost done my job and my UI looks like

    just see the arrow sign i want to put that text at the right hand side like above chart but due to lack of knowledge i am getting trouble.

    here i am giving my full code. please see to it and guide me what extra code i need to put to position that text. thanks

    Excel.Application xlApp ;
        Excel.Workbook xlWorkBook ;
        Excel.Worksheet xlWorkSheet ;
        Excel.Worksheet xlWorkSheetChart;
        object misValue = System.Reflection.Missing.Value;
    
        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
    
        try
        {
    
    
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Name = "Data";
    
            xlWorkSheetChart = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
            xlWorkSheetChart.Name = "Chart";
    
            //add data 
            xlWorkSheet.Cells[1, 1] = "Date";
            xlWorkSheet.Cells[2, 1] = "2013 10 15";
            xlWorkSheet.Cells[3, 1] = "2013 10 16";
            xlWorkSheet.Cells[4, 1] = "2013 10 17";
            xlWorkSheet.Cells[5, 1] = "2013 10 18";
            xlWorkSheet.Cells[6, 1] = "2013 10 21";
    
            xlWorkSheet.Cells[1, 2] = "TOTAL No of jobs";
            xlWorkSheet.Cells[2, 2] = "211";
            xlWorkSheet.Cells[3, 2] = "148";
            xlWorkSheet.Cells[4, 2] = "183";
            xlWorkSheet.Cells[5, 2] = "185";
            xlWorkSheet.Cells[6, 2] = "180";
    
            xlWorkSheet.Cells[1, 3] = "TOTAL Turnaround";
            xlWorkSheet.Cells[2, 3] = "76.03";
            xlWorkSheet.Cells[3, 3] = "36.27";
            xlWorkSheet.Cells[4, 3] = "32.38";
            xlWorkSheet.Cells[5, 3] = "64.62";
            xlWorkSheet.Cells[6, 3] = "43.15";
    
            xlWorkSheet.Cells[1, 4] = "TOTAL 1-day percentage";
            xlWorkSheet.Cells[2, 4] = "35.59";
            xlWorkSheet.Cells[3, 4] = "51.44";
            xlWorkSheet.Cells[4, 4] = "277.66";
            xlWorkSheet.Cells[5, 4] = "161.78";
            xlWorkSheet.Cells[6, 4] = "314.39";
    
            xlWorkSheet.Cells[1, 5] = "TOTAL hours";
            xlWorkSheet.Cells[2, 5] = "16042.33";
            xlWorkSheet.Cells[3, 5] = "5367.96";
            xlWorkSheet.Cells[4, 5] = "5925.54";
            xlWorkSheet.Cells[5, 5] = "11954.7";
            xlWorkSheet.Cells[6, 5] = "7767";
    
            xlWorkSheet.Cells[1, 6] = "Number of techs";
            xlWorkSheet.Cells[2, 6] = "37";
            xlWorkSheet.Cells[3, 6] = "19";
            xlWorkSheet.Cells[4, 6] = "18";
            xlWorkSheet.Cells[5, 6] = "20";
            xlWorkSheet.Cells[6, 6] = "28";
    
            xlWorkSheet.Cells[1, 7] = "Work hours/tech";
            xlWorkSheet.Cells[2, 7] = "8";
            xlWorkSheet.Cells[3, 7] = "8";
            xlWorkSheet.Cells[4, 7] = "8";
            xlWorkSheet.Cells[5, 7] = "8";
            xlWorkSheet.Cells[6, 7] = "8";
    
            xlWorkSheet.Cells[1, 8] = "Efficiency";
            xlWorkSheet.Cells[2, 8] = "0.7";
            xlWorkSheet.Cells[3, 8] = "0.7";
            xlWorkSheet.Cells[4, 8] = "0.7";
            xlWorkSheet.Cells[5, 8] = "0.7";
            xlWorkSheet.Cells[6, 8] = "0.7";
    
            xlWorkSheet.Cells[1, 9] = "Theoretical hours";
            xlWorkSheet.Cells[2, 9] = "207.2";
            xlWorkSheet.Cells[3, 9] = "106.4";
            xlWorkSheet.Cells[4, 9] = "100.8";
            xlWorkSheet.Cells[5, 9] = "112";
            xlWorkSheet.Cells[6, 9] = "156.8";
    
    
    
            Excel.SeriesCollection seriesCollection;
            Excel.Series series1, series2, series3;
    
            Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheetChart.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 80, 300, 250);
            Excel.Chart chartPage = myChart.Chart;
            chartPage.HasTitle = true;
            chartPage.ChartTitle.Text = "Daily Job History";
            myChart.Width = 500;
            chartPage.Legend.Position = XlLegendPosition.xlLegendPositionBottom;
    
    
            seriesCollection = (Excel.SeriesCollection)chartPage.SeriesCollection(Type.Missing);
            series1 = seriesCollection.NewSeries();
            series2 = seriesCollection.NewSeries();
            series3 = seriesCollection.NewSeries();
    
            series1.Name = "=Data!$B$1";
            series1.Values = "=Data!$B$2:$B$6";
            series1.ChartType = XlChartType.xlColumnClustered;
    
            series2.Name = "=Data!$C$1";
            series2.XValues = "=Data!$A$2:$A$6";
            series2.Values = "=Data!$C$2:$C$6";
            series2.ChartType = XlChartType.xlLineMarkers;
    
            series3.Name = "=Data!$D$1";
            series3.Values = "=Data!$D$2:$D$6";
            series3.ChartType = XlChartType.xlLineMarkers;
    
            Axis axis;
            axis = (Axis)chartPage.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
            axis.HasTitle = true;
            axis.AxisTitle.Text = "Right Side text";
            axis.HasMajorGridlines = false;
            axis.HasMinorGridlines = false;
    
    
            axis = (Axis)chartPage.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
            axis.HasTitle = true;
            axis.AxisTitle.Text = "Left Side text"; ;
            axis.HasMajorGridlines = true;
            axis.HasMinorGridlines = false;
    
            chartPage.Axes(Excel.XlAxisType.xlCategory).Select();
            chartPage.Axes(Excel.XlAxisType.xlCategory).TickLabels.Orientation = 35;
    
            string strpath = System.Windows.Forms.Application.StartupPath.ToString() + "\\test.xls";
    
            if (File.Exists(strpath))
            {
                File.Delete(strpath);
            }
    
    
            xlWorkBook.SaveAs(strpath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            xlWorkBook.Close(true, misValue, misValue);
            xlWorkBook = null;
            xlApp.Quit();
            xlApp = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
    
        }
        //releaseObject(xlWorkSheet);
        //releaseObject(xlWorkSheetChart);
        //releaseObject(xlWorkBook);
        //releaseObject(xlApp);
    
    
        System.Diagnostics.Process.GetCurrentProcess().Kill();
        System.Windows.Forms.Application.Exit();

    Thursday, October 31, 2013 3:07 PM

Answers

All replies