jdbcxydataset.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 577 行 · 第 1/2 页

JAVA
577
字号
            /// First column is X data            this.columnNames = new String[numberOfValidColumns - 1];            /// Get the column names and cache them.            int currentColumn = 0;            for (int column = 1; column < numberOfColumns; column++) {                if (columnTypes[column] != Types.NULL) {                    this.columnNames[currentColumn]                         = metaData.getColumnLabel(column + 1);                    ++currentColumn;                }            }            // Might need to add, to free memory from any previous result sets            if (this.rows != null) {                for (int column = 0; column < this.rows.size(); column++) {                    ArrayList row = (ArrayList) this.rows.get(column);                    row.clear();                }                this.rows.clear();            }            // Are we working with a time series.            switch (columnTypes[0]) {                case Types.DATE:                case Types.TIME:                case Types.TIMESTAMP:                    this.isTimeSeries = true;                    break;                default :                    this.isTimeSeries = false;                    break;            }            // Get all rows.            // rows = new ArrayList();            while (resultSet.next()) {                ArrayList newRow = new ArrayList();                for (int column = 0; column < numberOfColumns; column++) {                    Object xObject = resultSet.getObject(column + 1);                    switch (columnTypes[column]) {                        case Types.NUMERIC:                        case Types.REAL:                        case Types.INTEGER:                        case Types.DOUBLE:                        case Types.FLOAT:                        case Types.DECIMAL:                        case Types.BIGINT:                        case Types.SMALLINT:                            newRow.add(xObject);                            break;                        case Types.DATE:                        case Types.TIME:                        case Types.TIMESTAMP:                            newRow.add(new Long(((Date) xObject).getTime()));                            break;                        case Types.NULL:                            break;                        default:                            System.err.println("Unknown data");                            columnTypes[column] = Types.NULL;                            break;                    }                }                this.rows.add(newRow);            }            /// a kludge to make everything work when no rows returned            if (this.rows.size() == 0) {                ArrayList newRow = new ArrayList();                for (int column = 0; column < numberOfColumns; column++) {                    if (columnTypes[column] != Types.NULL) {                        newRow.add(new Integer(0));                    }                }                this.rows.add(newRow);            }            /// Determine max and min values.            if (this.rows.size() < 1) {                this.maxValue = 0.0;                this.minValue = 0.0;            }            else {                ArrayList row = (ArrayList) this.rows.get(0);                this.maxValue = Double.NEGATIVE_INFINITY;                this.minValue = Double.POSITIVE_INFINITY;                for (int rowNum = 0; rowNum < this.rows.size(); ++rowNum) {                    row = (ArrayList) this.rows.get(rowNum);                    for (int column = 1; column < numberOfColumns; column++) {                        Object testValue = row.get(column);                        if (testValue != null) {                            double test = ((Number) testValue).doubleValue();                                                    if (test < this.minValue) {                                this.minValue = test;                            }                            if (test > this.maxValue) {                                this.maxValue = test;                            }                        }                    }                }            }            fireDatasetChanged(); // Tell the listeners a new table has arrived.        }        finally {            if (resultSet != null) {                try {                    resultSet.close();                }                catch (Exception e) {                    // TODO: is this a good idea?                }            }            if (statement != null) {                try {                    statement.close();                }                catch (Exception e) {                    // TODO: is this a good idea?                }            }        }    }    /**     * Returns the x-value for the specified series and item.  The     * implementation is responsible for ensuring that the x-values are     * presented in ascending order.     *     * @param  seriesIndex  the series (zero-based index).     * @param  itemIndex  the item (zero-based index).     *     * @return The x-value     *     * @see XYDataset     */    public Number getX(int seriesIndex, int itemIndex) {        ArrayList row = (ArrayList) this.rows.get(itemIndex);        return (Number) row.get(0);    }    /**     * Returns the y-value for the specified series and item.     *     * @param  seriesIndex  the series (zero-based index).     * @param  itemIndex  the item (zero-based index).     *     * @return The yValue value     *     * @see XYDataset     */    public Number getY(int seriesIndex, int itemIndex) {        ArrayList row = (ArrayList) this.rows.get(itemIndex);        return (Number) row.get(seriesIndex + 1);    }    /**     * Returns the number of items in the specified series.     *     * @param  seriesIndex  the series (zero-based index).     *     * @return The itemCount value     *     * @see XYDataset     */    public int getItemCount(int seriesIndex) {        return this.rows.size();    }    /**     * Returns the number of items in all series.  This method is defined by      * the {@link TableXYDataset} interface.     *      * @return The item count.     */    public int getItemCount() {        return getItemCount(0);    }        /**     * Returns the number of series in the dataset.     *     * @return The seriesCount value     *     * @see XYDataset     * @see Dataset     */    public int getSeriesCount() {        return this.columnNames.length;    }    /**     * Returns the key for the specified series.     *     * @param seriesIndex  the series (zero-based index).     *     * @return The seriesName value     *     * @see XYDataset     * @see Dataset     */    public Comparable getSeriesKey(int seriesIndex) {        if ((seriesIndex < this.columnNames.length)                 && (this.columnNames[seriesIndex] != null)) {            return this.columnNames[seriesIndex];        }        else {            return "";        }    }    /**     * Returns the number of items that should be displayed in the legend.     *     * @return The legendItemCount value     */    public int getLegendItemCount() {        return getSeriesCount();    }    /**     * Returns the legend item labels.     *     * @return The legend item labels.     */    public String[] getLegendItemLabels() {        return this.columnNames;    }    /**     * Close the database connection     */    public void close() {        try {            this.connection.close();        }        catch (Exception e) {            System.err.println("JdbcXYDataset: swallowing exception.");        }    }    /**     * Returns the minimum y-value in the dataset.     *     * @param includeInterval  a flag that determines whether or not the     *                         y-interval is taken into account.     *      * @return The minimum value.     */    public double getRangeLowerBound(boolean includeInterval) {        return this.minValue;    }        /**     * Returns the maximum y-value in the dataset.     *     * @param includeInterval  a flag that determines whether or not the     *                         y-interval is taken into account.     *      * @return The maximum value.     */    public double getRangeUpperBound(boolean includeInterval) {        return this.maxValue;    }    /**     * Returns the range of the values in this dataset's range.     *     * @param includeInterval  a flag that determines whether or not the     *                         y-interval is taken into account.     *      * @return The range.     */    public Range getRangeBounds(boolean includeInterval) {        return new Range(this.minValue, this.maxValue);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?