我有一个jfreechart正在绘制一些模拟实时数据。当我的域轴设置为自动时,可以看到数据每秒更新一次。然而,我希望绘制更大范围的数据(比如说一整天)。当我更改范围时,除非放大,否则无法看到绘图。
放大后,该行不会覆盖整个图形,而只覆盖一部分。然后,这条线在图形上移动,而不是沿着它生长/绘制
/**@see http://stackoverflow.com/questions/5048852 */
public class DTSCTest extends ApplicationFrame {
private static final String TITLE = "Dynamic Series";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final float MINMAX = 100;
private static final int COUNT = 10;
private static final int FAST = 1000;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private double gateStart = ThreadLocalRandom.current().nextInt(0, 101);
private boolean returning = false;
private Timer timer;
public DTSCTest(final String title) {
super(title);
final DynamicTimeSeriesCollection dataset =
new DynamicTimeSeriesCollection(1, COUNT, new Second());
Date date = new Date();
dataset.setTimeBase(new Second(date));
float[] gateStartLoad = new float[1];
gateStartLoad[0] = (float)gateStart;
dataset.addSeries(gateStartLoad, 0, "Longwall Data");
JFreeChart chart = createChart(dataset);
final JComboBox combo = new JComboBox();
combo.addItem("Fast");
combo.addItem("Slow");
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("Fast".equals(combo.getSelectedItem())) {
timer.setDelay(FAST);
} else {
timer.setDelay(SLOW);
}
}
});
this.add(new ChartPanel(chart), BorderLayout.CENTER);
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(combo);
this.add(btnPanel, BorderLayout.SOUTH);
timer = new Timer(FAST, new ActionListener() {
float[] newData = new float[1];
@Override
public void actionPerformed(ActionEvent e) {
if(gateStart == 100){
returning = true;
}else if(gateStart == 0){
returning = false;
}
if(returning){
gateStart--;
}else{
gateStart++;
}
newData[0] = (float)gateStart;
dataset.advanceTime();
System.out.println(dataset.getNewestTime());
dataset.appendData(newData);
}
});
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
TITLE, "hh:mm:ss", "Shearer Position", dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
DateAxis domain = (DateAxis)plot.getDomainAxis();
Calendar calendar = Calendar.getInstance();
calendar.set(2021, 0, 6);
System.out.println(new Date());
System.out.println(calendar.getTime());
domain.setRange(new Date(), calendar.getTime());
domain.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));
ValueAxis range = plot.getRangeAxis();
range.setRange(0, 100);
return result;
}
public void start() {
timer.start();
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
DTSCTest demo = new DTSCTest(TITLE);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
demo.start();
}
});
}
}
如何使其连续(显示序列中每个观测点的数据),以及如何在手动设置范围时使其可见
1条答案
按热度按时间taor4pac1#
我搬走了
domain.setRange(new Date(), calendar.getTime());
改变了nMoments
在我的DynamicTimeSeriesCollection(1, COUNT, new Second());
,使计数为60的倍数并增加其值。我的x轴现在正确地显示了时间,但是图不是连续的,它在一段时间后消失了