Bar charts turned 3D after upgrade to 1.0.13

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
julie
Posts: 7
Joined: Fri Oct 05, 2007 9:36 am

Bar charts turned 3D after upgrade to 1.0.13

Post by julie » Wed Oct 07, 2009 4:51 pm

I upgraded jfreechart from 1.0.6 to 1.0.13, and all of my bar charts turned 3D! Has anyone else had this problem? I searched the forums and the docs and didn't see any similar issues.

I am impressed, however, that everything else worked perfectly. :)

Here is the graph in our current website:

Image

Here is the graph after I upgrade to 1.0.13:

Image

Here is a snippet of the code that generates those graphs:

Code: Select all

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.util.List;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.intermine.objectstore.ObjectStore;
import org.intermine.util.TypeUtil;
import org.intermine.web.logic.bag.InterMineBag;
import org.intermine.web.logic.widget.config.GraphWidgetConfig;
import org.jfree.chart.ChartColor;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.imagemap.ImageMapUtilities;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.CategoryURLGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;

/**
 * @author "Xavier Watkins"
 *
 */
public class GraphWidget extends Widget
{
    private static final Logger LOG = Logger.getLogger(GraphWidget.class);
    private int notAnalysed = 0;
    private DataSetLdr dataSetLdr;
    private InterMineBag bag;
    private ObjectStore os;
    private String fileName = null;
    private String imageMap = null;
    private String selectedExtraAttribute;
    private static final ChartColor BLUE = new ChartColor(47, 114, 255);
    private static final ChartColor LIGHT_BLUE = new ChartColor(159, 192, 255);
    private static final ChartColor DARK_BLUE = new ChartColor(39, 77, 216);

// ---- snip ----

    /**
     * {@inheritDoc}
     */
    public void process() {

        String dataSetLoader = config.getDataSetLoader();
        Class<?> clazz = TypeUtil.instantiate(dataSetLoader);

// --- snip ---

        if (dataSetLdr == null || dataSetLdr.getDataSet() == null) {
            LOG.error("No data found for graph widget");
            return;
        }
        
        JFreeChart chart = null;
        Dataset graphDataSet = dataSetLdr.getDataSet();
        String graphType = ((GraphWidgetConfig) config).getGraphType();

// --- snip ---

        if (StringUtils.isNotEmpty(graphType) && graphType.equals("BarChart")) {
            chart = ChartFactory.createBarChart(config.getTitle(), // chart title
                                                ((GraphWidgetConfig) config).getDomainLabel(),
                                                ((GraphWidgetConfig) config).getRangeLabel(),
                                                (CategoryDataset) graphDataSet, // data
                                                PlotOrientation.VERTICAL, true, true, // tooltips?
                                                false // URLs?
            );

            if (selectedExtraAttribute != null
                            && !selectedExtraAttribute.startsWith("any")) {
                TextTitle subtitleText = new TextTitle(selectedExtraAttribute);
                subtitleText.setFont(new Font("SansSerif", Font.ITALIC, 10));
                chart.addSubtitle(subtitleText);
            }
            CategoryPlot categoryPlot = chart.getCategoryPlot();
            CategoryItemRenderer categoryRenderer = new BarRenderer();
            ((BarRenderer) categoryRenderer).setItemMargin(0);
            categoryRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
            ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
            categoryRenderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(
            ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER));
            categoryPlot.setRenderer(categoryRenderer);
            
            // rotate the category labels
            categoryPlot.getDomainAxis().setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
            
            setURLGen(categoryRenderer);
            
            ((BarRenderer) categoryRenderer).setNegativeItemLabelPositionFallback(
            new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.BASELINE_LEFT));

            formatBarCharts(categoryPlot);
        }

        if (chart.getTitle() != null) {
            chart.getTitle().setFont(new Font("SansSerif", Font.BOLD, 12));
        }
        
        chart.setPadding(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        chart.setAntiAlias(false); // render the chart faster, but won't look as good.
        ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

        // generate the image and imagemap
        try {
            fileName = ServletUtilities.saveChartAsPNG(chart,
                       ((GraphWidgetConfig) config).getWidth(),
                       ((GraphWidgetConfig) config).getHeight(), info,
                       ((GraphWidgetConfig) config).getSession());
        } catch (IOException e) {
            throw new RuntimeException("error rendering html", e);
        }

        imageMap = ImageMapUtilities.getImageMap("chart" + fileName, info);
}

    private void formatBarCharts(CategoryPlot categoryPlot) {
        if (categoryPlot != null) {
            
            // display values for each column
            CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator();
            categoryPlot.getRenderer().setBaseItemLabelsVisible(true);
            categoryPlot.getRenderer().setBaseItemLabelGenerator(generator);
            categoryPlot.getRenderer().setBaseToolTipGenerator(new ToolTipGenerator());

            NumberAxis rangeAxis = (NumberAxis) categoryPlot.getRangeAxis();
            rangeAxis.setUpperMargin(0.15);
            rangeAxis.setLowerMargin(0.15);
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

            Font labelFont = new Font("SansSerif", Font.BOLD, 12);
            categoryPlot.getDomainAxis().setLabelFont(labelFont);
            rangeAxis.setLabelFont(labelFont);
            categoryPlot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10.0f);

            categoryPlot.getRenderer().setSeriesPaint(0, BLUE);    
            categoryPlot.getRenderer().setSeriesPaint(1, LIGHT_BLUE);
            categoryPlot.getRenderer().setSeriesPaint(2, DARK_BLUE);

            categoryPlot.getRenderer().setSeriesOutlineStroke(1, new BasicStroke(0.0F));
            
            categoryPlot.setBackgroundPaint(Color.white);
            categoryPlot.setDomainGridlinePaint(Color.LIGHT_GRAY);
            categoryPlot.setDomainGridlinesVisible(true);
            categoryPlot.setRangeGridlinePaint(Color.LIGHT_GRAY);
            
        }
    }

// --- snip ---

Any thoughts?

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by RichardWest » Wed Oct 07, 2009 5:03 pm

You can restore the old theme via:

Code: Select all

StandardChartTheme legacyTheme = StandardChartTheme.createLegacyTheme();
legacyTheme.applyToCategoryPlot(plot);
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

julie
Posts: 7
Joined: Fri Oct 05, 2007 9:36 am

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by julie » Thu Oct 08, 2009 9:52 am

I added this to my code:

Code: Select all

StandardChartTheme legacyTheme  = (StandardChartTheme) StandardChartTheme.createLegacyTheme();
legacyTheme.applyToCategoryPlot(categoryPlot);
And I get this message:
The method applyToCategoryPlot(CategoryPlot) from the type StandardChartTheme is not visible
I looked at the API and that method is protected, not public. What am I doing wrong?

Thank you!!

julie
Posts: 7
Joined: Fri Oct 05, 2007 9:36 am

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by julie » Thu Oct 08, 2009 1:38 pm

I got this idea from another post:

Code: Select all

ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
It turns the bar chart to be 2D, but it still has a shadow, white background, etc.

Image

Is there a way to revert everything? Or should I just set the background to be grey, etc manually?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by david.gilbert » Thu Oct 08, 2009 9:54 pm

You can get rid of the bar shadows by putting this code somewhere near the start of your program (BEFORE the first chart is created):

Code: Select all

BarRenderer.setDefaultShadowsVisible(false);
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

julie
Posts: 7
Joined: Fri Oct 05, 2007 9:36 am

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by julie » Fri Oct 09, 2009 9:30 am

Thank you!! That's exactly what I needed.

I really, really appreciate your help! :D

Sizzo
Posts: 3
Joined: Thu Oct 15, 2009 1:29 am
antibot: No, of course not.
Location: Bonn - Germany

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by Sizzo » Thu Oct 15, 2009 1:39 am

david.gilbert wrote:You can get rid of the bar shadows by putting this code somewhere near the start of your program (BEFORE the first chart is created):

Code: Select all

BarRenderer.setDefaultShadowsVisible(false);
Hi Folks,

thanks for the ChartFactory.setChartTheme() tip. That made my day.
I also want to get rid of the shadows, but trying your approach I only ran into:
java.lang.NoSuchMethodError: org.jfree.chart.renderer.category.BarRenderer.setDefaultShadowsVisible()

My BarCharts screwed up when I updated to 1.0.12. Now I already updated to 1.0.13 to get rid of the shadows, but no success so far.
Is there an other way, to disable those shadows?

Any help appreciated.

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

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by paradoxoff » Thu Oct 15, 2009 7:39 am

Maybe you have just forgotten the "true" parameter? :wink:

Sizzo
Posts: 3
Joined: Thu Oct 15, 2009 1:29 am
antibot: No, of course not.
Location: Bonn - Germany

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by Sizzo » Thu Oct 15, 2009 9:47 am

Unhappily it wasn't that easy to find, but my mistake.
Using JFreeChart in a webapplication, I forgot that our war archive gets updated by replacing jars with newer version and adding new jars. Since the names of the jfreechart libraries contain the version number, I've got both versions in the war file.

Nethertheless I'm still interested in an other way to disable the shadows beside the BarRenderer.setDefaultShadowsVisible() function.
---
Sizzo Stroh
Deutsche Telekom AG

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

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by paradoxoff » Thu Oct 15, 2009 10:17 am

You have a couple of opportunities:
- change the source and set the defaultShadowsVisible flag to false
- disable the shadows for the individual BarRenderer instances (barRenderer.setShadowVisible(false), note that the setter is using a "single shadow" whereas the flag itself and the getter refer to "shadows". If you are using the ChartFactory to create the charts and if you are using the factory methods of the StandardChartTheme to create your ChartTheme, the cleanest way is probably to call setShadowVisible(true) directly on the ChartTheme: ((StandardChartTheme)StandardChartTheme.createJFreeTheme()).setShadowVisible(true). Don´t use the legacy theme since that will not change the defaults, and the shadows will remain visible. You could also create your own StandardChartTheme implementation.
- create your own BarRenderer that is setting the flag to false during instantiation.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by skunk » Thu Oct 15, 2009 2:17 pm

Sizzo wrote:Nethertheless I'm still interested in an other way to disable the shadows beside the BarRenderer.setDefaultShadowsVisible() function.
I use this to disable the shadows in XYBarCharts

Code: Select all

XYBarRenderer.setDefaultBarPainter(new StandardXYBarPainter() {
    public void paintBarShadow(Graphics2D g2, XYBarRenderer renderer, int row,
                        int column, RectangularShape bar, RectangleEdge base,
                        boolean pegShadow) {}
});
Make the appropriate edits for BarRenderer

Sizzo
Posts: 3
Joined: Thu Oct 15, 2009 1:29 am
antibot: No, of course not.
Location: Bonn - Germany

Re: Bar charts turned 3D after upgrade to 1.0.13

Post by Sizzo » Fri Oct 16, 2009 12:07 am

As I modify every chart layout on the fly, I'm using this approach:
paradoxoff wrote:- disable the shadows for the individual BarRenderer instances (barRenderer.setShadowVisible(false)
Thanks a lot for all your ideas and help.
---
Sizzo Stroh
Deutsche Telekom AG

Locked