问题描述
在 JFreeChart 中是否可以完成以下任务?
Is there a way to accomplish the following in JFreeChart?
我要特别问一下每个栏顶部的印刷精美的数字.在这个特定的样本中,很容易猜出这些值,但是如果y轴成千上万甚至更多,则这种功能将非常有用.在这种情况下,条形的长度对发现确切的值没有多大帮助.
I am specifically asking about the elegantly printed numbers at the top of each bar.In this particular specimen it is easy to guess the values but such a feature would be useful if the y-axis run in the thousands or more. In such a case, the length of the bar wouldn't much help to discover the exact value.
推荐答案
是的,有可能.您需要在CategoryItemLabelGenerator
中指定ItemLabelPosition
.
Yes, it is possible. You need to specify ItemLabelPosition
in CategoryItemLabelGenerator
.
例如:
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
TextAnchor.TOP_CENTER);
renderer.setBasePositiveItemLabelPosition(position);
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
public class DemoBarChart {
public DemoBarChart() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(6, "test", "A");
dataset.setValue(7, "test", "B");
dataset.setValue(15, "test", "C");
JFreeChart chart = ChartFactory.createBarChart(
"Demo", "Test", "Value", dataset,
PlotOrientation.VERTICAL, false, false, false);
CategoryItemRenderer renderer = ((CategoryPlot)chart.getPlot()).getRenderer();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
TextAnchor.TOP_CENTER);
renderer.setBasePositiveItemLabelPosition(position);
JFrame frame = new JFrame("DemoTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ChartPanel(chart));
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DemoBarChart();
}
});
}
}
编辑(版本1.5.0):
EDIT (version 1.5.0):
在当前的JFreeChart版本(1.5.0)中,方法名称中的基本"已更改为默认".
In current JFreeChart version (1.5.0), the "Base" in methods name has changed to Default.
例如:
renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setDefaultItemLabelsVisible(true);
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
TextAnchor.TOP_CENTER);
renderer.setDefaultPositiveItemLabelPosition(position);
这篇关于条形图,其精确值打印在每个条形图的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!