gpt4 book ai didi

c# - EPPlus 图表 : how to plot vertical bar graph instead of the horizontal default

转载 作者:行者123 更新时间:2023-11-30 14:28:55 24 4
gpt4 key购买 nike

我遇到了 EPPlus (4.0.1) 的问题。我的条形图是水平绘制的,但我希望它是垂直的。我正在尝试更改“chart.Direction”属性,但出现错误,因为它是只读的。我也没有看到在构造函数中为我的“ExcelBarChart”变量设置变量的方法。抱歉,如果这是一个基本问题,我正在阅读文档但无法弄清楚。据我所知,chart.Direction 甚至可能不是正确的属性。我正在用 C# 编程。提前感谢任何答案。

OfficeOpenXml.Drawing.Chart.ExcelBarChart chart = (OfficeOpenXml.Drawing.Chart.ExcelBarChart)ws.Drawings.AddChart("barChart", OfficeOpenXml.Drawing.Chart.eChartType.BarClustered);
chart.SetSize(widthPx, heightPx);
chart.SetPosition(startTopPx, startLeftPx);
chart.Title.Text = "Clustered Bar Graph Report";
chart.Direction = eDirection.Column; // error: Property or indexer 'OfficeOpenXml.Drawing.Chart.ExcelBarChart.Direction' cannot be assigned to -- it is read only

ws.Cells["A1"].LoadFromDataTable(data, true); // load dataTable into Cells
int fromRow = 2;
int toRow = 2;
int fromCol = 2;
int toCol = 5;

int xRow = 2;
int xCol = 1;
chart.Series.Add(ExcelRange.GetAddress(fromRow, fromCol, toRow, toCol),
ExcelRange.GetAddress(xRow, xCol));

最佳答案

我认为您想使用 eChartType.ColumnClustered 而不是 eChartType.BarClustered 因为方向是在构建时设置的(在源代码中达到峰值)。像这样:

[TestMethod]
public void Vertical_Bar_Chart()
{
var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
if (existingFile.Exists)
existingFile.Delete();

using (var package = new ExcelPackage(existingFile))
{
var workbook = package.Workbook;
var ws = workbook.Worksheets.Add("newsheet");

//Some data
ws.Cells["A12"].Value = "wer";
ws.Cells["A13"].Value = "sdf";
ws.Cells["A14"].Value = "wer";
ws.Cells["A15"].Value = "ghgh";

ws.Cells["B12"].Value = 53;
ws.Cells["B13"].Value = 36;
ws.Cells["B14"].Value = 43;
ws.Cells["B15"].Value = 86;

//Create the chart
var chart = (ExcelBarChart)ws.Drawings.AddChart("barChart", eChartType.ColumnClustered);
chart.SetSize(300 ,300);
chart.SetPosition(10,10);
chart.Title.Text = "Clustered Bar Graph Report";
//chart.Direction = eDirection.Column; // error: Property or indexer 'OfficeOpenXml.Drawing.Chart.ExcelBarChart.Direction' cannot be assigned to -- it is read only

chart.Series.Add(ExcelRange.GetAddress(12, 2, 15, 2), ExcelRange.GetAddress(12, 1, 15, 1));

package.Save();

}
}

关于c# - EPPlus 图表 : how to plot vertical bar graph instead of the horizontal default,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27824215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com