469,352 Members | 2,094 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Plotting a graph in Excel file using C#.Net.

Hi all,
I am trying to plot a graph in excel file using C#.Net.
I am using the name space excel .
If my graph has one series(2 columns), i am able to draw the graph.
if my graph has two series(4 columns), i am unable to draw the correct graph.
In this case i need graph with 2 series, one with first and second column
and the second one with third and fourth columns. But i am getting 3 curves,
one is for first and second column
second is for first and third column
third is for first and fourth column, which i dont want.

please clarify my problem if you hav answers.
Thanks in advance
Bye
May 23 '07 #1
5 19678
kenobewan
4,871 Expert 4TB
Welcome to the site. Please post your relevant code. Do receive any errors?
May 23 '07 #2
xla = new Excel.Application();
xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;


ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject chartObj = chartObjs.Add(100, 20, 300, 300);
Chart xlChart = chartObj.Chart;
int[,] v1 ={ { 1, 2 ,5, 10}, { 2, 4,10, 20 }, { 3, 6 ,15,30} };
Range rg;
rg = ws.get_Range("A2", "D4");
rg.Value2 = v1;

xlChart.ChartType = XlChartType.xlXYScatterSmoothNoMarkers;
xlChart.SetSourceData(rg, Type.Missing);
May 23 '07 #3
in place of int[,] v1 ={ { 1, 2 ,5, 10}, { 2, 4,10, 20 }, { 3, 6 ,15,30} };(of my previous post), if i give int[,] v1 ={ { 1, 2 }, { 2, 4 }, { 3, 6} };, i am able to get
the curve. If i give int[,] v1 ={ { 1, 2 ,5, 10}, { 2, 4,10, 20 }, { 3, 6 ,15,30} };
i am getting 3 curves for the points
first curve (1,2),(2,4),(3,6)
second curve (1,5),(2,10),(3,15)
Third curve (1,10),(2,20),(3,30)
which i dont require.

What i require is

two curves
first one (1,2),(2,4),(3,6)
second curve (5,10),(10,20),(15,30)
May 23 '07 #4
naish
5
xla = new Excel.Application();
xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;


ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject chartObj = chartObjs.Add(100, 20, 300, 300);
Chart xlChart = chartObj.Chart;
int[,] v1 ={ { 1, 2 ,5, 10}, { 2, 4,10, 20 }, { 3, 6 ,15,30} };
Range rg;
rg = ws.get_Range("A2", "D4");
rg.Value2 = v1;

xlChart.ChartType = XlChartType.xlXYScatterSmoothNoMarkers;
xlChart.SetSourceData(rg, Type.Missing);
Hi,
I am doing same but i am not able to understand how to draw a chart in side the excel. I am looking your code. What's taht xlChart ? and what is this ChartObject? do i need any assambly for that?
Thanks,
Naish
Dec 5 '07 #5
cwenee
1
Ok so this is what you should do:
1.create a seriescollection cause you have several series (plots/lines) in one excel chart
2.create the seperate series
3. assign the values for each series.

Expand|Select|Wrap|Line Numbers
  1. Excel.SeriesCollection seriesCollection;
  2. Excel.Series series1, series2;
  3.  
  4. //this create the seriescollection and series
  5. seriesCollection = xlChart.SeriesCollection(misValue)
  6. series1 = seriesCollection.NewSeries();
  7. series2 = seriesCollection.NewSeries();
  8.  
  9. //this gives each series the values. values are y values, xvalues are x values 
  10. series1.Values = ws.get_Range("A1", "A4") //assuming y values are in column //A, change range according to which column your values are
  11. series2.XValues = ws.get_Range("B1", "B4") //if x values are in column B
  12.  
  13. //repeat for series2 
  14.  
Sep 25 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by george_y | last post: by
2 posts views Thread by KevinGPO | last post: by
11 posts views Thread by Chapman | last post: by
1 post views Thread by wayne | last post: by
7 posts views Thread by diffuser78 | last post: by
4 posts views Thread by Gouri | last post: by
3 posts views Thread by 9966 | last post: by
2 posts views Thread by Durand | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.