Plotting a graph in Excel file using C#.Net. | Newbie | | Join Date: May 2007
Posts: 3
| | |
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
|  | Moderator | | Join Date: Dec 2006
Posts: 4,745
| | | re: Plotting a graph in Excel file using C#.Net.
Welcome to the site. Please post your relevant code. Do receive any errors?
| | Newbie | | Join Date: May 2007
Posts: 3
| | | re: Plotting a graph in Excel file using C#.Net.
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);
| | Newbie | | Join Date: May 2007
Posts: 3
| | | re: Plotting a graph in Excel file using C#.Net.
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)
| | Newbie | | Join Date: Nov 2007
Posts: 5
| | | re: Plotting a graph in Excel file using C#.Net. Quote:
Originally Posted by Narendar123P 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
| | Newbie | | Join Date: Sep 2009
Posts: 1
| | | re: Plotting a graph in Excel file using C#.Net.
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. -
Excel.SeriesCollection seriesCollection;
-
Excel.Series series1, series2;
-
-
//this create the seriescollection and series
-
seriesCollection = xlChart.SeriesCollection(misValue)
-
series1 = seriesCollection.NewSeries();
-
series2 = seriesCollection.NewSeries();
-
-
//this gives each series the values. values are y values, xvalues are x values
-
series1.Values = ws.get_Range("A1", "A4") //assuming y values are in column //A, change range according to which column your values are
-
series2.XValues = ws.get_Range("B1", "B4") //if x values are in column B
-
-
//repeat for series2
-
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|