0
votes

I created a dynamic time series chart using JFreeChart API. and it is working fine. I have to add a combo box to it, in which time values will be there like 5 sec, 15 sec, 30 sec. when user choose on from it the time axis has to repaint with given interval and the timer has to delay the process for 5, 15,30 seconds(updating the chart). I this on my previous post this

with some extensions like adding combo box to it

but it's not worked for me, any help will be appreciated. thanks

EDIT 1 removed unwanted code and remaining code is

    final DynamicTimeSeriesCollection dataset =
    new DynamicTimeSeriesCollection(1, 60, new Second());

dataset.setTimeBase(new Second( 0,seriesvalue*2, 5, 11, 7, 2012));

dataset.addSeries(new float[]{0}, 0, "Currency Rate");
JFreeChart chart = createChart(dataset);
chartPanel = new ChartPanel(chart);


final JComboBox combo = new JComboBox();
combo.addItem("5");
combo.addItem("15");
combo.addItem("30");

combo.addActionListener(new ActionListener(){

  public void actionPerformed(ActionEvent e){

    if("5".equals(combo.getSelectedItem())){
      seriesvalue=5;
      timer.setDelay(seriesvalue*1000);
    }else  if("15".equals(combo.getSelectedItem())){

      seriesvalue=15;
      timer.setDelay(seriesvalue*1024);
      unit=new DateTickUnit(DateTickUnitType.MINUTE,seriesvalue);
      chartPanel.repaint();
    }
  }
});
    add(chartPanel,BorderLayout.CENTER);
    add(combo,BorderLayout.SOUTH);
timer = new Timer(999*seriesvalue, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
          for(int i=0;i<seriesvalue;i++){

            newData[0] = randy.getRandomvalue();
            dataset.advanceTime();
            dataset.appendData(newData);

          }             

        }
    });

create dataset code

  private JFreeChart createChart(final XYDataset dataset) {
   JFreeChart result= ChartFactory.createTimeSeriesChart(
      "Dyanmic chart", "hh:mm:ss", "Currency", dataset, true, true, false);
  final XYPlot plot = result.getXYPlot();

  dateAxis= (DateAxis)plot.getDomainAxis();
  unit = new DateTickUnit(DateTickUnitType.MINUTE,seriesvalue/2);
  return result;
}

main method

public static void main(final String[] args) {
  EventQueue.invokeLater(new Runnable() {
      public void run() {
          DynaChart chart = new DynaChart();
          chart.pack();
          RefineryUtilities.centerFrameOnScreen(chart);
          chart.setVisible(true);
          chart.start();
      }
  });
 }
1
Are you using TimeSeriesCollection or DynamicTimeSeriesCollection? Please edit your question to include an sscce that shows your approach.trashgod
I am using DynamicTimeSeriesCollectionmallikarjun

1 Answers

1
votes

I'm not sure I understand your requirement from the fragments you've posted. If you just need to sample the data source at a few different frequencies, you can set the javax.swing.Timer delay as shown in this example.

Addendum: I want to repaint the DateAxis.

The API notes that setTimeBase() "Will silently return if the time array was already populated." I suspect that you will need to recreate the DynamicTimeSeriesCollection in your combo's action listener. You may also want to alter the tick units and format of the DateAxis, as mentioned here.