Search in sources :

Example 1 with TimeSeries

use of org.jfree.data.time.TimeSeries in project adempiere by adempiere.

the class VChart method chartMouseClicked.

@Override
public void chartMouseClicked(ChartMouseEvent event) {
    if ((event.getEntity() != null) && (event.getTrigger().getClickCount() > 1)) {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        try {
            ChartEntity entity = event.getEntity();
            String key = null;
            String seriesName = null;
            if (entity instanceof CategoryItemEntity) {
                CategoryItemEntity item = ((CategoryItemEntity) entity);
                Comparable<?> colKey = item.getColumnKey();
                Comparable<?> rowKey = item.getRowKey();
                if (colKey != null && rowKey != null) {
                    key = colKey.toString();
                    seriesName = rowKey.toString();
                }
            } else if (entity instanceof PieSectionEntity) {
                Comparable<?> sectionKey = ((PieSectionEntity) entity).getSectionKey();
                if (sectionKey != null) {
                    key = sectionKey.toString();
                }
            }
            if (entity instanceof XYItemEntity) {
                XYItemEntity item = ((XYItemEntity) entity);
                if (item.getDataset() instanceof TimeSeriesCollection) {
                    TimeSeriesCollection data = (TimeSeriesCollection) item.getDataset();
                    TimeSeries series = data.getSeries(item.getSeriesIndex());
                    TimeSeriesDataItem dataitem = series.getDataItem(item.getItem());
                    seriesName = series.getKey().toString();
                    key = dataitem.getPeriod().toString();
                }
            }
            if (key == null)
                return;
            MQuery query = chartModel.getQuery(seriesName == null ? key : seriesName + "__" + key);
            if (query != null)
                AEnv.zoom(query);
        } finally {
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    }
}
Also used : XYItemEntity(org.jfree.chart.entity.XYItemEntity) TimeSeries(org.jfree.data.time.TimeSeries) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) TimeSeriesDataItem(org.jfree.data.time.TimeSeriesDataItem) MQuery(org.compiere.model.MQuery) ChartEntity(org.jfree.chart.entity.ChartEntity) CategoryItemEntity(org.jfree.chart.entity.CategoryItemEntity) PieSectionEntity(org.jfree.chart.entity.PieSectionEntity)

Example 2 with TimeSeries

use of org.jfree.data.time.TimeSeries in project adempiere by adempiere.

the class WChartEditor method render.

private void render(JFreeChart chart) {
    ChartRenderingInfo info = new ChartRenderingInfo();
    int width = 400;
    int height = chartModel.getWinHeight();
    BufferedImage bi = chart.createBufferedImage(width, height, BufferedImage.TRANSLUCENT, info);
    try {
        byte[] bytes = EncoderUtil.encode(bi, ImageFormat.PNG, true);
        AImage image = new AImage("", bytes);
        Imagemap myImage = new Imagemap();
        Panel panel = getComponent();
        myImage.setContent(image);
        if (panel.getPanelchildren() != null) {
            panel.getPanelchildren().getChildren().clear();
            panel.getPanelchildren().appendChild(myImage);
        } else {
            Panelchildren pc = new Panelchildren();
            panel.appendChild(pc);
            pc.appendChild(myImage);
        }
        int count = 0;
        for (Iterator<?> it = info.getEntityCollection().getEntities().iterator(); it.hasNext(); ) {
            ChartEntity entity = (ChartEntity) it.next();
            String key = null;
            String seriesName = null;
            if (entity instanceof CategoryItemEntity) {
                CategoryItemEntity item = ((CategoryItemEntity) entity);
                Comparable<?> colKey = item.getColumnKey();
                Comparable<?> rowKey = item.getRowKey();
                if (colKey != null && rowKey != null) {
                    key = colKey.toString();
                    seriesName = rowKey.toString();
                }
            } else if (entity instanceof PieSectionEntity) {
                Comparable<?> sectionKey = ((PieSectionEntity) entity).getSectionKey();
                if (sectionKey != null) {
                    key = sectionKey.toString();
                }
            }
            if (entity instanceof XYItemEntity) {
                XYItemEntity item = ((XYItemEntity) entity);
                if (item.getDataset() instanceof TimeSeriesCollection) {
                    TimeSeriesCollection data = (TimeSeriesCollection) item.getDataset();
                    TimeSeries series = data.getSeries(item.getSeriesIndex());
                    TimeSeriesDataItem dataitem = series.getDataItem(item.getItem());
                    seriesName = series.getKey().toString();
                    key = dataitem.getPeriod().toString();
                }
            }
            if (key == null)
                continue;
            Area area = new Area();
            myImage.appendChild(area);
            area.setCoords(entity.getShapeCoords());
            area.setShape(entity.getShapeType());
            area.setTooltiptext(entity.getToolTipText());
            area.setId(count + "_WG__" + seriesName + "__" + key);
            count++;
        }
        myImage.addEventListener(Events.ON_CLICK, new EventListener() {

            public void onEvent(Event event) throws Exception {
                MouseEvent me = (MouseEvent) event;
                String areaId = me.getArea();
                if (areaId != null) {
                    String[] strs = areaId.split("__");
                    if (strs.length == 3) {
                        chartMouseClicked(strs[2], strs[1]);
                    }
                }
            }
        });
    } catch (Exception e) {
        log.log(Level.SEVERE, "", e);
    }
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) MouseEvent(org.zkoss.zk.ui.event.MouseEvent) TimeSeriesDataItem(org.jfree.data.time.TimeSeriesDataItem) Panelchildren(org.zkoss.zul.Panelchildren) BufferedImage(java.awt.image.BufferedImage) XYItemEntity(org.jfree.chart.entity.XYItemEntity) Panel(org.zkoss.zul.Panel) Area(org.zkoss.zul.Area) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ChartRenderingInfo(org.jfree.chart.ChartRenderingInfo) AImage(org.zkoss.image.AImage) ValueChangeEvent(org.adempiere.exceptions.ValueChangeEvent) Event(org.zkoss.zk.ui.event.Event) MouseEvent(org.zkoss.zk.ui.event.MouseEvent) ChartEntity(org.jfree.chart.entity.ChartEntity) EventListener(org.zkoss.zk.ui.event.EventListener) CategoryItemEntity(org.jfree.chart.entity.CategoryItemEntity) PieSectionEntity(org.jfree.chart.entity.PieSectionEntity) Imagemap(org.zkoss.zul.Imagemap)

Example 3 with TimeSeries

use of org.jfree.data.time.TimeSeries in project adempiere by adempiere.

the class MChartDatasource method addData.

public void addData(MChart parent) {
    String value = getValueColumn();
    String category;
    String unit = "D";
    if (!parent.isTimeSeries())
        category = getCategoryColumn();
    else {
        if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Week)) {
            unit = "W";
        } else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Month)) {
            unit = "MM";
        } else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Quarter)) {
            unit = "Q";
        } else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Year)) {
            unit = "Y";
        }
        category = " TRUNC(" + getDateColumn() + ", '" + unit + "') ";
    }
    String series = DB.TO_STRING(getName());
    boolean hasSeries = false;
    if (getSeriesColumn() != null) {
        series = getSeriesColumn();
        hasSeries = true;
    }
    String where = getWhereClause();
    if (!Util.isEmpty(where)) {
        where = Env.parseContext(getCtx(), parent.getWindowNo(), where, true);
    }
    boolean hasWhere = false;
    String sql = "SELECT " + value + ", " + category + ", " + series + " FROM " + getFromClause();
    if (!Util.isEmpty(where)) {
        sql += " WHERE " + where;
        hasWhere = true;
    }
    Date currentDate = Env.getContextAsDate(getCtx(), "#Date");
    Date startDate = null;
    Date endDate = null;
    int scope = parent.getTimeScope();
    int offset = getTimeOffset();
    if (parent.isTimeSeries() && scope != 0) {
        offset += -scope;
        startDate = increment(currentDate, parent.getTimeUnit(), offset);
        endDate = increment(startDate, parent.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 + "') ";
    }
    MRole role = MRole.getDefault(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 = parent.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 = getName();
            String queryWhere = "";
            if (hasWhere)
                queryWhere += where + " AND ";
            queryWhere += series + " = " + DB.TO_STRING(seriesName) + " AND " + category + " = ";
            if (parent.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 (parent.getTimeUnit().equals(MChart.TIMEUNIT_Day))
                    period = new Day(date);
                else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Week))
                    period = new Week(date);
                else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Month))
                    period = new Month(date);
                else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Quarter))
                    period = new Quarter(date);
                else if (parent.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(getAD_Table_ID());
            String keyCol = MTable.get(getCtx(), getAD_Table_ID()).getKeyColumns()[0];
            String whereClause = keyCol + " IN (SELECT " + getKeyColumn() + " FROM " + getFromClause() + " WHERE " + queryWhere + " )";
            query.addRestriction(whereClause.toString());
            query.setRecordCount(1);
            HashMap<String, MQuery> map = parent.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);
}
Also used : Quarter(org.jfree.data.time.Quarter) DBException(org.adempiere.exceptions.DBException) DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) TimeSeries(org.jfree.data.time.TimeSeries) SQLException(java.sql.SQLException) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) Dataset(org.jfree.data.general.Dataset) DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date) Month(org.jfree.data.time.Month) Year(org.jfree.data.time.Year) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ResultSet(java.sql.ResultSet) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) Day(org.jfree.data.time.Day) Week(org.jfree.data.time.Week) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset)

Example 4 with TimeSeries

use of org.jfree.data.time.TimeSeries in project jgnash by ccavanaugh.

the class MonthlyAccountBalanceChart method createTimeSeriesCollection.

private TimeSeriesCollection createTimeSeriesCollection(final Account account) {
    List<LocalDate> dates = Collections.emptyList();
    if (subAccountCheckBox.isSelected()) {
        // Getting the dates to calculate
        final LocalDate start = startDateField.getLocalDate().with(TemporalAdjusters.firstDayOfMonth());
        final LocalDate stop = endDateField.getLocalDate().with(TemporalAdjusters.lastDayOfMonth());
        dates = DateUtils.getLastDayOfTheMonths(start, stop);
        TimeSeries t = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"), rb.getString("Column.Balance"));
        // For every month, calculate the total amount
        for (LocalDate date : dates) {
            final LocalDate d = date.with(TemporalAdjusters.lastDayOfMonth());
            final LocalDate s = date.with(TemporalAdjusters.firstDayOfMonth());
            // Get the total amount for the account and every sub accounts for the specified date
            // and include it in the chart
            t.add(new Month(DateUtils.asDate(date)), calculateTotal(s, d, account, account.getCurrencyNode()));
        }
        return new TimeSeriesCollection(t);
    }
    int count = account.getTransactionCount();
    if (count > 0) {
        LocalDate start = account.getTransactionAt(0).getLocalDate();
        LocalDate stop = account.getTransactionAt(count - 1).getLocalDate();
        dates = DateUtils.getLastDayOfTheMonths(start, stop);
    }
    TimeSeries t = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"), rb.getString("Column.Balance"));
    AccountType type = account.getAccountType();
    for (LocalDate localDate : dates) {
        // get balance for the whole month
        LocalDate d = localDate.with(TemporalAdjusters.lastDayOfMonth());
        LocalDate s = localDate.with(TemporalAdjusters.firstDayOfMonth());
        BigDecimal balance = AccountBalanceDisplayManager.convertToSelectedBalanceMode(type, account.getBalance(s, d));
        t.add(new Month(DateUtils.asDate(localDate)), balance);
    }
    return new TimeSeriesCollection(t);
}
Also used : Month(org.jfree.data.time.Month) TimeSeries(org.jfree.data.time.TimeSeries) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) LocalDate(java.time.LocalDate) AccountType(jgnash.engine.AccountType) BigDecimal(java.math.BigDecimal)

Example 5 with TimeSeries

use of org.jfree.data.time.TimeSeries in project jgnash by ccavanaugh.

the class MonthlyAccountBalanceChartCompare method createTimeSeriesCollection.

private TimeSeriesCollection createTimeSeriesCollection(final Account account, final Account a2) {
    //always use this method
    //if (subAccountCheckBox.isApproved()) {
    // Getting the dates to calculate
    LocalDate start = startDateField.getLocalDate().with(TemporalAdjusters.firstDayOfMonth());
    LocalDate stop = endDateField.getLocalDate().with(TemporalAdjusters.lastDayOfMonth());
    List<LocalDate> list = DateUtils.getLastDayOfTheMonths(start, stop);
    TimeSeries t = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"), rb.getString("Column.Balance"));
    TimeSeries t2 = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"), rb.getString("Column.Balance"));
    // For every month, calculate the total amount
    for (final LocalDate localDate : list) {
        final LocalDate d = localDate.with(TemporalAdjusters.lastDayOfMonth());
        final LocalDate s = localDate.with(TemporalAdjusters.firstDayOfMonth());
        // Get the total amount for the account and every sub accounts
        // for the specified date
        //BigDecimal bd_TotalAmount = calculateTotal(s, d, account, account.getCurrencyNode());
        BigDecimal bd_TotalAmount = calculateTotal(s, d, account, subAccountCheckBox.isSelected(), account.getCurrencyNode());
        // Include it in the graph
        t.add(new Month(DateUtils.asDate(localDate)), totalModulus(bd_TotalAmount, account.getAccountType()));
        if (jcb_compare.isSelected()) {
            bd_TotalAmount = calculateTotal(s, d, a2, subAccountCheckBox.isSelected(), account.getCurrencyNode());
            t2.add(new Month(DateUtils.asDate(localDate)), totalModulus(bd_TotalAmount, a2.getAccountType()));
        }
    }
    TimeSeriesCollection tsc = new TimeSeriesCollection();
    tsc.addSeries(t);
    if (jcb_compare.isSelected()) {
        tsc.addSeries(t2);
    }
    return tsc;
/*
                return new TimeSeriesCollection(t);
            }

            int count = account.getTransactionCount();

            if (count > 0) {
                Date start = account.getTransactionAt(0).getDate();
                Date stop = account.getTransactionAt(count - 1).getDate();
                list = DateUtils.getLastDayOfTheMonths(start, stop);
            }

            TimeSeries t = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"), rb.getString("Column.Balance"));

            AccountType type = account.getAccountType();

            for (Date aList : list) {
                // get balance for the whole month
                Date d = DateUtils.getLastDayOfTheMonth(aList);
                Date s = DateUtils.getFirstDayOfTheMonth(aList);

                BigDecimal balance = AccountBalanceDisplayManager.convertToSelectedBalanceMode(type, account.getBalance(s, d));
                t.add(new Month(aList), balance);
            }

            return new TimeSeriesCollection(t);
        */
}
Also used : Month(org.jfree.data.time.Month) TimeSeries(org.jfree.data.time.TimeSeries) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Aggregations

TimeSeries (org.jfree.data.time.TimeSeries)53 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)37 XYPlot (org.jfree.chart.plot.XYPlot)11 Date (java.util.Date)10 JFreeChart (org.jfree.chart.JFreeChart)10 Month (org.jfree.data.time.Month)9 TimeSeriesDataItem (org.jfree.data.time.TimeSeriesDataItem)8 BasicStroke (java.awt.BasicStroke)7 Color (java.awt.Color)7 Map (java.util.Map)7 Day (org.jfree.data.time.Day)6 RegularTimePeriod (org.jfree.data.time.RegularTimePeriod)6 IOException (java.io.IOException)5 List (java.util.List)5 DateAxis (org.jfree.chart.axis.DateAxis)5 NumberAxis (org.jfree.chart.axis.NumberAxis)5 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)5 FixedMillisecond (org.jfree.data.time.FixedMillisecond)5 BufferedImage (java.awt.image.BufferedImage)4 SimpleDateFormat (java.text.SimpleDateFormat)4