Jfreechart优化X轴标签 [英] Jfreechart optimize X axis labels

查看:79
本文介绍了Jfreechart优化X轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JFreechart中,我有一个带有日期(和时间)的X轴.

In JFreechart I have an X axis with dates (and times).

我如何要求JFreechart优化它们并充分利用它?

How can I ask JFreechart to optimize them and make the most out of it?

现在,它包含的标签多于空格,并且所有标签都转换为'...'.

Right now it contains more label than the space and all the labels gets converted into '...'.

如果不是所有的刻度线都带有标签,那是完全可以的,但是我要尽可能(如果它们适合并且可以完整显示).

It is totally fine if not all ticks will have labels, but I want as much as can be (if they fit and can be displayed fully).

我该如何实现?

这里是复制截断标签的完整最小资源. (还更新了屏幕截图).默认情况下,JFreechart不处理优化:

Here is the complete minimal source to reproduce the truncated labels. (also updated the screenshot). JFreechart does not handle the optimization by default:

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);

          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" ,
             "X-axis labels are truncated");

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

UPDATE2:

我建议将X轴标签旋转45°,因为@trashgod建议这样做. 但是,当图片中包含更多数据时,该方法将无法正常工作:

UPDATE2:

I prefer the 45° rotation for the X-axis labels as @trashgod recommended. However this approach is not working fine when more data comes into the picture:

是否可以设置最大允许标签数?我会将其设置为一些默认值,例如5或6.或者,如果可以隐藏周围环境,则可以定义边距或填充以提高可读性也是可以的.

Is it possible to set the maximal allowable labels count? I would set it to some default value like 5 or 6. Or, it is also fine to define a margin or padding for increasing readability if this would hide the surroundings.

推荐答案

您更新后的示例将创建一个CategoryDataset,并使用ChartFactory方法createLineChart()创建一个CategoryPlot.您可以调整标签的位置以提高可读性,如此处及以下所示.而且,

Your updated example creates a CategoryDataset and uses the ChartFactory method, createLineChart(), to create a CategoryPlot. You can adjust the label positions for readability as shown here and below. Moreover,

仅当标签可见时才有一条垂直的网格线并隐藏所有其他网格线是很好的选择.

it would be nice to have a vertical grid line only when the label is visible and hide all other grid lines.

plot.getDomainAxis().setCategoryLabelPositions(
    CategoryLabelPositions.UP_45);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinesVisible(false);

通常,创建一个TimeSeries并使用相应的ChartFactory方法createTimeSeriesChart().调整随附图表的大小时,生成的DateAxis将自动调整标签.另外,

More generally, create a TimeSeries and use the corresponding ChartFactory method, createTimeSeriesChart(). The resulting DateAxis will automatically adjust the labels when the enclosing chart is resized. In addition,

  • 您可以如此处所示调整日期格式.

要建立图表的初始大小,请按照此处的建议覆盖getPreferredSize().

To establish the chart's initial size, override getPreferredSize(), as suggested here.

使用setRange()时,查询基础数据集,如下所示.

When using setRange(), query the underlying dataset, as shown below.

仅在的Swing GUI对象="nofollow noreferrer">事件分配线程.

Construct and manipulate Swing GUI objects only on the event dispatch thread.

import java.awt.Dimension;
import java.awt.EventQueue;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

public class TempChart extends ApplicationFrame {

    public TempChart(String applicationTitle, String chartTitle) {
        super(applicationTitle);
        TimeSeries s = createSeries();
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            chartTitle, "Date", "Temperature", new TimeSeriesCollection(s));

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.getRangeAxis().setRange(Math.floor(s.getMinY()), Math.ceil(s.getMaxY()));

        ChartPanel chartPanel = new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(560, 367);
            }
        };
        add(chartPanel);
    }

    private TimeSeries createSeries() {
        TimeSeries series = new TimeSeries("Temperature");

        series.add(new Hour(0, new Day()), 26.44);
        series.add(new Hour(1, new Day()), 26.2);
        series.add(new Hour(2, new Day()), 25.93);
        series.add(new Hour(3, new Day()), 25.71);
        series.add(new Hour(4, new Day()), 25.54);
        series.add(new Hour(5, new Day()), 25.42);
        series.add(new Hour(6, new Day()), 25.25);
        series.add(new Hour(7, new Day()), 25.19);
        series.add(new Hour(8, new Day()), 25.25);
        series.add(new Hour(9, new Day()), 25.36);
        series.add(new Hour(10, new Day()), 25.52);
        series.add(new Hour(11, new Day()), 25.86);
        series.add(new Hour(12, new Day()), 26.51);
        series.add(new Hour(13, new Day()), 26.82);

        return series;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                TempChart chart = new TempChart(
                    "Temperature demo", "Time Axis labels adjust on resize");
                chart.pack();
                chart.setLocationRelativeTo(null);
                chart.setVisible(true);
            }
        });
    }
}

这篇关于Jfreechart优化X轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆