我有使用 Npoi 包生成 Excel 文件的代码,其中 Excel 工作表上的数据创建柱形图。我的柱形图中有 4 列,默认情况下所有列的颜色均为蓝色。我想为柱形图中的每个列设置自定义颜色,即我想要 4 列有 4 种不同的颜色。 如何使用 NPOI 包来实现这一点?
using NPOI.SS.UserModel;
using NPOI.SS.UserModel.Charts;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using System;
using System.IO;
namespace ColumnChart
{
class Program
{
const int NUM_OF_ROWS = 4;
const int NUM_OF_COLUMNS = 2;
private static void CreateChart(ISheet sheet, IDrawing drawing, IClientAnchor anchor, string serieTitle, int startDataRow, int endDataRow, int columnIndex)
{
XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor);
IColumnChartData<string, double> columnChartData = chart.ChartDataFactory.CreateColumnChartData<string, double>();
IChartLegend legend = chart.GetOrCreateLegend();
legend.Position = LegendPosition.Bottom;
IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
bottomAxis.MajorTickMark = AxisTickMark.None;
IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);
leftAxis.Crosses = AxisCrosses.AutoZero;
leftAxis.SetCrossBetween(AxisCrossBetween.Between);
IChartDataSource<string> categoryAxis = DataSources.FromStringCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, 0, 0));
IChartDataSource<double> valueAxis = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, columnIndex, columnIndex));
var serie = columnChartData.AddSeries(categoryAxis, valueAxis);
serie.SetTitle("Categories of Risk Associated");
chart.Plot(columnChartData, bottomAxis, leftAxis);
}
static void Main(string[] args)
{
using (IWorkbook wb = new XSSFWorkbook())
{
ISheet sheet = wb.CreateSheet();
IRow row;
ICell cell;
for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++)
{
row = sheet.CreateRow((short)rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
{
cell = row.CreateCell((short)colIndex);
if (colIndex == 0)
cell.SetCellValue("X" + rowIndex);
else
{
var x = colIndex * (rowIndex + 1);
cell.SetCellValue(x * x + 2 * x + 1);
}
}
}
XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
XSSFClientAnchor anchor = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, 3, 3, 10, 12);
CreateChart(sheet, drawing, anchor, "s1", 0, 9, 1);
using (FileStream fs = File.Create("C:\\Users\\lenovo\\Downloads\\columnChart.xlsx"))
{
wb.Write(fs, false);
}
Console.WriteLine("Done!");
Console.ReadLine();
}
}
}
}
尝试在
chart.Plot(columnChartData, bottomAxis, leftAxis);
后添加以下代码
chart.Plot(columnChartData, bottomAxis, leftAxis);
var plotaArea = chart.GetCTChart().plotArea;
plotaArea.barChart[0].ser[0].dPt = new List<NPOI.OpenXmlFormats.Dml.Chart.CT_DPt>();
for(int index = 0; index < endDataRow - startDataRow; index++)
{
var dpt = new NPOI.OpenXmlFormats.Dml.Chart.CT_DPt();
dpt.idx = new NPOI.OpenXmlFormats.Dml.Chart.CT_UnsignedInt();
dpt.idx.val = (uint)index;
dpt.spPr = new NPOI.OpenXmlFormats.Dml.Chart.CT_ShapeProperties();
var solidFill = dpt.spPr.AddNewSolidFill();
var color = solidFill.AddNewSrgbClr();
// random color
color.val = new byte[] { (byte)(125 + index*5), (byte)(50 + index * 8), (byte)(100 + index * 11) };
plotaArea.barChart[0].ser[0].dPt.Add(dpt);
}
随机颜色的结果是
您需要将逻辑添加到
for
循环中,并根据当前 color.val
设置 dataRow
。
System.Drawing.Color
可以帮助你。您可以这样设置值:
color.val = new byte[] { Color.Red.R, Color.Red.G, Color.Red.B }