我有堆叠条形图,因为我想显示每个标签的百分比(PDD1 标签为 80%)。
所以我添加了百分比记录,但无法从图表中隐藏并显示在每个标签区域的顶部中间。
我尝试了一些文章解决方案(https://stackoverflow.com/a/9036445/11010986),但我在 OfficeOpenXml.Drawing.Chart 中看不到 LoadEntries 选项
chart1.Legend.LoadEntries(chart1.Legend.LegendEntries().Count).Delete()
我看到了条目选项,但它是只读的。
C# 代码块:
dynamic DataList5 = new
{
label = new List<string>() { "PDD1", "PDD2", "PDD3" },
modeler = new { Category1 = new List<int>() { 1, 1, 1 }, Category2 = new List<int>() { 0, 0, 0 } },
tester = new { Category1 = new List<int>() { 2, 2, 2 }, Category2 = new List<int>() { 2, 2, 2 } },
percentage = new List<int>() { 150, 200, 10 }
};
if (DataList5.label.Count > 0)
{
int j = 0;
for (int i = 0; i < DataList5.label.Count * 2; i++)
{
if (i % 2 == 0)
{
ws2.Cells[i + 2, 2].Value = DataList5.label[j];
ws2.Cells[i + 2, 3].Value = "Category 1";
ws2.Cells[i + 2, 4].Value = DataList5.modeler.Category1[j];
ws2.Cells[i + 2, 5].Value = DataList5.tester.Category1[j];
ws2.Cells[i + 2, 6].Value = DataList5.percentage[j];
}
else
{
ws2.Cells[i + 2, 3].Value = "Category 2";
ws2.Cells[i + 2, 4].Value = DataList5.modeler.Category2[j];
ws2.Cells[i + 2, 5].Value = DataList5.tester.Category2[j];
j++;
}
}
}
else
{
ws2.Cells[2, 2].Value = "No Data Found";
}
var chart1 = ws1.Drawings.AddChart("chart1", eChartType.ColumnStacked);
chart1.SetPosition(1, 0, 0, 0);
chart1.SetSize(1800, 400);
chart1.Style = eChartStyle.Style12;
if (DataList5.label.Count > 0)
{
var series1 = chart1.Series.Add(ws2.Cells["D2:D" + (DataList5.label.Count * 2 + 1)], ws2.Cells["B2:C" + (DataList5.label.Count * 2 + 1)]);
var series2 = chart1.Series.Add(ws2.Cells["E2:E" + (DataList5.label.Count * 2 + 1)], ws2.Cells["B2:C" + (DataList5.label.Count * 2 + 1)]);
//percentage series
//var series3 = chart1.Series.Add(ws2.Cells["F2:F" + (DataList5.label.Count * 2 + 1)], ws2.Cells["B2:C" + (DataList5.label.Count * 2 + 1)]);
series1.Fill.Color = Color.FromArgb(255, 0, 112, 192);
series2.Fill.Color = Color.FromArgb(255, 239, 125, 49);
//Show percentage for each PDD1 label
////chart1.Legend.LoadEntries(chart1.Legend.LegendEntries().Count).Delete();
series1.Header = "Modeler";
series2.Header = "Tester";
//series3.Header = "Percentage";
}
else
{
var series1 = chart1.Series.Add(ws2.Cells["B2:C" + (DataList1.label.Count + 1)], ws2.Cells["B2:C" + (DataList1.label.Count + 1)]);
}
chart1.Title.Text = "Stacked bar chart example";
这里显示百分比对我来说是个问题。
甚至 PDD1 也应该位于前 2 个小节之间,但这不是这里主要关心的问题。
有人知道 C# EPPlus 或 OfficeOpenXML 中的解决方案吗?