Example usage for org.jfree.data.time RegularTimePeriod toString

List of usage examples for org.jfree.data.time RegularTimePeriod toString

Introduction

In this page you can find the example usage for org.jfree.data.time RegularTimePeriod toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a string representation of the time period.

Usage

From source file:org.pentaho.platform.uifoundation.chart.TimeSeriesCollectionChartComponent.java

@Override
public Document getXmlContent() {

    // Create a document that describes the result
    Document result = DocumentHelper.createDocument();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    setXslProperty("baseUrl", requestContext.getContextPath()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty("fullyQualifiedServerUrl", //$NON-NLS-1$
            PentahoSystem.getApplicationContext().getFullyQualifiedServerURL()); //$NON-NLS-2$ //$NON-NLS-3$
    String mapName = "chart" + AbstractChartComponent.chartCount++; //$NON-NLS-1$
    Document chartDefinition = jcrHelper.getSolutionDocument(definitionPath, RepositoryFilePermission.READ);

    if (chartDefinition == null) {
        Element errorElement = result.addElement("error"); //$NON-NLS-1$
        errorElement.addElement("title").setText( //$NON-NLS-1$
                Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", //$NON-NLS-1$
                definitionPath);/*from   ww  w.j a v a  2 s .c o  m*/
        errorElement.addElement("message").setText(message); //$NON-NLS-1$
        error(message);
        return result;
    }
    // create a pie definition from the XML definition
    dataDefinition = createChart(chartDefinition);

    if (dataDefinition == null) {
        Element errorElement = result.addElement("error"); //$NON-NLS-1$
        errorElement.addElement("title").setText( //$NON-NLS-1$
                Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0002_CHART_DATA_MISSING", actionPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        errorElement.addElement("message").setText(message); //$NON-NLS-1$
        // System .out.println( result.asXML() );
        return result;
    }

    // create an image for the dial using the JFreeChart engine
    PrintWriter printWriter = new PrintWriter(new StringWriter());
    // we'll dispay the title in HTML so that the dial image does not have
    // to
    // accommodate it
    String chartTitle = ""; //$NON-NLS-1$
    try {
        if (width == -1) {
            width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText()); //$NON-NLS-1$
        }
        if (height == -1) {
            height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText()); //$NON-NLS-1$
        }
    } catch (Exception e) {
        // go with the default
    }
    if (chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) != null) { //$NON-NLS-1$
        urlTemplate = chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) //$NON-NLS-1$
                .getText();
    }

    if (chartDefinition.selectSingleNode("/chart/paramName") != null) { //$NON-NLS-1$
        paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); //$NON-NLS-1$
    }

    Element root = result.addElement("charts"); //$NON-NLS-1$
    TimeSeriesCollection chartDataDefinition = (TimeSeriesCollection) dataDefinition;
    if (chartDataDefinition.getSeriesCount() > 0) {
        // create temporary file names
        String[] tempFileInfo = createTempFile();
        String fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX];
        String filePathWithoutExtension = tempFileInfo[AbstractChartComponent.FILENAME_WITHOUT_EXTENSION_INDEX];

        ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        JFreeChartEngine.saveChart(chartDataDefinition, chartTitle, "", filePathWithoutExtension, width, height, //$NON-NLS-1$
                JFreeChartEngine.OUTPUT_PNG, printWriter, info, this);
        applyOuterURLTemplateParam();
        populateInfo(info);
        Element chartElement = root.addElement("chart"); //$NON-NLS-1$
        chartElement.addElement("mapName").setText(mapName); //$NON-NLS-1$
        chartElement.addElement("width").setText(Integer.toString(width)); //$NON-NLS-1$
        chartElement.addElement("height").setText(Integer.toString(height)); //$NON-NLS-1$
        for (int row = 0; row < chartDataDefinition.getSeriesCount(); row++) {
            for (int column = 0; column < chartDataDefinition.getItemCount(row); column++) {
                Number value = chartDataDefinition.getY(row, column);
                Comparable rowKey = chartDataDefinition.getSeriesKey(row);
                RegularTimePeriod columnKey = chartDataDefinition.getSeries(row).getTimePeriod(column);
                Element valueElement = chartElement.addElement("value2D"); //$NON-NLS-1$
                valueElement.addElement("value").setText(value.toString()); //$NON-NLS-1$
                valueElement.addElement("row-key").setText(rowKey.toString()); //$NON-NLS-1$
                valueElement.addElement("column-key").setText(columnKey.toString()); //$NON-NLS-1$
            }
        }
        String mapString = ImageMapUtilities.getImageMap(mapName, info);
        chartElement.addElement("imageMap").setText(mapString); //$NON-NLS-1$
        chartElement.addElement("image").setText(fileName); //$NON-NLS-1$
    }
    return result;
}

From source file:org.adempiere.apps.graph.ChartBuilder.java

private void addData(MChartDatasource ds) {

    String value = ds.getValueColumn();
    String category;//from   ww w.j  a v  a 2 s  .co m
    String unit = "D";

    if (!chartModel.isTimeSeries())
        category = ds.getCategoryColumn();
    else {
        if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Week)) {
            unit = "W";
        } else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Month)) {
            unit = "MM";
        } else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Quarter)) {
            unit = "Q";
        } else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Year)) {
            unit = "Y";
        }

        category = " TRUNC(" + ds.getDateColumn() + ", '" + unit + "') ";
    }

    String series = DB.TO_STRING(ds.getName());
    boolean hasSeries = false;
    if (ds.getSeriesColumn() != null) {
        series = ds.getSeriesColumn();
        hasSeries = true;
    }

    String where = ds.getWhereClause();
    if (!Util.isEmpty(where)) {
        where = Env.parseContext(Env.getCtx(), chartModel.getWindowNo(), where, true);
    }

    boolean hasWhere = false;

    String sql = "SELECT " + value + ", " + category + ", " + series + " FROM " + ds.getFromClause();
    if (!Util.isEmpty(where)) {
        sql += " WHERE " + where;
        hasWhere = true;
    }

    Date currentDate = Env.getContextAsDate(Env.getCtx(), "#Date");
    Date startDate = null;
    Date endDate = null;

    int scope = chartModel.getTimeScope();
    int offset = ds.getTimeOffset();

    if (chartModel.isTimeSeries() && scope != 0) {
        offset += -scope;
        startDate = increment(currentDate, chartModel.getTimeUnit(), offset);
        endDate = increment(startDate, chartModel.getTimeUnit(), scope);
    }

    if (startDate != null && endDate != null) {
        sql += hasWhere ? " AND " : " WHERE ";
        sql += category + ">=TRUNC(" + DB.TO_DATE(new Timestamp(startDate.getTime())) + ", '" + unit
                + "') AND ";
        sql += category + "<=TRUNC(" + DB.TO_DATE(new Timestamp(endDate.getTime())) + ", '" + unit + "') ";
    }

    if (sql.indexOf('@') >= 0) {
        sql = Env.parseContext(Env.getCtx(), 0, sql, false, true);
    }

    MRole role = MRole.getDefault(Env.getCtx(), false);
    sql = role.addAccessSQL(sql, null, true, false);

    if (hasSeries)
        sql += " GROUP BY " + series + ", " + category + " ORDER BY " + series + ", " + category;
    else
        sql += " GROUP BY " + category + " ORDER BY " + category;

    log.log(Level.FINE, sql);

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    TimeSeries tseries = null;
    Dataset dataset = getDataset();

    try {
        pstmt = DB.prepareStatement(sql, null);
        rs = pstmt.executeQuery();
        while (rs.next()) {

            String key = rs.getString(2);
            String seriesName = rs.getString(3);
            if (seriesName == null)
                seriesName = ds.getName();
            String queryWhere = "";
            if (hasWhere)
                queryWhere += where + " AND ";

            queryWhere += series + " = " + DB.TO_STRING(seriesName) + " AND " + category + " = ";

            if (chartModel.isTimeSeries() && dataset instanceof TimeSeriesCollection) {

                if (tseries == null || !tseries.getKey().equals(seriesName)) {
                    if (tseries != null)
                        ((TimeSeriesCollection) dataset).addSeries(tseries);

                    tseries = new TimeSeries(seriesName);
                }

                Date date = rs.getDate(2);
                RegularTimePeriod period = null;

                if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Day))
                    period = new Day(date);
                else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Week))
                    period = new Week(date);
                else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Month))
                    period = new Month(date);
                else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Quarter))
                    period = new Quarter(date);
                else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Year))
                    period = new Year(date);

                tseries.add(period, rs.getBigDecimal(1));
                key = period.toString();
                queryWhere += DB.TO_DATE(new Timestamp(date.getTime()));
            } else {
                queryWhere += DB.TO_STRING(key);
            }

            MQuery query = new MQuery(ds.getAD_Table_ID());
            String keyCol = MTable.get(Env.getCtx(), ds.getAD_Table_ID()).getKeyColumns()[0];
            String whereClause = keyCol + " IN (SELECT " + ds.getKeyColumn() + " FROM " + ds.getFromClause()
                    + " WHERE " + queryWhere + " )";
            query.addRestriction(whereClause.toString());
            query.setRecordCount(1);

            HashMap<String, MQuery> map = getQueries();

            if (dataset instanceof DefaultPieDataset) {
                ((DefaultPieDataset) dataset).setValue(key, rs.getBigDecimal(1));
                map.put(key, query);
            } else if (dataset instanceof DefaultCategoryDataset) {
                ((DefaultCategoryDataset) dataset).addValue(rs.getBigDecimal(1), seriesName, key);
                map.put(seriesName + "__" + key, query);
            } else if (dataset instanceof TimeSeriesCollection) {
                map.put(seriesName + "__" + key, query);
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }

    if (tseries != null)
        ((TimeSeriesCollection) dataset).addSeries(tseries);

}