简体   繁体   中英

Reverting X and Y axes of a chart from C#

i'm creating a line chart from a dictionary values. Key is a long date time string and the value is a double value. So excel looks something like this:

9:22:39 AM 3.41

9:22:40 AM 3.91

9:22:41 AM 7.81

9:22:42 AM 2.44

9:22:43 AM 1.56

When I create the chart, time is on the Y axis and double values on the X axis. I want doubles to be the Y axis and time the X axis.

Excel.ChartObjects xlCharts = (Excel.ChartObjects)newWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject chart = (Excel.ChartObject)xlCharts.Add(10, 80, 300,250);             
Excel.Chart chartPage = chart.Chart;

Excel.Range chartRange = newWorkSheet.get_Range("B1", "A" + row);
chartPage.SetSourceData(chartRange, Type.Missing);
chartPage.ChartType = Excel.XlChartType.xlLine;
chartPage.HasLegend = false;

In SetSourceData I tried both using columns and rows (the second argument), however it doesn't solve the problem. Any ideas how can I fix this?

Let's say the time is B1:B5 and the double values is A1:A5 , please try this code.

Excel.ChartObjects xlCharts = (Excel.ChartObjects)newWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject chart = (Excel.ChartObject)xlCharts.Add(10, 80, 300,250);             
Excel.Chart chartPage = chart.Chart;

Excel.Range chartRange = newWorkSheet.get_Range("A1:A5");
chartPage.SetSourceData(chartRange, Type.Missing);
chartPage.ChartType = Excel.XlChartType.xlLine;
chartPage.HasLegend = false;

Series series = (Series)(chart.Chart.SeriesCollection(1));
series.XValues = newWorkSheet.get_Range("B1:B5");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM