编程语言
首页 > 编程语言> > java-使用Jfreechart更改条形图中值轴的起始值

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

作者:互联网

我的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"));

解决方法:

我想您要执行以下操作:

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

barChart是您的JFreeChart对象.

但是由于您的值高于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);
    }
}

标签:java,jfreechart,data-visualization,visualization,bar-chart
来源: https://codeday.me/bug/20191013/1910229.html