我一直在尝试使用数据在Excel工作表中创建图表。我的数据源是同一个工作簿。数据在表1中,我想在表2中创建图。我已经阅读了几乎所有StackOverflow线程和其他Web资源。我似乎无法找到我出错的地方。除了正确缩放轴外,图表不会显示任何内容。
我想在四个不同的线条/条形图中对B,C,D和E绘制A列(X轴)。我使用NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterShortStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
timeFormatter.dateStyle = NSDateFormatterNoStyle;
timeFormatter.timeStyle = NSDateFormatterShortStyle;
// load date format from localization files
NSString *dateFormat = @"%1$@ at %2$@"; //NSLocalizedString(@"my_date_format", @"My date format");
NSDate *date = [NSDate date];
NSString *localizedDate = [NSString stringWithFormat:dateFormat, [dateFormatter stringFromDate:date], [timeFormatter stringFromDate:date]];
NSLog(@"%@", localizedDate);
命名空间。
这是代码段。
Microsoft.Office.Interop.Excel
这是带有注释Worksheet sheet2 = workBook.Worksheets[2];
ChartObjects chart = sheet2.ChartObjects(Type.Missing);
ChartObject[] chartObj = {chart.Add(10, 10, 550, 300), chart.Add(580, 10, 550, 300), chart.Add(10, 350, 550, 300), chart.Add(580, 350, 550, 300) };
int[,] rangeVal = { { 0, 400 }, { 0, 100 }, { 0, 300 }, { 0, 5 } };
for (int col = 2; col <= colsCount; col++)
{
Chart myChart = chartObj[col-2].Chart;
chartObj[col - 2].Select();
myChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
SeriesCollection seriesCollection = myChart.SeriesCollection();
Series series1 = seriesCollection.NewSeries();
series1.Name = sheet1.Cells[1,col].Value.ToString();
series1.XValues = sheet1.get_Range("A2","A" + (rowsCount + 1).ToString());
series1.Values = sheet1.get_Range((char)((int)'A' + col - 1) + "2", (char)((int)'A' + col - 1) + (rowsCount + 1).ToString());
series1.ChartType = XlChartType.xlLine;
myChart.PlotBy = XlRowCol.xlRows;
myChart.Axes(XlAxisType.xlValue).MinimumScale = rangeVal[col-2,0];
myChart.Axes(XlAxisType.xlValue).MaximumScale = rangeVal[col-2,1];
//myChart.SetSourceData(sheet1.get_Range((char)((int)'A' + col - 1) + "2", (char)((int)'A' + col - 1) + (rowsCount + 1).ToString()));
}
声明的图表的屏幕截图:
这是启用了SetSourceData
的图表的屏幕截图:
我的数据集没有任何零值。
任何帮助将不胜感激!