de.erichseifert.gral.data.DataTable Java Examples

The following examples show how to use de.erichseifert.gral.data.DataTable. 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: SineGraph.java    From Java-Data-Science-Cookbook with MIT License 6 votes vote down vote up
public SineGraph() throws FileNotFoundException, IOException {
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	setSize(1600, 1400);

	DataTable data = new DataTable(Double.class, Double.class);
	for (double x = -5.0; x <= 5.0; x+=0.25) {
           double y = 5.0*Math.sin(x);
           data.add(x, y);
       }

	XYPlot plot = new XYPlot(data);
	getContentPane().add(new InteractivePanel(plot));
	LineRenderer lines = new DefaultLineRenderer2D();
	plot.setLineRenderer(data, lines);
	Color color = new Color(0.0f, 0.0f, 0.0f);
	plot.getPointRenderer(data).setColor(color);
	plot.getLineRenderer(data).setColor(color);
}
 
Example #2
Source File: ScatterPlot.java    From Java-Data-Science-Cookbook with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
public ScatterPlot() {
	// Generate 100,000 data points
	DataTable data = new DataTable(Double.class, Double.class);
	for (int i = 0; i <= SAMPLE_COUNT; i++) {
		data.add(random.nextGaussian()*2.0,  random.nextGaussian()*2.0);
	}

	// Create a new xy-plot
	XYPlot plot = new XYPlot(data);

	// Format plot
	plot.setInsets(new Insets2D.Double(20.0, 40.0, 40.0, 40.0));
	plot.getTitle().setText(getDescription());

	// Format points
	plot.getPointRenderer(data).setColor(COLOR1);

	// Add plot to Swing component
	add(new InteractivePanel(plot), BorderLayout.CENTER);
}
 
Example #3
Source File: AreaPlot.java    From Java-Data-Science-Cookbook with 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 #4
Source File: SimplePiePlot.java    From Java-Data-Science-Cookbook with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public SimplePiePlot() {
	// Create data
	DataTable data = new DataTable(Integer.class);
	for (int i = 0; i < SAMPLE_COUNT; i++) {
		int val = random.nextInt(8) + 2;
		data.add((random.nextDouble() <= 0.15) ? -val : val);
	}

	// Create new pie plot
	PiePlot plot = new PiePlot(data);

	// Format plot
	plot.getTitle().setText(getDescription());
	// Change relative size of pie
	plot.setRadius(0.9);
	// Display a legend
	plot.setLegendVisible(true);
	// Add some margin to the plot area
	plot.setInsets(new Insets2D.Double(20.0, 40.0, 40.0, 40.0));

	PieSliceRenderer pointRenderer =
			(PieSliceRenderer) plot.getPointRenderer(data);
	// Change relative size of inner region
	pointRenderer.setInnerRadius(0.4);
	// Change the width of gaps between segments
	pointRenderer.setGap(0.2);
	// Change the colors
	LinearGradient colors = new LinearGradient(COLOR1, COLOR2);
	pointRenderer.setColor(colors);
	// Show labels
	pointRenderer.setValueVisible(true);
	pointRenderer.setValueColor(Color.WHITE);
	pointRenderer.setValueFont(Font.decode(null).deriveFont(Font.BOLD));

	// Add plot to Swing component
	add(new InteractivePanel(plot), BorderLayout.CENTER);
}
 
Example #5
Source File: SimpleBoxPlot.java    From Java-Data-Science-Cookbook with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public SimpleBoxPlot() {
	setPreferredSize(new Dimension(400, 600));

	// Create example data
	DataTable data = new DataTable(Integer.class, Integer.class, Integer.class);
	for (int i = 0; i < SAMPLE_COUNT; i++) {
		int x = (int) Math.round(5.0*random.nextGaussian());
		int y = (int) Math.round(5.0*random.nextGaussian());
		int z = (int) Math.round(5.0*random.nextGaussian());
		data.add(x, y, z);
	}

	// Create new box-and-whisker plot
	DataSource boxData = BoxPlot.createBoxData(data);
	BoxPlot plot = new BoxPlot(boxData);

	// Format plot
	plot.setInsets(new Insets2D.Double(20.0, 50.0, 40.0, 20.0));

	// Format axes
	plot.getAxisRenderer(BoxPlot.AXIS_X).setCustomTicks(
		DataUtils.map(
				new Double[] {1.0, 2.0, 3.0},
				new String[] {"Column 1", "Column 2", "Column 3"}
		)
	);

	// Format boxes
	/*Stroke stroke = new BasicStroke(2f);
	ScaledContinuousColorMapper colors =
		new LinearGradient(GraphicsUtils.deriveBrighter(COLOR1), Color.WHITE);
	colors.setRange(1.0, 3.0);*/

	BoxWhiskerRenderer pointRenderer =
			(BoxWhiskerRenderer) plot.getPointRenderer(boxData);
	/*pointRenderer.setWhiskerStroke(stroke);
	pointRenderer.setBoxBorderStroke(stroke);
	pointRenderer.setBoxBackground(colors);*/
	pointRenderer.setBoxBorderColor(COLOR1);
	pointRenderer.setWhiskerColor(COLOR1);
	pointRenderer.setCenterBarColor(COLOR1);

	plot.getNavigator().setDirection(XYNavigationDirection.VERTICAL);

	// Add plot to Swing component
	InteractivePanel panel = new InteractivePanel(plot);
	add(panel);
}
 
Example #6
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 #7
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 #8
Source File: ScatterPlot.java    From cf4j with 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;
}