C#Excel图表x轴(类别)标签 [英] C# Excel Chart x-axis (category) labels

查看:132
本文介绍了C#Excel图表x轴(类别)标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候。这已经困扰了我好几个小时!我正在从单元格中的数据创建一个excel图表对象。

数据看起来像这样:

时间|温度

22:35:22 | 50

22:35:23 | 60

22:35:24 | 70

22:35:25 | 65

22:35:26 | 55

22:35:27 | 45

22:35:28 | 50

22:35:29 | 55

22:35:30 | 65

22:35:31 | 60



当我在c#中创建图表对象时,我无法弄清楚如何在Excel应用程序中选择数据时指定类别(x轴)标签对于图表,您可以选择正确的列来选择x轴标签(图片:此处)。

无论何时创建图表,默认的x轴标签都是1,2,3等,但我希望它是A列中的时间戳。



这是我正在使用的代码:

Greetings. This has been bugging me for hours! I am creating a excel chart object from data in cells.
Data looks like this:
Time | Temp
22:35:22 | 50
22:35:23 | 60
22:35:24 | 70
22:35:25 | 65
22:35:26 | 55
22:35:27 | 45
22:35:28 | 50
22:35:29 | 55
22:35:30 | 65
22:35:31 | 60

When I create the chart object in c#, I cannot figure out how to specify the category (x-axis) labels like when you select data in the Excel app for a chart, you have the right column to select the x-axis labels (picture: here).
Whenever the chart is created, the default x-axis labels are 1,2,3,etc., but I want it to be the timestamp in column A.

Here's the code I'm using:

var excel = new Excel.Application();
excel.Workbooks.Add();

Excel._Worksheet sheet = excel.ActiveSheet;

var labels = new List<string>();
int x = 1;
foreach (string line in listBox1.Items)
{
    string[] values = line.Split(',');
    ((Excel.Range)sheet.Cells[x, "A"]).NumberFormat = "HH:MM:SS";
    ((Excel.Range)sheet.Cells[x, "A"]).Value = ConvertFromUnixTimestamp(Convert.ToDouble(values[0])).ToLocalTime();
    sheet.Cells[x, "B"] = values[1];
    sheet.Cells[x, "C"] = values[2];
    labels.Add(ConvertFromUnixTimestamp(Convert.ToDouble(values[0])).ToLocalTime().ToString());
    x++;
}
sheet.Columns[1].AutoFit();
sheet.Columns[2].AutoFit();
sheet.Columns[3].AutoFit();

var charts = sheet.ChartObjects() as Excel.ChartObjects;
var chartObject = charts.Add(60, 10, 300, 300) as Excel.ChartObject;
var chart = chartObject.Chart;

var range = sheet.get_Range("C1", "C"+x.ToString());

Excel.Axis xAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
xAxis.HasTitle = true;
xAxis.AxisTitle.Caption = "Time";
xAxis.CategoryNames = (Excel.Range)sheet.get_Range("A1", "A" + x.ToString());

chart.SetSourceData(range);

chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLineMarkers;
chart.ChartWizard(Source: range,
    Title: "Temperature Log",
    CategoryTitle: "Time",
    ValueTitle: "Temp");


sheet.SaveAs(saver.FileName);

excel.Quit();





请帮忙!非常烦我!

谢谢,

Sam



Please help! Extremely bugging me!
Thank you,
Sam

推荐答案

包括您通过的范围内的两列到SetSourceData,并确保左上角的单元格为空(删除时间标签)。 Excel将按您希望的方式解析范围,因此您无需指定X或Y值。
Include both columns in the range that you pass to SetSourceData, and make sure the top left cell is blank (remove the "Time" label). Excel will parse the range the way you want, so you don't need to specify X or Y values.


将整个第一行留空是很奇怪。那些(第一列除外)应该用作图表中的系列名称(图例条目)。



也许现在轴代码消失了,你可以再试一次只有第一个单元格空白。
Weird about leaving the entire first row blank. Those (except for the first column) should be used as series names (legend entries) in the chart.

Maybe now with the axis code gone, you could try again with only the first cell blank.


这篇关于C#Excel图表x轴(类别)标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆