且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JFreeChart折线图中添加标签值和错误栏?

更新时间:2023-11-24 08:09:10

StatisticalLineAndShapeRenderer displays labels when the parent LineAndShapeRenderer method getItemShapeVisible() returns true. In the example below, I've eschewed the chart factory and used the explicit StatisticalLineAndShapeRenderer constructor that enables both shapes and lines.

StatisticalLineAndShapeRenderer renderer
        = new StatisticalLineAndShapeRenderer(true, true);

有什么特殊的原因可以避免使用ChartFactory?

重新使用ChartFactory的做法很方便,但让人想起在烤面包上订购鸡肉,握住鸡肉,敬酒.更重要的是,丢弃的 LineAndShapeRenderer带有请求的工具提示和URL生成器,可能使将来的维护者感到困惑.

While repurposing the ChartFactory is expedient, it's reminiscent of ordering chicken on toast, hold the chicken, to get toast. More critically, the discarded LineAndShapeRenderer takes with it the requested tooltip and URL generators, perhaps puzzling a future maintainer.

有什么方法可以禁用形状吗?

Any way to keep the shapes disabled?

As you suggest, an empty Shape is effective, e.g.

renderer.setSeriesShape(0, new Rectangle2D.Double(0, 0, 0, 0));

import java.awt.Dimension;
import java.awt.EventQueue;
import java.text.NumberFormat;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.StatisticalLineAndShapeRenderer;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;

/**
 * @see https://***.com/a/38080778/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DefaultStatisticalCategoryDataset dataset
            = new DefaultStatisticalCategoryDataset();
        dataset.add(1, 0.1, "series", "A");
        dataset.add(2, 0.4, "series", "B");
        dataset.add(2, 0.2, "series", "C");

        CategoryAxis domain = new CategoryAxis();
        ValueAxis range = new NumberAxis();
        StatisticalLineAndShapeRenderer renderer
            = new StatisticalLineAndShapeRenderer(true, true);
        CategoryPlot plot = new CategoryPlot(dataset, domain, range, renderer);
        JFreeChart chart = new JFreeChart(
            "ErrorBars", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
        renderer.setBaseItemLabelGenerator(
            new StandardCategoryItemLabelGenerator("{2}",
                NumberFormat.getNumberInstance()));
        renderer.setBaseItemLabelsVisible(true);
        renderer.setSeriesShape(0, new Rectangle2D.Double(0, 0, 0, 0));
        new StandardChartTheme("JFree").apply(chart);
        f.add(new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(600, 300);
            }
        });

        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}