سؤال

I have legend text of varying length and need to align the legend items to the left of the legend for a consistent layout.

|         My Legend         |

|     X what I have now     |

|  X what I have now long   |   -->  causes irregular layout

| X what I need             |

| X what I need long        |   --> nice, regular layout

Must be something obvious but have been looking at this for hours and do not seem to be any closer to a working example. Thanks in advance for your help!

EDIT:

I am trying to produce a pie-chart so have multiple series, each of which will need the series symbol and the appropriate series datapoint text, as is the case in the default legend layout. My legend creation method:

public Legend CreateLegend()
{
    var legend = new Legend();

    legend.Enabled = true;
    legend.Font = new Font("Arial", 11F);
    legend.ForeColor = Color.FromArgb(102, 102, 102);
    legend.InsideChartArea = "Result Chart";

    legend.Position = new ElementPosition(50, 20, 50, height);

    legend.LegendStyle = LegendStyle.Column;

    return legend;
}

And my series creation method (which currently takes the legend as a parameter from my experiments/ideas for a solution here):

public Series CreateSeries(List<ChartDivision> series, Legend legend)
{
    var seriesDetail = new Series();
    seriesDetail.Name = "Result Chart";
    seriesDetail.IsValueShownAsLabel = false;
    seriesDetail.ChartType = SeriesChartType.Pie;
    seriesDetail.BorderWidth = 2;

    foreach(var datapoint in series)
    {
        var p = seriesDetail.Points.Add(datapoint.Logged);
        p.LegendText = datapoint.Name;                
    }

    seriesDetail.ChartArea = "Result Chart";
    return seriesDetail;
}
هل كانت مفيدة؟

المحلول

Speaking of the System.Windows.Forms.DataVisualization.Charting.Chart here. This is in fact default behavior.

But you can override it: Select the chart in the designer, click on the Legends-property. In the appropriate legend, edit CellColumns-Property. This is by default empty. You can add two columns and set the first one to "ColumnType=SeriesSymbol" to get the default with custom columns. Then, on the second column, the alignment property (by default on MiddleCenter) should be what you are looking for.


So here is my test program: http://pastebin.com/ZTwMhsXB Add a chart control and two buttons to the form and run it. However, I was unable to reproduce your problem.

Note that I did some more wiring. Can you please confirm that you have these lines somewhere:

chart1.Legends.Add(leg);
// and
legend.Name = // whatever
seriesDetail.Legend = legend.Name;

Because else it may be that you are not seeing your created legend but the default one. If you don't add the legend to the chartarea, it is invisible.


Okay, so we are now at the point that the text is left-justified within its column, bit the columns are always centered within the legend-area. See this SO thread for the solution: How to control winform mschart legend text alignment c#?

Edit the code like this:

legend.LegendStyle = LegendStyle.Column;
legend.CellColumns.Add(new LegendCellColumn() {
    ColumnType = LegendCellColumnType.SeriesSymbol,
    MinimumWidth = 250,
    MaximumWidth = 250
});
legend.CellColumns.Add(new LegendCellColumn()
{
    ColumnType = LegendCellColumnType.Text,
    Alignment = ContentAlignment.MiddleLeft,
    MinimumWidth = 1500,
    MaximumWidth = 1500
});

Note that the numbers 250 and 1500 are percentages of the font size therefore 1500 means 15-times the font height. I have updated the pastebin accordingly ( http://pastebin.com/GGCZGWF9 ) and here is a screenshot of my sample program:

Screenshot of my sample program

نصائح أخرى

Try something like this:

myChart.Legends["MySeries Name"].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.Text, "MySeries Name"));
myChart.Legends["MySeries Name"].CellColumns[1].Alignment = ContentAlignment.MiddleLeft;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top