Home » excel » c# – Excel Chart Type vertical value

c# – Excel Chart Type vertical value

Posted by: admin March 9, 2020 Leave a comment

Questions:

From a long time i am trying to generate graph like this enter image description here

Codes i tried.

            Excel.Range chartRange1;
            Excel.ChartObjects xlCharts1 = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart1 = (Excel.ChartObject)xlCharts1.Add(350, 500, 500, 350);
            Excel.Chart chartPage1 = myChart1.Chart;
            chartRange1 = worksheet.get_Range("A33", "b56");
            chartPage1.SetSourceData(chartRange1, Type.Missing);
            chartPage1.ChartType = Excel.XlChartType.xlBarStacked;

            Excel.Range xValues = worksheet.Range["B33", "B56"];
            Excel.Range values = worksheet.Range["a33", "a56"];

            Excel.SeriesCollection seriesCollection = (Excel.SeriesCollection)chartPage1.SeriesCollection();

            Excel.Series series1 = seriesCollection.NewSeries();
            series1.XValues = xValues;
            series1.Values = values; 

Please help which chart type i should use or i am making any mistake. A

After changing chart type, it works perfectly fine but its not working for last row text. As shown in image below.
enter image description here

How to&Answers:

Yes, you need to change the chart type.

using Excel = Microsoft.Office.Interop.Excel;

chartPage1.ChartType = Excel.XlChartType.xlBarClustered

You may have to adjust gridlines depending on how you want them to appear. I can provide more code for that if needed.

Edit – Also, don’t forget to do

chartPage1.PlotBy = Excel.XlRowCol.xlColumns;

Answer:

I don’t know how but the moment i commented chartPage1.SetSourceData(chartRange1, Type.Missing); in code it works fine, might be possible two data source are set one by commented line and one by series collection.