3

I want to provide the custome angle to my labels on the x-axis to like -35 degrees. i.e. i want to format the in Excel ==> Format Axis ==>Alignment ==>Custom angle

newWorksheet.Select(Type.Missing);
            Excel.Range chartRange;
            object misValue = System.Reflection.Missing.Value;
            Excel.ChartObjects xlCharts = (Excel.ChartObjects)newWorksheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
            Excel.Chart chartPage = myChart.Chart;
            chartRange = newWorksheet.get_Range(cell1, cell2);
            chartPage.SetSourceData(chartRange, Excel.XlRowCol.xlColumns);
            chartPage.ChartType = Excel.XlChartType.xlColumnClustered;

            var labels = new List<string>();
            string date = string.Empty;
            foreach (KeyValuePair<string, List<TestDetails>> kvp in writetocsv)
            {
                date = kvp.Key.Substring(0, (kvp.Key.Length - kvp.Key.IndexOf('_')) + 1);
                labels.Add(date);
            }
            var series = (Excel.Series)chartPage.SeriesCollection(1);
            series.XValues = labels.ToArray();
            //series.HasDataLabels = true;
            Excel.Axis valueAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);


            chartPage.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsNewSheet, "Chart_" + sheetName);
User123
  • 373
  • 2
  • 7
  • 14

1 Answers1

5

CODE

Excel.Range chartRange;

Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("A1", "d5");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;

//~~> From here this is the part which you want
chartPage.ApplyLayout(6, Type.Missing);
chartPage.Axes(Excel.XlAxisType.xlValue).AxisTitle.Select();
chartPage.Axes(Excel.XlAxisType.xlValue).AxisTitle.Orientation = Excel.XlOrientation.xlHorizontal;

//~~> 35 Deg angle
for (int i = 1; i <= 35; i++)
{
    chartPage.Axes(Excel.XlAxisType.xlValue).AxisTitle.Orientation = -i;
}

SNAPSHOT

enter image description here

FOLLOWUP

Hi Siddharth, Actullay i want to provide the Angle to "Term1", "Term2" – User123 10 mins ago

Try this for X Axis Tick Labels

Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("A1", "d5");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
chartPage.ApplyLayout(6, Type.Missing);

chartPage.Axes(Excel.XlAxisType.xlCategory).Select();
chartPage.Axes(Excel.XlAxisType.xlCategory).TickLabels.Orientation = 35;

SNAPSHOT

enter image description here

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • 1
    Hi Siddharth, Actullay i want to provide the Angle to "Term1", "Term2" – User123 Aug 16 '12 at 19:10
  • when i tried chartPage.Axes(Excel.XlAxisType.xlCategory).Select(); chartPage.Axes(Excel.XlAxisType.xlCategory).TickLabels.Orientation = 35; But chartPage.Axes takes two objects – User123 Aug 16 '12 at 19:56
  • Excel.Axis valueAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlCategory, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary); valueAxis.Select(); valueAxis.TickLabels.Orientation = Microsoft.Office.Interop.Excel.XlTickLabelOrientation.xlTickLabelOrientationUpward; Code above atleast makes it 90 deg – User123 Aug 16 '12 at 20:01
  • What does `valueAxis.TickLabels.Orientation = 35;` give you? – Siddharth Rout Aug 16 '12 at 20:14
  • valueAxis.TickLabels.Orientation it doesnot take the int value. – User123 Aug 16 '12 at 20:17