0

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.

Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51

2 Answers2

0

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.

UndeadBob
  • 1,110
  • 1
  • 15
  • 34
0

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;
Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51