为什么图形很大时x轴会消失 [英] Why does the x axis disappear when the graph very big
问题描述
我正在尝试使用JFreeChart
加载大型图形.但是,当缓冲的图像超过特定大小时,X轴存在问题.值在X轴上消失.可以在图片的第三张图中看到.
I am trying to load large graphs using JFreeChart
. However, I have a problem with the X axis when the buffered image is above a certain size. The values disappear on the X axis. This can be seen in the third graph on the image.
希望能为您解决问题提供帮助
I would appreciate any help in fixing the problem
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Program2 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
StackPane p = new StackPane();
primaryStage.setTitle("Chart Application");
Label loader = new Label("Loading...");
loader.setGraphic(new ImageView(new Image("https://media.giphy.com/media/FmcNeI0PnsAKs/giphy.gif")));
loader.setFont(new javafx.scene.text.Font(35));
p.setStyle("-fx-background: #FFFFFF;");
p.getChildren().add(loader);
StackPane.setAlignment(loader, Pos.CENTER);
Scene scene = new Scene(p, 600, 600);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
Task<ArrayList<ImageView>> loadInitial = new Task<ArrayList<ImageView>>() {
@Override
public ArrayList<ImageView> call() {
ArrayList<ImageView> images = new ArrayList<ImageView>();
XYSeries data = new XYSeries(1);
for(int j = 0; j <= 100; j += 2) {
data.add(j, -0.2);
data.add(j, 1);
data.add(j + 1, 1);
data.add(j + 1, -0.2);
}
XYSeriesCollection dataset = new XYSeriesCollection(data);
JFreeChart chart = ChartFactory.createXYAreaChart("", "", "", dataset, PlotOrientation.VERTICAL, false, false, false);
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderVisible(false);
chart.setAntiAlias(true);
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis range = plot.getRangeAxis();
range.setLowerMargin(0);
range.setUpperMargin(0);
range.setVisible(false);
ValueAxis domainAxis = plot.getDomainAxis();
domainAxis.setLowerMargin(0);
domainAxis.setUpperMargin(0);
BufferedImage capture = chart.createBufferedImage(1000, 50);
ImageView imageView = new ImageView();
Image chartImg = SwingFXUtils.toFXImage(capture, null);
imageView.setImage(chartImg);
images.add(imageView);
BufferedImage capture2 = chart.createBufferedImage(10000, 50);
ImageView imageView2 = new ImageView();
Image chartImg2 = SwingFXUtils.toFXImage(capture2, null);
imageView2.setImage(chartImg2);
images.add(imageView2);
BufferedImage capture3 = chart.createBufferedImage(100000, 50);
ImageView imageView3 = new ImageView();
Image chartImg3 = SwingFXUtils.toFXImage(capture3, null);
imageView3.setImage(chartImg3);
images.add(imageView3);
return images;
}
};
loadInitial.setOnSucceeded(e -> {
VBox images = new VBox();
ArrayList<ImageView> result = loadInitial.getValue();
for(ImageView image : result) {
images.getChildren().add(image);
}
ScrollPane scrollPane = new ScrollPane(images);
scrollPane.setStyle("-fx-background: #FFFFFF;");
scene.setRoot(scrollPane);
});
new Thread(loadInitial).start();
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意 之所以引起此问题,是因为图形很大时, This problem is caused because the number of 为了解决此问题,您可以使用增加的 In order to fix this problem, you can rebuild the package with an increased value for 来自此处 修改 Edit 更改为 Changes to 更改为 Changes to 到 最后,我使用 Apache Ant 这篇关于为什么图形很大时x轴会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
我还发布了推荐答案
ticks
(轴上的标记)的数量超过了ValueAxis.MAXIMUM_TICK_COUNT
. ticks
(markers on the axis) exceeds the ValueAxis.MAXIMUM_TICK_COUNT
when a graph is that large. ValueAxis.MAXIMUM_TICK_COUNT
值来重建程序包,或者可以编写一种方法来设置最大刻度数,然后重建程序包.这将允许您设置其他Java文件中的刻度数.这可能是诸如ValueAxis.setMaximumTickCount(int ticks)
ValueAxis.MAXIMUM_TICK_COUNT
or you could write a method that will allow you to set the maximum tick count and then rebuild the pack. This will allow you to set the amount of ticks in a different java file. This could be a method such as ValueAxis.setMaximumTickCount(int ticks)
为了实现这一点,我从org.jfree.chart.axis
编辑了两个文件; ValueAxis.java
和NumberAxis.java
.
In order to achieve this, I edited two files from org.jfree.chart.axis
; ValueAxis.java
and NumberAxis.java
.ValueAxis.java
.我将以下内容添加到了课程的底部.ValueAxis.java
. I added the following to the bottom of the class.private int maxTicks = MAXIMUM_TICK_COUNT;
public void setMaxTicks(int max) {
maxTicks = max;
}
public int getMaxTicks() {
return maxTicks;
}
NumberAxis.java
.
在957
行和1052
行上,我将if语句更改为NumberAxis.java
.
On the line 957
and line 1052
I changed an if statement fromif (count <= ValueAxis.MAXIMUM_TICK_COUNT) {
if (count <= getMaxTicks()) {