<commit_before>package org.mwc.cmap.xyplot.views;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IMemento;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.XYDotRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.mwc.cmap.xyplot.views.providers.ICrossSectionDatasetProvider;

import MWC.GUI.Layers;
import MWC.GUI.Shapes.LineShape;
import MWC.GenericData.HiResDate;


public class CrossSectionViewer
{

	private List<ISelectionChangedListener> _listeners = new ArrayList<ISelectionChangedListener>();
	
	/**
	 * the Swing control we insert the plot into
	 */
	private Frame _chartFrame;
		
	private JFreeChart _chart;
	
	private ICrossSectionDatasetProvider _datasetProvider;
	
	private XYSeriesCollection _dataset = new XYSeriesCollection();
	
	private XYLineAndShapeRenderer _snailRenderer = new XYLineAndShapeRenderer();
	
	private XYDotRenderer _discreteRenderer = new XYDotRenderer();
	
	/**
	 * The current time we are looking at
	 */
	private HiResDate _currentTime = null;
	
	/**
	 * the chart marker
	 */
	private static final Shape _markerShape = new Rectangle2D.Double(-2, -2, 2, 2);
	private static final int _markerSize = 4;
	
	private long _timePeriod = 0;
	
	private String _plotId;
	
	/**
	 * Memento parameters
	 */
	private static interface PLOT_ATTRIBUTES
	{
		final String TIME_PERIOD = "Time_Period";
		final String IS_SNAIL = "Is_Snail";
		final String ID = "Plot_Id";
	}
	
	protected CrossSectionViewer(final Composite parent)
	{
	    //we need an SWT.EMBEDDED object to act as a holder
		final Composite holder = new Composite(parent, SWT.EMBEDDED);
		holder.setLayoutData(new GridData(GridData.FILL_VERTICAL
						| GridData.FILL_HORIZONTAL));

		// now we need a Swing object to put our chart into
		_chartFrame = SWT_AWT.new_Frame(holder);

		_chart = ChartFactory.createXYLineChart
				("Cross Section", // Title
				"Distance (km)", // X-Axis label
				"Elevation (m)", // Y-Axis label
				_dataset, // Dataset,
				PlotOrientation.VERTICAL,
				true, // Show legend,
				true, //tooltips
				true //urs
				);
        
		// Fix the axises start at zero
		final ValueAxis yAxis = _chart.getXYPlot().getRangeAxis(); 
		yAxis.setLowerBound(0);
		final ValueAxis xAxis = _chart.getXYPlot().getDomainAxis();
		xAxis.setLowerBound(0);
		
		final ChartPanel jfreeChartPanel = new ChartPanel(_chart);
		_chartFrame.add(jfreeChartPanel);		
	}
	
	protected boolean isSnail()
	{
		return _timePeriod != 0;
	}
	
	public void newTime(final HiResDate newDTG)
	{
		_currentTime = newDTG;
	}
	
	public void fillPlot(final Layers theLayers, final LineShape line,
			final ICrossSectionDatasetProvider prov)
	{
		fillPlot(theLayers, line, prov, _timePeriod);
	}
	
	public void fillPlot(final Layers theLayers, final LineShape line,
			final ICrossSectionDatasetProvider prov,
			final long timePeriod)
	{
		_timePeriod = timePeriod;
		if (theLayers == null || line == null)
			return;
		_datasetProvider = prov;		
		_dataset.removeAllSeries();
	
		if (_currentTime != null)
		{
			if(isSnail())
			{
				final HiResDate startDTG = new HiResDate(_currentTime
						.getDate().getTime() - _timePeriod);	
				_dataset = _datasetProvider.getDataset(line, theLayers, startDTG, _currentTime);
				final Map<Integer, Color> colors = _datasetProvider.getSeriesColors();
				for(Integer seriesKey: colors.keySet())
				{
					setSnailRenderer(seriesKey, colors.get(seriesKey));
				}
				_chart.getXYPlot().setRenderer(_snailRenderer);
			}
			else
			{
				_dataset = _datasetProvider.getDataset(line, theLayers, _currentTime);
				final Map<Integer, Color> colors = _datasetProvider.getSeriesColors();
				for(Integer seriesKey: colors.keySet())
				{
					setDiscreteRenderer(seriesKey, colors.get(seriesKey));
				}
				_chart.getXYPlot().setRenderer(_discreteRenderer);
			}
			
		}
		
		double maxX = 0;
		double maxY = 0;
		
		for (int i = 0; i < _dataset.getSeriesCount(); i++) 
		{
			final XYSeries series = _dataset.getSeries(_dataset.getSeriesKey(i));
			final double x = series.getMaxX(); 
			if (maxX < x)
				maxX = x;
			final double y = series.getMaxY(); 
			if (maxY < y)
				maxY = y;
		}
		
		final ValueAxis yAxis = _chart.getXYPlot().getRangeAxis(); 
		if (yAxis.getUpperBound() < maxY)
			yAxis.setUpperBound(maxY + 5);
		
		final ValueAxis xAxis = _chart.getXYPlot().getDomainAxis();
		if (xAxis.getUpperBound() < maxX)
			xAxis.setUpperBound(maxX + 5);
		
		_chart.getXYPlot().setDataset(_dataset);
	}
	
	private void setDiscreteRenderer(final int series, final Color paint)
	{	    
		_discreteRenderer.setSeriesShape(series, _markerShape);
		
		_discreteRenderer.setSeriesFillPaint(series, paint);
		_discreteRenderer.setSeriesPaint(series, paint);
		_discreteRenderer.setDotHeight(_markerSize);
		_discreteRenderer.setDotWidth(_markerSize);	   
	}
	
	private void setSnailRenderer(final int series, final Color paint) 
	{		
		_snailRenderer.setSeriesShape(series, _markerShape);
		
		_snailRenderer.setSeriesFillPaint(series, paint);
		_snailRenderer.setSeriesPaint(series, paint);

		_snailRenderer.setUseFillPaint(true);
		_snailRenderer.setSeriesShapesFilled(series, true);
		_snailRenderer.setSeriesShapesVisible(series, true);	  
	}	<commit_msg>Fixed axis max/min bounds<commit_after>package org.mwc.cmap.xyplot.views;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IMemento;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.XYDotRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.mwc.cmap.xyplot.views.providers.ICrossSectionDatasetProvider;

import MWC.GUI.Layers;
import MWC.GUI.Shapes.LineShape;
import MWC.GenericData.HiResDate;


public class CrossSectionViewer
{

	private List<ISelectionChangedListener> _listeners = new ArrayList<ISelectionChangedListener>();
	
	/**
	 * the Swing control we insert the plot into
	 */
	private Frame _chartFrame;
		
	private JFreeChart _chart;
	
	private ICrossSectionDatasetProvider _datasetProvider;
	
	private XYSeriesCollection _dataset = new XYSeriesCollection();
	
	private XYLineAndShapeRenderer _snailRenderer = new XYLineAndShapeRenderer();
	
	private XYDotRenderer _discreteRenderer = new XYDotRenderer();
	
	/**
	 * The current time we are looking at
	 */
	private HiResDate _currentTime = null;
	
	/**
	 * the chart marker
	 */
	private static final Shape _markerShape = new Rectangle2D.Double(-2, -2, 2, 2);
	private static final int _markerSize = 4;
	
	private long _timePeriod = 0;
	
	private String _plotId;
	
	/**
	 * Memento parameters
	 */
	private static interface PLOT_ATTRIBUTES
	{
		final String TIME_PERIOD = "Time_Period";
		final String IS_SNAIL = "Is_Snail";
		final String ID = "Plot_Id";
	}
	
	protected CrossSectionViewer(final Composite parent)
	{
	    //we need an SWT.EMBEDDED object to act as a holder
		final Composite holder = new Composite(parent, SWT.EMBEDDED);
		holder.setLayoutData(new GridData(GridData.FILL_VERTICAL
						| GridData.FILL_HORIZONTAL));

		// now we need a Swing object to put our chart into
		_chartFrame = SWT_AWT.new_Frame(holder);

		_chart = ChartFactory.createXYLineChart
				("Cross Section", // Title
				"Distance (km)", // X-Axis label
				"Elevation (m)", // Y-Axis label
				_dataset, // Dataset,
				PlotOrientation.VERTICAL,
				true, // Show legend,
				true, //tooltips
				true //urs
				);
        
		// Fix the axises start at zero
		final ValueAxis yAxis = _chart.getXYPlot().getRangeAxis(); 
		yAxis.setLowerBound(0);
		final ValueAxis xAxis = _chart.getXYPlot().getDomainAxis();
		xAxis.setLowerBound(0);
		
		final ChartPanel jfreeChartPanel = new ChartPanel(_chart);
		_chartFrame.add(jfreeChartPanel);		
	}
	
	protected boolean isSnail()
	{
		return _timePeriod != 0;
	}
	
	public void newTime(final HiResDate newDTG)
	{
		_currentTime = newDTG;
	}
	
	public void fillPlot(final Layers theLayers, final LineShape line,
			final ICrossSectionDatasetProvider prov)
	{
		fillPlot(theLayers, line, prov, _timePeriod);
	}
	
	public void fillPlot(final Layers theLayers, final LineShape line,
			final ICrossSectionDatasetProvider prov,
			final long timePeriod)
	{
		_timePeriod = timePeriod;
		if (theLayers == null || line == null)
			return;
		_datasetProvider = prov;		
		_dataset.removeAllSeries();
	
		if (_currentTime != null)
		{
			if(isSnail())
			{
				final HiResDate startDTG = new HiResDate(_currentTime
						.getDate().getTime() - _timePeriod);	
				_dataset = _datasetProvider.getDataset(line, theLayers, startDTG, _currentTime);
				final Map<Integer, Color> colors = _datasetProvider.getSeriesColors();
				for(Integer seriesKey: colors.keySet())
				{
					setSnailRenderer(seriesKey, colors.get(seriesKey));
				}
				_chart.getXYPlot().setRenderer(_snailRenderer);
			}
			else
			{
				_dataset = _datasetProvider.getDataset(line, theLayers, _currentTime);
				final Map<Integer, Color> colors = _datasetProvider.getSeriesColors();
				for(Integer seriesKey: colors.keySet())
				{
					setDiscreteRenderer(seriesKey, colors.get(seriesKey));
				}
				_chart.getXYPlot().setRenderer(_discreteRenderer);
			}
			
		}
		
		double maxX = 0;
		double maxY = 0;
		double minX = Integer.MAX_VALUE;
		double minY = Integer.MAX_VALUE;
		
		for (int i = 0; i < _dataset.getSeriesCount(); i++) 
		{
			final XYSeries series = _dataset.getSeries(_dataset.getSeriesKey(i));
			final double x = series.getMaxX(); 
			if (maxX < x)
				maxX = x;
			final double mX = series.getMinX();
			if (minX > mX)
				minX = mX;
			final double y = series.getMaxY(); 
			if (maxY < y)
				maxY = y;
			final double mY = series.getMinY();
			if (minY > mY)
				minY = mY;
		}
		
		final ValueAxis yAxis = _chart.getXYPlot().getRangeAxis(); 
		if (yAxis.getUpperBound() < maxY)
			yAxis.setUpperBound(maxY + 5);
		if (yAxis.getLowerBound() > minY)
			yAxis.setLowerBound(minY - 5);
		
		final ValueAxis xAxis = _chart.getXYPlot().getDomainAxis();
		if (xAxis.getUpperBound() < maxX)
			xAxis.setUpperBound(maxX + 5);
		if (xAxis.getLowerBound() > minX)
			xAxis.setLowerBound(minX - 5);
		
		_chart.getXYPlot().setDataset(_dataset);
	}
	
	private void setDiscreteRenderer(final int series, final Color paint)
	{	    
		_discreteRenderer.setSeriesShape(series, _markerShape);
		
		_discreteRenderer.setSeriesFillPaint(series, paint);
		_discreteRenderer.setSeriesPaint(series, paint);
		_discreteRenderer.setDotHeight(_markerSize);
		_discreteRenderer.setDotWidth(_markerSize);	   
	}
	
	private void setSnailRenderer(final int series, final Color paint) 
	{		
		_snailRenderer.setSeriesShape(series, _markerShape);
		
		_snailRenderer.setSeriesFillPaint(series, paint);
		_snailRenderer.setSeriesPaint(series, paint);

		_snailRenderer.setUseFillPaint(true);
		_snailRenderer.setSeriesShapesFilled(series, true);
		_snailRenderer.setSeriesShapesVisible(series, true);	  
	}	
{'fraction_non_alphanumeric': 0.10446146162810055, 'fraction_numerical': 0.002830031629765274, 'mean_word_length': 4.128040973111395, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6990651', 'n_tokens_mistral': 4530, 'n_tokens_neox': 3935, 'n_words': 867}