使图表图例代表两种颜色 [英] Make Chart Legend represent two colors

查看:183
本文介绍了使图表图例代表两种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中创建了一个柱形图,如下所示:

I created a column chart in my application which look like this:

您可以看到正值是绿色,负值是红色.我需要在传说中代表这一点.我就是不知道.

As you can see the positive values are green and the negative values are red. I need to represent this in the legend. I just don't know how.

我已经尝试过的:

我在Legend上添加了CustomItems.这是代码:

I added CustomItems to the Legend. Here is the code:

Legend currentLegend = chart.Legends.FindByName(chart.Series[series].Legend);
if (currentLegend != null)
{
    currentLegend.LegendStyle   = LegendStyle.Table;
    LegendItem li               = new LegendItem();
    li.Name                     = series;
    li.Color                    = Color.Red;
    li.BorderColor              = Color.Transparent;
    currentLegend.CustomItems.Add(li);
}

这将导致以下表示形式:

This results in the following representation:

我可以忍受.但是,一旦我在图表中添加更多的序列,元素的顺序就会被破坏.这是一个示例:

I could live with that. But as soon as I add further series to the chart the order of the elements gets destroyed. Here is an example:

我想选择两个选项之一:

I would like to have one of the two options:

  1. 将正色和负色保持在一起
  2. 甚至更好的解决方案是在图例中只有一个瓷砖是双色的.像这样:
  1. keep the positive and negative color together
  2. or an even better solution could be to have just one tile in the legend which is double colored. Something like this:

您能帮我解决这个问题吗?

Could you please help me solving this issue?

非常感谢!

推荐答案

是的,您可以这样做.但是请注意,您不能真正修改原始的Legend.因此,要获得完美的结果,您需要创建一个新的自定义Legend.

Yes, you can do that. Note however that you can't really modify the original Legend. So for a perfect result you would need to create a new custom Legend instead.

请参见

See here for an example that does that; note especially the positioning..!

但是也许您可以轻松一些.见下文!

But maybe you can get away a little easier; see below!

要理解的第一个规则是,总是将LegendItems添加到列表的末尾.因此,除非您在开始时添加了Series,否则无法将它们放在一起.您可以使用Series.Insert(..)来做到这一点,但是使用那些两个颜色的矩形会更好,imo ..

The first rule to understand is that added LegendItems always go to the end of the list. So you can't keep them together, unless your added Series are at the start. You can do that by using Series.Insert(..) but using those two-color rectangles is much nicer, imo..

要显示所需的图形,只需将它们创建为位图,就可以在磁盘上或动态地将它们存储在图表的Images集合中:

To show the graphics you want, simply create them as bitmaps, either on disk or on the fly and store them in the Images collection of the chart:

Legend L = chart1.Legends[0];
Series S = chart1.Series[0];

// either load an image from disk (or resources)
Image img = Image.FromFile(someImage);

// or create it on the fly:
Bitmap bmp = new Bitmap(32, 14);
using (Graphics G = Graphics.FromImage(bmp))
{
    G.Clear(Color.Red);
    G.FillPolygon(Brushes.LimeGreen, new Point[] { new Point(0,0), 
        new Point(32,0), new Point(0,14)});
}

现在将其添加到图表的NamedImage集合中:

Now add it to the chart's NamedImage collection:

chart1.Images.Add(new NamedImage("dia", bmp));

现在,您可以根据需要创建任意数量的LegendItems:

Now you can create as many LegendItems as you need:

LegendItem newItem = new LegendItem();
newItem.ImageStyle = LegendImageStyle.Rectangle;
newItem.Cells.Add(LegendCellType.Image, "dia", ContentAlignment.MiddleLeft);
newItem.Cells.Add(LegendCellType.Text, S.Name, ContentAlignment.MiddleLeft);

并将它们添加到Legend:

L.CustomItems.Add(newItem);

很遗憾,您无法删除原始项目.

Unfortunately you can't delete the original item.

除了从头开始创建新的Legend之外,您还可以做些什么:

What you can do, besides creating a new Legend from scratch, is this:

清除这样的文字:

S.LegendText = " "; // blank, not empty!

无论如何将全部Colors设置为DataPoints,您也可以摆脱蓝色矩形:

As you have set the Colors of all the DataPoints anyway, you can also get rid of the blue rectangle:

S.Color = Color.Transparent;

这还将使所有不带颜色的点透明,因此请确保将它们全部着色!

This will also make all points without colors transparent, so make sure to color them all!

请注意,图例中的某些空格仍然会占用!

Note that some space in the Legend it still taken!

以下是结果,其中带有一些彩色点,并添加了线系列:

Here is the result, with a few colored points and your line series added:

这篇关于使图表图例代表两种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆