Buffered image loses domain axis values at a certain point

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Vusa360
Posts: 2
Joined: Sat Dec 17, 2016 5:12 pm
antibot: No, of course not.

Buffered image loses domain axis values at a certain point

Post by Vusa360 » Sun Dec 18, 2016 2:46 pm

Whilst answering a Stack Overflow question about creating a buffered image in JavaFX using the method createBufferedImage I noticed when the image reaches a certain width the domain (x) axis' values disappear. I created a small runnable file which clearly demonstrates the problem (posted below) and there is an image as well. I would appreciate any help in solving this issue.

Image

Code: Select all

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

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Buffered image loses domain axis values at a certain poi

Post by paradoxoff » Sun Dec 18, 2016 6:06 pm

If the number of visible ticks excess ValueAxis.MAXIMUM_TICK_COUNT (which is 500), no tick labels will be drawn. With a chart width of 10000 pixels, you might exceed that limit. As a quick fix, increase the value to a more appropriate one (10000 looks like a reasonable value), and recompile the package.

Vusa360
Posts: 2
Joined: Sat Dec 17, 2016 5:12 pm
antibot: No, of course not.

Re: Buffered image loses domain axis values at a certain poi

Post by Vusa360 » Sun Dec 18, 2016 8:43 pm

paradoxoff wrote:If the number of visible ticks excess ValueAxis.MAXIMUM_TICK_COUNT (which is 500), no tick labels will be drawn. With a chart width of 10000 pixels, you might exceed that limit. As a quick fix, increase the value to a more appropriate one (10000 looks like a reasonable value), and recompile the package.
Thank you
See this Stack Overflow answer to see what I did to programmatically set a maximum tick value

Locked