jfreechar.getParent()没有这个方法
时间: 2024-03-21 09:42:58 浏览: 40
非常抱歉,我犯了一个错误。JFreeChart没有getParent()方法。如果你想获取ChartPanel对象,你可以在创建ChartPanel时将JFreeChart对象作为参数传递进去,例如:
```
ChartPanel chartPanel = new ChartPanel(chart);
```
这样,你就可以通过chartPanel对象访问JFreeChart对象的各种属性,例如获取宽度和高度:
```
int width = chartPanel.getWidth();
int height = chartPanel.getHeight();
```
请注意,这里的chart是你的JFreeChart对象。如果你已经创建了JFreeChart对象,你可以将它替换为你的对象。
相关问题
生成一个曲线图JFreechar对象
好的,以下是在Java中创建曲线图JFreeChart对象的示例代码:
```java
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import javax.swing.*;
import java.awt.*;
public class LineChart extends JFrame {
public LineChart(String title) {
super(title);
JFreeChart chart = createChart();
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
private JFreeChart createChart() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, "Series 1", "Category 1");
dataset.addValue(4.0, "Series 1", "Category 2");
dataset.addValue(3.0, "Series 1", "Category 3");
dataset.addValue(5.0, "Series 1", "Category 4");
dataset.addValue(5.0, "Series 1", "Category 5");
dataset.addValue(7.0, "Series 1", "Category 6");
dataset.addValue(7.0, "Series 1", "Category 7");
dataset.addValue(8.0, "Series 1", "Category 8");
JFreeChart chart = ChartFactory.createLineChart(
"Line Chart",
"Category",
"Value",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
return chart;
}
public static void main(String[] args) {
LineChart chart = new LineChart("Line Chart");
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);
}
}
```
这个示例代码创建了一个简单的曲线图,并在窗口中显示出来。你可以根据你的需要对代码进行修改和扩展。
创建一个动态曲线图JFreechar对象
好的,以下是在Java中创建动态曲线图JFreeChart对象的示例代码:
```java
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import javax.swing.*;
import java.awt.*;
public class DynamicLineChart extends JFrame {
private TimeSeries series;
public DynamicLineChart(String title) {
super(title);
series = new TimeSeries("Random Data");
TimeSeriesCollection dataset = new TimeSeriesCollection(series);
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
private JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Dynamic Line Chart",
"Time",
"Value",
dataset,
true,
true,
false
);
XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
return chart;
}
public void addData(double data) {
series.add(new Second(), data);
}
public static void main(String[] args) {
DynamicLineChart chart = new DynamicLineChart("Dynamic Line Chart");
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);
while (true) {
double data = Math.random() * 100;
chart.addData(data);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
这个示例代码创建了一个动态曲线图,并在窗口中显示出来。通过addData方法可以向图表中添加数据,示例代码中每隔一秒钟随机生成一个数据点并添加到图表中。你可以根据你的需要对代码进行修改和扩展。
阅读全文