有没有更简单的方法来改变系列的颜色?
我试过用这个。但是serieNode
正在产生一个NullReferenceException
at serieNode.AppendChild(spPr);
。
这是生成图表的代码:
private void GenerateLicenseUsageStatsChart(FileInfo excelFileInfo, FileInfo
csvFileInfo, DateTime lastCheckedDate)
{
string worksheetsName = "Sheet1";
const bool firstRowIsHeader = false;
var excelTextFormat = new ExcelTextFormat { Delimiter = ',' };
// excelTextFormat.EOL = "\r";
using (ExcelPackage package = new ExcelPackage(excelFileInfo))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromText(csvFileInfo, excelTextFormat,
OfficeOpenXml.Table.TableStyles.None, firstRowIsHeader);
var workbook = package.Workbook;
ExcelWorksheet workSheet = workbook.Worksheets[1];
var chart = workSheet.Drawings.AddChart("chart",
eChartType.ColumnClustered);
chart.SetPosition(10, 250);
chart.SetSize(700, 500);
chart.Title.Text = $"LicenseUsageStats {lastCheckedDate:MMM}
{lastCheckedDate.Year}";
chart.YAxis.MajorUnit = 1;
chart.YAxis.MinorUnit = 1;
chart.XAxis.MajorTickMark = eAxisTickMark.None;
chart.XAxis.MinorTickMark = eAxisTickMark.None;
chart.XAxis.Title.Text = "Hour";
chart.XAxis.Title.Font.Size = 10;
var maxLicensesSerie = chart.Series.Add("B2:B25", "A2:A25");
maxLicensesSerie.Header = "Max Licenses Used";
var avgLicensesSerie = chart.Series.Add("C2:C25", "A2: A25");
avgLicensesSerie.Header = "Avg Licenses Used";
var maxLineChart = (ExcelLineChart)
chart.PlotArea.ChartTypes.Add(eChartType.Line);
var maxThreshLineSerie = maxLineChart.Series.Add("D2:D25",
"A2:A25");
maxThreshLineSerie.Header = "Max";
SetLineChartColor(maxLineChart, 0, Color.Red);
var warningLineChart =
(ExcelLineChart)chart.PlotArea.ChartTypes.Add(eChartType.Line);
var warningThreshLineSerie =
warningLineChart.Series.Add("E2:E25", "A2:A25");
warningThreshLineSerie.Header = "Warning";
SetLineChartColor(warningLineChart, 1, Color.Yellow);
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
// workSheet.Column(4).Hidden = true;
// workSheet.Column(5).Hidden = true;
package.Save();
}
}
由于 excel 没有创建水平线的简洁方法,我创建了最大和警告列。我也想隐藏这些列,而不影响图表。也许我可以通过将它们移动到另一张纸来“隐藏”它们?