IntervalBarRenderer:显示栏内的特定信息

时间:2017-02-05 10:18:38

标签: java swing jfreechart

DefaultIntervalCategoryDataset中,我想在条形图中显示间隔范围信息。我想丢弃下图中包含的信息,只想显示间隔值。

image

以下是代码。如何仅显示间隔值?

public class IntervalBarChartTest0 extends ApplicationFrame {
    public IntervalBarChartTest0(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }
    private static DefaultIntervalCategoryDataset createDataset1() {

        Number[] series1_Start = new Number[] {25,50};
        Number[] series1_End = new Number[] {50,75};
        Number[][] starts = new Number[][]{series1_Start};
        Number[][] ends = new Number[][] {series1_End};
        String[]  categoryKeys = {"50th%ile", "90th%ile"};
        String[] seriesKeys = {"Series1"};
        DefaultIntervalCategoryDataset dataset= new DefaultIntervalCategoryDataset(seriesKeys,categoryKeys,starts, ends);
        return dataset;
    }

    private static JFreeChart createChart(DefaultIntervalCategoryDataset dataset) {

        CategoryAxis domainAxis = new CategoryAxis("Percentiles");

        NumberAxis rangeAxis = new NumberAxis("ms1");
        IntervalBarRenderer renderer = new IntervalBarRenderer();
        rendererSettings(renderer);
        CategoryPlot plot = new CategoryPlot(dataset, domainAxis, rangeAxis,renderer);
        JFreeChart chart = new JFreeChart("Percentiles", plot);
        ChartUtilities.applyCurrentTheme(chart);
        return chart;
    }
    private static void rendererSettings(IntervalBarRenderer renderer){
        renderer.setItemLabelFont(new Font("Verdana",Font.PLAIN,8));
        renderer.setBaseItemLabelGenerator(new IntervalCategoryItemLabelGenerator());
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE6, TextAnchor.BOTTOM_CENTER));
        renderer.setItemMargin(0.1);
        renderer.setBaseItemLabelsVisible(true);    

    }
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset1());
        return new ChartPanel(chart);
    }

    public static void main(String[] args) {
        JFrame demo = new IntervalBarChartTest0(
                "Testing");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:3)

这是默认IntervalCategoryItemLabelGenerator的一项功能。 DEFAULT_LABEL_FORMAT_STRING指定以下MesageFormat值:

`"({0}, {1}) = {3} - {4}"`

含义由createItemArray()定义。而不是默认值,你可能想要这样的东西:

renderer.setBaseItemLabelGenerator(
    new IntervalCategoryItemLabelGenerator(
        "{2}", NumberFormat.getNumberInstance()));

image