我需要帮助以编程方式绘制超出单个 Excel 系列的点。
根据http://office.microsoft.com/en-us/excel/HP100738491033.aspx,Excel 2007 图表上可显示的最大点数为 256000。鉴于每个系列的上限为 32000 点,因此需要 8 个系列绘制完整的 256000 点。由于我们使用的数据集很大,我的客户需要绘制每个图表的最大点数。
我在 C#/Excel 互操作方面有中等经验,所以我认为以编程方式创建工作表然后循环遍历每组 32000 个点并将它们作为一个系列添加到图表中会很容易,当数据完全绘制或 8 个系列时停止被绘制。如果着色得当,8 系列在视觉上与单个系列无法区分。
不幸的是我在这里。我遇到的主要问题是:
(全尺寸) 您可以在二维图表的数据系列中使用的最大数据点数为 32,000... http://img14.imageshack.us/img14/9630/errormessagen.png
奇怪的是,当我执行该行时会出现此弹出窗口:
并附有:
HRESULT 异常:0x800AC472 http://img21.imageshack.us/img21/5153/exceptionb.png
在我指定要绘制的数据之前,我不明白如何生成这样的弹出/警告/异常。Excel在这里想变得聪明吗?
作为一种临时解决方法,我已将 chart.ChartType = chartType 语句放入 try-catch 块中,以便继续进行。
如下所示,我的“分块”代码按预期工作,但在尝试向图表添加数据时仍然遇到同样的问题。Excel 说我试图绘制太多点,而我显然不是。
(全尺寸图片) 带有监视窗口的代码块 http://img12.imageshack.us/img12/5360/snippet.png
我知道我可能还没有与每个系列正确关联的 X 值,但我正试图让这个工作在我更进一步之前发挥作用。
任何帮助将不胜感激。
这是完整的代码:
public void DrawScatterGraph(string xColumnLetter, string yColumnLetterStart, string yColumnLetterStop, string xAxisLabel, string yAxisLabel, string chartTitle, Microsoft.Office.Interop.Excel.XlChartType chartType, bool includeTrendline, bool includeLegend)
{
int totalRows = dataSheet.UsedRange.Rows.Count; //dataSheet is a private class variable that
//is already properly set to the worksheet
//we want to graph from
if (totalRows < 2) throw new Exception("Not generating graph for " + chartTitle.Replace('\n', ' ')
+ " because not enough data was present");
ChartObjects charts = (ChartObjects)dataSheet.ChartObjects(Type.Missing);
ChartObject chartObj = charts.Add(100, 300, 500, 300);
Chart chart = chartObj.Chart;
try { chart.ChartType = chartType; }
catch { } //i don't know why this is throwing an exception, but i'm
//going to bulldoze through this problem temporarily
if (totalRows < SizeOfSeries) //we can graph the data in a single series - yay!
{
Range xValues = dataSheet.get_Range(xColumnLetter + "2", xColumnLetter + totalRows.ToString());
Range yValues = dataSheet.get_Range(yColumnLetterStart + "1", yColumnLetterStop + totalRows.ToString());
chart.SetSourceData(yValues, XlRowCol.xlColumns);
SeriesCollection seriesCollection = (SeriesCollection)chart.SeriesCollection(Type.Missing);
foreach (Series s in seriesCollection)
{
s.XValues = xValues;
}
}
else // we need to split the data across multiple series -- this doesn't work yet
{
int startRow = 1;
while (startRow < totalRows)
{
int stopRow = (startRow + SizeOfSeries)-1;
if (stopRow > totalRows) stopRow = totalRows;
Range curRange = dataSheet.get_Range(yColumnLetterStart + startRow.ToString(), yColumnLetterStop + stopRow.ToString());
try
{
((SeriesCollection)chart.SeriesCollection(Type.Missing)).Add(curRange, XlRowCol.xlColumns,
Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception exc)
{
throw new Exception(yColumnLetterStart + startRow.ToString() + "!" + yColumnLetterStop + stopRow.ToString() + "!" + exc.Message);
}
startRow = stopRow+1;
}
}
chart.HasLegend = includeLegend;
chart.HasTitle = true;
chart.ChartTitle.Text = chartTitle;
Axis axis;
axis = (Axis)chart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
axis.HasTitle = true;
axis.AxisTitle.Text = xAxisLabel;
axis.HasMajorGridlines = false;
axis.HasMinorGridlines = false;
axis = (Axis)chart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
axis.HasTitle = true;
axis.AxisTitle.Text = yAxisLabel;
axis.HasMajorGridlines = true;
axis.HasMinorGridlines = false;
if (includeTrendline)
{
Trendlines t = (Trendlines)((Series)chart.SeriesCollection(1)).Trendlines(Type.Missing);
t.Add(XlTrendlineType.xlLinear, Type.Missing, Type.Missing, 0, 0, Type.Missing, false, false, "AutoTrendlineByChameleon");
}
chart.Location(XlChartLocation.xlLocationAsNewSheet, "Graph");
}