How to use JFreeChart for custom renderer

2019-08-20 07:21发布

问题:

first of: I am really having a problem with JFreechart and mainly I really believe that this is my fault because I start using the library without fully understanding how it fully function or use second of: these are some helpful resource that helped me :

check it out 1

check it out 2

check it out 3

my current state : my problem is in making use of the drawPrimaryLine() in my already build project so I am still having a problem in connecting the dots in my way, not in a sequence way

example: if I enter (10,10) and (15,15) and (20,20) and (25,25) in this sequence, this is what I will end up with (the left side without connecting, the right side with connecting)

My problem is: 1 - when drawing a line is showing on the right side, I don't want the line to be generated or created until all of the points are add and the button done has been clicked *show in the most bottom right side

2 - I don't want the showing line to be in a sequence way I want the line to be shown base on some algorithm and not all dots will have or a line will pass through it, only a line will pass in some case. so, in summary: not all dots will be connected only some based on an algorithm.

this is my code :

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

second class : public class GraphingData extends 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));

}
    }

Third Class :

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

Any kinda of help I would be greatfull for .