All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pepstock.charba.client.options.Line Maven / Gradle / Ivy

There is a newer version: 6.5-gwt
Show newest version
/**
    Copyright 2017 Andrea "Stock" Stocchero

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

	    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package org.pepstock.charba.client.options;

import java.util.List;

import org.pepstock.charba.client.commons.ArrayInteger;
import org.pepstock.charba.client.commons.ArrayListHelper;
import org.pepstock.charba.client.commons.Key;
import org.pepstock.charba.client.commons.NativeObject;
import org.pepstock.charba.client.defaults.IsDefaultLine;
import org.pepstock.charba.client.enums.CapStyle;
import org.pepstock.charba.client.enums.CubicInterpolationMode;
import org.pepstock.charba.client.enums.JoinStyle;

/**
 * Line elements are used to represent the line in a line chart.
 * 
 * @author Andrea "Stock" Stocchero
 */
public final class Line extends AbstractElement implements IsDefaultLine, HasFill {

	/**
	 * Name of properties of native object.
	 */
	private enum Property implements Key
	{
		TENSION("tension"),
		BORDER_CAP_STYLE("borderCapStyle"),
		BORDER_DASH("borderDash"),
		BORDER_DASH_OFFSET("borderDashOffset"),
		BORDER_JOIN_STYLE("borderJoinStyle"),
		CUBIC_INTERPOLATION_MODE("cubicInterpolationMode"),
		CAP_BEZIER_POINTS("capBezierPoints"),
		STEPPED("stepped");

		// name value of property
		private final String value;

		/**
		 * Creates with the property value to use in the native object.
		 * 
		 * @param value value of property name
		 */
		private Property(String value) {
			this.value = value;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.pepstock.charba.client.commons.Key#value()
		 */
		@Override
		public String value() {
			return value;
		}

	}

	// instance of fill handler
	private final FillHandler fillHandler;

	/**
	 * Creates the object with the parent, the key of this element, default values and native object to map java script properties.
	 * 
	 * @param elements parent node to use to add this element where changed
	 * @param childKey the property name of this element to use to add it to the parent.
	 * @param defaultValues default provider
	 * @param nativeObject native object to map java script properties
	 */
	Line(Elements elements, Key childKey, IsDefaultLine defaultValues, NativeObject nativeObject) {
		super(elements, childKey, defaultValues, nativeObject);
		// creates fill handler instance
		this.fillHandler = new FillHandler(this, getDefaultValues().getFill(), getNativeObject());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.pepstock.charba.client.options.HasFill#getFillHandler()
	 */
	@Override
	public FillHandler getFillHandler() {
		return fillHandler;
	}

	/**
	 * Returns the B\u00e9zier curve tension (0 for no B\u00e9zier curves).
	 * 
	 * @param tension the B\u00e9zier curve tension (0 for no B\u00e9zier curves).
	 */
	public void setTension(double tension) {
		setValueAndAddToParent(Property.TENSION, tension);
	}

	/**
	 * Returns the B\u00e9zier curve tension (0 for no B\u00e9zier curves).
	 * 
	 * @return the B\u00e9zier curve tension (0 for no B\u00e9zier curves).
	 */
	@Override
	public double getTension() {
		return getValue(Property.TENSION, getDefaultValues().getTension());
	}

	/**
	 * Sets how the end points of every line are drawn.
	 * 
	 * @param borderCapStyle how the end points of every line are drawn.
	 */
	public void setBorderCapStyle(CapStyle borderCapStyle) {
		setValueAndAddToParent(Property.BORDER_CAP_STYLE, borderCapStyle);
	}

	/**
	 * Returns how the end points of every line are drawn.
	 * 
	 * @return how the end points of every line are drawn.
	 */
	@Override
	public CapStyle getBorderCapStyle() {
		return getValue(Property.BORDER_CAP_STYLE, CapStyle.values(), getDefaultValues().getBorderCapStyle());
	}

	/**
	 * Sets the line dash pattern used when stroking lines, using an array of values which specify alternating lengths of lines and gaps which describe the pattern.
	 * 
	 * @param borderDash the line dash pattern used when stroking lines, using an array of values which specify alternating lengths of lines and gaps which describe the pattern.
	 */
	public void setBorderDash(int... borderDash) {
		setArrayValueAndAddToParent(Property.BORDER_DASH, ArrayInteger.fromOrNull(borderDash));
	}

	/**
	 * Returns the line dash pattern used when stroking lines, using an array of values which specify alternating lengths of lines and gaps which describe the pattern.
	 * 
	 * @return the line dash pattern used when stroking lines, using an array of values which specify alternating lengths of lines and gaps which describe the pattern.
	 */
	public List getBorderDash() {
		ArrayInteger array = getArrayValue(Property.BORDER_DASH);
		return ArrayListHelper.list(array);
	}

	/**
	 * Sets the line dash pattern offset.
	 * 
	 * @param borderDashOffset the line dash pattern offset.
	 */
	public void setBorderDashOffset(double borderDashOffset) {
		setValueAndAddToParent(Property.BORDER_DASH_OFFSET, borderDashOffset);
	}

	/**
	 * Returns the line dash pattern offset.
	 * 
	 * @return the line dash pattern offset.
	 */
	@Override
	public double getBorderDashOffset() {
		return getValue(Property.BORDER_DASH_OFFSET, getDefaultValues().getBorderDashOffset());
	}

	/**
	 * Sets how two connecting segments (of lines, arcs or curves) with non-zero lengths in a shape are joined together (degenerate segments with zero lengths, whose specified end
	 * points and control points are exactly at the same position, are skipped).
	 * 
	 * @param borderJoinStyle how two connecting segments (of lines, arcs or curves) with non-zero lengths in a shape are joined together
	 */
	public void setBorderJoinStyle(JoinStyle borderJoinStyle) {
		setValueAndAddToParent(Property.BORDER_JOIN_STYLE, borderJoinStyle);
	}

	/**
	 * Returns how two connecting segments (of lines, arcs or curves) with non-zero lengths in a shape are joined together (degenerate segments with zero lengths, whose specified
	 * end points and control points are exactly at the same position, are skipped).
	 * 
	 * @return how two connecting segments (of lines, arcs or curves) with non-zero lengths in a shape are joined together
	 */
	@Override
	public JoinStyle getBorderJoinStyle() {
		return getValue(Property.BORDER_JOIN_STYLE, JoinStyle.values(), getDefaultValues().getBorderJoinStyle());
	}

	/**
	 * Sets true to keep B\u00e9zier control inside the chart, false for no restriction.
	 * 
	 * @param capBezierPoints true to keep B\u00e9zier control inside the chart, false for no restriction.
	 */
	public void setCapBezierPoints(boolean capBezierPoints) {
		setValueAndAddToParent(Property.CAP_BEZIER_POINTS, capBezierPoints);
	}

	/**
	 * Returns true to keep B\u00e9zier control inside the chart, false for no restriction.
	 * 
	 * @return true to keep B\u00e9zier control inside the chart, false for no restriction.
	 */
	@Override
	public boolean isCapBezierPoints() {
		return getValue(Property.CAP_BEZIER_POINTS, getDefaultValues().isCapBezierPoints());
	}

	/**
	 * Sets algorithm used to interpolate a smooth curve from the discrete data points.
* The following interpolation modes are supported:
*
* *
	 * 'default'
	 * 'monotone'
	 * 
* *
* The 'default' algorithm uses a custom weighted cubic interpolation, which produces pleasant curves for all types of datasets.
* The 'monotone' algorithm is more suited to y = f(x) datasets : it preserves monotonicity (or piecewise monotonicity) of the dataset being interpolated, and ensures local * extremums (if any) stay at input data points. * * @param mode algorithm used to interpolate a smooth curve from the discrete data points */ public void setCubicInterpolationMode(CubicInterpolationMode mode) { setValueAndAddToParent(Property.CUBIC_INTERPOLATION_MODE, mode); } /** * Returns algorithm used to interpolate a smooth curve from the discrete data points. * * @return algorithm used to interpolate a smooth curve from the discrete data points. */ @Override public CubicInterpolationMode getCubicInterpolationMode() { return getValue(Property.CUBIC_INTERPOLATION_MODE, CubicInterpolationMode.values(), getDefaultValues().getCubicInterpolationMode()); } /** * Sets true to show the line as a stepped line (tension will be ignored). * * @param stepped true to show the line as a stepped line (tension will be ignored). */ public void setStepped(boolean stepped) { setValueAndAddToParent(Property.STEPPED, stepped); } /** * Returns true to show the line as a stepped line (tension will be ignored). * * @return true to show the line as a stepped line (tension will be ignored). */ @Override public boolean isStepped() { return getValue(Property.STEPPED, getDefaultValues().isStepped()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy