`
mryangjw
  • 浏览: 20746 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JSTOCKCHART中【Duplicates are not permitted. Try using the addOrUpdate() method.】

阅读更多

关于使用【JSTOCKCHART】插件开发过程中【You are attempting to add an observation for the time period  Thu Jun 24 10:34:26 CST 2010 but the series already contains an observation for that time period. Duplicates are not permitted.  Try using the addOrUpdate() method.】问题的解决办法

 

问题描述:

  更新坐标点上纵坐标的值时,出现此类异常。

 

原因分析:

  在【dataset】中添加新项目(调用【addDataItem】方法)时,程序最后会调用到【JFREECHART】插件中的【TimeSeries】类的【add(TimeSeriesDataItem item, boolean notify)】方法,在该【add】方法中会根据横坐标去判断该坐标是否已经存在,如果不存在,则添加,如果存在,就会抛出如上异常信息。【add】方法的详细内容如下:

    注:标【***********】的为重点

 

    /**
     * Adds a data item to the series and sends a {@link SeriesChangeEvent} to
     * all registered listeners.
     *
     * @param item  the (timeperiod, value) pair (<code>null</code> not
     *              permitted).
     * @param notify  notify listeners?
     */
    public void add(TimeSeriesDataItem item, boolean notify) {
        if (item == null) {
            throw new IllegalArgumentException("Null 'item' argument.");
        }
        Class c = item.getPeriod().getClass();
        if (this.timePeriodClass == null) {
            this.timePeriodClass = c;
        }
        else if (!this.timePeriodClass.equals(c)) {
            StringBuffer b = new StringBuffer();
            b.append("You are trying to add data where the time period class ");
            b.append("is ");
            b.append(item.getPeriod().getClass().getName());
            b.append(", but the TimeSeries is expecting an instance of ");
            b.append(this.timePeriodClass.getName());
            b.append(".");
            throw new SeriesException(b.toString());
        }

        // make the change (if it's not a duplicate time period)...
        boolean added = false;
        int count = getItemCount();
        if (count == 0) {
            this.data.add(item);
            added = true;
        }
        else {
            RegularTimePeriod last = getTimePeriod(getItemCount() - 1);
            if (item.getPeriod().compareTo(last) > 0) {
                this.data.add(item);
                added = true;
            }
            else {

                // ***************************************************

                // 查找数据集中是否已经包含要添加的记录(以横坐标作为key)
                int index = Collections.binarySearch(this.data, item);

                // 不存在添加
                if (index < 0) {
                    this.data.add(-index - 1, item);
                    added = true;
                }

                // 存在就抛出以下异常信息
                else {
                      StringBuffer b = new StringBuffer();
                      b.append("You are attempting to add an observation for ");
                      b.append("the time period ");
                      b.append(item.getPeriod().toString());
                      b.append(" but the series already contains an observation");
                      b.append(" for that time period. Duplicates are not ");
                      b.append("permitted.  Try using the addOrUpdate() method.");
                      throw new SeriesException(b.toString());
                }

                // ***************************************************
            }
        }
        if (added) {
            // check if this addition will exceed the maximum item count...
            if (getItemCount() > this.maximumItemCount) {
                this.data.remove(0);
            }

            removeAgedItems(false);  // remove old items if necessary, but
                                     // don't notify anyone, because that
                                     // happens next anyway...
            if (notify) {
                fireSeriesChanged();
            }
        }

    }

 

解决方法:

  把程序中的这一段内容注释掉,然后添加【this.data.set(index, item);】语句即可。

//                    StringBuffer b = new StringBuffer();
//                    b.append("You are attempting to add an observation for ");
//                    b.append("the time period ");
//                    b.append(item.getPeriod().toString());
//                    b.append(" but the series already contains an observation");
//                    b.append(" for that time period. Duplicates are not ");
//                    b.append("permitted.  Try using the addOrUpdate() method.");
//                    throw new SeriesException(b.toString());
                 this.data.set(index, item);

重新编译下工程,然后再运行,OK,问题解决!
如有不对之处,希望批评指正!

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics