2016-09-02 5 views
1

JFreeChartを使ってボックスプロット(下側のコード)を作っています。私は(私はそれらをしたいと)彼らは広い描かれ、正しくセンタリングされているボックスあたりAA着色を追加しない場合:JFreechartボックスの色を変えるときのボックスのサイズを変更する

enter image description here

を私はその後、x軸ラベルによってそれらを色付けする場合しかし、彼らは小さく取得し、もはや正しくセンタリングされていません。

enter image description here

は、どのように私は2番目の図のが、最初のボックスのサイズとカラーリングを得ることができますか?


package test; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.HashMap; 

import org.jfree.chart.ChartPanel; 
import org.jfree.chart.ChartUtilities; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.axis.CategoryAxis; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.labels.BoxAndWhiskerToolTipGenerator; 
import org.jfree.chart.plot.CategoryPlot; 
import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer; 
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset; 

public class test { 
    public static void main(String[] args) throws Exception { 
    DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset(); 

    // example data 
    HashMap<String, ArrayList<Double>> test = new HashMap<String, ArrayList<Double>>(); 
    test.put("A",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2))); 
    test.put("B",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2))); 
    test.put("C",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2))); 
    test.put("D",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2))); 
    test.put("E",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2))); 
    for (String k : test.keySet()){ 
     /* change to 
     *  String xAxisLabel = ""; 
     * to get wide plot 
     */ 
     String xAxisLabel = k; 
     dataset.add(test.get(k), xAxisLabel, k);// + beta of interactionterm"); 
    } 
    final CategoryAxis xAxis = new CategoryAxis("Example x-axis"); 
    final NumberAxis yAxis = new NumberAxis("Example y-axis"); 
    yAxis.setAutoRangeIncludesZero(false); 
    final BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer(); 
    renderer.setFillBox(true); 
    renderer.setSeriesToolTipGenerator(1, new BoxAndWhiskerToolTipGenerator()); 
    renderer.setMeanVisible(false); 
    final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer); 

    final JFreeChart chart = new JFreeChart(
      "Example", 
      plot 
      ); 
    final ChartPanel chartPanel = new ChartPanel(chart); 
    chartPanel.setPreferredSize(new java.awt.Dimension(3000,1800)); 
    ChartUtilities.saveChartAsPNG(new File("test.png"), chart, 1000, 600); 
    } 
} 

答えて

1

違いは、あなたの最初の画像は1シリーズを持っていますが、2番目の画像は5つのシリーズを持っていることです。シリーズをたくさん追加する代わりに、トップ写真のような5つのシリーズを1つ追加します。 getItemPaint()を上書きするカスタムBoxAndWhiskerRendererを使用すると、XYLineAndShapeRendererの場合、hereと表示されるように異なる色を表示できます。

編集:一致する凡例を取得するには、新しいDrawingSupplierthisなど)が必要です。

+0

これは良い例です。 http://www.java2s.com/Code/Java/Chart/JFreeChartBarChartDemo3differentcolorswithinaseries.htm – SamTew

+0

ああ、それは私を思い出させる。一致する凡例を取得するには、新しい['DrawingSupplier'](https://stackoverflow.com/a/13010394/261156)が必要です。 –

関連する問題