de.erichseifert.gral.plots.BarPlot Java Examples

The following examples show how to use de.erichseifert.gral.plots.BarPlot. 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 File: SimpleBarPlot.java    From Java-Data-Science-Cookbook with 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 #2
Source File: HistogramPlot.java    From Java-Data-Science-Cookbook with 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 #3
Source File: HistogramPlot.java    From cf4j with Apache License 2.0 4 votes vote down vote up
@Override
protected AbstractPlot getGralPlot() {
  // Create histogram plot
  double minValue = this.data.getStatistics().get(Statistics.MIN);
  double maxValue = this.data.getStatistics().get(Statistics.MAX);
  double barWidth = (maxValue - minValue) / this.numBins;

  double displayOffset = 10 + Math.abs(minValue); // Hack to avoid visualization of 0 tick label

  Histogram1D h1d = new Histogram1D(this.data, Orientation.VERTICAL, this.numBins);
  DataSource histogram =
      new EnumeratedData(h1d, displayOffset + minValue + barWidth / 2, barWidth);

  BarPlot plot = new BarPlot(histogram);

  // Customize plot
  plot.setBackground(PlotSettings.getBackgroundColor());
  plot.setBorderColor(PlotSettings.getBackgroundColor());
  plot.setBarWidth(barWidth);

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

  BarPlot.BarRenderer barRenderer =
      (BarPlot.BarRenderer) plot.getPointRenderers(histogram).get(0);
  barRenderer.setColor(PlotSettings.getColor(0));
  barRenderer.setBorderColor(PlotSettings.getBackgroundColor());
  barRenderer.setBorderStroke(new BasicStroke(1));

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

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

  xAxisRenderer.setTickFont(PlotSettings.getSecondaryFont());
  xAxisRenderer.setTickSpacing(Double.MAX_VALUE); // Hack to avoid default tick labels
  xAxisRenderer.setMinorTicksVisible(false);
  xAxisRenderer.setTickAlignment(0);
  xAxisRenderer.setTickLength(xAxisRenderer.getTickLength() / 2.0);
  xAxisRenderer.setIntersection(0);

  Map<Double, String> xTicks = new HashMap<>();
  DecimalFormat df = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.US));
  for (int i = 0; i < this.numBins + 1; i++) {
    double tick = minValue + i * barWidth;
    xTicks.put(displayOffset + tick, df.format(tick));
  }
  xAxisRenderer.setCustomTicks(xTicks);

  double xMin = displayOffset + minValue - (maxValue - minValue) / 1000;
  double xMax = displayOffset + maxValue + (maxValue - minValue) / 1000;
  plot.getAxis(BarPlot.AXIS_X).setRange(xMin, xMax);

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

  yAxisRenderer.setLabel(new Label("Count"));
  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);
  yAxisRenderer.setIntersection(displayOffset + minValue);

  double yMax = histogram.getStatistics().get(Statistics.MAX, Orientation.VERTICAL, 1) * 1.05;
  plot.getAxis(BarPlot.AXIS_Y).setRange(0, yMax);

  // Customize navigator settings
  plot.getNavigator().setZoomable(true);

  return plot;
}