0

Im using classic asp.net charting to present some data then placing it into a PDF. A problem I cant seem to solve is when I add the legends to the right hand side of the graph it gets pushed down. Can anyone see why?

enter image description here

        _chart.Height = _chartHeight;
    _chart.Width = _chartWidth;

    Legend legend = new Legend("Default");
    legend.LegendStyle = LegendStyle.Column;
    legend.IsTextAutoFit = false;
    legend.Docking = Docking.Right;
    legend.Alignment = System.Drawing.StringAlignment.Near;

    _chart.Legends.Add(legend);

    Series series = new Series("Default");
    _chart.Series.Add(series);

    ChartArea chartArea = new ChartArea("ChartArea1");
    //chartArea.Position.Y = 0;
    _chart.ChartAreas.Add(chartArea);
    //-- this data would usually be collected from _travelRepository.TravelModesForTheYear 
    //-- but for speed I've hardcoded as its same for everyone in country 
    _chart.Series["Default"].Points.DataBindXY(xValues, yValues);

    System.Drawing.Color[] colours = new System.Drawing.Color[] { _orange, _yellow, _pink, _red, _green, _purple };

    int i = 0;
    foreach (System.Web.UI.DataVisualization.Charting.DataPoint point in _chart.Series["Default"].Points)
    {
        _chart.Series["Default"].Points[i].Color = colours[i];
        if(showLegends)
            _chart.Series["Default"].Points[i].Label = "#PERCENT";

        i++;
    }

    _chart.Series["Default"].ChartType = SeriesChartType.Pie;

    _chart.Series["Default"]["PieLabelStyle"] = showLegends ? "Inside" : "Disabled";
    _chart.Series["Default"].LabelForeColor = System.Drawing.Color.White;
    _chart.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
    _chart.ChartAreas["ChartArea1"].AlignmentStyle = AreaAlignmentStyles.Position;
    _chart.BorderlineWidth = 0;
    _chart.ChartAreas["ChartArea1"].AxisY.LabelStyle.Enabled = showLegends;
    _chart.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = showLegends;
    _chart.ChartAreas["ChartArea1"].AlignmentOrientation = AreaAlignmentOrientations.Horizontal;
    _chart.Legends[0].Enabled = showLegends;
user987723
  • 945
  • 3
  • 12
  • 33

1 Answers1

0

You can set the Position and IsDockedInsideChartArea of the legend. Also the ChartArea itself can be positioned so it doesn't move around. You have to play a bit with the values to get it right.

legend.Position.X = 0
legend.Position.Y = 0
legend.IsDockedInsideChartArea = False

area.Position.Height = 94
area.Position.Width = 98
area.Position.Y = 0
area.Position.X = 0
Cerveser
  • 752
  • 8
  • 23