简体   繁体   English

带换行符的JFree图表图例

[英]JFree Chart legend with line break

I need to add break line in some legend in JFree Chart. 我需要在JFree Chart的某些图例中添加断行。 I have some legends with 316 characters and need to break every 80. Finally, I'll have 4 lines. 我有一些有316个字符的传说,需要每80分钟才能打破。最后,我将有4行。

Anyway, I tried with "\\n", "\␤" and " ". 无论如何,我试过“\\ n”,“\\ u2424”和“”。 It did nothing. 它没有做任何事。 (From http://www.jfree.org/forum/viewtopic.php?f=3&t=10226 & http://www.jfree.org/forum/viewtopic.php?f=3&t=22417 ) (从http://www.jfree.org/forum/viewtopic.php?f=3&t=10226http://www.jfree.org/forum/viewtopic.php?f=3&t=22417

The only solution I could find (but I wished it could be avoided, since I want it to be dynamically done) is to fix a width for each legend, so it should break as I need to. 我能找到的唯一解决方案(但我希望它可以避免,因为我希望它是动态完成的)是为每个图例修复一个宽度,所以它应该按我的需要打破。 Edit : that even didn't work. 编辑 :即使没有工作。

I'm using jFree Chart 0.9.20 我正在使用jFree Chart 0.9.20


EDIT 编辑

For the moment, with a small legend, that's what I have : 目前,有一个小传说,这就是我所拥有的: 好

It's fine but when I have my long legends : 这很好,但是当我有很长的传说时: 问题:'(

For that last picture, I logged my legend and break lines are here, but they don't show up with jFree Chart. 对于最后一张图片,我记录了我的传奇和断裂线在这里,但它们没有显示jFree图表。

Two alternatives to consider: Given an abbreviated legend display string, 要考虑两种选择:给出一个缩写的图例显示字符串,

  • Use setLegendItemToolTipGenerator() to display the full, unbroken string as a tool tip. 使用setLegendItemToolTipGenerator()将完整的,不间断的字符串显示为工具提示。

     renderer.setLegendItemToolTipGenerator( new StandardXYSeriesLabelGenerator("Legend {0}")); 
  • Use addChartMouseListener() , shown here , and forward mouse moved events over the legend to an adjacent text component. 使用此处显示的addChartMouseListener() ,并将图例上的鼠标移动事件转发到相邻的文本组件。

Alright, I made it work as my client wanted. 好吧,我按照客户的意愿使它成功了。

First, you need to make a new kind of Legend , for example named MyLegend (but please, don't name it like that in the real world). 首先,你需要制作一种新的Legend ,例如名为MyLegend (但请不要在现实世界中将其命名为)。

That class needs to extend Legend and implement Serializable , the same way StandardLegend does. 该类需要扩展Legend并实现Serializable ,就像StandardLegend一样。

To be honest, I even copied/pasted the whole StandardLegend in MyLegend . 说实话,我甚至在MyLegend复制/粘贴了整个StandardLegend Then, you can modify the standard legend to your custom one. 然后,您可以将标准图例修改为自定义图例。

For my needs, I changed : 为了我的需要,我改变了:

  • draw() for the height and width calculation of the whole Legend group draw()用于整个Legend组的高度和宽度计算
  • drawSeriesElements() to split the legend's label and draw every lines one under another. drawSeriesElements()用于分割图例的标签并将每一行绘制在另一个下面。

// Multi line management for Legend
String[] multiline = item.getItem().getLabel().split(System.getProperty("line.separator"));
for(int j = 0; j<multiline.length; j++) {
    RefineryUtilities.drawAlignedString(multiline[j], g2,
        (float) item.getLabelPosition().getX(), (float) item
        .getLabelPosition().getY() + g2.getFontMetrics().getHeight()*j, TextAnchor.CENTER_LEFT);
}
  • createDrawableLegendItem() to calculate each item width and height. createDrawableLegendItem()计算每个项目的宽度和高度。 Since, now legends are multiline, each line of one item doesn't have the same width than others. 由于现在的图例是多行的,因此一个项目的每一行与其他项目的宽度不同。 We need to find the longest one to define the item's real width. 我们需要找到最长的一个来定义项目的实际宽度。 Same goes for height. 身高也一样。 Now it's multiline, so it needs to calculate how many lines it got to know the item's real height. 现在它是多线的,所以它需要计算知道物品真实高度的线数。

Optionally, you could change drawLegendTitle() to make it multiline too. 或者,您可以更改drawLegendTitle()以使其成为多行。

When that class is configured as you want to, you need to apply it on your chart. 根据需要配置该类时,需要将其应用于图表。

So, you do as usual : 所以,你照常做:

JFreeChart chart = new JFreeChart(...);
chart.set ... // apply your series and options

MyLegend legend = new MyLegend();
legend.set... // apply your legend options if applicable
chart.setLegend(legend);

That's it. 而已。

Result : 结果:

最后结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM