JFreeChart:图表背景用黑色覆盖

时间:2014-03-31 13:51:53

标签: charts jfreechart

我创建了以下饼图:

public class MyPieChart extends ChartComposite {

public MyPieChart(Composite parent, int style, CategoryToPieDataset dataset, ProductivityViewCtrl control) {
    super(/* Composite comp */parent,
    /* int style */style,
    /* JFreeChart jfreechart */null,
    /* int width */10,
    /* int height */10,
    /* int minimumDrawW */1,
    /* int minimumDrawH */1,
    /* int maximumDrawW */Integer.MAX_VALUE,
    /* int maximumDrawH */Integer.MAX_VALUE,
    /* boolean usingBuffer */false,
    /* boolean properties */false,
    /* boolean save */false,
    /* boolean print */false,
    /* boolean zoom */false,
    /* boolean tooltips */true);


    ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow"));


    String machineName = "Dummy";
    JFreeChart chart = ChartFactory.createPieChart(machineName, // chart title
            createDataset(), // data
            true, // include legend
            true, false);


    this.setChart(chart);
    chart.setBackgroundPaint(Color.LIGHT_GRAY.brighter());              
    chart.setAntiAlias(true);

    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(Color.GREEN);

    plot.setInteriorGap(0.04);
    plot.setOutlineVisible(false);

    // Color between the segments
    plot.setBaseSectionOutlinePaint(Color.BLACK);
    plot.setSectionOutlinesVisible(true);
    plot.setBaseSectionOutlineStroke(new BasicStroke(2.0f));

    plot.setLabelFont(new Font("Tahoma", Font.BOLD, 20));
    plot.setLabelLinkPaint(Color.WHITE);
    plot.setLabelLinkStroke(new BasicStroke(2.0f));
    plot.setLabelOutlineStroke(null);
    plot.setLabelPaint(Color.WHITE);
    plot.setLabelBackgroundPaint(null);

    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("({0}) {2}"));
    plot.setToolTipGenerator(new StandardPieToolTipGenerator("{0}: {2}"));

    plot.setNoDataMessage("No data available");
    plot.setIgnoreZeroValues(true);

}

private static PieDataset createDataset() {
    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("One", new Double(43.2));
    dataset.setValue("Two", new Double(10.0));
    dataset.setValue("Three", new Double(27.5));
    dataset.setValue("Four", new Double(17.5));
    dataset.setValue("Five", new Double(11.0));
    dataset.setValue("Six", new Double(19.4));
    return dataset;
}

显示此图表时,我得到以下结果: enter image description here

如您所见,背景不是绿色而是黑色。如果你仔细观察图表的底部,你可以看到一条1像素的绿线,所以从技术上来说,背景是绘制的,但在某些时候会过度压缩。

现在让我们将数据集设置为null,这会得到预期的结果,背景为绿色: enter image description here

我做错了什么,如果我有一个有效的数据集,为什么背景会用黑色覆盖?

我现在已经尝试了大约两个小时但无法解决它:/

SSCCEE:

该图表是eclispe RCP内部视图的一部分,MyViewID,下注册,该MyView也存储在全局最终变量ID的类PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(MyView.ID); 中}。

我使用以下命令创建视图:

package at.mypackage.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

    import at.mypackage.charts.MyPieChart;


    public class MyView extends ViewPart {

        public static final String ID = "at.mypackage.views.MyView";

        @Override
        public void createPartControl(Composite parent) {
            parent.setLayoutData(new FillLayout(SWT.BORDER));
            MyPieChart chart = new MyPieChart(parent, SWT.BORDER);
        }

        @Override
        public void setFocus() {
            // TODO Auto-generated method stub

        }

}

MyView的:

package at.mypackage.charts;


import java.awt.Color;

import org.eclipse.swt.widgets.Composite;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public class MyPieChart extends ChartComposite {

    public MyPieChart(Composite parent, int style) {
        super(/* Composite comp */parent,
        /* int style */style,
        /* JFreeChart jfreechart */null,
        /* int width */10,
        /* int height */10,
        /* int minimumDrawW */1,
        /* int minimumDrawH */1,
        /* int maximumDrawW */Integer.MAX_VALUE,
        /* int maximumDrawH */Integer.MAX_VALUE,
        /* boolean usingBuffer */false,
        /* boolean properties */false,
        /* boolean save */false,
        /* boolean print */false,
        /* boolean zoom */false,
        /* boolean tooltips */true);

        JFreeChart chart = ChartFactory.createPieChart("Dummy", // chart title
                createDataset(), // data
                true, // include legend
                true, false);

        this.setChart(chart);
        chart.setBackgroundPaint(Color.LIGHT_GRAY.brighter());              

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setBackgroundPaint(Color.GREEN);       

    }

    private static PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("One", new Double(43.2));
        dataset.setValue("Two", new Double(10.0));
        dataset.setValue("Three", new Double(27.5));
        dataset.setValue("Four", new Double(17.5));
        dataset.setValue("Five", new Double(11.0));
        dataset.setValue("Six", new Double(19.4));
        return dataset;
    }



}

MyPieChart:

{{1}}

1 个答案:

答案 0 :(得分:0)

禁用阴影生成器解决了问题:)

plot.setShadowGenerator(null);