drawing an excel chart from c# - c#

please help me with this c# issue.
i have this code for adding a chart in excel, by selecting data from a table from c3 to f10 cells. i want to change the axis and i don't know how. now the elements from horizontal axis are from the rows in the table and on the vertical are elements from columns in the table. i want to pun the rows on the vertical axis and the columns on the horizontal axis. how can i do that? please help [CLOSED]
Excel.ChartObjects xlCharts =(Excel.ChartObjects)excelWorksheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(100, number*50, 300, 250);
Excel.Chart chart = myChart.Chart;
chartRange = excelWorksheet.get_Range("c3","f10");
chart.SetSourceData(chartRange, misValue);
chart.ChartType = Excel.XlChartType.xlColumnClustered;

Related

Indication in Stacked chart for MIN, MAX (stuck here for a month ..)

Stuck in trying to adjust an arrow (sort of) inside an Excel chart.
I have a column chart with series of data.
I want to point out minimum, maximum, and average value from the chart.
What i was planning is, to mark it by an arrow.
Is it possible via C#?
For the below chart, I want to indicate MIN & MAX, like the ones I have drawn.
I started by getting the charts by this:
Excel.Application app = new Excel.Application();
Excel.Workbook book;
Excel.Worksheet sheet;
Excel.ChartObject chartObj;
Excel.Chart chart;
book = app.Workbooks.Open(#"Path to workbook");//Open the work book
sheet = book.Sheets["sheet name"];//Select the sheet the chart is on
chartObj = sheet.ChartObjects("Chart name");//Select the ChartObject
chart = chartObj.Chart; //Select the Chart from the ChartObjec
Findings:
I could not even find a general way - to represent min,max in a stacked chart.

c# excel radar chart - how to change horizontal axis?

In my WPF application I allready exported data to Excel to plot them in a radar chart, just to re-import this chart to the application. I need to do this for real-time analysis of the data and with WPF toolkit I couldn't create radar charts yet (I spend lot of time by searching for a solution before I got to Excel).
My problem now is, that I cant change the horizontal axis of the radar chart via C#. In Excel itself I would do that easy by setting up the chart options, but in my code I didn't got a clue yet what to do and also in debugging I didn't find any chart item, that told me more. My code so far is following:
Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Open(fileName);
Excel.Worksheet worksheet = workbook.Worksheets[1] as Excel.Worksheet;
CreateData(worksheet, sortangle, sortmu);
// Add chart.
Excel.ChartObjects xlCharts = worksheet.ChartObjects() as Excel.ChartObjects;
Excel.ChartObject myChart = xlCharts.Add(120, 10, 300, 300) as Excel.ChartObject;
Excel.Chart chart = myChart.Chart;
chart.ChartType = Excel.XlChartType.xlRadar;
var scale_range = worksheet.get_Range("A1", "A" + sortangle.Count.ToString());
var data_range = worksheet.get_Range("B1", "B" + sortangle.Count.ToString());
chart.SetSourceData(data_range);
chart.HasLegend = false;
// Set chart properties.
chart.ChartWizard(Source: data_range);
So with this I get such a result:
But the rotation axis need to be scaled by 0 to 360 degree, cause the data I process are stored with the information of an angle.
So I tried, by looking to other C# Excel like questions here e.g. this one, adding different lines like chart.SeriesCollection(1).XValues = scale_range; or chart.Axes(Excel.XlAxisType.xlValue).Source = scale_range;. Even by changing the ChartWizard to chart.ChartWizard(Source: data_range, SeriesLabels: scale_range); I didn't got far.
For any suggestions I would be grateful.
EDIT:
By checking the code you may see, that scale_range is not used atm. I also tried to bring it in the data_range item, but then I get two lines.
Okay. Finally got the solution (with help from here) and hope I can help anyone else with the same question. This works for radar charts too. Just had to correct the code for changing the horizontal axis by this:
Excel.Axis xAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
xAxis.CategoryNames = scale_range;

Centering, Merging, and Wrapping Text In Cells - EPPlus

I'm trying to center a header(s) for an excel file like so:
But I am missing a few details, since the code below one writes on one line and does not expand the cell's height. Here is my code so far:
ws.Cells[$"A{row}:F{row}"].Merge = true;
ws.Cells[$"A{row}"].Style.WrapText = true;
ws.SelectedRange[$"A{row}"].Value = purchaseHistory[0].LineText;
To center the merged cell both vertical and horizontally just do this:
//Only need to grab the first cell of the merged range
ws.Cells[$"A{row}"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
ws.Cells[$"A{row}"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
If you need to do something with the height of the rows you will want to look at the CustomHeight setting. This should explain it: Autofit rows in EPPlus

Put limit to vertical line on chart by software control

Actually my goal is just put limit line to my existing chart...
already solve the graph plot to my chart by c#. (plot is done by c#)
but now the problem is how to put limit vertical line to my chart.
Excel.Range chartRange;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)excelWorksheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 50, 500, 250);
Excel.Chart chartPage = myChart.Chart;
excelWorksheet.Cells[4, 1] = "";
chartRange = excelWorksheet.get_Range("A4", "DSC5"); /// whatever range is ~~~
chartPage.SetSourceData(chartRange, misValue2);
chartPage.ChartType = Excel.XlChartType.xlXYScatterSmoothNoMarkers;
above code just plot graph by c#. (working successfully,,,,
problem is after plot my chart and then i just want put vertical limit line.
i couldn't do with c# but i implemented with manually in my excel.. (use my hand with mouse lol )
above picture implement by manually..
simply select chart by mouse ==> design tab == > select data ==> add ==> put name ==> xval choose D4 cell, ==> Y val 0 ==> ok
layout tab ==> error bars ==> other option and change setting.
implement by manually is ok but i couldn't do this process with C#.
did anyone know that how to implement this with C#.
actually other way is ok to add vertical line to my chart.
i only want use D4 cell and E4 cell for range select.
please advice.

Add power regression trendline programatically to a chart via Visual Studio Tools for Office

After searching through ther internet, I cannot get the right path to be able to plot a trendline (power regression one) programatically with a chart designed in C#.
For the Moment I have :
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("mydata");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlXYScatter;
//Add a PowerRegression Line to the Chart
I beleive the only way to do it is do it by hand ( or better use the linear regression tool of Excel (which is accessible from C#) with log(data) and then plot it on the chart).
Any Alternatives?
Thanks,
You can add a trendline from vsto like this:
Excel.Series series = chart.SeriesCollection(1);
Excel.Trendlines trendlines = series.Trendlines();
tl.Add(Excel.XlTrendlineType.xlPower);
This adds a trendline to the first series of the chart. Here are all the possible trendline types from which you can choose: Trendline types.
If you would want to do a trendline that is not supported you would need to calculate the points for it and just add it as a standalone series to the chart - like you already mentioned.

Categories