我正在为我的应用程序使用jfree图表。我需要图表看起来像这样

预期的

但是我已经以多种方式格式化了图表,但我只能像这样。

实际

请帮助我获得预期的图表。我需要一个没有x轴线的图表。

我的图表代码是

final JFreeChart chart = ChartFactory.createLineChart(
                "", // chart title
                "", // domain axis label
                "", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                false, // tooltips
                false // urls
                );

        final CategoryPlot plot = chart.getCategoryPlot();
        ValueAxis range = plot.getRangeAxis();
        range.setVisible(false);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeZeroBaselinePaint(Color.RED);
        plot.setOutlineVisible(false);
        chart.setBackgroundPaint(Color.WHITE);
        final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setItemLabelsVisible(false);
        renderer.setBaseShapesVisible(true);
        renderer.setBaseShapesFilled(true);
        plot.setRangeGridlinePaint(Color.white);
        renderer.setSeriesStroke(0, new BasicStroke(2.0f));
        plot.setDomainGridlinePaint(Color.BLUE);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setVisible(false);
        rangeAxis.setLabelPaint(Color.BLUE);
        Shape circle = new Ellipse2D.Double(-3, -3, 6, 6);
        renderer.setSeriesShape(0, circle);
        plot.getRenderer().setSeriesPaint(0, Color.BLUE);
        rangeAxis.setRange(0, 3);
        try {
            ChartUtilities.saveChartAsPNG(new File("E:\\jfreeLinechart.png"), chart, 211, 90);
            System.out.println("=====chart=====");
        } catch (Exception e) {
            e.printStackTrace();
        }

最佳答案

您只需要设置类别轴即可。具体设置setAxisLineVisible(false)setTickMarksVisible(false)

我创建了一个运行示例,该示例可以满足您的需求。

package chart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class chartExample extends ApplicationFrame {

    private static final long serialVersionUID = 1L;

    public chartExample(final String title) {
        super(title);
        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(600, 350));
        setContentPane(chartPanel);
    }

    private CategoryDataset createDataset() {

        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(1.0, "Line", "2009");
        dataset.addValue(2.0, "Line", "2010");

        return dataset;

    }

    private JFreeChart createChart(final CategoryDataset dataset) {

        final JFreeChart chart = ChartFactory.createLineChart(
            "",                        // chart title
            "",                        // domain axis label
            "",                        // range axis label
            dataset,                   // data
            PlotOrientation.VERTICAL,  // orientation
            true,                      // include legend
            true,                      // tooltips
            false                      // urls
        );

        chart.setBackgroundPaint(Color.WHITE);

        final CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeZeroBaselinePaint(Color.RED);
        plot.setOutlineVisible(false);
        plot.setRangeGridlinePaint(Color.white);
        plot.setDomainGridlinePaint(Color.BLUE);

        final CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();
        categoryAxis.setAxisLineVisible(false);
        categoryAxis.setTickMarksVisible(false);
        //categoryAxis.setVisible(false);

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        //rangeAxis.setAxisLineVisible(false);
        rangeAxis.setVisible(false);
        rangeAxis.setLabelPaint(Color.BLUE);
        rangeAxis.setRange(0, 3);

        final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        //renderer.setItemLabelsVisible(false); //Deprecated.
        renderer.setBaseShapesVisible(true);
        renderer.setBaseShapesFilled(true);
        renderer.setSeriesStroke(0, new BasicStroke(2.0f));
        Shape circle = new Ellipse2D.Double(-3, -3, 6, 6);
        renderer.setSeriesShape(0, circle);
        plot.getRenderer().setSeriesPaint(0, Color.BLUE);

        // Removed and added a main method.
        //try {
        //    ChartUtilities.saveChartAsPNG(new File("E:\\jfreeLinechart.png"), chart, 211, 90);
        //    System.out.println("=====chart=====");
        //} catch (Exception e) {
        //    e.printStackTrace();
        //}


        return chart;
    }

    public static void main(final String[] args) {

        final chartExample ce = new chartExample("Chart Example");
        ce.pack();
        RefineryUtilities.centerFrameOnScreen(ce);
        ce.setVisible(true);

    }

}

07-25 23:50