如何在epplus中的Excel中将图表图例位置设置为左上角
How to set graph Legend position to top left in Excel in epplus
我需要将图表的图例位置设置为左上角。但无法设置,因为 eLegendPosition 上不存在 属性。
var myChart = myWorksheet.Drawings.AddChart("Chart2", eChartType.ColumnClustered) as ExcelBarChart;
**Adding an series to chart**
var serie2a = myChart.Series.Add(myWorksheet.Cells[2, 2, dataRow, 2], myWorksheet.Cells[2, 1, dataRow, 1]);
//serie2a.Header = myWorksheet.Cells[1, 2].Value.ToString();
serie2a.Header = "Plan";
myChart.YAxis.Title.Text = "Daily MH %";
myChart.YAxis.Title.Font.Size = 8;
chart2b.UseSecondaryAxis = true; //Flip the axes
chart5e.UseSecondaryAxis = true; //Flip the axes
chart2b.YAxis.Title.Text = "Cumul Daily MH %";
chart2b.YAxis.Title.Font.Size = 8;
**Here I am Facing an issue while setting it to TopLeft**
myChart.Legend.Position = eLegendPosition.Bottom;
myChart.Border.Fill.Color = System.Drawing.Color.Green;
myChart.Title.Text = "OVERALL S CURVE";
myChart.Title.Font.Size = 10;
myChart.SetSize(800, 400);
// Add to 7th row and to the 7th column
myChart.SetPosition(7, 0, 7, 0);
Epplus 的枚举与 excel 匹配,因此单独设置不会满足您的需要。但是您可以使用 ManualLayout
节点在 XML 中手动设置它:
与 How to add to Text Label For excel Charts using Open xml or EPPLUS 中的做法类似。
这里有一个辅助方法可以使它更简洁:
/// <summary>
/// Sets the chart's layout node <c>c:chartSpace/c:chart/c:legend/c:layout</c> for the
/// passed <see cref="ExcelBarChart"/>.
/// </summary>
/// <param name="chart">Chart to set </param>
/// <param name="x">Position X Value (percentage).</param>
/// <param name="y">Position Y Value (percentage).</param>
/// <param name="culture">Culture to use when converting the doubles to string based on excel settings.</param>
public static void SetChartManualLayout(this ExcelBarChart chart, double x, double y, CultureInfo culture)
{
//Set layout via xml
var chartXml = chart.ChartXml;
var nsm = new XmlNamespaceManager(chartXml.NameTable);
var nsuri = chartXml.DocumentElement.NamespaceURI;
nsm.AddNamespace("c", nsuri);
nsm.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
//Get the title layout and add the manual section
var layoutNode = chartXml.SelectSingleNode("c:chartSpace/c:chart/c:legend/c:layout", nsm);
var manualLayoutNode = chartXml.CreateElement("c:manualLayout", nsuri);
layoutNode.AppendChild(manualLayoutNode);
//Add coordinates
var xModeNode = chartXml.CreateElement("c:xMode", nsuri);
var attrib = chartXml.CreateAttribute("val");
attrib.Value = "edge";
xModeNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(xModeNode);
var yModeNode = chartXml.CreateElement("c:yMode", nsuri);
attrib = chartXml.CreateAttribute("val");
attrib.Value = "edge";
yModeNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(yModeNode);
var xNode = chartXml.CreateElement("c:x", nsuri);
attrib = chartXml.CreateAttribute("val");
attrib.Value = x.ToString(culture);
xNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(xNode);
var yNode = chartXml.CreateElement("c:y", nsuri);
attrib = chartXml.CreateAttribute("val");
attrib.Value = x.ToString(culture);
yNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(yNode);
}
要在您的代码中使用它,只需执行以下操作:
// Add to 7th row and to the 7th column
myChart.SetPosition(7, 0, 7, 0);
myChart.Legend.Position = eLegendPosition.Left;
myChart.SetChartManualLayout(0.015, 0.015, CultureInfo.CurrentCulture);
以百分比形式提供 X 和 Y。 0,0 会把它放在左上角,1,1 会把它放在右下角。此外,如果 Excel 的设置与您的应用不同,请务必考虑 Culture
。
我需要将图表的图例位置设置为左上角。但无法设置,因为 eLegendPosition 上不存在 属性。
var myChart = myWorksheet.Drawings.AddChart("Chart2", eChartType.ColumnClustered) as ExcelBarChart;
**Adding an series to chart**
var serie2a = myChart.Series.Add(myWorksheet.Cells[2, 2, dataRow, 2], myWorksheet.Cells[2, 1, dataRow, 1]);
//serie2a.Header = myWorksheet.Cells[1, 2].Value.ToString();
serie2a.Header = "Plan";
myChart.YAxis.Title.Text = "Daily MH %";
myChart.YAxis.Title.Font.Size = 8;
chart2b.UseSecondaryAxis = true; //Flip the axes
chart5e.UseSecondaryAxis = true; //Flip the axes
chart2b.YAxis.Title.Text = "Cumul Daily MH %";
chart2b.YAxis.Title.Font.Size = 8;
**Here I am Facing an issue while setting it to TopLeft**
myChart.Legend.Position = eLegendPosition.Bottom;
myChart.Border.Fill.Color = System.Drawing.Color.Green;
myChart.Title.Text = "OVERALL S CURVE";
myChart.Title.Font.Size = 10;
myChart.SetSize(800, 400);
// Add to 7th row and to the 7th column
myChart.SetPosition(7, 0, 7, 0);
Epplus 的枚举与 excel 匹配,因此单独设置不会满足您的需要。但是您可以使用 ManualLayout
节点在 XML 中手动设置它:
与 How to add to Text Label For excel Charts using Open xml or EPPLUS 中的做法类似。
这里有一个辅助方法可以使它更简洁:
/// <summary>
/// Sets the chart's layout node <c>c:chartSpace/c:chart/c:legend/c:layout</c> for the
/// passed <see cref="ExcelBarChart"/>.
/// </summary>
/// <param name="chart">Chart to set </param>
/// <param name="x">Position X Value (percentage).</param>
/// <param name="y">Position Y Value (percentage).</param>
/// <param name="culture">Culture to use when converting the doubles to string based on excel settings.</param>
public static void SetChartManualLayout(this ExcelBarChart chart, double x, double y, CultureInfo culture)
{
//Set layout via xml
var chartXml = chart.ChartXml;
var nsm = new XmlNamespaceManager(chartXml.NameTable);
var nsuri = chartXml.DocumentElement.NamespaceURI;
nsm.AddNamespace("c", nsuri);
nsm.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
//Get the title layout and add the manual section
var layoutNode = chartXml.SelectSingleNode("c:chartSpace/c:chart/c:legend/c:layout", nsm);
var manualLayoutNode = chartXml.CreateElement("c:manualLayout", nsuri);
layoutNode.AppendChild(manualLayoutNode);
//Add coordinates
var xModeNode = chartXml.CreateElement("c:xMode", nsuri);
var attrib = chartXml.CreateAttribute("val");
attrib.Value = "edge";
xModeNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(xModeNode);
var yModeNode = chartXml.CreateElement("c:yMode", nsuri);
attrib = chartXml.CreateAttribute("val");
attrib.Value = "edge";
yModeNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(yModeNode);
var xNode = chartXml.CreateElement("c:x", nsuri);
attrib = chartXml.CreateAttribute("val");
attrib.Value = x.ToString(culture);
xNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(xNode);
var yNode = chartXml.CreateElement("c:y", nsuri);
attrib = chartXml.CreateAttribute("val");
attrib.Value = x.ToString(culture);
yNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(yNode);
}
要在您的代码中使用它,只需执行以下操作:
// Add to 7th row and to the 7th column
myChart.SetPosition(7, 0, 7, 0);
myChart.Legend.Position = eLegendPosition.Left;
myChart.SetChartManualLayout(0.015, 0.015, CultureInfo.CurrentCulture);
以百分比形式提供 X 和 Y。 0,0 会把它放在左上角,1,1 会把它放在右下角。此外,如果 Excel 的设置与您的应用不同,请务必考虑 Culture
。