C# 如何手动定位 ASP.Net Chart Legend?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19279438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:35:37  来源:igfitidea点击:

How to manually position ASP.Net Chart Legend?

c#asp.netchartslegend

提问by jokeefe

Using the Chart controls built into ASP.Net, I'm trying to manually position the Title and the Legend so that they are directly next to each other horizontally just above the ChartArea. I've been able to manually position the Title using the following code:

使用 ASP.Net 中内置的图表控件,我尝试手动定位标题和图例,以便它们在 ChartArea 上方水平地直接相邻。我已经能够使用以下代码手动定位标题:

chart.Titles["Title1"].Position.Auto = false;
chart.Titles["Title1"].Position.X = 10;
chart.Titles["Title1"].Position.Y = 5;

There's nothing to it, really. However, I'm attempting to position the Legend to the right of it with the following code, and the Legend doesn't even appear:

真的没什么好说的 但是,我尝试使用以下代码将图例放置在其右侧,并且图例甚至没有出现:

chart.Legends["Legend1"].Position.Auto = false;
chart.Legends["Legend1"].Position.X = 30;
chart.Legends["Legend1"].Position.Y = 5;

Any ideas what I'm doing wrong? This seems like it should be relatively simple. I've even tried various other coordinates, and I can't get the Legend to appear anywhere. It does appear if I use the built-in positioning such as below, but this positioning does not suit my purposes:

任何想法我做错了什么?这看起来应该比较简单。我什至尝试了其他各种坐标,但我无法让 Legend 出现在任何地方。如果我使用如下所示的内置定位,它确实会出现,但这种定位不适合我的目的:

chart.Legends["Legend1"].Docking = Docking.Top;
chart.Legends["Legend1"].DockedToChartArea = "ChartArea1";
chart.Legends["Legend1"].IsDockedInsideChartArea = false;
chart.Legends["Legend1"].Alignment = StringAlignment.Far;

采纳答案by Karl Anderson

Try newing up an ElementPositionobject, like this:

尝试新建一个ElementPosition对象,如下所示:

chart.Legends["Legend1"].Position.Auto = false;
chart.Legends["Legend1"].Position = new ElementPosition(30, 5, 100, 20);

Note: The constructor for ElementPositiontakes 0 or 4 parameters (x, y, width, height).

注意: 的构造函数ElementPosition接受 0 或 4 个参数(x、y、宽度、高度)。

回答by P S

I stumbled on this question for looking how to move legend at the bottom of a chart.

我偶然发现了这个问题,以寻找如何在图表底部移动图例。

Answer for that is to use Dockingproperty

答案是使用Docking财产

Chart1.Legends["Legend1"].Docking = Docking.Bottom;

It may be helpful for someone in future, as this is the first link in google search.

将来可能对某人有帮助,因为这是谷歌搜索中的第一个链接。