how to pass two columns values to plot line chart in C# interop

1.1k Views Asked by At

I am trying to plot line chart after generating excel file using Microsoft interop. Let my column A is Date, B is Rate and column C is volume and I want to print a chart between Volume and Date.

I am using this code to plot line chart.

var charts = worKsheeT.ChartObjects();
var chartObject = charts.Add(60, 10, 300, 300);
var chart = chartObject.Chart;
var range = worKsheeT.get_Range("C1", "C9");
chart.SetSourceData(range);
 chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;

But it is giving me a chart which has yAxis as Volume and xAxis is simple 1,2,3,4,5....

Thanks in Advance.

2

There are 2 best solutions below

0
On BEST ANSWER

After trying a lot I got this solution

Excel.Series series;
Excel.SeriesCollection seriesCollection;
series = seriesCollection.NewSeries();
var Values = "=Data!$B$2:$B$50";//this is range of columns
var XValues = "=Data!$A$2:$A$50";//this is range of columns
series.Name = "series name";
series.Values = Values;
series.XValues = XValues;
series.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
0
On

Try adding another variable:

var series = chart.SeriesCollection();

Then maybe

series[1].XValues = worKsheeT.get_Range("A1", "A9");

Remember, C is zero-based, but Excel (even through interop) is one-based.