0

我使用经典的 asp.net 图表来呈现一些数据,然后将其放入 PDF 中。我似乎无法解决的一个问题是,当我将图例添加到图表的右侧时,它会被向下推。谁能明白为什么?

在此处输入图像描述

        _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;
4

1 回答 1

0

您可以设置图例的 Position 和 IsDockedInsideChartArea。ChartArea 本身也可以定位,因此它不会四处移动。您必须对这些值进行一些操作才能使其正确。

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
于 2015-07-10T12:46:04.350 回答