简体   繁体   English

JFreeChart标签宽度

[英]JFreeChart Label Width

在此输入图像描述

I am new to JFreeChart. 我是JFreeChart的新手。

My chart is attached. 我的图表已附上。 As you can see, the labels are truncated. 如您所见,标签被截断。 How do I specify the width of the label to avoid truncation (I have plenty of real estate to fit them in)? 如何指定标签的宽度以避免截断(我有足够的空间来容纳它们)?

My current code is as follows: 我目前的代码如下:

   DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    dataset.setValue(24, "Major", "Mathematics");
    dataset.setValue(20, "Major", "Philosophy");
    dataset.setValue(18, "Major", "Chemical Engineering");
    dataset.setValue(15, "Major", "Sociology");
    dataset.setValue(14, "Major", "Stuff");
    dataset.setValue(13, "Major", "A Program");
    dataset.setValue(13, "Major", "Bleg");

    JFreeChart chart = ChartFactory.createBarChart(" ", "Major", "Score", dataset, PlotOrientation.HORIZONTAL,
            false, true, false);
    CategoryPlot plot = chart.getCategoryPlot();

    Font font1 = new Font("Dialog", Font.PLAIN, 25);
    Font font2 = new Font("Dialog", Font.PLAIN, 15);
    Font font3 = new Font("Dialog", Font.PLAIN, 25);

    plot.getDomainAxis().setLabelFont(font3);
    plot.getRangeAxis().setLabelFont(font3);

    CategoryAxis axisDomain = plot.getDomainAxis();
    ValueAxis axisRange = plot.getRangeAxis();

    axisDomain.setTickLabelFont(font1);

    axisRange.setTickLabelFont(font2);
    chart.setAntiAlias(true);

Among the approaches suggested here , the example below overrides getPreferredSize() . 此处建议的方法中,下面的示例将覆盖getPreferredSize()

图片

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
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.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * @see https://stackoverflow.com/a/37216788/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.setValue(24, "Major", "Mathematics");
        dataset.setValue(20, "Major", "Philosophy");
        dataset.setValue(18, "Major", "Chemical Engineering");
        dataset.setValue(15, "Major", "Sociology");
        dataset.setValue(14, "Major", "Stuff");
        dataset.setValue(13, "Major", "A Program");
        dataset.setValue(13, "Major", "Bleg");
        JFreeChart chart = ChartFactory.createBarChart("Title", "Major", "Score",
            dataset, PlotOrientation.HORIZONTAL, false, true, false);
        CategoryPlot plot = chart.getCategoryPlot();
        Font font1 = new Font("Dialog", Font.PLAIN, 25);
        Font font2 = new Font("Dialog", Font.PLAIN, 15);
        Font font3 = new Font("Dialog", Font.PLAIN, 25);
        plot.getDomainAxis().setLabelFont(font3);
        plot.getRangeAxis().setLabelFont(font3);
        CategoryAxis axisDomain = plot.getDomainAxis();
        axisDomain.setTickLabelFont(font1);
        ValueAxis axisRange = plot.getRangeAxis();
        axisRange.setTickLabelFont(font2);
        chart.setAntiAlias(true);
        f.add(new ChartPanel(chart){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(1024, 512);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM