问题描述
我在将由JFreeChart生成的点转换为行时遇到问题.
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
- 使用JfreeChart动态将点添加到XYSeries
- JFreechart烛台图在拖动时的行为异常
- http://www.java2s.com/Code/Java/Chart /JFreeChartXYLineAndShapeRendererDemo.htm
- Adding points to XYSeries dynamically with JfreeChart
- JFreechart candlestick chart weird behaviour on drag
- http://www.java2s.com/Code/Java/Chart/JFreeChartXYLineAndShapeRendererDemo.htm
第二,这是我的代码
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);
}}
这是来自输出的图像
我的主要问题是计算点数.我该怎么做?我尝试做类似使用这种方法的事情
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.
解决方案只不过是这样:
Solution is simply to put this:
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);
推荐答案
如此处所述,您选择的ChartFactory
使用 XYLineAndShapeRenderer
显示形状,但不显示线条.你可以
As noted here, your chosen ChartFactory
uses an XYLineAndShapeRenderer
that displays shapes but not lines. You can
-
选择其他工厂,如此处,
写您自己的工厂,如此处或
获取对渲染器的引用并使行清晰可见:
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);
}
这篇关于如何在JFreeChart中将点转移到线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!