jfreechart-更改图例中的颜色样本 [英] jfreechart - change sample of colors in legend

查看:425
本文介绍了jfreechart-更改图例中的颜色样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何在jfreechart的图例中更改系列颜色的样本.我现在所拥有的是系列颜色的细线,例如:

我希望拥有这些颜色的正方形样本.这是一个示例

Can someone tell me how to change samples of series color in legend in jfreechart. What I have now is small line of series color eg:

I would like to have square sample of those colors. Here is an example

有人可以帮助我吗?

好的,我找到了解决方案.至少我认为.当然,没有简单的方法可以做到这一点.现在,有一种setShape(square)方法可以解决问题,至少我还没有找到一种方法.

Ok I found the solution. At least I think. Of course there is no simple way to do this. There is now, you know, setShape(square) method, that will do the trick, at least i haven't found one.

例如,与条形图相反,默认情况下,基本XY图表和时间图表默认具有线型"图例(如果默认情况下具有方形图例).因此,我必须删除当前的图例,并用正方形的颜色样本创建一个新的图例,然后将这个新的图例添加到我的时间表中.

Basicly XY chart and time chart have "line style" legend by default in contrary to bar chart for example (if has square legend by default). So I had to remove current legend and create new one with square samples of color and this new legend add to my time chart.

LegendItemCollection legend = new LegendItemCollection();    
for (int i = 0; i < seriecCount; ++i) {
    chart.getXYPlot().getRenderer().setSeriesPaint(i, colorPalette.get(i));
    LegendItem li = new LegendItem(data.getSeriesName(i), "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, colorPalette.get(i));
    legend.add(li);
}  
chart.getXYPlot().setFixedLegendItems(legend);

感谢您的关注.我希望它能对某人有所帮助.

Thanks for attention. I hope it will help someone.

推荐答案

像上面所做的那样,生成自己的图例是在JFreeChart中做事的一种完全可以接受的方式.如果您不想这样做,也可以使用重写的lookupLegendShape()方法来定义自己的渲染器.

Generating your own legend, as you do above, is a perfectly acceptable way of doing things in JFreeChart. If you didn't want to do it, you can also define your own renderer with the lookupLegendShape() method overridden.

thePlot.setRenderer(new XYLineAndShapeRenderer()
      {
         public Shape lookupLegendShape(int series)
         {
            return new Rectangle(15, 15);
         }
      });

这篇关于jfreechart-更改图例中的颜色样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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