public void chartProgress(ChartProgressEvent chartprogressevent) {
   if (chartprogressevent.getType() != 2) return;
   if (chartPanel != null) {
     JFreeChart jfreechart = chartPanel.getChart();
     if (jfreechart != null) {
       XYPlot xyplot = (XYPlot) jfreechart.getPlot();
       XYDataset xydataset = xyplot.getDataset();
       @SuppressWarnings("rawtypes")
       Comparable comparable = xydataset.getSeriesKey(0);
       double d = xyplot.getDomainCrosshairValue();
       model.setValueAt(comparable, 0, 0);
       long l = (long) d;
       model.setValueAt(new Long(l), 0, 1);
       int i = series.getIndex(new Minute(new Date(l)));
       if (i >= 0) {
         TimeSeriesDataItem timeseriesdataitem =
             series.getDataItem(Math.min(199, Math.max(0, i)));
         TimeSeriesDataItem timeseriesdataitem1 = series.getDataItem(Math.max(0, i - 1));
         TimeSeriesDataItem timeseriesdataitem2 = series.getDataItem(Math.min(199, i + 1));
         long l1 = timeseriesdataitem.getPeriod().getMiddleMillisecond();
         double d1 = timeseriesdataitem.getValue().doubleValue();
         long l2 = timeseriesdataitem1.getPeriod().getMiddleMillisecond();
         double d2 = timeseriesdataitem1.getValue().doubleValue();
         long l3 = timeseriesdataitem2.getPeriod().getMiddleMillisecond();
         double d3 = timeseriesdataitem2.getValue().doubleValue();
         model.setValueAt(new Long(l1), 0, 1);
         model.setValueAt(new Double(d1), 0, 2);
         model.setValueAt(new Long(l2), 0, 3);
         model.setValueAt(new Double(d2), 0, 4);
         model.setValueAt(new Long(l3), 0, 5);
         model.setValueAt(new Double(d3), 0, 6);
       }
     }
   }
 }
    /**
     * Handles a chart progress event.
     *
     * @param event the event.
     */
    public void chartProgress(ChartProgressEvent event) {
      if (event.getType() != ChartProgressEvent.DRAWING_FINISHED) {
        return;
      }
      if (this.chartPanel != null) {
        JFreeChart c = this.chartPanel.getChart();
        if (c != null) {
          XYPlot plot = c.getXYPlot();
          XYDataset dataset = plot.getDataset();
          Comparable seriesKey = dataset.getSeriesKey(0);
          double xx = plot.getDomainCrosshairValue();

          // update the table...
          this.model.setValueAt(seriesKey, 0, 0);
          long millis = (long) xx;
          this.model.setValueAt(new Long(millis), 0, 1);
          int itemIndex = this.series.getIndex(new Minute(new Date(millis)));
          if (itemIndex >= 0) {
            TimeSeriesDataItem item =
                this.series.getDataItem(Math.min(199, Math.max(0, itemIndex)));
            TimeSeriesDataItem prevItem = this.series.getDataItem(Math.max(0, itemIndex - 1));
            TimeSeriesDataItem nextItem = this.series.getDataItem(Math.min(199, itemIndex + 1));
            long x = item.getPeriod().getMiddleMillisecond();
            double y = item.getValue().doubleValue();
            long prevX = prevItem.getPeriod().getMiddleMillisecond();
            double prevY = prevItem.getValue().doubleValue();
            long nextX = nextItem.getPeriod().getMiddleMillisecond();
            double nextY = nextItem.getValue().doubleValue();
            this.model.setValueAt(new Long(x), 0, 1);
            this.model.setValueAt(new Double(y), 0, 2);
            this.model.setValueAt(new Long(prevX), 0, 3);
            this.model.setValueAt(new Double(prevY), 0, 4);
            this.model.setValueAt(new Long(nextX), 0, 5);
            this.model.setValueAt(new Double(nextY), 0, 6);
          }
        }
      }
    }
  /** Some more checks for the addOrUpdate() method. */
  @Test
  public void testAddOrUpdate4() {
    TimeSeries ts = new TimeSeries("S");
    TimeSeriesDataItem overwritten = ts.addOrUpdate(new Year(2009), 20.09);
    assertNull(overwritten);
    overwritten = ts.addOrUpdate(new Year(2009), 1.0);
    assertEquals(new Double(20.09), overwritten.getValue());
    assertEquals(new Double(1.0), ts.getValue(new Year(2009)));

    // changing the overwritten record shouldn't affect the series
    overwritten.setValue(null);
    assertEquals(new Double(1.0), ts.getValue(new Year(2009)));

    TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2010), 20.10);
    overwritten = ts.addOrUpdate(item);
    assertNull(overwritten);
    assertEquals(new Double(20.10), ts.getValue(new Year(2010)));
    // changing the item that was added should not change the series
    item.setValue(null);
    assertEquals(new Double(20.10), ts.getValue(new Year(2010)));
  }
  /**
   * Tests the correct output of a DataSet to a TimeSeries by outputting it, then iterating through
   * TimeSeries object checking the correct values were stored/output.
   */
  public void testOutput()
      throws ClassNotFoundException, NoSuchMethodException, InstantiationException,
          IllegalAccessException, InvocationTargetException, InstantiationException {
    // Constants used to determine size of test
    int NUMBER_OF_TIME_PERIODS = 10;
    String TIME_VARIABLE = "t";

    // Set up array for expected results
    double expectedValue[] = new double[NUMBER_OF_TIME_PERIODS];

    // We'll set up periods starting from today
    RegularTimePeriod period = new Day();

    // Create a test DataSet for output
    //  - note that only one independent variable (the time variable)
    //    will be output. This is expected.
    DataSet dataSet = new DataSet();
    dataSet.setTimeVariable(TIME_VARIABLE);
    for (int d = 0; d < NUMBER_OF_TIME_PERIODS; d++) {
      double value = (double) d;
      DataPoint obs = new Observation(value);
      obs.setIndependentValue(TIME_VARIABLE, period.getMiddleMillisecond());
      dataSet.add(obs);

      period = period.next();
      expectedValue[d] = value;
    }

    assertEquals(
        "Checking only one independent variable exists in dataSet",
        1,
        dataSet.getIndependentVariables().length);

    assertEquals(
        "Checking dataSet has correct number of entries", NUMBER_OF_TIME_PERIODS, dataSet.size());

    // Create TimeSeriesOutputter and use it to output dataSet
    TimeSeries timeSeries = new TimeSeries("test");
    TimeSeriesOutputter outputter = new TimeSeriesOutputter(timeSeries, period.getClass());
    outputter.output(dataSet);

    assertEquals(
        "Checking number of items in time series",
        NUMBER_OF_TIME_PERIODS,
        timeSeries.getItemCount());

    // Reset period to start checking from today onwards
    period = new Day();
    for (int d = 0; d < NUMBER_OF_TIME_PERIODS; d++) {
      TimeSeriesDataItem dataItem = timeSeries.getDataItem(d);

      period = dataItem.getPeriod();
      assertNotNull("Checking time period", period);

      long timeValue = period.getMiddleMillisecond();
      assertTrue(
          "Checking time periods match",
          (double) timeValue >= period.getFirstMillisecond()
              && (double) timeValue <= period.getLastMillisecond());

      assertEquals(
          "Checking values for period " + dataItem.getPeriod() + " match",
          expectedValue[d],
          dataItem.getValue().doubleValue(),
          TOLERANCE);

      period = period.next();
    }
  }