Excel charts +C# - c#

I am using excel to draw charts from c#, but i need the chart to be one series related to each other not two series (when i select a range that has two columns of data)
can any one help:
xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;
// Now create the chart.
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject chartObj = chartObjs.Add(100, 20, 300, 300);
Chart xlChart = chartObj.Chart;
Range rg = ws.get_Range("B2", "C17");
xlChart.SetSourceData(chartRange, XlRowCol.xlColumns);
thanks

I cleaned the code up a bit and added the generation of random data so this should run on its own.
Random random = new Random();
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;
// Now create the chart.
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects();
ChartObject chartObj = chartObjs.Add(150, 20, 300, 300);
Chart xlChart = chartObj.Chart;
// generate some random data
for (int row = 0; row < 16; row++)
{
ws.Cells[row + 2, 2] = row + 1;
ws.Cells[row + 2, 3] = random.Next(100);
}
Range xValues = ws.Range["B2", "B17"];
Range values = ws.Range["C2", "C17"];
SeriesCollection seriesCollection = xlChart.SeriesCollection();
Series series1 = seriesCollection.NewSeries();
series1.XValues = xValues;
series1.Values = values;

Related

c# excel interop charts

I have a question: how to remove the displaying of zeros and percents?
chart
And second question: if I have only zeros then the diagram is empty. Is there any method to fill the chart with the inscription? For example, "no data".
chart2
#region 123
Excel.ChartObjects xlChart1 = (Excel.ChartObjects)WorkSheet_2.ChartObjects(Type.Missing);
Excel.ChartObject myxlChart1 = (Excel.ChartObject)xlChart1.Add(600, 1110, 450, 450);
Excel.Chart chartpage1 = myxlChart1.Chart;
Excel.SeriesCollection col = chartpage1.SeriesCollection();
chartpage1.ChartType = Excel.XlChartType.xlPie;
chartpage1.ChartArea.Border.Color = Color.FromArgb(30, 82, 255).ToArgb();
Excel.SeriesCollection chartpage1seriesCollection = chartpage1.SeriesCollection();
SeriesCollection seriesCollection3ddef = (SeriesCollection)chartpage1.SeriesCollection(Type.Missing);
Series series1= seriesCollection3ddef.NewSeries();
series1.XValues = WorkSheet_2.get_Range("BF3", "BM3");
series1.Values = WorkSheet_2.get_Range("BF15", "BM15");
Excel.Axis myaxis = chartpage1.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Excel.Axis;
series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowLabel, false, Type.Missing, false, false, false, true, Excel.XlDataLabelsType.xlDataLabelsShowPercent, false, "\n");
series1.DataLabels().Font.Name = "Arial";
series1.DataLabels().Font.Size = 12;
series1.DataLabels().Orientation = 0;
chartpage1.HasLegend = true;
chartpage1.Legend.Border.Color = ColorTranslator.ToOle(Color.DarkRed);
chartpage1.Legend.Position = XlLegendPosition.xlLegendPositionCorner;
chartpage1.Legend.Font.Name = "Arial";
chartpage1.Legend.Font.Size = 10;
;
chartpage1.HasTitle = true;
chartpage1.ChartTitle.Text = "MyChart, \n N, pcs";
chartpage1.ChartTitle.Position = XlChartElementPosition.xlChartElementPositionAutomatic;
chartpage1.ChartTitle.Font.Size = 14;
chartpage1.ChartTitle.Font.Name = "Arial";
chartpage1.ChartTitle.Font.Bold = true;
x3l.PlotArea(chartpage1, ChartHeight * 0.30, ChartWidth * 0.06, ChartHeight * 0.8, ChartWidth * 0.8);
#endregion
Thanks!

Change Date format in chart axis but not in excel

I am trying to get a xlWorkSheet range and format it to "MMM/YYYY" in the xAxis of a chart. But I dont want to change the date format in that WorkSheet cells where I am taking the info and where I want to keep having a "MM/YYYY" format.
I've already formatted the row to "MM/YYYY" and "MMM/YYYY", but not to use one in the excel table and the other one in just the chart.
My code is:
ChartObject myChart
Chart chartPage = myChart.Chart;
chartPage.ChartType = XlChartType.xlArea;
var yAxis = (Axis)chartPage.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
Axis.HasTitle = false;
Range chartRange = xlWorkSheet.Range[xlWorkSheet.Cells[4, 1],
xlWorkSheet.Cells[rows, columns]];
Range x_Range = xlWorkSheet.Range[xlWorkSheet.Cells[1, 2], xlWorkSheet.Cells[1, columnsCount]];
Range x_Range2 = x_Range.EntireRow.NumberFormat("MMM/YYYY");
Axis xAxis = (Axis)chartPage.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
xAxis.CategoryNames = x_Range2;
(...series definition... and rest of code)

C# charting - two chart areas with different Y values

Good day nobbles of programming,
I have a question. I made a code to create chart with datagridview as data source with chartarea1.
Chart chart1 = new Chart();
chart1.Size = new System.Drawing.Size(1024, 768);
ChartArea chartArea1 = new ChartArea();
chartArea1.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisX.IntervalType = DateTimeIntervalType.Months;
chartArea1.AxisX.Interval = 1;
chart1.ChartAreas.Add(chartArea1);
chart1.Series.Add(new Series());
chart1.Series[0].XValueMember = dataGridView1.Columns[0].DataPropertyName;
chart1.Series[0].YValueMembers = dataGridView1.Columns[1].DataPropertyName;
chart1.DataSource = dataGridView1.DataSource;
chart1.Series[0].ChartType = SeriesChartType.Line;
Now i want to create second chartarea within chart1 with same XValueMember but different YValueMember from different datagridview, for example datagridview2. Is it possible to do it?
Thanks in advance.
Ok, so i tried this:
Chart chart1 = new Chart();
ChartArea chartArea1 = new ChartArea();
Series series1 = new Series();
chart1.DataSource = dataGridView1.DataSource;
chartArea1 = chart1.ChartAreas.Add("ca1");
chartArea1.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisX.IntervalType = DateTimeIntervalType.Months;
chartArea1.AxisX.Interval = 1;
series1 = chart1.Series.Add("s1");
series1.Points.DataBindXY(dataGridView1.Columns[0].DataPropertyName, dataGridView1.Columns[1].DataPropertyName);
series1.ChartType = SeriesChartType.Line;
chart1.SaveImage("chart.png", ChartImageFormat.Png);
Now getting this error:
Y values cannot be data bound to the string object.
Parameter name: yValues
You should not bind to the whole Chart then but to the Series.Points !
There are many way to use Chart DataBinding
You also should control the Name of each Series and of the ChartAreas; this matters when you want/need to associate the 2nd series with the 2nd chartarea.
The preferred/recommended way to add a ChartArea or a Series is like so:
chartArea1 = chart1.ChartAreas.Add("ca1"); // or any other, more useful name
Series series1 = chart1.Series.Add("s1"); // dito
The association is done like this:
series1.ChartArea = "ca1"; // note the string!!
To bind only one series use..
series1.Points.DataBindXY()
..or some other overload from the table in the link at the top.

Using secondary axis for chart cause x-axis and primary y-axis issue (Excel)

Making a chart using secondary axis,
makes my chart primary y-axis is shown with some values which I don't want to have.
Only x-axis and secondary y-axis.
and also the x-axis is drawn without the date values what I've passed.
Code:
chartType2 = GetChartType(worksheet, chartToDraw, endcolcnt, i, chartType2, chartType);
chartType2.UseSecondaryAxis = true;
Scale(headerString, endcolcnt, worksheet, chartType2, stcol, isFieldSame, endcol, stcolumn1, endrow, startRow);
and Scale Function only assigns the header names and all.
Details about the series taken
Output:
Input
Hard to say without more code. What are those functions doing exactly?
Are you trying to just get the axis on the right side? Based on the black chart you posted that would seem like what you are after. You could just do chartType.YAxis.Crosses = eCrosses.Max.
Or do you actually want TWO axes which would require two charts/series? If you want that then you would need to create a second chart based on the first (looks like your function might be doing that) and then add a unique series to each but with a common x-value dataset. Just make sure you add them in the right order.
This shows both scenarios:
[TestMethod]
public void Chart_Secondary_Axis_Test()
{
//http://stackoverflow.com/questions/28540458/using-secondary-axis-for-chart-cause-x-axis-and-primary-y-axis-issue-excel
var existingFile = new FileInfo(#"c:\temp\temp.xlsx");
if (existingFile.Exists)
existingFile.Delete();
using (var pck = new ExcelPackage(existingFile))
{
var wsContent = pck.Workbook.Worksheets.Add("Content");
//Some data
wsContent.Cells["A1"].Value = "A"; wsContent.Cells["B1"].Value = "B"; wsContent.Cells["C1"].Value = "C"; wsContent.Cells["D1"].Value = "D";
wsContent.Cells["A2"].Value = 100; wsContent.Cells["A3"].Value = 400; wsContent.Cells["A4"].Value = 200; wsContent.Cells["A5"].Value = 300; wsContent.Cells["A6"].Value = 600; wsContent.Cells["A7"].Value = 500;
wsContent.Cells["B2"].Value = 300; wsContent.Cells["B3"].Value = 200; wsContent.Cells["B4"].Value = 1000; wsContent.Cells["B5"].Value = 600; wsContent.Cells["B6"].Value = 500; wsContent.Cells["B7"].Value = 200;
wsContent.Cells["D2"].Value = new DateTime(2015, 1, 1); wsContent.Cells["D3"].Value = new DateTime(2015, 1, 2); wsContent.Cells["D4"].Value = new DateTime(2015, 1, 3); wsContent.Cells["D5"].Value = new DateTime(2015, 1, 4); wsContent.Cells["D6"].Value = new DateTime(2015, 1, 5); wsContent.Cells["D7"].Value = new DateTime(2015, 1, 6);
const int dataRow = 7;
const string FORMATDATE = "m/d/yy";
wsContent.Cells[2, 4, dataRow, 4].Style.Numberformat.Format = FORMATDATE;
//Single Axis with intersection on the right
var chart1 = wsContent.Drawings.AddChart("Chart1", eChartType.XYScatterLines);
chart1.SetSize(600, 400);
var serie1 = (ExcelScatterChartSerie)chart1.Series.Add(wsContent.Cells[2, 1, dataRow, 1], wsContent.Cells[2, 4, dataRow, 4]);
serie1.Header = wsContent.Cells[1, 1].Value.ToString();
chart1.YAxis.Crosses = eCrosses.Max;
//Dual Axis
var chart2a = wsContent.Drawings.AddChart("Chart2", eChartType.ColumnStacked);
chart2a.SetSize(600, 400);
chart2a.SetPosition(400, 0);
var serie2a = chart2a.Series.Add(wsContent.Cells[2, 2, dataRow, 2], wsContent.Cells[2, 4, dataRow, 4]);
serie2a.Header = wsContent.Cells[1, 2].Value.ToString();
var chart2b = chart2a.PlotArea.ChartTypes.Add(eChartType.XYScatterLines);
var serie2b = chart2b.Series.Add(wsContent.Cells[2, 1, dataRow, 1], wsContent.Cells[2, 4, dataRow, 4]);
serie2b.Header = wsContent.Cells[1, 1].Value.ToString();
chart2b.UseSecondaryAxis = true; //Flip the axes
pck.Save();
}
}

How to position excel chart using c#

I am developing a excel chart programmatically using c# and excel interop dll.
I develop this kind of chart. here is the screen shot
But i want chart like..(New Chart)
Here i am giving my full code. please see to it and guide me what extra code i need to make new chart.
Excel Data..
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart;
Excel.Range chartRange;
Excel.Chart chartPage;
myChart = (Excel.ChartObject)xlCharts.Add(630, 20, 300, 250);
chartRange = xlWorkSheet.Range["AW2", "BA5"];
chartPage = myChart.Chart;
chartPage.Legend.Delete();
myChart.Height = 380;
myChart.Width = 565;
myChart.Chart.HasDataTable = true;
myChart.Chart.DataTable.Font.Size = 10;
myChart.Chart.DataTable.Font.Name = "+Body";
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlLineMarkers;
I solved problem. Now just add some lines of code in above given code. Call this function chart_series_design(chartPage, "", ""); at the end. Function is like
public void chart_series_design(Excel.Chart chartPage,string xmsg,string ymsg)
{
chartPage.set_HasAxis(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary, true);
chartPage.set_HasAxis(Excel.XlAxisType.xlSeriesAxis, Excel.XlAxisGroup.xlSecondary, true);
chartPage.set_HasAxis(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlSecondary, true);
chartPage.set_HasAxis(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary, true);
chartPage.set_HasAxis(Excel.XlAxisType.xlSeriesAxis, Excel.XlAxisGroup.xlPrimary, true);
chartPage.set_HasAxis(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary, true);
Excel.Series series = (Excel.Series)chartPage.SeriesCollection(1);
series.AxisGroup = Excel.XlAxisGroup.xlSecondary;
series.AxisGroup = Excel.XlAxisGroup.xlPrimary;
series.Format.Line.Weight = 1.0F;
series.Format.Line.Visible = MsoTriState.msoFalse; //Tri-State
series.Format.Line.ForeColor.RGB = (int)Microsoft.Office.Interop.Excel.XlRgbColor.rgbWhite;
series = (Excel.Series)chartPage.SeriesCollection(2);
series.AxisGroup = Excel.XlAxisGroup.xlSecondary;
series.AxisGroup = Excel.XlAxisGroup.xlPrimary;
series.Format.Line.Weight = 1.0F;
series.Format.Line.Visible = MsoTriState.msoFalse; //Tri-State
series.Format.Line.ForeColor.RGB = (int)Microsoft.Office.Interop.Excel.XlRgbColor.rgbWhite;
series = (Excel.Series)chartPage.SeriesCollection(3);
series.AxisGroup = Excel.XlAxisGroup.xlSecondary;
series.AxisGroup = Excel.XlAxisGroup.xlPrimary;
series.Format.Line.Weight = 1.0F;
series.Format.Line.Visible = MsoTriState.msoFalse; //Tri-State
series.Format.Line.ForeColor.RGB = (int)Microsoft.Office.Interop.Excel.XlRgbColor.rgbWhite;
series = (Excel.Series)chartPage.SeriesCollection(4);
series.AxisGroup = Excel.XlAxisGroup.xlSecondary;
series.AxisGroup = Excel.XlAxisGroup.xlPrimary;
series.Format.Line.Weight = 1.0F;
series.Format.Line.Visible = MsoTriState.msoFalse; //Tri-State
series.Format.Line.ForeColor.RGB = (int)Microsoft.Office.Interop.Excel.XlRgbColor.rgbWhite;
Excel.Axis axis;
axis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
axis.HasTitle = true;
axis.AxisTitle.Text = xmsg;
axis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
axis.HasTitle = true;
axis.AxisTitle.Text = ymsg;
}

Categories

Resources