Search in sources :

Example 91 with TimeSeriesCollection

use of org.jfree.data.time.TimeSeriesCollection in project ta4j by ta4j.

the class CandlestickChartWithChopIndicator method createAdditionalDataset.

/**
 * Builds an additional JFreeChart dataset from a ta4j bar series.
 *
 * @param series a bar series
 * @return an additional dataset
 */
private static TimeSeriesCollection createAdditionalDataset(BarSeries series) {
    ClosePriceIndicator indicator = new ClosePriceIndicator(series);
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    org.jfree.data.time.TimeSeries chartTimeSeries = new org.jfree.data.time.TimeSeries("Btc price");
    for (int i = 0; i < series.getBarCount(); i++) {
        Bar bar = series.getBar(i);
        chartTimeSeries.add(new Second(new Date(bar.getEndTime().toEpochSecond() * 1000)), indicator.getValue(i).doubleValue());
    }
    dataset.addSeries(chartTimeSeries);
    return dataset;
}
Also used : Bar(org.ta4j.core.Bar) Second(org.jfree.data.time.Second) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Date(java.util.Date)

Example 92 with TimeSeriesCollection

use of org.jfree.data.time.TimeSeriesCollection in project pentaho-platform by pentaho.

the class TimeSeriesCollectionChartComponent method getXmlContent.

@Override
public Document getXmlContent() {
    // Create a document that describes the result
    Document result = DocumentHelper.createDocument();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty("baseUrl", requestContext.getContextPath());
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty("fullyQualifiedServerUrl", PentahoSystem.getApplicationContext().getFullyQualifiedServerURL());
    // $NON-NLS-1$
    String mapName = "chart" + AbstractChartComponent.chartCount++;
    Document chartDefinition = jcrHelper.getSolutionDocument(definitionPath, RepositoryFilePermission.READ);
    if (chartDefinition == null) {
        // $NON-NLS-1$
        Element errorElement = result.addElement("error");
        errorElement.addElement("title").setText(// $NON-NLS-1$ //$NON-NLS-2$
        Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART"));
        // $NON-NLS-1$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", definitionPath);
        // $NON-NLS-1$
        errorElement.addElement("message").setText(message);
        error(message);
        return result;
    }
    // create a pie definition from the XML definition
    dataDefinition = createChart(chartDefinition);
    if (dataDefinition == null) {
        // $NON-NLS-1$
        Element errorElement = result.addElement("error");
        errorElement.addElement("title").setText(// $NON-NLS-1$ //$NON-NLS-2$
        Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART"));
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0002_CHART_DATA_MISSING", actionPath);
        // $NON-NLS-1$
        errorElement.addElement("message").setText(message);
        // 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
    // $NON-NLS-1$
    String chartTitle = "";
    try {
        if (width == -1) {
            // $NON-NLS-1$
            width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText());
        }
        if (height == -1) {
            // $NON-NLS-1$
            height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText());
        }
    } catch (Exception e) {
    // go with the default
    }
    if (chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) != null) {
        // $NON-NLS-1$
        urlTemplate = // $NON-NLS-1$
        chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME).getText();
    }
    if (chartDefinition.selectSingleNode("/chart/paramName") != null) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        paramName = chartDefinition.selectSingleNode("/chart/paramName").getText();
    }
    // $NON-NLS-1$
    Element root = result.addElement("charts");
    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, JFreeChartEngine.OUTPUT_PNG, printWriter, info, // $NON-NLS-1$
        this);
        applyOuterURLTemplateParam();
        populateInfo(info);
        // $NON-NLS-1$
        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));
        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);
                // $NON-NLS-1$
                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());
            }
        }
        String mapString = ImageMapUtilities.getImageMap(mapName, info);
        // $NON-NLS-1$
        chartElement.addElement("imageMap").setText(mapString);
        // $NON-NLS-1$
        chartElement.addElement("image").setText(fileName);
    }
    return result;
}
Also used : Element(org.dom4j.Element) Document(org.dom4j.Document) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) StandardEntityCollection(org.jfree.chart.entity.StandardEntityCollection) StringWriter(java.io.StringWriter) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ChartRenderingInfo(org.jfree.chart.ChartRenderingInfo) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) PrintWriter(java.io.PrintWriter)

Example 93 with TimeSeriesCollection

use of org.jfree.data.time.TimeSeriesCollection in project idempiere by idempiere.

the class ChartRenderer method render.

/**
 * @param panel
 * @param width
 */
public void render(Component parent, int width, int height) {
    chartBuilder = new ChartBuilder(chartModel);
    JFreeChart chart = chartBuilder.createChart();
    chart.getPlot().setForegroundAlpha(0.6f);
    ChartRenderingInfo info = new ChartRenderingInfo();
    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();
        myImage.setContent(image);
        parent.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 + "__" + System.currentTimeMillis());
            count++;
        }
        myImage.addEventListener(Events.ON_CLICK, new EventListener<Event>() {

            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 == 4) {
                        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) ChartBuilder(org.adempiere.apps.graph.ChartBuilder) TimeSeriesDataItem(org.jfree.data.time.TimeSeriesDataItem) JFreeChart(org.jfree.chart.JFreeChart) BufferedImage(java.awt.image.BufferedImage) XYItemEntity(org.jfree.chart.entity.XYItemEntity) Area(org.zkoss.zul.Area) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ChartRenderingInfo(org.jfree.chart.ChartRenderingInfo) AImage(org.zkoss.image.AImage) Event(org.zkoss.zk.ui.event.Event) MouseEvent(org.zkoss.zk.ui.event.MouseEvent) ChartEntity(org.jfree.chart.entity.ChartEntity) CategoryItemEntity(org.jfree.chart.entity.CategoryItemEntity) PieSectionEntity(org.jfree.chart.entity.PieSectionEntity) Imagemap(org.zkoss.zul.Imagemap)

Example 94 with TimeSeriesCollection

use of org.jfree.data.time.TimeSeriesCollection in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class JFreeChartTest method testSerialization3.

/**
 * Serialize a time series chart, restore it, and check for equality.
 */
@Test
public void testSerialization3() {
    RegularTimePeriod t = new Day();
    TimeSeries<String> series = new TimeSeries<>("Series 1");
    series.add(t, 36.4);
    t = t.next();
    series.add(t, 63.5);
    TimeSeriesCollection<String> dataset = new TimeSeriesCollection<>();
    dataset.addSeries(series);
    JFreeChart c1 = ChartFactory.createTimeSeriesChart("Test", "Date", "Value", dataset);
    JFreeChart c2 = TestUtils.serialised(c1);
    assertEquals(c1, c2);
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) Day(org.jfree.data.time.Day) Test(org.junit.jupiter.api.Test)

Aggregations

TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)94 TimeSeries (org.jfree.data.time.TimeSeries)69 JFreeChart (org.jfree.chart.JFreeChart)35 XYPlot (org.jfree.chart.plot.XYPlot)29 Date (java.util.Date)23 Day (org.jfree.data.time.Day)20 DateAxis (org.jfree.chart.axis.DateAxis)17 NumberAxis (org.jfree.chart.axis.NumberAxis)16 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)16 SimpleDateFormat (java.text.SimpleDateFormat)14 Month (org.jfree.data.time.Month)14 RegularTimePeriod (org.jfree.data.time.RegularTimePeriod)11 Color (java.awt.Color)9 Map (java.util.Map)9 Font (java.awt.Font)7 File (java.io.File)7 IOException (java.io.IOException)7 List (java.util.List)7 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)7 BasicStroke (java.awt.BasicStroke)6