gpt4 book ai didi

java - 时间序列图,X轴刻度标签变成

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:56 25 4
gpt4 key购买 nike

我正在使用 JFreeChart 制作条形图与时间。由于某些原因,在这些图表上,x 轴上的刻度标签偶尔会变成“...”。标签似乎有足够的空间来扩展,但它只是切断了整个东西。我怎样才能解决这个问题。

我尝试使用图像按钮上传图片,但它似乎不起作用。

这是与我的项目设置类似的代码。奇怪的是,它的行为与我的构建发生的事情不同。我的不是说“侯...”,而是说“...”。请忽略评论和所有其他不需要的东西。

package dataDisplay;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class mockTest extends JPanel{

ChartPanel chartPanel;
JFreeChart chart;
CategoryAxis domainAxis;
NumberAxis rangeAxis;

public mockTest()
{
//Mock data
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int[] times = new int[]{1,2,3,4,5,6,7,8,9,10,11,12};

for ( int i = 0; i < times.length; i++ ){
dataset.addValue(times[i], "Time", "Houreee" + String.valueOf(i+1));;

}
CategoryPlot plot = new CategoryPlot();

//create the plot

//add the first dataset, and render as bar values
CategoryItemRenderer renderer = new BarRenderer();
plot.setDataset(0,dataset);
plot.setRenderer(0,renderer);

//set axis
domainAxis = new CategoryAxis("Time");
rangeAxis = new NumberAxis("Value");

plot.setDomainAxis(0,domainAxis);
plot.setRangeAxis(rangeAxis);
chart = new JFreeChart(plot);
chartPanel = new ChartPanel( chart );

this.addComponentListener(new ComponentAdapter() {
@Override
/**
* Makes it so it does not stretch out text. Resizes the fonts to scale with the screen width..
*/
public void componentResized(ComponentEvent e) {
chartPanel.setMaximumDrawHeight(e.getComponent().getHeight());
chartPanel.setMaximumDrawWidth(e.getComponent().getWidth());
chartPanel.setMinimumDrawWidth(e.getComponent().getWidth());
chartPanel.setMinimumDrawHeight(e.getComponent().getHeight());

// Makes the font size scale according to the width of the chart panel.
rangeAxis.setLabelFont(new Font("SansSerif", Font.PLAIN,e.getComponent().getWidth()/60));
domainAxis.setTickLabelFont(new Font("SansSerif", Font.PLAIN,e.getComponent().getWidth()/80));
rangeAxis.setTickLabelFont(new Font("SansSerif", Font.PLAIN,e.getComponent().getWidth()/75));
}
});


this.add(chartPanel, "Center");
}
public static void main (String[] args)
{

// Get the default toolkit
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
Dimension scrnsize = toolkit.getScreenSize();
int scrnWidth= (int)scrnsize.getWidth();
int scrnHeight = (int) scrnsize.getHeight();

JFrame J= new JFrame();
JPanel jP = new JPanel();
J.setContentPane(jP);
J.setSize(scrnWidth, scrnHeight);
jP.setBackground(Color.white);
jP.setBounds(0,0,scrnWidth,scrnHeight);
int xPercent= 50;
int yPercent = 50;
int widthPercent=50;
int heightPercent=43;
jP.setLayout(null);
jP.setSize(scrnWidth, scrnHeight);
mockTest b= new mockTest();


jP.add(b);
b.setBounds(new Rectangle((int)(scrnWidth*((double)xPercent/100)),(int)(scrnHeight*((double)yPercent/100)),(int)(scrnWidth*((double)widthPercent/100)),(int)(scrnHeight*((double)heightPercent/100))));
J.setUndecorated(true);
J.setVisible(true);




}

最佳答案

不要使用null 布局;让布局管理器完成工作。 JPanel 的默认布局是 FlowLayout,它会忽略您后续的更改。在下面的示例中,

  • chartPanel 被赋予了 GridLayout;当添加到封闭框架的 CENTER 时,图表将随着框架的大小调整而自由增长。

  • 避免不必要的嵌套面板。

  • 使用 setExtendedState() 最大化框架。

  • 如有必要,使用建议的方法之一 here更改图表的初始大小。

  • 如果您选择更改字体,请使用deriveFont() 以避免用户所选设置出现突然差异。

image

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

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


public void display() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < 12; i++) {
dataset.addValue(i, "Time", "Hours" + String.valueOf(i + 1));
}
CategoryPlot plot = new CategoryPlot();
CategoryItemRenderer renderer = new BarRenderer();
plot.setDataset(0, dataset);
plot.setRenderer(0, renderer);
CategoryAxis domainAxis = new CategoryAxis("Time");
NumberAxis rangeAxis = new NumberAxis("Value");
plot.setDomainAxis(0, domainAxis);
plot.setRangeAxis(rangeAxis);
JFreeChart chart = new JFreeChart(plot);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setLayout(new GridLayout());

JFrame f = new JFrame();
f.add(chartPanel);
f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
f.setUndecorated(true);
f.setVisible(true);
}

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

关于java - 时间序列图,X轴刻度标签变成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011608/

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