Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

BarChart axis tick-labels are missing with JDK1.8

shrudaJul 10 2014 — edited Jul 16 2014

Hi guys!

Currently I try to integrate a JavaFX BarChart into my existing Java application. Therefore I need a BufferedImage of my BarChart, so that I can render that snapshot on a Graphics instance.

I adapted the existing BarChartSample.java (Chart Sample | JavaFX Tutorials and Documentation) and removed all animations. The code itselfs works fine but I identified one problem -> my TickLabels are missing

The TickLabels are visible If I use JDK 1.7u51 but if I change to JDK 1.8u05.... than the stlye of my BarChart will be changed and my tick labels are not visible.

Did I missed something or is that a bug?

Thanks in advance!

Best Regards,

Steve

----------

using JDK 1.7u51

https://drive.google.com/file/d/0B7P_rknS1TWxMUVaRHNzNzBEWVk/edit?usp=sharing

using JDK 1.8u05

https://drive.google.com/file/d/0B7P_rknS1TWxZFV6ZVZvR3ZqM3M/edit?usp=sharing

My adapted code is the following:

package barchartsample;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.FutureTask;

import javafx.application.Platform;

import javafx.embed.swing.JFXPanel;

import javafx.embed.swing.SwingFXUtils;

import javafx.scene.Scene;

import javafx.scene.chart.BarChart;

import javafx.scene.chart.CategoryAxis;

import javafx.scene.chart.NumberAxis;

import javafx.scene.chart.XYChart;

import javafx.scene.image.WritableImage;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class BarChartSample

{

  /**

  * Initialize JavaFX

  */

  private static final JFXPanel initializeJFX = new JFXPanel();

  final static String austria = "Austria";

  final static String brazil = "Brazil";

  final static String france = "France";

  final static String italy = "Italy";

  final static String usa = "USA";

  public static void main(String[] args) throws InterruptedException, ExecutionException

  {

      JFrame frame = new JFrame();

      frame.setSize(815, 640);

      frame.add(new JPanel()

      {

            @Override

            public void paint(Graphics g)

            {

                try

                {

                      g.drawImage(getChartImage(), 0, 0, null);

                }

                catch (Throwable t)

                {

                      t.printStackTrace();

                }

            }

      });

      frame.setVisible(true);

  }

  static BufferedImage getChartImage() throws InterruptedException, ExecutionException

  {

      FutureTask<BufferedImage> future = new FutureTask<BufferedImage>(new Callable<BufferedImage>()

      {

            @Override

            public BufferedImage call() throws Exception

            {

                final CategoryAxis xAxis = new CategoryAxis();

                final NumberAxis yAxis = new NumberAxis();

                final BarChart<String, Number> bc =

                new BarChart<>(xAxis, yAxis);

                bc.setTitle("Country Summary");

                xAxis.setLabel("Country");

                yAxis.setLabel("Value");

      

                XYChart.Series series1 = new XYChart.Series();

                series1.setName("2003");

                series1.getData().add(new XYChart.Data(austria, 25601.34));

                series1.getData().add(new XYChart.Data(brazil, 20148.82));

                series1.getData().add(new XYChart.Data(france, 10000));

                series1.getData().add(new XYChart.Data(italy, 35407.15));

                series1.getData().add(new XYChart.Data(usa, 12000));

                XYChart.Series series2 = new XYChart.Series();

                series2.setName("2004");

                series2.getData().add(new XYChart.Data(austria, 57401.85));

                series2.getData().add(new XYChart.Data(brazil, 41941.19));

                series2.getData().add(new XYChart.Data(france, 45263.37));

                series2.getData().add(new XYChart.Data(italy, 117320.16));

                series2.getData().add(new XYChart.Data(usa, 14845.27));

                XYChart.Series series3 = new XYChart.Series();

                series3.setName("2005");

                series3.getData().add(new XYChart.Data(austria, 45000.65));

                series3.getData().add(new XYChart.Data(brazil, 44835.76));

                series3.getData().add(new XYChart.Data(france, 18722.18));

                series3.getData().add(new XYChart.Data(italy, 17557.31));

                series3.getData().add(new XYChart.Data(usa, 92633.68));

  

                bc.getData().addAll(series1, series2, series3);

                Scene scene = new Scene(bc, 800, 600);

                WritableImage fximage = scene.snapshot(null);

                return SwingFXUtils.fromFXImage(fximage, new BufferedImage((int) Math.ceil(fximage.getWidth()), (int) Math.ceil(fximage.getHeight()),  BufferedImage.TYPE_INT_ARGB_PRE));

            }

      });

      Platform.runLater(future);

      return future.get();

  }

}

This post has been answered by shruda on Jul 16 2014
Jump to Answer

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 13 2014
Added on Jul 10 2014
2 comments
2,722 views