Java Code Examples for de.erichseifert.gral.util.GraphicsUtils

The following examples show how to use de.erichseifert.gral.util.GraphicsUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Java-Data-Science-Cookbook   Source File: AreaPlot.java    License: MIT License 5 votes vote down vote up
public AreaPlot() {
	// Generate data
	DataTable data = new DataTable(Double.class, Double.class, Double.class, Double.class);
	for (double x = 0.0; x < 50; x ++) {
		double y1 = Double.NaN, y2 = Double.NaN, y3 = Double.NaN;
		y1 = random.nextGaussian();
		y2 = random.nextGaussian();
		y3 = random.nextGaussian();
		data.add(x, y1, y2, y3);
	}

	// Create data series
	DataSeries data1 = new DataSeries("series 1", data, 0, 1);
	DataSeries data2 = new DataSeries("series 2", data, 0, 2);
	DataSeries data3 = new DataSeries("series 3", data, 0, 3);

	// Create new xy-plot
	XYPlot plot = new XYPlot(data1, data2, data3);
	plot.setLegendVisible(true);
	plot.setInsets(new Insets2D.Double(20.0, 40.0, 20.0, 20.0));

	// Format data series
	formatFilledArea(plot, data1, COLOR2);
	formatFilledArea(plot, data2, COLOR1);
	formatLineArea(plot, data3, GraphicsUtils.deriveDarker(COLOR1));

	// Add plot to Swing component
	add(new InteractivePanel(plot));
}
 
Example 2
Source Project: Java-Data-Science-Cookbook   Source File: AreaPlot.java    License: MIT License 5 votes vote down vote up
private static void formatFilledArea(XYPlot plot, DataSource data, Color color) {
	PointRenderer point = new DefaultPointRenderer2D();
	point.setColor(color);
	plot.setPointRenderer(data, point);
	LineRenderer line = new DefaultLineRenderer2D();
	line.setColor(color);
	line.setGap(3.0);
	line.setGapRounded(true);
	plot.setLineRenderer(data, line);
	AreaRenderer area = new DefaultAreaRenderer2D();
	area.setColor(GraphicsUtils.deriveWithAlpha(color, 64));
	plot.setAreaRenderer(data, area);
}
 
Example 3
Source Project: Java-Data-Science-Cookbook   Source File: SimpleBarPlot.java    License: MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public SimpleBarPlot() {
	// Create example data
	DataTable data = new DataTable(Double.class, Integer.class, String.class);
	data.add(0.1,  1, "January");
	data.add(0.2,  3, "February");
	data.add(0.3, -2, "March");
	data.add(0.4,  6, "April");
	data.add(0.5, -4, "May");
	data.add(0.6,  8, "June");
	data.add(0.7,  9, "July");
	data.add(0.8, 11, "August");

	// Create new bar plot
	BarPlot plot = new BarPlot(data);

	// Format plot
	plot.setInsets(new Insets2D.Double(40.0, 40.0, 40.0, 40.0));
	plot.setBarWidth(0.075);

	// Format bars
	BarRenderer pointRenderer = (BarRenderer) plot.getPointRenderer(data);
	pointRenderer.setColor(
		new LinearGradientPaint(0f,0f, 0f,1f,
				new float[] { 0.0f, 1.0f },
				new Color[] { COLOR1, GraphicsUtils.deriveBrighter(COLOR1) }
		)
	);
	/*pointRenderer.setBorderStroke(new BasicStroke(3f));
	pointRenderer.setBorderColor(
		new LinearGradientPaint(0f,0f, 0f,1f,
				new float[] { 0.0f, 1.0f },
				new Color[] { GraphicsUtils.deriveBrighter(COLOR1), COLOR1 }
		)
	);*/
	pointRenderer.setValueVisible(true);
	pointRenderer.setValueColumn(2);
	pointRenderer.setValueLocation(Location.CENTER);
	pointRenderer.setValueColor(GraphicsUtils.deriveDarker(COLOR1));
pointRenderer.setValueFont(Font.decode(null).deriveFont(Font.BOLD));

	// Add plot to Swing component
	add(new InteractivePanel(plot));
}
 
Example 4
Source Project: Java-Data-Science-Cookbook   Source File: HistogramPlot.java    License: MIT License 4 votes vote down vote up
public HistogramPlot() {
	// Create example data
	Random random = new Random();
	DataTable data = new DataTable(Double.class);
	for (int i = 0; i < SAMPLE_COUNT; i++) {
		data.add(random.nextGaussian());
	}

	// Create histogram from data
	Histogram1D histogram = new Histogram1D(data, Orientation.VERTICAL,
			new Number[] {-4.0, -3.2, -2.4, -1.6, -0.8, 0.0, 0.8, 1.6, 2.4, 3.2, 4.0});
	// Create a second dimension (x axis) for plotting
	DataSource histogram2d = new EnumeratedData(histogram, (-4.0 + -3.2)/2.0, 0.8);

	// Create new bar plot
	BarPlot plot = new BarPlot(histogram2d);

	// Format plot
	plot.setInsets(new Insets2D.Double(20.0, 65.0, 50.0, 40.0));
	plot.getTitle().setText(
			String.format("Distribution of %d random samples", data.getRowCount()));
	plot.setBarWidth(0.78);

	// Format x axis
	plot.getAxisRenderer(BarPlot.AXIS_X).setTickAlignment(0.0);
	plot.getAxisRenderer(BarPlot.AXIS_X).setTickSpacing(0.8);
	plot.getAxisRenderer(BarPlot.AXIS_X).setMinorTicksVisible(false);
	// Format y axis
	plot.getAxis(BarPlot.AXIS_Y).setRange(0.0,
			MathUtils.ceil(histogram.getStatistics().get(Statistics.MAX)*1.1, 25.0));
	plot.getAxisRenderer(BarPlot.AXIS_Y).setTickAlignment(0.0);
	plot.getAxisRenderer(BarPlot.AXIS_Y).setMinorTicksVisible(false);
	plot.getAxisRenderer(BarPlot.AXIS_Y).setIntersection(-4.4);

	// Format bars
	plot.getPointRenderer(histogram2d).setColor(
		GraphicsUtils.deriveWithAlpha(COLOR1, 128));
	plot.getPointRenderer(histogram2d).setValueVisible(true);

	// Add plot to Swing component
	InteractivePanel panel = new InteractivePanel(plot);
	panel.setPannable(false);
	panel.setZoomable(false);
	add(panel);
}
 
Example 5
Source Project: cf4j   Source File: ScatterPlot.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected AbstractPlot getGralPlot() {

  // Create XY plot with data
  DataTable data = new DataTable(Double.class, Double.class);
  for (Pair<Double, Double> point : this.points) {
    data.add(point.getFirst(), point.getSecond());
  }

  DataSeries series = new DataSeries("Series", data);

  XYPlot plot = new XYPlot(series);

  // Customize plot
  plot.setBackground(PlotSettings.getBackgroundColor());

  PointRenderer pr = new DefaultPointRenderer2D();
  pr.setShape(new Ellipse2D.Double(-3.0, -3.0, 6.0, 6.0));
  pr.setColor(GraphicsUtils.deriveWithAlpha(PlotSettings.getColor(0), 128));
  plot.setPointRenderers(series, pr);

  plot.setInsets(
      new Insets2D.Double(
          PlotSettings.getClearInset(),
          PlotSettings.getyAxisInset(),
          PlotSettings.getxAxisInset(),
          PlotSettings.getClearInset()));

  // Customize x axis
  AxisRenderer xAxisRenderer = plot.getAxisRenderer(XYPlot.AXIS_X);

  xAxisRenderer.setLabel(new Label(xLabel));
  xAxisRenderer.getLabel().setFont(PlotSettings.getPrimaryFont());
  xAxisRenderer.setLabelDistance(PlotSettings.getxAxisLabelDistance());

  xAxisRenderer.setTickFont(PlotSettings.getSecondaryFont());
  xAxisRenderer.setTickLabelFormat(NumberFormat.getInstance(Locale.US));
  xAxisRenderer.setTicksAutoSpaced(true);

  // Customize y axis
  AxisRenderer yAxisRenderer = plot.getAxisRenderer(XYPlot.AXIS_Y);

  yAxisRenderer.setLabel(new Label(yLabel));
  yAxisRenderer.getLabel().setFont(PlotSettings.getPrimaryFont());
  yAxisRenderer.getLabel().setRotation(90);
  yAxisRenderer.setLabelDistance(PlotSettings.getyAxisLabelDistance());

  yAxisRenderer.setTickFont(PlotSettings.getSecondaryFont());
  yAxisRenderer.setTickLabelFormat(NumberFormat.getInstance(Locale.US));
  yAxisRenderer.setTicksAutoSpaced(true);

  xAxisRenderer.setIntersection(-Double.MAX_VALUE);
  yAxisRenderer.setIntersection(-Double.MAX_VALUE);

  // Customize navigator settings
  plot.getNavigator().setZoom(0.9);
  plot.getNavigator().setZoomable(false);

  return plot;
}