2014-03-31 64 views
1

疊加我創建了以下餅圖:的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像素的綠色線條,因此從技術上講,背景會繪製出來,但稍後會在某個時刻過度渲染。

現在讓我們設置的數據集爲空,這給了預期的結果,背景是綠色: enter image description here

我在做什麼錯,爲什麼是黑字overwriteen如果我有一個有效的數據集?

我一直在努力,現在約兩個小時,卻無法解決它:/

SSCCEE:

下圖是其中MyView所註冊的Eclispe的RCP內部視圖的一部分ID,,其也存儲在全局最終變量ID中的類MyView中。

我創建視圖witht以下命令他:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(MyView.ID); 

MyView的:

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 

     } 

} 

MyPieChart:

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



} 
+1

它在JfreeChart中使用版本1.14工作。你使用的是什麼版本的JFreeChart,你可以提供一個[SSCCE](http://www.sscce.org/)?你想創建一個Eclipse插件嗎? – GrahamA

+0

我正在使用jfreechart 1.0.14。這是我接管的一個eclipse應用程序的一部分。我已經使用這個API實現了各種StackedBar圖表。我會盡快提供一個SSCCE! – Markus

+0

我使用SSCCEE更新了我的帖子。由於它是一個elcipse RCP,它不是100%自我包含的,但我盡了全力。我也可能嘗試升級我的jfreechart版本,但是我擔心它會破壞某些東西,因爲我正在使用的應用程序自幾年以來正在開發中。 – Markus

回答

0

禁用陰影生成固定的問題: )

plot.setShadowGenerator(null);