jfreechart - 如何使用 JFreeChart 绘制狄拉克

标签 jfreechart bar-chart

我想画一个狄拉克(或克罗内克符号,如果你愿意的话),我的意思是在我的图表上用这样的垂直条(没有标签)绘制离散值:

Good example 我可以用条形图来制作它,但它看起来不太好看......有什么好的方法吗?

编辑:我使用了你的方法并且做到了:

enter image description here

但是正如您所看到的,一些条形有不同的颜色,也许是颜色渐变?你知道如何让条形全部变成红色吗?

这是代码:

public class XYDiscreteBarChart {

    private final Color CHART_BACKGROUND_COLOR = new JPanel().getBackground();
    private final Color CHART_FOREGROUND_COLOR = new JLabel().getForeground();
    private final Color CROSSHAIR_COLOR = Color.BLUE;
    private final String title; // Titre du graphique
    private final String xTitle; // Titre de l'axe des abscisses
    private final String yTitle; // Titre de l'axe des ordonnées
    private final XYDataset dataset; // Les données affichées dans le graphique
    private final boolean isLegendVisible; // Affichage de la légende
    private final boolean isTooltipsVisible; // Affichge des tooltips
    private final boolean isUrlVisible; // Affichage des urls
    private final boolean isGridXVisible; // Affichage de la grille verticale
    private final boolean isGridYVisible; // Affichage de la grille horizontale
    private JFreeChart chart; // Le graphique
    private XYPlot plot; // La zone de dessin des courbes

    public XYDiscreteBarChart(String title, String xAxisLabel, String yAxisLabel, XYDataset dataset, boolean legend,
        boolean tooltip, boolean url) {
        this.title = title;
        this.xTitle = xAxisLabel;
        this.yTitle = yAxisLabel;
        this.isLegendVisible = legend;
        this.isTooltipsVisible = tooltip;
        this.isUrlVisible = url;
        this.dataset = dataset;
        this.isGridXVisible = true;
        this.isGridYVisible = true;

        createChart();
        setChartStyle();
    }

    private void createChart() {
        this.chart = ChartFactory.createXYBarChart(
            this.title,
            this.xTitle,
            false,
            this.yTitle,
            formatDataset(),
            PlotOrientation.VERTICAL,
            this.isLegendVisible,
            this.isTooltipsVisible,
            this.isUrlVisible);
        this.plot = (XYPlot) this.chart.getPlot();
    }

    private void setChartStyle() {
        this.plot.setBackgroundAlpha((float) 0.0);
        this.plot.setDomainCrosshairVisible(this.isGridXVisible);
        this.plot.setDomainCrosshairLockedOnData(true);
        this.plot.setRangeCrosshairVisible(this.isGridYVisible);
        this.plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
        this.plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));

        this.chart.setBackgroundPaint(this.CHART_BACKGROUND_COLOR);
        this.plot.getDomainAxis(0).setAxisLinePaint(this.CHART_FOREGROUND_COLOR);
        this.plot.getDomainAxis(0).setLabelPaint(this.CHART_FOREGROUND_COLOR);
        this.plot.getDomainAxis(0).setTickLabelPaint(this.CHART_FOREGROUND_COLOR);
        this.plot.getDomainAxis(0).setTickMarkPaint(this.CHART_FOREGROUND_COLOR);
        this.plot.getRangeAxis(0).setAxisLinePaint(this.CHART_FOREGROUND_COLOR);
        this.plot.getRangeAxis(0).setLabelPaint(this.CHART_FOREGROUND_COLOR);
        this.plot.getRangeAxis(0).setTickLabelPaint(this.CHART_FOREGROUND_COLOR);
        this.plot.getRangeAxis(0).setTickMarkPaint(this.CHART_FOREGROUND_COLOR);
        this.plot.setBackgroundPaint(this.CHART_FOREGROUND_COLOR);
        this.plot.setDomainGridlinePaint(this.CHART_FOREGROUND_COLOR);
        this.plot.setRangeGridlinePaint(this.CHART_FOREGROUND_COLOR);
        this.plot.setDomainCrosshairPaint(this.CROSSHAIR_COLOR);
        this.plot.setRangeCrosshairPaint(this.CROSSHAIR_COLOR);

        XYBarRenderer renderer = (XYBarRenderer) this.plot.getRenderer();
        renderer.setBarPainter(createBarPainter());
        renderer.setSeriesPaint(0, Color.RED);
    }

    private IntervalXYDataset formatDataset() {
        final XYSeries series = new XYSeries("Filter Coefficients");

        for (int i = 0; i < this.dataset.getItemCount(0); i++) {
            series.add(this.dataset.getX(0, i), this.dataset.getY(0, i));
        }

        final XYSeriesCollection dataset = new XYSeriesCollection(series);
        return dataset;
    }

    private GradientXYBarPainter createBarPainter() {
        return new GradientXYBarPainter() {

            private static final long serialVersionUID = -1997018568242678921L;

            @Override
            public void paintBar(Graphics2D g2, XYBarRenderer renderer, int row, int column,
                RectangularShape bar, RectangleEdge base) {
                double wCoeff = 0.25;
                double hCoeff = 1.0;
                double newWidth, deltaW, deltaX;

                Rectangle2D rect = bar.getFrame();
                newWidth = rect.getWidth() * wCoeff;
                deltaW = rect.getWidth() - newWidth;
                deltaX = deltaW / 2;
                rect.setRect(rect.getX() + deltaX, rect.getY(), newWidth, rect.getHeight() * hCoeff);
                bar.setFrame(rect);
                super.paintBar(g2, renderer, row, column, bar, base);
            }
        };
    }
}

最佳答案

假设您指的是箭头,XYBarRenderer 似乎是一个可能的起点。您可以将其 XYBarPainter 替换为自定义实现,例如 example .

关于jfreechart - 如何使用 JFreeChart 绘制狄拉克,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14262284/

相关文章:

javascript - D3.js 从 json 更新条形图

r - 拆分上方和下方部分的图以进行 3 阶乘条形图

java - JFreechart XYAreaChart 改变颜色

java - 教程中的 jFreeChart 自定义条形颜色错误

java - 当 JComboBox 位于 JLayeredPane 中的 JFreeChart 上方时消失

android - "java.lang.NoClassDefFoundError: com.google.android.gms.R$string error"添加"libs/mpandroidchartlibrary-2-1-6.jar"后

r - ggplot 在 x 轴上显示表达式

java - 我怎样才能制作如图所示的图表?

JFreeChart 刻度标签被切断

javascript - 使 highchart 成为动态数据驱动的