在图例中显示禁用的系列

时间:2013-01-02 15:29:29

标签: c# winforms charts legend

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

这是我正在寻找的链接:

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

2 个答案:

答案 0 :(得分: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);
    }
}

结果:

all series enabled

one series disabled

答案 1 :(得分:0)

扩展上面的示例后,您可以使用 Tooltip 对象在 MouseMove 事件中显示/隐藏,并在图例应设置动画时触发 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 事件以检查工具提示 .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));
            }
        }
    }
};