有关如何为X轴和Y轴设置范围的任何建议。
我的“X轴”范围是“0.00到1.00”,差异为“0.05”。我的意思是0.00 0.05 0.10 0.15 ..... 0.90 0.95 1.00
我的“Y轴”范围从“0.0到1.0”,差异为“0.1”。我的意思是0.0 0.1 0.2 0.3 0.4 ......... 0.9 1.0
我试过这样做,但它没有反映在图表上;我不知道如何将其应用于ChartFactory.createScatterPlot()
。
final NumberAxis domainAxis = new NumberAxis("X-Axis");
domainAxis.setRange(0.00,1.00);
domainAxis.setTickUnit(new NumberTickUnit(0.1));
final NumberAxis rangeAxis = new NumberAxis("Y-Axis");
rangeAxis.setRange(0.0,1.0);
rangeAxis.setTickUnit(new NumberTickUnit(0.05));
public JPanel createDemoPanel() {
XYDataset dataset1 = samplexydataset2();
JFreeChart jfreechart = ChartFactory.createScatterPlot("Scatter Plot Demo",
"X", "Y",dataset1, PlotOrientation.VERTICAL, true, true, false);
}
对此有任何帮助都很棒。
答案 0 :(得分:28)
我猜你的新NumberAxis
个实例没有被剧情使用;使用工厂现有的可能更容易。
import java.awt.BorderLayout;
import java.awt.Color;
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.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* @see http://stackoverflow.com/questions/7231824
* @see http://stackoverflow.com/questions/7205742
* @see http://stackoverflow.com/questions/7208657
* @see http://stackoverflow.com/questions/7071057
*/
public class ScatterAdd extends JFrame {
private static final int N = 8;
private static final String title = "Scatter Add Demo";
private static final Random rand = new Random();
private XYSeries added = new XYSeries("Added");
public ScatterAdd(String s) {
super(s);
final ChartPanel chartPanel = createDemoPanel();
this.add(chartPanel, BorderLayout.CENTER);
JPanel control = new JPanel();
control.add(new JButton(new AbstractAction("Add") {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < N; i++) {
added.add(rand.nextDouble(), rand.nextDouble());
}
}
}));
this.add(control, BorderLayout.SOUTH);
}
private ChartPanel createDemoPanel() {
JFreeChart jfreechart = ChartFactory.createScatterPlot(
title, "X", "Y", createSampleData(),
PlotOrientation.VERTICAL, true, true, false);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(0, Color.blue);
NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
domain.setRange(0.00, 1.00);
domain.setTickUnit(new NumberTickUnit(0.1));
domain.setVerticalTickLabels(true);
NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
range.setRange(0.0, 1.0);
range.setTickUnit(new NumberTickUnit(0.1));
return new ChartPanel(jfreechart);
}
private XYDataset createSampleData() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries series = new XYSeries("Random");
for (int i = 0; i < N * N; i++) {
double x = rand.nextDouble();
double y = rand.nextDouble();
series.add(x, y);
}
xySeriesCollection.addSeries(series);
xySeriesCollection.addSeries(added);
return xySeriesCollection;
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ScatterAdd demo = new ScatterAdd(title);
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
});
}
}
答案 1 :(得分:15)
除非您粘贴的代码不完整,否则您的问题似乎是您没有将您创建的NumberAxis对象与绘图相关联。
尝试获取与绘图关联的默认ValueAxis对象,然后修改其属性,而不是手动创建新的NumberAxis对象:
XYPlot xyPlot = jfreechart.getXYPlot();
ValueAxis domainAxis = xyPlot.getDomainAxis();
ValueAxis rangeAxis = xyPlot.getRangeAxis();
domainAxis.setRange(0.0, 1.0);
domainAxis.setTickUnit(new NumberTickUnit(0.1));
rangeAxis.setRange(0.0, 1.0);
rangeAxis.setTickUnit(new NumberTickUnit(0.05));