JFreeChartsFX setColor() problem (renderer/pass)

A discussion forum for FXGraphics2D (adds a Java2D API to the JavaFX Canvas).
Post Reply
olbrich
Posts: 16
Joined: Fri Dec 27, 2013 9:39 am
antibot: No, of course not.

JFreeChartsFX setColor() problem (renderer/pass)

Post by olbrich » Tue Sep 29, 2020 12:39 pm

Hi David,
mee again after several years only enjoying the work with JFreeCharts (and JFreeReports).

Now we are investigating in JavaFX, of course trying to upgrade JFreeCharts FatClient Apps.

1. Am i right here to discuss JfreeCharts FX ? Else please redirect me.

2. Trying to upgrade we discovered possibly a little optimization BUG in org.jfree.fx.FXGraphics2D.setColor().
What do you meen? Se this section of code:


/* start of my Code ******************************************************/
// plot.setSectionPaint("Others", Color.BLUE);
// plot.setSectionPaint("Samsung", Color.RED);
// plot.setSectionPaint("Apple", Color.GREEN);
// plot.setSectionPaint("Nokia", Color.YELLOW);
// activate Outline drawing
plot.setDefaultSectionOutlinePaint(Color.WHITE);
plot.setSectionOutlinesVisible(true);
plot.setDefaultSectionOutlineStroke(new BasicStroke(4.0f));
/* end of my Code ******************************************************/




@Override
public void setColor(Color c) {
// if (c == null || c.equals(this.color)) { /* elimating this will solve our problem */
if (c == null) {
return;
}
this.color = c;
this.paint = c;
javafx.scene.paint.Color fxcolor = awtColorToJavaFX(c);
this.gc.setFill(fxcolor);
this.gc.setStroke(fxcolor);
}

We render multiple series in a chart with different individual customizable settings for colors etc.
Therein a bar is a curveElement and has a content (fill()) and a border (outline) each has different color settings.
When the color is paint and an instance of GradientPaint we run into a problem.

This can be simple reproduced with a simple modification of your example app:
{code}
/* ====================
* PieChartFXDemo1.java
* ====================
*
* Copyright (c) 2014, 2017, Object Refinery Limited.
* All rights reserved.
*
* https://github.com/jfree/jfree-fxdemos
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the Object Refinery Limited nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

package org.jfree.chart.fx.demo;

import static javafx.application.Application.launch;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.geom.Point2D;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.fx.ChartViewer;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.ui.HorizontalAlignment;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
* A pie chart demo in JavaFX.
*/
public class PieChartFXDemo1 extends Application {

/**
* Creates a sample dataset.
*
* Source: http://www.bbc.co.uk/news/business-15489523
*
* @return A sample dataset.
*/
private static PieDataset createDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Samsung", new Double(27.8));
dataset.setValue("Others", new Double(55.3));
dataset.setValue("Nokia", new Double(16.8));
dataset.setValue("Apple", new Double(17.1));
return dataset;
}

/**
* Creates a chart.
*
* @param dataset the dataset.
*
* @return A chart.
*/
private static JFreeChart createChart(PieDataset dataset) {

JFreeChart chart = ChartFactory.createPieChart(
"Smart Phones Manufactured / Q3 2011", dataset);

// set a custom background for the chart
chart.setBackgroundPaint(new GradientPaint(new Point(0, 0),
new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY));

// customise the title position and font
TextTitle t = chart.getTitle();
t.setHorizontalAlignment(HorizontalAlignment.LEFT);
t.setPaint(new Color(240, 240, 240));
t.setFont(new Font("Arial", Font.BOLD, 26));

PiePlot plot = (PiePlot) chart.getPlot();
plot.setBackgroundPaint(null);
plot.setInteriorGap(0.04);
plot.setOutlinePaint(Color.WHITE);
plot.setOutlineStroke(new BasicStroke(4));;
plot.setOutlineVisible(false);

// use gradients and white borders for the section colours
plot.setSectionPaint("Others", createGradientPaint(new Color(200, 200, 255), Color.BLUE));
plot.setSectionPaint("Samsung", createGradientPaint(new Color(255, 200, 200), Color.RED));
plot.setSectionPaint("Apple", createGradientPaint(new Color(200, 255, 200), Color.GREEN));
plot.setSectionPaint("Nokia", createGradientPaint(new Color(200, 255, 200), Color.YELLOW));

/* start of my Code ******************************************************/
// plot.setSectionPaint("Others", Color.BLUE);
// plot.setSectionPaint("Samsung", Color.RED);
// plot.setSectionPaint("Apple", Color.GREEN);
// plot.setSectionPaint("Nokia", Color.YELLOW);
// activate Outline drawing
plot.setDefaultSectionOutlinePaint(Color.WHITE);
plot.setSectionOutlinesVisible(true);
plot.setDefaultSectionOutlineStroke(new BasicStroke(4.0f));
/* end of my Code ******************************************************/

// customise the section label appearance
plot.setLabelFont(new Font("Courier New", Font.BOLD, 20));
plot.setLabelLinkPaint(Color.WHITE);
plot.setLabelLinkStroke(new BasicStroke(2.0f));
plot.setLabelOutlineStroke(null);
plot.setLabelPaint(Color.WHITE);
plot.setLabelBackgroundPaint(null);

// add a subtitle giving the data source
TextTitle source = new TextTitle("Source: http://www.bbc.co.uk/news/business-15489523",
new Font("Courier New", Font.PLAIN, 12));
source.setPaint(Color.WHITE);
source.setPosition(RectangleEdge.BOTTOM);
source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
chart.addSubtitle(source);
return chart;

}

/**
* A utility method for creating gradient paints.
*
* @param c1 color 1.
* @param c2 color 2.
*
* @return A radial gradient paint.
*/
private static RadialGradientPaint createGradientPaint(Color c1, Color c2) {
Point2D center = new Point2D.Float(0, 0);
float radius = 200;
float[] dist = {0.0f, 1.0f};
return new RadialGradientPaint(center, radius, dist,
new Color[] {c1, c2});
}

@Override
public void start(Stage stage) throws Exception {
PieDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartViewer viewer = new ChartViewer(chart);
stage.setScene(new Scene(viewer));
stage.setTitle("JFreeChart: PieChartFXDemo1.java");
stage.setWidth(700);
stage.setHeight(390);
stage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}
{code}

Post Reply