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?
Related
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]
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;
}
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
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
I am drawing a pie chart with some data:
private void DrawFractionChart(Excel.Worksheet activeSheet, Excel.ChartObjects xlCharts, Excel.Range xRange, Excel.Range yRange)
{
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 500, 200, 100);
Excel.Chart chartPage = myChart.Chart;
Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
Excel.Series series1 = seriesCollection.NewSeries();
series1.XValues = activeSheet.Range["E1","E3"];
series1.Values = activeSheet.Range["F1","F3"];
chartPage.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, true,true,false,true,true,true,true);
chartPage.ChartType = Excel.XlChartType.xlDoughnut;
Excel.Axis axis = chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Excel.Axis;
}
I just can't figure out how to turn on data labels. I googled everywhere for it but nothing's been helpful so far sadly.
Try this (TRIED AND TESTED)
private void DrawFractionChart(Excel.Worksheet activeSheet, Excel.ChartObjects xlCharts, Excel.Range xRange, Excel.Range yRange)
{
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 500, 200, 100);
Excel.Chart chartPage = myChart.Chart;
Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
Excel.Series series1 = seriesCollection.NewSeries();
series1.XValues = activeSheet.Range["E1", "E3"];
series1.Values = activeSheet.Range["F1", "F3"];
chartPage.ChartType = Excel.XlChartType.xlDoughnut;
Excel.Axis axis = chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Excel.Axis;
series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, true, true, false, true, true, true, true);
}
One quick question though. If you are not using xRange and yRange then why declare it?
This is the completed code that is tried and tested.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Excel.Application();
xlexcel.Visible = true;
// Add a Workbook
xlWorkBook = xlexcel.Workbooks.Add();
// Set Sheet 1 as the sheet you want to work with
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 5] = "Apples";
xlWorkSheet.Cells[2, 5] = "Oranges";
xlWorkSheet.Cells[3, 5] = "Pears";
xlWorkSheet.Cells[1, 6] = "80";
xlWorkSheet.Cells[2, 6] = "65";
xlWorkSheet.Cells[3, 6] = "45";
Excel.ChartObjects myCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
// Specified xlWorkSheet.Cells[3, 6], xlWorkSheet.Cells[3, 6] just for the heck of it.
DrawFractionChart(xlWorkSheet, myCharts, xlWorkSheet.Cells[3, 6], xlWorkSheet.Cells[3, 6]);
//Once done close and quit Excel
//xlWorkBook.Close(true, misValue, misValue);
//xlexcel.Quit();
//releaseObject(xlWorkSheet);
//releaseObject(xlWorkBook);
//releaseObject(xlexcel);
}
private void DrawFractionChart(Excel.Worksheet activeSheet, Excel.ChartObjects xlCharts, Excel.Range xRange, Excel.Range yRange)
{
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 500, 200, 100);
Excel.Chart chartPage = myChart.Chart;
Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
Excel.Series series1 = seriesCollection.NewSeries();
series1.XValues = activeSheet.Range["E1", "E3"];
series1.Values = activeSheet.Range["F1", "F3"];
chartPage.ChartType = Excel.XlChartType.xlDoughnut;
Excel.Axis axis = chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Excel.Axis;
series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, true, true, false, true, true, true, true);
}
//private void releaseObject(object obj)
//{
// try
// {
// System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
// obj = null;
// }
// catch (Exception ex)
// {
// obj = null;
// MessageBox.Show("Unable to release the Object " + ex.ToString());
// }
// finally
// {
// GC.Collect();
// }
//}
}
}
SNAPSHOT
#region Export Excel To Chart
Excel.Range chartRange;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(300, 50, 500, 500);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("A6", "B" + (counter-1));
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlPie;// type of chart
chartPage.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, Excel.XlDataLabelsType.xlDataLabelsShowLabel, true, false, false, true, false, true);// set label
chartPage.Export(#"C:\excel_chart_export.bmp","BMP",misValue );
#endregion
The solution above didn't work for me in VS 2013 and Excel 2013.
Add the following references:
Microsoft.Office.Core
Microsoft.Office.Interop.Excel
and use the following method:
Chart oChart = ...
oChart.SetElement(Microsoft.Office.Core.MsoChartElementType.msoElementDataLabelOutSideEnd);
Source (MSDN):
https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.chart.setelement.aspx
https://msdn.microsoft.com/en-us/library/microsoft.office.core.msochartelementtype.aspx