2017-10-24 1 views
1

jfreechart 극좌표 플롯을 사용하여 Java에서 하늘 음모를 작성하려고합니다. 모든 것이 잘 작동합니다. 그러나 나는 다양한 시리즈 범례 마커와 크기를 변경할 수 없었습니다.극좌표 플롯에서 범례 모양 및 글꼴을 변경할 수 없습니다.

public class Skyplot { 
     private JFrame plotFrame = null; 

     public static void main (String[] args){ 
      Skyplot sp = new Skyplot(); 
      sp.display(); 
     } 

     public Skyplot(){ 
     } 

     public void display(){ 
      javax.swing.SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
        plot(); 
       } 
      }); 
     } 

     private void plot(){ 
      // create and set up the window 
      plotFrame = new JFrame("Visualizer"); 
      plotFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
      //Display the window. 
      plotFrame.pack(); 
      plotFrame.setVisible(true); 
      plotFrame.setLocation(500, 500); 
      plotFrame.setSize(1200, 900); 

      // set up the content pane 
      Container C = plotFrame.getContentPane(); 

      Plotter pl = new Plotter(); 
      pl.setBorder(BorderFactory.createRaisedBevelBorder()); 
      pl.setBackground(Color.WHITE); 

      C.setLayout(new GridLayout(1, 1)); 
      C.add(pl); 
     } 


     private class Plotter extends JPanel { 
      private static final long serialVersionUID = 1L; 

      public Plotter(){ 
       XYDataset dataset = getXYDataset(); 

       final ChartPanel chartPanel = createChartPanel(dataset); 
       this.add(chartPanel, BorderLayout.CENTER); 
      } 

      private ChartPanel createChartPanel(XYDataset dataset) { 
       // Create chart 
       JFreeChart chart = ChartFactory.createPolarChart("Skyplot", dataset, true, true, false); 

       PolarPlot polPlot = (PolarPlot) chart.getPlot(); 
       polPlot.setRadiusMinorGridlinesVisible(false); 
       polPlot.setBackgroundPaint(Color.WHITE); 
       polPlot.setRadiusGridlinePaint(Color.DARK_GRAY); 
       polPlot.setAngleGridlinePaint(Color.BLACK); 

       DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) polPlot.getRenderer(); 
       renderer.setBaseLegendShape(new Rectangle(15,15)); 
       Font legend_font = new Font("Verdana", Font.PLAIN, 24); 
       renderer.setBaseLegendTextFont(legend_font); 

       NumberAxis rangeAxis = (NumberAxis) polPlot.getAxis(); 
       rangeAxis.setTickUnit(new NumberTickUnit(10.0)); 
       rangeAxis.setMinorTickMarksVisible(false); 
       rangeAxis.setRange(0.0, 90.0); 
       rangeAxis.setInverted(true); 


       return new ChartPanel(chart){ 
        @Override 
        public Dimension getPreferredSize() { 
         double H = plotFrame.getHeight()*0.9; 
         double W = plotFrame.getWidth()*0.9; 
         return new Dimension((int)W, (int)H); 
        } 
       }; 
      } 

      private XYDataset getXYDataset() { 
       XYSeriesCollection dataset = new XYSeriesCollection(); 

       XYSeries g01= new XYSeries("G01"); 
       series.add(35, 45); 
       dataset.addSeries(g01); 

       XYSeries g02= new XYSeries("G02"); 
       series.add(105, 73); 
       dataset.addSeries(g01); 

       XYSeries g03= new XYSeries("G03"); 
       series.add(264, 15); 
       dataset.addSeries(g03); 

       return dataset; 
      } 

     } 

    } 

어떤 생각이 왜 작동하지 않을 : 여기

내 코드에 대한 예입니다? 렌더러에 문제가 있습니까? 도움이 될 것입니다. 고맙습니다.

+0

[편집] 당신의 질문 [mcve]를 포함하십시오, 대표 데이터를 포함 당신이 묘사하는 문제를 보여주는 – trashgod

+0

@trashgod 전체 예제를 추가했습니다. – Dan

답변

1

누락 된 조각이 부모에게 말하는 것 같습니다 AbstractRenderer에서 setShapesVisible().

renderer.setShapesVisible(true); 

나는 모든 모양의 사각형과 전설 더 큰 내부의 텍스트를 만들고 싶어. here을 제안

는 사용자 정의 DrawingSupplier를 사용, 차트 및 범례에 색상과 모양을 조정합니다. 정확한 세부 정보는 사용 사례에 따라 다르지만 기본 접근 방법은 DefaultDrawingSupplier 메서드 getNextPaint()getNextShape()을 재정 의하여 선택한 색과 모양을 반환합니다.

범례 텍스트를 더 크게 만들려면 아래 그림과 같이 차트 LegendTitle을 업데이트하십시오.

LegendTitle title = chart.getLegend(); 
title.setItemFont(new Font(Font.SERIF, Font.BOLD, 24)); 

image

시험으로 :

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.Rectangle; 
import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.axis.NumberTickUnit; 
import org.jfree.chart.plot.PolarPlot; 
import org.jfree.chart.renderer.DefaultPolarItemRenderer; 
import org.jfree.chart.title.LegendTitle; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.data.xy.XYSeriesCollection; 

public class Skyplot { 

    private double[] position = null; 
    private JFrame plotFrame = null; 

    public static void main(String[] args) { 
     Skyplot sp = new Skyplot(); 
     sp.display(); 
    } 

    public Skyplot() { 
    } 

    public void display() { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       plot(); 
      } 
     }); 
    } 

    private void plot() { 
     // create and set up the window 
     plotFrame = new JFrame("Visualizer"); 
     plotFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     //Display the window. 
     plotFrame.pack(); 
     plotFrame.setSize(800, 600); 
     plotFrame.setLocationRelativeTo(null); 
     plotFrame.setVisible(true); 

     // set up the content pane 
     Container C = plotFrame.getContentPane(); 

     Plotter pl = new Plotter(); 
     pl.setBorder(BorderFactory.createRaisedBevelBorder()); 
     pl.setBackground(Color.WHITE); 

     C.setLayout(new GridLayout(1, 1)); 
     C.add(pl); 
    } 

    private class Plotter extends JPanel { 

     private static final long serialVersionUID = 1L; 

     public Plotter() { 
      XYDataset dataset = getXYDataset(); 

      final ChartPanel chartPanel = createChartPanel(dataset); 
      this.add(chartPanel, BorderLayout.CENTER); 
     } 

     private ChartPanel createChartPanel(XYDataset dataset) { 
      // Create chart 
      JFreeChart chart = ChartFactory.createPolarChart("Skyplot", dataset, true, true, false); 
      LegendTitle title = chart.getLegend(); 
      title.setItemFont(new Font(Font.SERIF, Font.BOLD, 24)); 

      PolarPlot polPlot = (PolarPlot) chart.getPlot(); 
      polPlot.setRadiusMinorGridlinesVisible(false); 
      polPlot.setBackgroundPaint(Color.WHITE); 
      polPlot.setRadiusGridlinePaint(Color.DARK_GRAY); 
      polPlot.setAngleGridlinePaint(Color.BLACK); 

      DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) polPlot.getRenderer(); 
      renderer.setBaseLegendShape(new Rectangle(15, 15)); 
      renderer.setShapesVisible(true); 

      NumberAxis rangeAxis = (NumberAxis) polPlot.getAxis(); 
      rangeAxis.setTickUnit(new NumberTickUnit(10.0)); 
      rangeAxis.setMinorTickMarksVisible(false); 
      rangeAxis.setRange(0.0, 90.0); 
      rangeAxis.setInverted(true); 

      return new ChartPanel(chart) { 
       @Override 
       public Dimension getPreferredSize() { 
        double H = plotFrame.getHeight() * 0.9; 
        double W = plotFrame.getWidth() * 0.9; 
        return new Dimension((int) W, (int) H); 
       } 
      }; 
     } 

     private XYDataset getXYDataset() { 
      XYSeriesCollection dataset = new XYSeriesCollection(); 

      XYSeries g01 = new XYSeries("G01"); 
      g01.add(35, 45); 
      dataset.addSeries(g01); 

      XYSeries g02 = new XYSeries("G02"); 
      g02.add(105, 73); 
      dataset.addSeries(g02); 

      XYSeries g03 = new XYSeries("G03"); 
      g03.add(264, 15); 
      dataset.addSeries(g03); 

      return dataset; 
     } 
    } 
} 
+0

코드의 추가 라인은 어떤 일도하지 않는 것 같습니다. 이전에 같은 결과가 나타났습니다. 내가 원하는 것은 모든 모양을 직사각형으로 만들고 범례 안에있는 텍스트를 더 크게 만드는 것입니다. – Dan

+0

나는 정교했다. – trashgod

+0

고맙습니다. 나는 시도 할 것이다. – Dan

0

나는 위에서 조언을 사용했다. 차트를 만들 수 방법은 이제 다음과 같습니다

private ChartPanel createChartPanel(XYDataset dataset) { 
       // Create chart 
       JFreeChart chart = ChartFactory.createPolarChart("Skyplot", dataset, true, true, false); 

       PolarPlot polPlot = (PolarPlot) chart.getPlot(); 
       polPlot.setRadiusMinorGridlinesVisible(false); 
       polPlot.setBackgroundPaint(Color.WHITE); 
       polPlot.setRadiusGridlinePaint(Color.DARK_GRAY); 
       polPlot.setAngleGridlinePaint(Color.BLACK); 
       polPlot.setDrawingSupplier(new DrawingSupplier() { 
        @Override 
        public Paint getNextFillPaint() { 
         // TODO Auto-generated method stub 
         return null; 
        } 

        @Override 
        public Paint getNextOutlinePaint() { 
         // TODO Auto-generated method stub 
         return null; 
        } 

        @Override 
        public Stroke getNextOutlineStroke() { 
         // TODO Auto-generated method stub 
         return null; 
        } 

        @Override 
        public Paint getNextPaint() { 
         Random rand = new Random(); 
         int r = rand.nextInt(255); 
         int g = rand.nextInt(255); 
         int b = rand.nextInt(255); 

         return new Color(r,g,b); 
        } 

        @Override 
        public Shape getNextShape() { 
         return ShapeUtilities.createDiamond(5f); 
        } 


        @Override 
        public Stroke getNextStroke() { 
         // TODO Auto-generated method stub 
         return null; 
        } 

       }); 

       NumberAxis rangeAxis = (NumberAxis) polPlot.getAxis(); 
       rangeAxis.setTickUnit(new NumberTickUnit(10.0)); 
       rangeAxis.setMinorTickMarksVisible(false); 
       rangeAxis.setRange(0.0, 90.0); 
       rangeAxis.setInverted(true); 

       LegendTitle legend = chart.getLegend(); 
       Font legend_font = new Font("Verdana", Font.BOLD, 18); 
       legend.setItemFont(legend_font); 

       return new ChartPanel(chart){ 
        @Override 
        public Dimension getPreferredSize() { 
         double H = plotFrame.getHeight()*0.9; 
         double W = plotFrame.getWidth()*0.9; 
         return new Dimension((int)W, (int)H); 
        } 
       }; 
      } 

다음과 같이 DrawingSupplier 사용의 결과는 다음과 같습니다 resulting skyplot

관련 문제