如何在JFreeChart中将点转移到行?

时间:2017-12-09 20:23:57

标签: java jfreechart

我在将由JFreeChart生成的点转换为一行时遇到问题。

首先,有一些来源真正帮助我达到了这一点

其次,这是我的代码

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

}}

这是输出中的图像

imge of the output

我的主要问题是点数。我该怎么做?我尝试过使用这种方法

   renderer.setSeriesLinesVisible

但是,我没有为我工作。

编辑:

解决方案只是简单地说:

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

取代这个:

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

1 个答案:

答案 0 :(得分:2)

如上所述here,您选择的ChartFactory使用显示形状但不显示线条的XYLineAndShapeRenderer。你可以

  • 选择其他工厂,如here

  • 所示
  • 编写您自己的工厂,如here所示,或

  • 获取对渲染器的引用并明确显示这些行:

    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