简体   繁体   English

从C#恢复图表的X和Y轴

[英]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: 所以excel看起来像这样:

9:22:39 AM 3.41 3.41上午9:22:39

9:22:40 AM 3.91 上午9:22:40 3.91

9:22:41 AM 7.81 上午9:22:41 7.81

9:22:42 AM 2.44 上午9:22:42 2.44

9:22:43 AM 1.56 1.56:9:22:43

When I create the chart, time is on the Y axis and double values on the X axis. 创建图表时,时间在Y轴上,而双精度值在X轴上。 I want doubles to be the Y axis and time the X axis. 我希望将double用作Y轴并将X轴作为时间。

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. 在SetSourceData中,我尝试同时使用列和行(第二个参数),但是并不能解决问题。 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. 假设时间为B1:B5 ,双精度值为A1:A5 ,请尝试以下代码。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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