2

我有一个复选框,允许我的程序启用图表上显示的禁用系列。当我禁用该系列时,它会从 chartArea 以及图例中删除。即使禁用,是否可以将其保留在图例中?我想更改它在图例中的透明度,使其看起来像已禁用,并在启用时将其设置回全彩。

这是我正在寻找的链接:

http://postimage.org/image/kj675p14j/

4

2 回答 2

1

编辑 :

我找到了另一种(更简单)的方法来做到这一点。您可以简单地处理CustomizeLegend事件(并使用禁用系列series.Enabled):

void chart1_CustomizeLegend(object sender, CustomizeLegendEventArgs e)
{
    e.LegendItems.Clear();
    foreach (var series in this.chart1.Series)
    {
        var legendItem = new LegendItem();
        legendItem.SeriesName = series.Name;
        legendItem.ImageStyle = LegendImageStyle.Rectangle;
        legendItem.BorderColor = Color.Transparent;
        legendItem.Name = series.Name + "_legend_item";

        int i = legendItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleCenter);
        legendItem.Cells.Add(LegendCellType.Text, series.Name, ContentAlignment.MiddleCenter);

        if (series.Enabled)
            legendItem.Color = series.Color;
        else
            legendItem.Color = Color.FromArgb(100, series.Color);
        e.LegendItems.Add(legendItem);
    }
}

结果:

所有系列启用

一系列禁用

于 2013-01-02T16:10:38.713 回答
0

随着上面的示例展开,您可以使用在MouseMove事件中显示/隐藏的Tooltip对象,并在图例应该动画/更新时触发CustomizeLegend事件。单击图例将隐藏显示每个系列。

private int lastX; private int lastY;
ToolTip tip = new ToolTip();

Chart.MouseMove += (object sender, MouseEventArgs e) =>
{

    //Cancel tooltip re-triggering itself when shown 
    if (e.X == this.lastX && e.Y == this.lastY)
    {
        return;
    }

    //Get any object clicked
    var hit = Chart.HitTest(e.X, e.Y);

    if (hit.Object is LegendItem legendItem)
    {
        tip.Tag = legendItem;
        tip.ToolTipTitle = legendItem.SeriesName;
        tip.Show(legendItem.ToolTip, Chart, new Point(e.X + Chart.Cursor.Size.Width / 2, e.Y + Chart.Cursor.Size.Height / 2));
        Chart.Legends[0].Enabled = true; //trigger CustomizeLegend event
    }
    else
    {
        if (tip.Active)
        {
            tip.UseFading = true;
            tip.Hide(Chart);
            tip.Tag = null;
            Chart.Legends[0].Enabled = true; //trigger CustomizeLegend event
        }
    }

    this.lastX = e.X;
    this.lastY = e.Y;
};

CustomizeLegend事件扩展为检查当前 legendItem的工具提示 .Tag

Chart.CustomizeLegend += (o, e) =>
{

    e.LegendItems.Clear();
    foreach (var series in this.Chart.Series)
    {
        var legendItem = new LegendItem();
        legendItem.Tag = series;
        legendItem.SeriesName = series.Name;
        legendItem.ImageStyle = LegendImageStyle.Rectangle;

        //Check if Tooltip as active for the legend
        if (tip.Tag is LegendItem tipLegendItem && legendItem.SeriesName.Equals(tipLegendItem.SeriesName))
        {
            //Show legend highlighted
            legendItem.ImageStyle = LegendImageStyle.Rectangle;
            legendItem.BorderColor = Color.Black;
            legendItem.BorderWidth = 1;
        }
        else
        {
            //Show legend normal
            legendItem.BorderColor = Color.Transparent;
        }

        legendItem.Name = series.Name + "_legend_item";
        legendItem.ToolTip = "on/off";

        int i = legendItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleCenter);
        legendItem.Cells.Add(LegendCellType.Text, series.Name, ContentAlignment.MiddleLeft);

        //Show legend faint or active by state of Series.Enabled  
        if (series.Enabled) { 
            legendItem.Color = series.Color;
            legendItem.Cells[1].ForeColor = Color.Black;
        }
        else
        {
            legendItem.Color = Color.FromArgb(100, series.Color);
            legendItem.Cells[1].ForeColor = Color.LightGray;
        }

        e.LegendItems.Add(legendItem);
    }
};

和用于切换 Series.Enabled的MouseDown事件

Chart.MouseDown += (o, e) =>
{
    {
        HitTestResult result = Chart.HitTest(e.X, e.Y);
                    
        // When user hits the LegendItem
        if (result?.Object is LegendItem legendItem)
        {
            if (legendItem.Tag is Series series )
            {
                series.Enabled = !series.Enabled;
                legendItem.ToolTip = series.Enabled ? "Enabled" : "Disabled";
                tip.Show(legendItem.ToolTip, Chart, new Point(e.X + Chart.Cursor.Size.Width / 2, e.Y + Chart.Cursor.Size.Height / 2));
            }
        }
    }
};
于 2020-12-25T05:02:07.377 回答