use of org.jfree.data.time.TimeSeriesCollection in project pinot by linkedin.
the class AnomalyGraphGenerator method createChart.
/**
* Generates a JFreeChart object corresponding to the provided response data. See
* {@link #createChart(TimeOnTimeComparisonResponse, TimeGranularity, long, Map).
*/
public JFreeChart createChart(final TimeOnTimeComparisonResponse data, final TimeGranularity timeGranularity, final long windowMillis, final Map<RawAnomalyResultDTO, String> anomaliesWithLabels) {
Set<String> metrics = data.getMetrics();
// TODO error if more than one metric?
String metric = metrics.iterator().next();
LOG.info("Creating time series collections for {}", metric);
final TimeSeriesCollection dataset = createTimeSeries(data);
return createChart(dataset, metric, timeGranularity, windowMillis, anomaliesWithLabels);
}
use of org.jfree.data.time.TimeSeriesCollection 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));
}
}
}
use of org.jfree.data.time.TimeSeriesCollection 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);
}
}
use of org.jfree.data.time.TimeSeriesCollection 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);
}
use of org.jfree.data.time.TimeSeriesCollection in project jgnash by ccavanaugh.
the class MonthlyAccountBalanceChart method createVerticalXYBarChart.
private JFreeChart createVerticalXYBarChart(Account a) {
DateFormat df = new SimpleDateFormat("MM/yy");
TimeSeriesCollection data = createTimeSeriesCollection(a);
DateAxis dateAxis = new DateAxis(rb.getString("Column.Date"));
dateAxis.setTickUnit(new DateTickUnit(DateTickUnitType.MONTH, 1, df));
dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
// if (a.getTransactionCount() > 0) {
Date start = DateUtils.asDate(DateUtils.getFirstDayOfTheMonth(startDateField.getLocalDate()));
Date end = DateUtils.asDate(DateUtils.getLastDayOfTheMonth(endDateField.getLocalDate()));
dateAxis.setRange(start, end);
// }
NumberAxis valueAxis = new NumberAxis(rb.getString("Column.Balance"));
StandardXYToolTipGenerator tooltipGenerator = new StandardXYToolTipGenerator("{1}, {2}", df, NumberFormat.getNumberInstance());
XYBarRenderer renderer = new XYBarRenderer(0.2);
renderer.setBaseToolTipGenerator(tooltipGenerator);
XYPlot plot = new XYPlot(data, dateAxis, valueAxis, renderer);
String title = rb.getString("Title.AccountBalance") + " - " + a.getPathName();
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, false);
chart.setBackgroundPaint(null);
return chart;
}
Aggregations