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;
Related
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.
I'm currently working on building a report in c# and then outputting it to word.
I've got the chart building and being populated by data however one thing I cant get my head around is how to set the chart size / height.
any help would be appreciated, spent hours on this seemingly simple task.
CODE:
Chart CashflowChart = _document.InlineShapes.AddChart(XlChartType.xlColumnClustered).Chart;
CashflowChart.ChartArea.Height = 100;
This results in the following error :
{"Not implemented (Exception from HRESULT: 0x80004001 (E_NOTIMPL))"}
After hours of messing around and contrary to what msdn docs says you cant edit the chartarea directly, you can edit the chartobject which is the parent of the chartarea :
CashflowChart.Parent.width= 550;
CashflowChart.Parent.height = 150;
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.
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.
I am currently trying to find a way using VSTO in C# for Excel, to draw a diagonal line in a cell using C# code. But i can't find anyone on the web who even tried to do this.
Does anyone know how to achieve this ?
Thank you
(Excuse me for my bad english but it's not my language)
You can manipulate borders as follows:
Excel.Range range = ... the cell(s) you want ...;
var border = range.Borders[Excel.XlBordersIndex.xlDiagonalDown];
border.Weight = Excel.XlBorderWeight.xlThin;
border.LineStyle = Excel.XlLineStyle.xlContinuous;
The XlBordersIndex enumeration specifies which border you want to update:
xlDiagonalDown
xlDiagonalUp
xlEdgeBottom
xlEdgeLeft
xlEdgeRight
xlEdgeTop
...
I do not how in VSTO, but using COM you can do something like this:
ActiveSheet.Shapes.AddLine(BeginX, BeginY, EndX, EndY);