Skip to content

Commit

Permalink
-Add options to set the shape, line style, and colour to time series …
Browse files Browse the repository at this point in the history
…plotter
  • Loading branch information
pbronka committed Mar 31, 2021
1 parent bc4d48b commit 9ad6abc
Showing 1 changed file with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package microsim.gui.plot;

import java.awt.Color;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JInternalFrame;
Expand Down Expand Up @@ -69,7 +71,11 @@ public class TimeSeriesSimulationPlotter extends JInternalFrame implements Event
private ArrayList<Source> sources;

private XYSeriesCollection dataset;


private XYPlot plot;

private XYLineAndShapeRenderer renderer;

private int maxSamples = 0;

public TimeSeriesSimulationPlotter(String title, String yaxis) { //Include legend by default
Expand Down Expand Up @@ -106,17 +112,18 @@ public TimeSeriesSimulationPlotter(String title, String yaxis, boolean includeLe
// chart.getLegend().setItemFont(new Font(fontName, style, (int)MicrosimShell.scale*size));

// get a reference to the plot for further customisation...
final XYPlot plot = chart.getXYPlot();
plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);

final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer = new XYLineAndShapeRenderer();
// renderer.setSeriesLinesVisible(0, false);
// renderer.setSeriesShapesVisible(1, false);

plot.setRenderer(renderer);


// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
// rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
Expand All @@ -131,6 +138,8 @@ public TimeSeriesSimulationPlotter(String title, String yaxis, boolean includeLe
this.setSize(400, 400);
}



public void onEvent(Enum<?> type) {
if (type instanceof CommonEventType && type.equals(CommonEventType.Update)) {
double d = 0.0;
Expand Down Expand Up @@ -270,6 +279,23 @@ public void addSeries(String legend, IDoubleSource plottableObject) {
dataset.addSeries(series);
}

public void addSeries(String legend, IDoubleSource plottableObject, Color lineColor, boolean shapesFilled, boolean isDashed, Shape shape) {
sources.add(new DSource(legend, plottableObject, IDoubleSource.Variables.Default));
//plot.addLegend(sources.size() - 1, legend);
XYSeries series = new XYSeries(legend);
if(maxSamples > 0) series.setMaximumItemCount(maxSamples);
dataset.addSeries(series);

int seriesIndex = dataset.getSeriesIndex(series.getKey()); //Get int Index of series using its key
Stroke dashed = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
getRenderer().setSeriesPaint(seriesIndex, lineColor); //Set color of the series in the renderer to what was requested
getRenderer().setSeriesShapesFilled(seriesIndex, shapesFilled); //Set if shapes should be filled or not
if (isDashed) {
getRenderer().setSeriesStroke(seriesIndex, dashed);
}
getRenderer().setSeriesShape(seriesIndex, shape);
}

/**
* Build a series retrieving data from a IDoubleSource object.
*
Expand All @@ -290,6 +316,37 @@ public void addSeries(String legend, IDoubleSource plottableObject,
dataset.addSeries(series);
}

public void addSeries(String legend, IDoubleSource plottableObject, Enum<?> variableID, Color lineColor, boolean shapesFilled, boolean isDashed, Shape shape) {
sources.add(new DSource(legend, plottableObject, variableID));
//plot.addLegend(sources.size() - 1, legend);
XYSeries series = new XYSeries(legend);
if(maxSamples > 0) series.setMaximumItemCount(maxSamples);
dataset.addSeries(series);
int seriesIndex = dataset.getSeriesIndex(series.getKey()); //Get int Index of series using its key

Stroke dashed = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
getRenderer().setSeriesPaint(seriesIndex, lineColor); //Set color of the series in the renderer to what was requested
getRenderer().setSeriesShapesFilled(seriesIndex, shapesFilled); //Set if shapes should be filled or not
if (isDashed) {
getRenderer().setSeriesStroke(seriesIndex, dashed);
}
getRenderer().setSeriesShape(seriesIndex, shape);

}

public void addSeries(String legend, IDoubleSource plottableObject, Enum<?> variableID, Color lineColor, boolean validation) {
if (validation) {
Shape myRectangle = new Rectangle2D.Float(-3,-3,6,6);
addSeries(legend, plottableObject, variableID, lineColor, false, true, myRectangle);
}
else {
Shape myCircle = new Ellipse2D.Float(-3, -3, 6, 6);
addSeries(legend, plottableObject, lineColor, true, false, myCircle);

}

}

/**
* Build a series from a IFloatSource object, using the default variableId.
*
Expand Down Expand Up @@ -447,12 +504,22 @@ public int getMaxSamples() {
return maxSamples;
}


/**
* Set the max sample parameter.
* @param maxSamples Maximum number of time-steps rendered on x axis.
*/
public void setMaxSamples(int maxSamples) {
this.maxSamples = maxSamples;
}

}


public XYLineAndShapeRenderer getRenderer() {
return (XYLineAndShapeRenderer) plot.getRenderer();
}

public void setRenderer(XYLineAndShapeRenderer renderer) {
plot.setRenderer(renderer);
}

}

0 comments on commit 9ad6abc

Please sign in to comment.