0

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();
      }
  });
 }
Community
  • 1
  • 1
mallikarjun
  • 1,862
  • 5
  • 23
  • 47

1 Answers1

1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • i am setting delay in the above code in combo box action event. but i want to repaint the dateaxis. eg if i choose 15 seconds the time axis has to display 7 min interval on the line. and if i choose 5sec it has to show 2 min interval on time line. so i assigned **unit** combo box code and repaint the chartpanel. it didn't work for me. – mallikarjun Jul 12 '12 at 01:38
  • I've elaborated above; don't hesitate to edit your question to include an [sscce](http://sscce.org/) that shows your current approach. – trashgod Jul 12 '12 at 16:25