2016-07-01 9 views
1

JFreeChartを使用してBoxAndWhiskerRendererプロットを作成しました。これは、自動的に一種の凡例を作成しているようです(添付のピクチャーを参照)。BoxAndWhiskerプロットの凡例をカスタマイズする方法

enter image description here

この伝説の外枠を削除して、凡例項目のラベルのフォントをカスタマイズする方法はありますか?ここで

は私のコードの例です:

//Get the desired BoxAndWhiskerCategoryDataset from a LinkedHashMap 
BoxAndWhiskerCategoryDataset dataset = values.get(b); 

//Create X axis 
CategoryAxis xAxis = new CategoryAxis(); 
xAxis.setAxisLineVisible(false); 

//Create Y axis 
NumberAxis yAxis = new NumberAxis(b.getLabel()); 
yAxis.setAxisLineVisible(false); 
yAxis.setTickLabelFont(FDFont.getFont(12f)); 
yAxis.setLabelFont(FDFont.getFont()); 
yAxis.setLabelPaint(FDPalette.secondaryText); 
yAxis.setTickLabelPaint(FDPalette.secondaryText); 
yAxis.setAutoRangeIncludesZero(false); 

//Create renderer 
BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer(); 
int count = 0; 
for(Map.Entry<Integer,Color> map : clusterColor.entrySet()){ 
    //Set color for the series (I have a previously created map which links colors and series) 
    renderer.setSeriesPaint(count,map.getValue()); 
    count++; 
} 
renderer.setFillBox(true); 
renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator()); 
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer); 

JFreeChart chart = new JFreeChart(plot); 
chart.setBackgroundPaint(white); 
chart.setBorderVisible(false); 

ChartPanel chartPanel = new ChartPanel(chart); 
chartPanel.setPreferredSize(new java.awt.Dimension(300, 270)); 
+0

含まれるようにあなたの質問を編集してください現在のアプローチを示す[mcve] [example](http://stackoverflow.com/q/6844759/230513) 。 – trashgod

答えて

2

hereを示すように、最も簡単なJFreeChartコンストラクタは、デフォルトで凡例を追加します。そのためのコードはhereと表示されています。あなたの望むフレーム、色、位置を単に代用してください。

このexampleから始めて、次の変更は、以下に示すチャート生成:このデフォルトの伝説に比較

private void createChartPanel() { 
    … 
    JFreeChart chart = new JFreeChart("BoxAndWhiskerDemo", plot); 
    LegendTitle legend = chart.getLegend(); 
    legend.setFrame(new LineBorder(Color.white, new BasicStroke(1.0f), 
     new RectangleInsets(1.0, 1.0, 1.0, 1.0))); 
    legend.setItemFont(legend.getItemFont().deriveFont(16f)); 
    chartPanel = new ChartPanel(chart); 
} 

image

を:

image

+0

関連[例](http://stackoverflow.com/q/15163545/230513)も参照してください。 – trashgod

+0

あなたの提案に感謝します。あなたが投稿した例をテストする際に問題があります。例えば。 'LegendTitle legend = chart.getLegend();'を試してみると、「型不一致:LegendTitleに変換できません」と表示され、LegendTitleに 'Legend'をキャストできません。私は自分のコードの例を含めて私の前の投稿を編集しました。 – Roberto

+0

あなたはどのようにキャストできませんか?あなたの質問を編集して、 'getLegend()'を呼び出す現在のアプローチを示す[mcve]を含めてください。あなたはカスタムフォントと色をスキップします。 – trashgod

関連する問題