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

li.rudin.mavenjs.charting.calculations.SeriesIntegratorTask Maven / Gradle / Ivy

The newest version!
package li.rudin.mavenjs.charting.calculations;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import li.rudin.mavenjs.charting.result.ChartResult;
import li.rudin.mavenjs.charting.result.SeriesEntry;

public class SeriesIntegratorTask implements Callable
{
	
	public SeriesIntegratorTask(ChartResult result, int maxItems)
	{
		this.result = result;
		this.maxItems = maxItems;
	}
	
	private final ChartResult result;
	private final int maxItems;

	@Override
	public ChartResult call() throws Exception
	{
		long startTime = System.currentTimeMillis();
		
		//Copy data locally
		List fullData = result.getData();
		List calculatedData = new ArrayList();
		
		int itemCount = fullData.size();
		double itemsToAverage = (double)itemCount / (double)maxItems;

		itemsToAverage = Math.ceil(itemsToAverage);

		if (itemsToAverage <= 1.0)
			//No averaging to perform
			return result;

		
		int start = 0;
		
		while(true)
		{
			int end = start + (int)itemsToAverage;
			
			if (end > itemCount-1)
				end = itemCount-1;
			
			List subList = fullData.subList(start, end+1);
			int sublistSize = subList.size();
			
			long time = 0;
			double value = 0;
			Double min = null, max = null;
			
			for (SeriesEntry entry: subList)
			{
				value += entry.getValue();
				time += entry.getTime();
				
				//Check value min
				if (min == null || min > entry.getValue())
					min = entry.getValue();
				
				//Check min min
				if (entry.getMin() != null && min < entry.getMin())
					min = entry.getMin();
				
				//Check value max
				if (max == null || max < entry.getValue())
					max = entry.getValue();
				
				//Check max max
				if (entry.getMax() != null && max > entry.getMax())
					max = entry.getMax();
				
			}
			
			time /= sublistSize;
			value /= sublistSize;
			
			//Add new data
			calculatedData.add(new SeriesEntry(time, value, min, max));
			
			if (end == itemCount-1)
				break;
			
			start += (int)itemsToAverage;
		}

		long diffTime = System.currentTimeMillis() - startTime;
		
		result.setData(calculatedData);
		result.setDataGenerationTime(diffTime);

		
		return result;
	}
	

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy