4

I am having a problem converting dots, which is generated by JFreeChart, into a line.

First of all, there some source that really helped me reach this point

Secondly , this is my code

  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.EventQueue;
  import java.awt.event.ActionEvent;
  import java.util.*;
  import javax.swing.AbstractAction;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import org.jfree.chart.*;
  import org.jfree.chart.axis.NumberAxis;
  import org.jfree.chart.plot.PlotOrientation;
  import org.jfree.chart.plot.XYPlot;
  import org.jfree.chart.renderer.xy.XYItemRenderer;
  import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
  import org.jfree.data.xy.XYDataset;
  import org.jfree.data.xy.XYSeries;
  import org.jfree.data.xy.XYSeriesCollection;



public class x_y_2 extends JFrame {

private static final String title = "Connecting The Dots";
private XYSeries added = new XYSeries("Added");
private LinkedList<XYSeries> ls = new LinkedList<>();

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("");       
        }
    }));
    this.add(control, BorderLayout.SOUTH);
    control.add(error);
}
private ChartPanel createDemoPanel() {
    JFreeChart jfreechart = ChartFactory.createScatterPlot(
        title, "X", "Y", createSampleData(),
        PlotOrientation.VERTICAL, true, true, false);

    return new ChartPanel(jfreechart);
}


private XYDataset createSampleData() {
    XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
    xySeriesCollection.addSeries(added);
    return xySeriesCollection;
}

public static void main(String args[]) {
            x_y_2 demo = new x_y_2(title);
            demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            demo.pack();
            demo.setLocationRelativeTo(null);
            demo.setVisible(true);

}}

this is an image from the output

imge of the output

My main problem is counting the dots. How can I do it? I have tried doing something like using this method

   renderer.setSeriesLinesVisible

but, I didn't work for me.

EDIT:

Solution is simply to put this:

JFreeChart jfreechart = ChartFactory.createXYLineChart(
    title, "X", "Y", createSampleData(),
    PlotOrientation.VERTICAL, true, true, false);

in place of this:

JFreeChart jfreechart = ChartFactory.createScatterPlot(
    title, "X", "Y", createSampleData(),
    PlotOrientation.VERTICAL, true, true, false);
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
OnlySalman
  • 112
  • 1
  • 12

1 Answers1

3

As noted here, your chosen ChartFactory uses an XYLineAndShapeRenderer that displays shapes but not lines. You can

  • Choose a different factory, as shown here,

  • Write you own factory, as shown here, or

  • Get a reference to the renderer and make the lines visible explicitly:

    private ChartPanel createDemoPanel() {
        JFreeChart jfreechart = ChartFactory.createScatterPlot(
            title, "X", "Y", createSampleData(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = jfreechart.getXYPlot();
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseLinesVisible(true);
        return new ChartPanel(jfreechart);
    }
    

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • you are outstanding! I have seen a lot of your answers, thanks so much. I have another question if you got the time .. is it possible to draw the dot only, then after adding all the dot and clicking done algorithm will start and only draw a line to some point but not all of them, I am really having a problem with this part, any help I would be thankful for, so simply my question is: all the point are added can I draw a line to some but not all ? – OnlySalman Dec 10 '17 at 21:44
  • You'll need a custom renderer, for [example](https://stackoverflow.com/a/29243713/230513); cite this answer if you pose a new question. – trashgod Dec 11 '17 at 01:00