Put limit to vertical line on chart by software control - c#

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.

Related

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;

Chart control width with many series

I have this chart with some series in legend and this chart is docking fill in panel. The legend has these properties:
chart1.Legends.Add(seriesName);
chart1.Legends[0].Docking = Docking.Bottom;
chart1.Legends[0].TableStyle = LegendTableStyle.Wide;
chart1.Legends[0].BorderColor = Color.CornflowerBlue;
chart1.Legends[0].BorderDashStyle = ChartDashStyle.Dash;
chart1.Legends[0].BorderWidth = 1;
Chart with only a few series:
With even more series but the same interval date I have this result:
The issue is not the scale of the data but the reduced size of the ChartArea itself.. - How can I fix this?
The reason is in this line:
chart1.Legends.Add(seriesName);
What it does is adding one totally empty Legend, most likely for each of the Series you add. It is placed at the default position, that is to the right. And if you get enough of them they push the ChartArea back to the left..
Simply remove the line, as all Series get added to the default Legend anyway. It is only that default Legend your next lines style and position docked to the bottom.
To demostrate the effect you can add a LegendItem to each:
Legend l = chart1.Legends.Add(chart1.Legends.Count + ""));
l.CustomItems.Add(Color.HotPink, chart1.Legends.Count + " *");
Result:
As you can see even very few extra Legends push the ChartArea way to the left. Yours are empty, so they don't take as musch room, but still..

drawing an excel chart from 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;

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.

Combine bar chart and line chart in excel using C#

I am able to create bar chart and displaying strip line through MS Chart below snap:
Same chart I am trying to create in C# at run time feel like to export the chart in excel. Can anyone guide me how can draw the Strip line on bar chart OR can combine bar chart line chart so line chart seems displaying the strip line.
one possible approach is to add a new series like this:
add a new series to the bar chart, using 2 cells equal to the value where you want to have vertical line (x column in illustration)
select the new series bars and change chart type to X Y (Scatter) - the one with both markers and lines so you can then select it again
edit data range for the new series, so x-values point to your value and y-values to 0 and 1
change the right axis to Minimum: Fixed 0,0, Maximum: Fixed 1,0 and change major tick lines and axis labels to None
remove markers and change line style

Categories