Package com.positive.charts.plot

Source Code of com.positive.charts.plot.DefaultDrawingSupplier

package com.positive.charts.plot;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.widgets.Display;

import com.positive.charts.util.Stroke;
import com.positive.colorchecker.StaticColorChecker;

/**
* A default implementation of the {@link DrawingSupplier} interface. All
* {@link Plot} instances have a new instance of this class installed by
* default.
*/
public class DefaultDrawingSupplier implements DrawingSupplier {

  /** The paint sequence. */
  private transient Color[] paintSequence;

  /** The current paint index. */
  private int paintIndex;

  /** The paint sequence. */
  private transient Color[] paintOutlineSequence;

  /** The current paint index. */
  private int paintOutlineIndex;

  /** The shape sequence. */
  private transient Path[] shapeSequence;

  /** The current shape index. */
  private int shapeIndex;

  /** Device which holds colors and paths. */
  protected final Device device;

  /**
   * <p>
   * Creates a new supplier, with default sequences for fill paint, outline
   * paint, stroke and shapes. All drawing assest are created on a supplied
   * device. If <code>device</code> is <code>null</code>, default
   * {@link Display} is used.
   *
   * <p>
   * Supplier must be disposed using {@link #dispose()} after it is no longer
   * used, to free the allocated OS resources.
   *
   * @see Display#getDefault()
   */
  public DefaultDrawingSupplier(Device device) {
    if (device == null) {
      device = Display.getDefault();
    }
    this.device = device;
    this.createStandardSeriesColors();
    this.createStandardSeriesOutlineColors();
    this.createStandardSeriesShapes();
  }

  private void createStandardSeriesColors() {
    this.paintSequence = new Color[] {
        new Color(this.device, 0xFF, 0x55, 0x55), // Pink
        new Color(this.device, 0x55, 0x55, 0xFF), // Green
        new Color(this.device, 0x55, 0xFF, 0x55), // Blue
        new Color(this.device, 0xFF, 0xFF, 0x55), // Yellow
        new Color(this.device, 0xFF, 0x55, 0xFF), // Violet
        new Color(this.device, 0x55, 0xFF, 0xFF), // Olive

        StaticColorChecker.dublicateColor(SWT.COLOR_RED),//this.device.getSystemColor(SWT.COLOR_RED),
        StaticColorChecker.dublicateColor(SWT.COLOR_GREEN),//this.device.getSystemColor(SWT.COLOR_GREEN),
        StaticColorChecker.dublicateColor(SWT.COLOR_BLUE),//this.device.getSystemColor(SWT.COLOR_BLUE)
        StaticColorChecker.dublicateColor(SWT.COLOR_CYAN),//this.device.getSystemColor(SWT.COLOR_CYAN)
        StaticColorChecker.dublicateColor(SWT.COLOR_MAGENTA),//this.device.getSystemColor(SWT.COLOR_MAGENTA)
        StaticColorChecker.dublicateColor(SWT.COLOR_YELLOW),//this.device.getSystemColor(SWT.COLOR_YELLOW)
        StaticColorChecker.dublicateColor(SWT.COLOR_DARK_RED),//this.device.getSystemColor(SWT.COLOR_DARK_RED)
        StaticColorChecker.dublicateColor(SWT.COLOR_DARK_GREEN),//this.device.getSystemColor(SWT.COLOR_DARK_GREEN)
        StaticColorChecker.dublicateColor(SWT.COLOR_DARK_BLUE),//this.device.getSystemColor(SWT.COLOR_DARK_BLUE)
        StaticColorChecker.dublicateColor(SWT.COLOR_DARK_CYAN),//this.device.getSystemColor(SWT.COLOR_DARK_CYAN)
        StaticColorChecker.dublicateColor(SWT.COLOR_DARK_MAGENTA),//this.device.getSystemColor(SWT.COLOR_DARK_MAGENTA)
        StaticColorChecker.dublicateColor(SWT.COLOR_DARK_YELLOW),//this.device.getSystemColor(SWT.COLOR_DARK_YELLOW)
        StaticColorChecker.dublicateColor(SWT.COLOR_GRAY),//this.device.getSystemColor(SWT.COLOR_GRAY)
        StaticColorChecker.dublicateColor(SWT.COLOR_DARK_GRAY), };//this.device.getSystemColor(SWT.COLOR_DARK_GRAY)
  }

  private void createStandardSeriesOutlineColors() {
    this.paintOutlineSequence = new Color[] { StaticColorChecker.dublicateColor(SWT.COLOR_BLACK) };//this.device.getSystemColor(SWT.COLOR_BLACK) };
  }

  /**
   * Creates an array of standard shapes to display for the items in series on
   * charts.
   *
   * @return The array of shapes.
   */
  private void createStandardSeriesShapes() {
    // Path[] result = new Path[10];
    final List paths = new LinkedList();
    Path path;

    final float size = 6.0f;
    final float delta = size / 2.0f;

    // square
    // result[0] = new Rectangle2D.Double(-delta, -delta, size, size);
    path = new Path(this.device);
    path.addRectangle(-delta, -delta, size, size);
    paths.add(path);

    // circle
    path = new Path(this.device);
    path.addArc(0, 0, size, size, 0, 360);
    paths.add(path);

    // up-pointing triangle
    // xpoints = intArray(0.0, delta, -delta);
    // ypoints = intArray(-delta, delta, delta);
    // result[2] = new Polygon(xpoints, ypoints, 3);

    // diamond
    // xpoints = intArray(0.0, delta, 0.0, -delta);
    // ypoints = intArray(-delta, 0.0, delta, 0.0);
    // result[3] = new Polygon(xpoints, ypoints, 4);

    // horizontal rectangle
    // result[4] = new Rectangle2D.Double(-delta, -delta / 2, size, size /
    // 2);

    // down-pointing triangle
    // xpoints = intArray(-delta, +delta, 0.0);
    // ypoints = intArray(-delta, -delta, delta);
    // result[5] = new Polygon(xpoints, ypoints, 3);

    // horizontal ellipse
    // result[6] = new Ellipse2D.Double(-delta, -delta / 2, size, size / 2);

    // right-pointing triangle
    // xpoints = intArray(-delta, delta, -delta);
    // ypoints = intArray(-delta, 0.0, delta);
    // result[7] = new Polygon(xpoints, ypoints, 3);

    // vertical rectangle
    // result[8] = new Rectangle2D.Double(-delta / 2, -delta, size / 2,
    // size);

    // left-pointing triangle
    // xpoints = intArray(-delta, delta, delta);
    // ypoints = intArray(0.0, -delta, +delta);
    // result[9] = new Polygon(xpoints, ypoints, 3);

    this.shapeSequence = (Path[]) paths.toArray(new Path[paths.size()]);
  }

  public void dispose() {
    for (int i = 0; i < this.shapeSequence.length; i++) {
      final Path path = this.shapeSequence[i];
      path.dispose();
    }
    for (int a = 0; a < this.paintSequence.length; a++) {
      this.paintSequence[a].dispose();
    }
  }

  public Color getNextOutlinePaint() {
    final Color result = this.paintOutlineSequence[this.paintOutlineIndex
        % this.paintOutlineSequence.length];
    this.paintOutlineIndex++;
    return result;
  }

  public Stroke getNextOutlineStroke() {
    return this.getNextStroke();
  }

  /**
   * Returns the next paint in the sequence.
   *
   * @return The paint.
   */
  public Color getNextPaint() {
    final Color result = this.paintSequence[this.paintIndex
        % this.paintSequence.length];
    this.paintIndex++;
    return result;
  }

  /**
   * Returns the next shape in the sequence.
   *
   * @return The shape.
   */
  public Path getNextShape() {
    final Path result = this.shapeSequence[this.shapeIndex
        % this.shapeSequence.length];
    this.shapeIndex++;
    return result;
  }

  public Stroke getNextStroke() {
    // TODO : Make more intellectual
    return new Stroke(1);
  }

}
TOP

Related Classes of com.positive.charts.plot.DefaultDrawingSupplier

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.