gpt4 book ai didi

java - JFreeChart - RangeAxis 自动调整范围并且不从 0 开始

转载 作者:行者123 更新时间:2023-11-30 10:01:12 25 4
gpt4 key购买 nike

在 JFreeChart 中,我如何要求 RangeAxis 自动“优化”自身?

现在我有一堆数据要显示在 25.00 到 27.00 之间,但范围总是从 0 开始,这看起来不太好:

enter image description here

我尝试设置 plot.getRangeAxis().setAutoRange(true),但它不起作用。当然,我可以计算我的最小值和最大值,然后我可以用 setRange(min, max) 设置它,但是有没有任何捆绑函数可以做到这一点?

复制:

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class LineChart_AWT extends ApplicationFrame {

public LineChart_AWT( String applicationTitle , String chartTitle ) {
super(applicationTitle);
JFreeChart lineChart = ChartFactory.createLineChart(
chartTitle,
"Dates","Temperature",
createDataset(),
PlotOrientation.VERTICAL,
true,true,false);

CategoryPlot plot = (CategoryPlot) lineChart.getPlot();
//plot.getRangeAxis().setRange(25, 27);
//plot.getRangeAxis().setAutoRange(true);

ChartPanel chartPanel = new ChartPanel( lineChart );
chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
setContentPane( chartPanel );
}

private DefaultCategoryDataset createDataset( ) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset( );

dataset.addValue( 26.44,"Temperature","2019-08-18 00:00");
dataset.addValue( 26.2,"Temperature","2019-08-18 01:00");
dataset.addValue( 25.93,"Temperature","2019-08-18 02:00");
dataset.addValue( 25.71,"Temperature","2019-08-18 03:00");
dataset.addValue( 25.54,"Temperature","2019-08-18 04:00");
dataset.addValue( 25.42,"Temperature","2019-08-18 05:00");
dataset.addValue( 25.25,"Temperature","2019-08-18 06:00");
dataset.addValue( 25.19,"Temperature","2019-08-18 07:00");
dataset.addValue( 25.25,"Temperature","2019-08-18 08:00");
dataset.addValue( 25.36,"Temperature","2019-08-18 09:00");
dataset.addValue( 25.52,"Temperature","2019-08-18 10:00");
dataset.addValue( 25.86,"Temperature","2019-08-18 11:00");
dataset.addValue( 26.51,"Temperature","2019-08-18 12:00");
dataset.addValue( 26.82,"Temperature","2019-08-18 13:00");


return dataset;
}

public static void main( String[ ] args ) {
LineChart_AWT chart = new LineChart_AWT(
"X-axis demo" ,
"Y-range is wrong");

chart.pack( );
RefineryUtilities.centerFrameOnScreen( chart );
chart.setVisible( true );
}
}

最佳答案

另外,必须用NumberAxis#setAutoRangeIncludesZero指定零值是否应包含在显示的区间内:

plot.getRangeAxis().setAutoRange(true);                            // uncomment
((NumberAxis)plot.getRangeAxis()).setAutoRangeIncludesZero(false); // add

结果:

enter image description here

关于java - JFreeChart - RangeAxis 自动调整范围并且不从 0 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57544667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com