Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually position ASP.Net Chart Legend?

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:

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:

chart.Legends["Legend1"].Docking = Docking.Top;
chart.Legends["Legend1"].DockedToChartArea = "ChartArea1";
chart.Legends["Legend1"].IsDockedInsideChartArea = false;
chart.Legends["Legend1"].Alignment = StringAlignment.Far;
like image 615
jokeefe Avatar asked Oct 09 '13 18:10

jokeefe


People also ask

How do I change the position of my legend in a graph?

Click the chart, and then click the Chart Design tab. Click Add Chart Element > Legend. To change the position of the legend, choose Right, Top, Left, or Bottom. To change the format of the legend, click More Legend Options, and then make the format changes that you want.

Where is a chart's legend usually displayed?

A Legend is a representation of legend keys or entries on the plotted area of a chart or graph, which are linked to the data table of the chart or graph. By default, it may show on the bottom or right side of the chart.

What is a chart legend?

A chart legend appears by default when you first create a chart. For most charts, legends show the names and colors of each series of data. The legend text is taken from the chart's data range.


2 Answers

Try newing up an ElementPosition object, like this:

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

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

like image 159
Karl Anderson Avatar answered Oct 16 '22 23:10

Karl Anderson


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

Answer for that is to use Docking property

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

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

like image 7
P S Avatar answered Oct 17 '22 00:10

P S