Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207985
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: Java

2008-04-05 08:02:06

 
Overview
  JFreeChart will allow you to display item labels in, on or near to each data item in a chart.Refer to 1.bmp.
 
A word of advice: use this feature sparingly.
 
This chapter covers how to:
• make item labels visible(for the chart types that support item labels);
• change the appearance (font and color) of item labels;
• specify the location of item labels;
• customise the item label text.


Limitations
  There are some limitations with respect to the item labels in the current release of JFreeChart:
• some renderers do not support item labels;
• axis ranges are not automatically adjusted to take into account the item labels—some labels may disappear off the chart if sufficient margins are not set (use the setUpperMargin() and/or setLowerMargin() methods in the relevant axis to adjust this).

Displaying Item Labels
  Item labels are not visible by default, so you need to configure the renderer to create and display them. This involves two steps:
• assign a CategoryItemLabelGenerator or XYItemLabelGenerator to the renderer—this is an object
that assumes responsibility for creating the labels;
• set a flag in the renderer to make the labels visible, either for all series or, if you prefer, on a per series basis.
  In addition, you have the option to customise the position, font and color of the item labels.

Assigning a Label Generator
 
Item labels are created by a label generator that is assigned to a renderer
 
To assign a generator to a CategoryItemRenderer, use the following code:
  CategoryItemRenderer renderer = plot.getRenderer();
  CategoryItemLabelGenerator generator = new 
    StandardCategoryItemLabelGenerator(
      "{2}", new DecimalFormat("0.00"));
  renderer.setLabelGenerator(generator);
  Similarly, to assign a generator to an XYItemRenderer, use the following code:
  XYItemRenderer renderer = plot.getRenderer();
  XYItemLabelGenerator generator = new
    StandardXYItemLabelGenerator(
    "{2}", new DecimalFormat("0.00"));
    renderer.setLabelGenerator(generator);

Making it visible
 
For a CategoryItemRenderer:
  CategoryItemRenderer renderer = plot.getRenderer();
    renderer.setItemLabelsVisible(true);
  Similarly, for a XYItemRenderer:
  XYItemRenderer renderer = plot.getRenderer();
    renderer.setItemLabelsVisible(true);
  (TIPS)Once set, this flag takes precedence over any per series settings you may have made elsewhere. In order for the per series settings to apply, you need to set this flag to null.

Making Labels Visible For Selected Series
 
You can use code similar to the following:
  CategoryItemRenderer renderer = plot.getRenderer();
   
// clears the ALL series   
    flag
renderer.setItemLabelsVisible(null);
    renderer.setSeriesItemLabelsVisible(0, true);
    renderer.setSeriesItemLabelsVisible(1, false);

Troubleshooting
 
If, after following the steps outlined in the previous sections, you still can’t see any labels on your chart, there are a couple of things to consider:
  • the renderer must have a label generator assigned to it—this is an object that creates the text items that are used for each label.
• some renderers don’t yet support the display of item labels (refer to the documentation for the renderer you are using).

Item Label Appearance
  You can change the appearance of the item labels by changing the font and/or the color used to display the labels. As for most other renderer attributes, the settings can be made once for all series, or on a per series basis.

Changing the Label Font
 
Change the font for the item labels in all series
    CategoryItemRenderer renderer = plot.getRenderer();
    renderer.setItemLabelFont(new Font("SansSerif", Font.PLAIN, 10));
  Similarly, to set the font for individual series:
  CategoryItemRenderer renderer = plot.getRenderer();
  // clear the settings for ALL series...
  renderer.setItemLabelFont(null);
  // add settings for individual series...
  renderer.setSeriesItemLabelFont(0, new
    Font("SansSerif", Font.PLAIN, 10));
  renderer.setSeruesItemLabelFont(1, new  
    Font("SansSerif", Font.BOLD, 10));

Changing the Label Color
 
All series:
 
CategoryItemRenderer renderer = plot.getRenderer();
  renderer.setItemLabelPaint(Color.red);
  Similarly, to set the color for individual series:
  CategoryItemRenderer renderer = plot.getRenderer();
  // clear the settings for ALL series...
  renderer.setItemLabelPaint(null);
  // add settings for individual series...
  renderer.setSeriesItemLabelPaint(0, Color.red);
  renderer.setSeriesItemLabelPaint(1, Color.blue);
  Once again, notice how the paint for all series has been set to null to prevent it from overriding the per series settings.

Item Label Positioning
  Overview
  The positioning of item labels is controlled by four attributes that are combined into an ItemLabelPosition
object. You can define label positions for items with positive and negative values independently, via the following methods in the CategoryItemRenderer interface:
  public void setPositiveItemLabelPosition(
    ItemLabelPosition position);

  public void setNegativeItemLabelPosition(
    ItemLabelPosition position);
  ItemLabelPosition
 
There are four attributes:
  • the item label anchor - determines the base location for the item label;ItemLabelAnchor
  • the text anchor - determines the point on the label that is aligned to the base location;TextAnchor
  • the rotation anchor - this is the point on the label text about which the rotation (if any) is applied;
  • the rotation angle - the angle through which the label is rotated.

 
Customising the Item Label Text
 
If you want to have complete control over the label text, you can write your own class that implements the CategoryItemLabelGenerator interface.
  Implementing a Custom Item Label Generator
 
To develop a custom label generator, you simply need to write a class that implements the method defined in the CategoryItemLabelGenerator interface:
  public String generateLabel(CategoryDataset dataset,
    int series, int category);
  (How the generateLabel function used)The renderer will call this method at the point that it requires a String use for a label.The method can return an arbitrary String value, so you can apply any formatting you want to the result. It is also valid to return null if you prefer no label to be displayed.

Example 1 - Values Above a Threshold
  Refer to 3.bmp
  In this first example, the goal is to display labels for the items that have a value greater than some predefined threshold value.
  It isn’t all that difficult to achieve, we simply need to:
  • write a class that implements the CategoryItemLabelGenerator interface, and implement the
generateItemLabel() method in such a way that it returns null for any item where the value is less than the threshold;
  • create an instance of this new class, and assign it to the renderer using the setLabelGenerator() method.
文件:demo.zip
大小:52KB
下载:下载



阅读(2292) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~