小编典典

使用Jfreechart更改条形图中值轴的起始值

java

我的Java Web
应用程序的BAR图表中显示了以下值。9.46373791E8 9.45942547E8 9.45559945E8 9.45187023E8 9.44856693E8
9.44417826E8 9.44007878E8

如您所见,这些值实际上很接近,并且有微小的差异。当我
使用Jfreechart生成条形图时,所有条形都显示几乎相同的
高度,并且无法从视觉上分辨出差异。所以我想
将(0,0)更改为(0,9),以使x轴在y轴上的数字为9。我仍然想
显示条形图代表条形图顶部的真实值。

请提出想法。我尝试了以下内容,但没有成功

    Double d=(double) 9;
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLowerMargin(d);

编辑:这是我的原始程序供您参考

JFreeChart chart =
            ChartFactory.createBarChart(
                    title,
            "File Date",
            "File Size",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false);
         chart.setBackgroundPaint(Color.white);



      // Set the background color of the chart
        chart.getTitle().setPaint(Color.DARK_GRAY);
        chart.setBorderVisible(true);
        // Adjust the color of the title
        CategoryPlot plot = chart.getCategoryPlot();
        plot.getRangeAxis().setLowerBound(d);
        // Get the Plot object for a bar graph

        plot.setBackgroundPaint(Color.white);     
        plot.setRangeGridlinePaint(Color.blue);
        CategoryItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.decode("#00008B"));

阅读 376

收藏
2020-11-30

共1个答案

小编典典

我想您想执行以下操作:

barChart.getCategoryPlot().getRangeAxis().setLowerBound(9.0);

你在哪里barChart JFreeChart Object

但是,由于您的值高于9.0E8(超过9000),因此您不应该将
下限设置为9.0E8而不是9.0,因为
当您达到9.0E8或更高时,0和9之间的差异并不大。

编辑:我已经测试了您的代码,并且可以在Windows
Vista下的计算机上正常工作…

我的完整代码在这里:

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class ChartTester extends JFrame {

    private static final long serialVersionUID = 1L;

    public ChartTester(final String title) {
        super(title);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);

    }

    /**
     * Returns a sample dataset.
     * @return The dataset.
     */
    private CategoryDataset createDataset() {
        final String rowName = "Row";
        final String[] columnName = { "Column1","Column2","Column3","Column4","Column5"};
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(9.2, rowName, columnName[0]);
        dataset.addValue(9.3, rowName, columnName[1]);
        dataset.addValue(9.4, rowName, columnName[2]);
        dataset.addValue(9.5, rowName, columnName[3]);
        dataset.addValue(10.0, rowName, columnName[4]);

        return dataset;
    }

    /**
     * Creates a sample chart.
     * @param dataset  the dataset.
     * @return The chart.
     */
    private JFreeChart createChart(final CategoryDataset dataset) {
        double d =9.0;
        final JFreeChart chart =
            ChartFactory.createBarChart(
                    "Chart Title",
                    "X Axis",
                    "Y Axis",
                    dataset,
                    PlotOrientation.VERTICAL,
                    true,
                    true,
                    false);
        chart.setBackgroundPaint(Color.white);  
        // Set the background color of the chart
        chart.getTitle().setPaint(Color.DARK_GRAY);
        chart.setBorderVisible(true);
        // Adjust the color of the title
        CategoryPlot plot = chart.getCategoryPlot();
        plot.getRangeAxis().setLowerBound(d);
        // Get the Plot object for a bar graph
        plot.setBackgroundPaint(Color.white);     
        plot.setRangeGridlinePaint(Color.blue);
        CategoryItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.decode("#00008B"));
        return chart;
    }

    public static void main(final String[] args) {
        final ChartTester test = new ChartTester("Test");
        test.pack();
        test.setVisible(true);
    }
}
2020-11-30