All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.diirt.datasource.graphene.HistogramGraph2DFunction Maven / Gradle / Ivy

There is a newer version: 3.1.7
Show newest version
/**
 * Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
 * All rights reserved. Use is subject to license terms. See LICENSE.TXT
 */
package org.diirt.datasource.graphene;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.diirt.graphene.*;
import org.diirt.datasource.QueueCollector;
import org.diirt.datasource.ReadFunction;
import org.diirt.vtype.VImage;
import org.diirt.vtype.VNumber;
import org.diirt.vtype.VNumberArray;
import org.diirt.vtype.VTable;
import org.diirt.vtype.VType;
import org.diirt.vtype.ValueUtil;

/**
 *
 * @author carcassi
 */
class HistogramGraph2DFunction implements ReadFunction {
    
    private ReadFunction arrayData;
    
    private AreaGraph2DRenderer renderer = new AreaGraph2DRenderer(300, 200);
    
    private Graph2DResult previousImage;
    private final QueueCollector rendererUpdateQueue = new QueueCollector<>(100);

    public HistogramGraph2DFunction(ReadFunction arrayData) {
        this.arrayData = new CheckedReadFunction(arrayData, "Data", VNumberArray.class);
    }
    
    public QueueCollector getUpdateQueue() {
        return rendererUpdateQueue;
    }

    @Override
    public Graph2DResult readValue() {
        VNumberArray data = arrayData.readValue();
        
        // Data must be available
        if (data == null) {
            return null;
        }
        
        // TODO: check array is one dimensional

        Cell1DDataset dataset = null;
        dataset = DatasetConversions.cell1DDatasetsFromVNumberArray(data);
        
        // Process all renderer updates
        for (AreaGraph2DRendererUpdate rendererUpdate : getUpdateQueue().readValue()) {
            renderer.update(rendererUpdate);
        }
        
        // If no size is set, don't calculate anything
        if (renderer.getImageHeight() == 0 && renderer.getImageWidth() == 0)
            return null;
        
        BufferedImage image = new BufferedImage(renderer.getImageWidth(), renderer.getImageHeight(), BufferedImage.TYPE_3BYTE_BGR);
        renderer.draw(image.createGraphics(), dataset);
        int index = -1;
        if (renderer.getFocusValueIndex() != null) {
            index = renderer.getFocusValueIndex();
        }
        
        return new Graph2DResult(data, ValueUtil.toVImage(image),
                new GraphDataRange(renderer.getXPlotRange(), dataset.getXRange(), dataset.getXRange()),
                new GraphDataRange(renderer.getYPlotRange(), dataset.getStatistics().getRange(), renderer.getYAggregatedRange()),
                index);
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy