Add a 2nd Series to an Excel chart using C# - c#

I am trying to add two series to an Excel chart. I am able to add one series to the chart with this code. How can i add one more series to my chart?
worksheet.Select(Type.Missing);
Excel.Range chartRange;
object misValue = System.Reflection.Missing.Value;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = worksheet.get_Range("B2", "B10");
chartPage.SetSourceData(chartRange, Excel.XlRowCol.xlColumns);
chartPage.ChartType = Excel.XlChartType.xlLine;
var series = (Excel.Series)chartPage.SeriesCollection(1);
series.Values = chartRange;
chartRange = worksheet.get_Range("A2", "A10");
series.XValues = chartRange;

Don't know the C# for it, but in VBA I would use something like:
With ChartPage.SeriesCollection.NewSeries
.Values = ActiveSheet.Range("C2:C10")
End With

Related

Add legends to chart

I have a Excel chart. How do I add a legend value?
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("B12", "B15");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xl3DPieExploded;
foreach (Series series in chartPage.SeriesCollection())
{
series.Name = "Diagram Name";
}
chartPage.HasLegend = true;
chartPage.Is
//export chart as picture file
String destPath = Directory.GetCurrentDirectory();
chartPage.Export(Directory.GetCurrentDirectory() + "\\dig2.bmp", "BMP", misValue);
Method "Series" not available.
This worked for me. I added in some data so the example would work stand alone.
Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Workbook xlWorkBook;
Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name = "Sheet";
ChartObjects xlCharts = (ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
Chart chartPage = myChart.Chart;
chartPage.Legend.Position = XlLegendPosition.xlLegendPositionBottom;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Title";
xlWorkSheet.Cells[12, 1] = "Test";
xlWorkSheet.Cells[13, 1] = "Test2";
xlWorkSheet.Cells[14, 1] = "Test3";
xlWorkSheet.Cells[15, 1] = "Test4";
xlWorkSheet.Cells[12, 2] = 12;
xlWorkSheet.Cells[13, 2] = 13;
xlWorkSheet.Cells[14, 2] = 14;
xlWorkSheet.Cells[15, 2] = 15;
var sc = chartPage.SeriesCollection();
var series1 = sc.NewSeries();
series1.Name = "Title";
series1.XValues = $"'{xlWorkSheet.Name}'!A12:A15";
series1.Values = $"'{xlWorkSheet.Name}'!B12:B15";
series1.ChartType = XlChartType.xl3DPieExploded;
chartPage.ApplyDataLabels();
//export chart as picture file
String destPath = Directory.GetCurrentDirectory();
chartPage.Export(Directory.GetCurrentDirectory() + "\\dig2.bmp", "BMP", misValue);
[![Excel.Range chartRange;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
//диаграмма 2
chartRange = xlWorkSheet.get_Range("D12", "E16");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xl3DPieExploded;
foreach (Series series in chartPage.SeriesCollection())
{
series.Name = "Уровень удовлетворенности респондентов длительностью ожидания";
}
chartPage.HasLegend = true;
//export chart as picture file
String destPath = Directory.GetCurrentDirectory();
chartPage.ApplyDataLabels(XlDataLabelsType.xlDataLabelsShowValue, false, true, false, false, false, false, true, false, false);
chartPage.Export(Directory.GetCurrentDirectory() + "\\dig2.bmp", "BMP", misValue);
//конец диаграмы 2][1]][1]

Add values and legend to chart from excel

I've built a chart on the values of their Excel Worksbook. And then save the picture.
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
// Diagram 2
chartRange = xlWorkSheet.get_Range("B12", "B15");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xl3DPieExploded;
foreach (Series series in chartPage.SeriesCollection())
{
series.Name = "Уровень удовлетворенности респондентов длительностью ожидания";
}
// export chart as picture file
String destPath = Directory.GetCurrentDirectory();
chartPage.Export(Directory.GetCurrentDirectory() + "\\dig2.bmp", "BMP", misValue);
How can I add a legend and values to the chart?

C# generate multiple charts in excel

I want to generate two charts in excel. this is my code for generating the first one, then i want to generate one more in the same document using another range.
Range chartRange;
ChartObjects xlCharts = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
Chart chartPage = myChart.Chart;
chartRange = ws.get_Range("A4", "AZ4");
chartPage.ChartType = XlChartType.xlLineMarkers;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Lägenhetstyp 1";
chartPage.HasLegend = false;
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = XlChartType.xlColumnClustered;
Something like this.
Range chartRange;
//First Chart
ChartObjects xlCharts = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
Chart chartPage = myChart.Chart;
chartRange = ws.get_Range("A4", "AZ4");
chartPage.ChartType = XlChartType.xlLineMarkers;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Chart 1";
chartPage.HasLegend = false;
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = XlChartType.xlColumnClustered;
//Second chart
ChartObjects xlCharts = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
Chart chartPage = myChart.Chart;
chartRange = ws.get_Range("A8", "AZ8");
chartPage.ChartType = XlChartType.xlLineMarkers;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Chart 2";
chartPage.HasLegend = false;
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = XlChartType.xlColumnClustered;
This is how i solved it.
for (int i = 1; i <= 2; i++)
{
Range chartRange;
ChartObjects xlCharts = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
Chart chartPage = myChart.Chart;
if (i == 1)
{
chartRange = ws.get_Range("A4", "AZ4");
}
else
{
chartRange = ws.get_Range("A8", "AZ8");
}
chartPage.ChartType = XlChartType.xlLineMarkers;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Chart " + i;
chartPage.HasLegend = false;
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = XlChartType.xlColumnClustered;
}

Create a secondary axis in excel using charts

I try to create secondary axis in my excel chart from c#. I try this code-
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("C2", GetLetterFromNum(columns.Length - 1 + Convert.ToInt32(NumCust.Text)) + Convert.ToString(rowIndex));
TimeRange = xlWorkSheet.get_Range("A2", "A" + Convert.ToString(rowIndex));
SecondryRange = xlWorkSheet.get_Range("B2", "B" + Convert.ToString(rowIndex));
chartPage.SetSourceData(chartRange, misValue);
chartPage.SeriesCollection(1).XValues = TimeRange;
chartPage.SeriesCollection(1).YAxisType = AxisType.Primary;
chartPage.SeriesCollection(1).YAxisType = AxisType.Secondary;
but the complayer doesn't recognize the "AxisType" command,
but I always get error message
The name 'AxisType' does not exist in the current context
I couldn't find what I do wrong...
i found this post at msdn where an example with a secondary axe is given

How to add the Chart sheet in Excel using C#

I have created the sheet1 and populated some data in the sheet, using the data from sheet1 i want to create a chart sheet with ploting the data
try
{
app = new Excel.Application();
app.Visible = true;
workbook = app.Workbooks.Add(1);
worksheet = (Excel.Worksheet)workbook.Sheets[1];
PopulateDateInExcel(pathtologsfolder, startdate, enddate);
// create a chart
Excel.Range chartRange;
object misValue = System.Reflection.Missing.Value;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)workbook.Charts[2];
Excel.Chart chartPage = myChart.Chart;
chartRange = worksheet.get_Range("AN1", "AP6");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xl3DLine;
}
catch (Exception e)
{
//Console.Write("Error");
}
finally
{
}
Thanks in advance,
Excel Automation
Try this (UNTESTED)
Excel.ChartObject myChart = (Excel.ChartObject)charts.Add(10, 70, 250, 250);
instead of
Excel.ChartObject myChart = (Excel.ChartObject)workbook.Charts[2];
and then once your chart is created, move it to a chart sheet using this code
chart.Location(XlChartLocation.xlLocationAsNewSheet, Type.Missing);
The way to do this would be to use the Add method:
Excel.Workbook xlWorkbook;
Excel.Chart chartName = (Excel.Chart)xlWorkbook.Charts.Add();
Admittedly, at least in my experiments this causes trouble as the dataset it is using by default is fairly random (Excel magic...) so you might be needing to reformat it afterwards (delete and add datasets, etc), but that's the method to do it.
EDIT:
I should note that Excel support documentation seems to recommend the Add2 method method, but I haven't managed to use it successfully:
Excel.Workbook xlWorkbook;
Excel.Chart chartName = (Excel.Chart)xlWorkbook.Charts.Add2();
Excel.Application oXL = new Excel.Application();
and after then
oXL.Run() and see attached:

Categories