如何将JFreeChart用于自定义渲染器

时间:2017-12-13 20:37:13

标签: java charts jfreechart

第一个:我真的遇到了JFreechart的问题,主要是我真的相信这是我的错,因为我开始使用该库而没有完全理解它是如何完全运行或使用的 第二个:这些是帮助我的一些有用的资源:

check it out 1

check it out 2

check it out 3

我目前的状态:我的问题在于使用drawPrimaryLine() 在我已经建立的项目中,所以我仍然遇到连接点的问题 以我的方式,而不是顺序方式

示例:如果我按此顺序输入(10,10)和(15,15)以及(20,20)和(25,25),这就是我最终会得到的结果(左侧没有连接,右侧连接)

image to clarify

我的问题是: 1 - 当在右侧显示绘制线条时,我不希望生成或创建线条,直到添加了所有点并且点击了完成的按钮*显示在最右下方< / p>

2 - 我不希望显示行按顺序排列我希望根据某些算法显示该行,并且不是所有的点都有或者一条线将通过它,只有一行会在某些情况下通过。 因此,总而言之:并非所有的点都只基于算法连接一些。

这是我的代码:

public class x_y_2 extends JFrame {

private static final String title = "Connecting The Dots";
private XYSeries added = new XYSeries("Added");
public List ls  = new LinkedList<XYSeries>();
private XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
final XYSeriesCollection dataset = new XYSeriesCollection();

private XYPlot plot ;



public x_y_2(String s) {
    super(s);
    final ChartPanel chartPanel = createDemoPanel();
    this.add(chartPanel, BorderLayout.CENTER);
    JPanel control = new JPanel();

    JLabel label = new JLabel("Enter 'x' value");
    JTextField Field_x = new JTextField();
    Field_x.setPreferredSize( new Dimension( 100, 24 ));

    JLabel label2 = new JLabel("Enter 'y' value");
    JTextField Field_y = new JTextField();
    JLabel error = new JLabel("Importent*** in case no value is entered,value  is set to '1' ");
    error.setForeground(Color.RED);
    Field_y.setPreferredSize( new Dimension( 100, 24 ));

    control.add(label);
    control.add(Field_x);
    control.add(label2);
    control.add(Field_y);

    control.add(new JButton(new AbstractAction("Add") {

        @Override
        public void actionPerformed(ActionEvent e) {
                if (Field_x.getText().isEmpty()) {
                    Field_x.setText("1"); ; 
                }
                if (Field_y.getText().isEmpty()) {
                    Field_y.setText("1"); 
                }
            Double x = Double.parseDouble(Field_x.getText());
            Double y = Double.parseDouble(Field_y.getText());

            added.add(x,y);
            ls.add(added);

            Field_x.setText("");
            Field_y.setText("");       
        }
    }));
    control.add(error);

    control.add(new JButton(new AbstractAction("Done..") {

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setVisible(false);
            label2.setVisible(false);
            Field_x.setVisible(false);
            Field_y.setVisible(false);
            error.setVisible(false);
            PrimaryLine pr = new PrimaryLine(3);
            GraphingData graphingdata = new GraphingData(2,4,2,10);
   //         pr.drawPrimaryLine(state, g2, plot, dataset, pass, series, item, domainAxis, rangeAxis, dataArea);
            System.out.println(ls.size());
            for (int i = 0 ; i < ls.size() ; i++) {
                XYSeries xy = (XYSeries)ls.get(i);
                System.out.println(xy.getX(i) +" "+xy.getY(i));
            }       
        }
    }));
    this.add(control, BorderLayout.SOUTH);
}
private XYDataset createSampleData() {
    dataset.addSeries(added);
    return dataset;
}
private ChartPanel createDemoPanel() {
    JFreeChart jfreechart = ChartFactory.createXYLineChart(
        title, "X", "Y", createSampleData(),PlotOrientation.VERTICAL, true, true, false);
            plot    =jfreechart.getXYPlot();
            renderer.setSeriesLinesVisible(0, true);
        renderer.setSeriesShapesVisible(0, true);            
        plot.setRenderer(renderer);
            return new ChartPanel(jfreechart);
 }}

第二课:        公共类GraphingData扩展了JPanel {

   double x_st , y_st , x_ed, y_ed = 0;
   public Graphics2D g2 ;
   public GraphingData(double x_st,double y_st,double x_ed,double y_ed) {
    this.x_st = x_st ; 
    this.y_st = y_st;
    this.x_ed = x_ed;
    this.y_ed = y_ed;
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

    g2.draw(new Line2D.Double(x_st,y_st,x_ed, y_ed));

}
    }

第三类:

   public class PrimaryLine extends XYLineAndShapeRenderer {
    private final int anchor;

    public PrimaryLine(int acnchor) {
        this.anchor = acnchor;
    }

    @Override
    protected void drawPrimaryLine(XYItemRendererState state, Graphics2D g2,
        XYPlot plot, XYDataset dataset, int pass, int series, int item,
        ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) {
        if (item == anchor) {
            return;
        }
    }


    public  void chart() {
        PrimaryLine r = new PrimaryLine(8);
        XYPlot plot = new XYPlot(createSampleData(),new NumberAxis("X"), new 
        NumberAxis("Y"), r);
            JFreeChart chart = new JFreeChart(plot);
    }


   private XYDataset createSampleData() {
   XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
   XYSeries added = new XYSeries("added");
   added.add(4,2);
   added.add(2,1);
   xySeriesCollection.addSeries(added);
   return xySeriesCollection;
   }
  }

我会很乐意为你提供任何帮助。

0 个答案:

没有答案