How can I set the values of a chart series in SpreadsheetGear programmatically?
According to MSDN, Excel permits setting the Values property of a chart series programmatically from an array:
Charts("Chart1").SeriesCollection(1).Values = _ Array(1, 3, 5, 7, 11, 13, 17, 19)
How do I do this with a SpreadsheetGear chart? In my particular problem, referring to a cell range i开发者_C百科s not feasible. I have tried to set the Values to a list of doubles and to a string of the format "1, 3, 5, ..., 19". Neither of those ways works, and SpreadsheetGear's documentation gives no examples of the expected format.
You can use a formula consisting of an array of constant values for a chart series. You’ll need to format the string passed into ISeries.Values as follows: “={1,2,3,4}”. Here is a more complete example that replaces the first series in a chart, assuming you have a chart name “Chart 1” on worksheet “Sheet1” in the opened workbook:
IWorkbook workbook = Factory.GetWorkbook(@"C:\chart.xlsx");
SpreadsheetGear.Charts.IChart chart = workbook.Worksheets["Sheet1"].Shapes["Chart 1"].Chart;
ISeries series1 = chart.SeriesCollection[0];
series1.Values = "={10,20,30,40,50,60}";
workbook.Save();
A couple additional notes. For the best compatibility with Excel, you will need to use the Open XML (XLSX/XLSM) file format. Excel has no problem reading in the file above after it has been saved in XLSX/XLSM. However, Excel will convert this series formula to a static “data cache” and drop the series formula above. SpreadsheetGear has limited support for this “data cache” (we read it in but don’t write it out to the file format), so depending on how much interacting and saving you’ll be doing between SpreadsheetGear and Excel after you set the series formula above, you may run into these limitations upon opening this workbook in SpreadsheetGear after several passes between SpreadsheetGear and Excel. If you are simply using this workbook for reporting purposes, you should not run into any problems whatsoever.
精彩评论