简体   繁体   English

JFreeChart-如何在TimeSeries图表的X轴上实时显示并保存数据

[英]JFreeChart - How to show real-time on the X-Axis of a TimeSeries chart and keep datas

I am new to JFreeChart. 我是JFreeChart的新手。

I'd like to change the code of Trashgod to monitor something in Java. 我想更改Trashgod的代码以监视Java中的某些内容。 So I need to: 所以我需要:

1 - Keep all datas shown. 1-保留所有数据显示。 His code keeps only the last 10 samples. 他的代码仅保留最后10个样本。

2 - Having a variable scale on X-axis : 2-在X轴上具有可变比例:

  • Having an int input howManyDays (or something like an int input, something a little bit clever would be great ;-) ) : int howManyDays = 7;//in this case : 1 week of datas 有一个int输入howManyDays(或类似int输入的东西,有点聪明就好了;-)):int howManyDays = 7; //在这种情况下:1周的数据

  • Having a variable scale on X-axis a little bit complex : For example during the first seconds of execution it has for scale 1 minute. 在X轴上具有可变比例会有点复杂:例如,在执行的最初几秒钟内,比例为1分钟。 Then 10 minutes... When midnight has been passed, it should print the current day. 然后10分钟...午夜过后,它应该打印当天。

I know that this instruction 我知道这条指令

      Timer timer = new Timer(100, new ActionListener()

means that every 100 ms it adds a new data and then it updates the chart. 表示每100毫秒添加一个新数据,然后更新图表。 I'll change it to 10 000 -> 10 seconds. 我将其更改为10000-> 10秒。 My Timer for testing JFreeChart : 我的计时器测试JFreeChart:

            Timer timer = new Timer(10000, new ActionListener() {
                private int i,data;
                @Override
                public void actionPerformed(ActionEvent e) {
                    if ((i%10)==0) {
                        this.data += (int) Math.ceil(2*Math.random());
                    }
                    this.i++;
                    chart.update(this.data);
                }
            });

Of course I don't need the solution. 当然,我不需要解决方案。

What do I really need : 我真正需要的是:

  • Maybe if someone could add comment on Trashgod's code ? 也许有人可以在Trashgod的代码上添加注释?
  • Or if someone could give me some tricks for doing this kind of code ? 或者,如果有人可以给我一些执行此类代码的技巧?
  • Or maybe someone has already done something like that ? 也许有人已经做了类似的事情?

Have a good day ! 祝你有美好的一天 !

While your question is somewhat broad, here are some pointers to approaching such a task. 尽管您的问题有些笼统,但这里有一些指向实现此任务的指示。

  • DynamicTimeSeriesCollection is designed to retain only recent data, not all; DynamicTimeSeriesCollection旨在仅保留最近的数据,而不保留所有数据; note here how the time slots are filled. 在此处注意如何填充时隙。

  • To keep all data, use a TimeSeries as suggested here ; 要保留所有数据,请使用此处建议的TimeSeries aged items will be removed only if you invoke setMaximumItemAge() . 仅当您调用setMaximumItemAge()才会删除setMaximumItemAge()

  • To format the time axis, use setDateFormatOverride() as suggested here ; 要格式化时间轴,请按照此处的建议使用setDateFormatOverride() the tick units will adjust automatically as data accumulates; 刻度单位将随着数据累积而自动调整; don't change them unless you have some some specific reason to do so. 除非您有某些特定原因,否则请勿更改它们。

  • To collect data in the background, use a SwingWorker , as shown here . 在后台收集数据,使用SwingWorker ,如图所示这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM