Search code examples
c#wpfexcelradar-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:

enter image description here

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.


Solution

  • 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;