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

org.pepstock.charba.client.impl.charts.GaugeDataset 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.impl.charts;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

import org.pepstock.charba.client.Defaults;
import org.pepstock.charba.client.colors.IsColor;
import org.pepstock.charba.client.defaults.IsDefaultOptions;

/**
 * The Gauge chart allows a number of properties to be specified for each dataset. These are used to set display properties for a specific dataset.
* Is equals of Meter dataset.
* The minimum value of data is 0 (see {@link MeterDataset#MINIMUM_VALUE}).
* To set the data, is mandatory to use {@link MeterDataset#setValue(double)}) method instead of {@link org.pepstock.charba.client.data.Dataset#setData(double...)}) one. * * @author Andrea "Stock" Stocchero */ public final class GaugeDataset extends MeterDataset { // list if thresholds private final List thresholds = new LinkedList<>(); // current status related to threshold private Threshold current = DefaultThreshold.NORMAL.getThreshold(); // flag to understand if the absolute or percentage value must be used // to compare thresholds private boolean percentageThreshold = true; /** * Comparator to sort the thresholds using the thresholds value, always ASCENDING */ private static final Comparator COMPARATOR = (Threshold o1, Threshold o2) -> (int) (o1.getValue() - o2.getValue()); /** * Creates a dataset for gauge with maximum value of data. It uses the global options has default. * * @param max maximum value of data. */ public GaugeDataset(double max) { this(max, Defaults.get().getGlobal()); } /** * Creates a dataset setting the maximum value of dataset and defaults value. * * @param max maximum value of dataset. * @param defaultValues default options */ public GaugeDataset(double max, IsDefaultOptions defaultValues) { super(GaugeChart.CONTROLLER_TYPE, max, defaultValues); // loads all gauge thresholds by default for (DefaultThreshold t : DefaultThreshold.values()) { thresholds.add(t.getThreshold()); } // sets current color super.setColor(current.getColor()); } /* * (non-Javadoc) * * @see org.pepstock.charba.client.impl.charts.MeterDataset#setValueColor(org.pepstock.charba.client.colors.IsColor) */ @Override public void setColor(IsColor valueColor) { // value color must be override because // depends on threshold // checking if consistent super.setColor(IsColor.isConsistent(current.getColor()) ? current.getColor().toRGBA() : DefaultThreshold.NORMAL.getColor().toRGBA()); } /* * (non-Javadoc) * * @see org.pepstock.charba.client.impl.charts.MeterDataset#setValueColor(java.lang.String) */ @Override public void setColor(String valueColor) { // value color must be override because // depends on threshold // checking if consistent super.setColor(IsColor.isConsistent(current.getColor()) ? current.getColor().toRGBA() : DefaultThreshold.NORMAL.getColor().toRGBA()); } /** * Returns the current threshold. * * @return the current */ public Threshold getCurrent() { return current; } /** * Returns true if percentage threshold is used, otherwise false. * * @return true if percentage threshold is used, otherwise false */ public boolean isPercentageThreshold() { return percentageThreshold; } /** * Sets true if percentage threshold is used, otherwise false. * * @param percentageThreshold true if percentage threshold is used, otherwise false */ public void setPercentageThreshold(boolean percentageThreshold) { // sets to use the value of thresholds as percentage of value this.percentageThreshold = percentageThreshold; // checks the threshold current = checkLevel(); // sets color setColor(current.getColor()); } /** * Sets all thresholds to use for this gauge. * * @param newThresholds thresholds array. */ public void setThresholds(Threshold... newThresholds) { // checks if arguments are consistent if (newThresholds != null && newThresholds.length > 0) { // clears existing thresholds thresholds.clear(); // adds all new ones thresholds.addAll(Arrays.asList(newThresholds)); // checks the threshold current = checkLevel(); // sets color setColor(current.getColor()); } } /** * Gets all thresholds by an unmodifiable list. * * @return all thresholds by an unmodifiable list */ public List getThresholds() { return Collections.unmodifiableList(thresholds); } /* * (non-Javadoc) * * @see org.pepstock.charba.client.data.MeterDataset#setValue(double) */ @Override public void setValue(double value) { // sets value super.setValue(value); // checks the threshold current = checkLevel(); // sets color setColor(current.getColor()); } /** * Checks the threshold status for this dataset, comparing the value with thresholds. * * @return the current threshold of this dataset. */ private Threshold checkLevel() { // checks if the thresholds are set if (!thresholds.isEmpty()) { // sorts the thresholds by value Collections.sort(thresholds, COMPARATOR); // if the percentage value must be used to compare the threshold // or to use the absolute value final double valueToCheck = isPercentageThreshold() ? getValue() / getMax() * 100 : getValue(); double lowLimit = 0; // scans all thresholds for (Threshold t : thresholds) { // checks if value is in the threshold if (t.isInRange(valueToCheck, lowLimit)) { // returns threshold return t; } // stores the previous threshold value as low limit // for next check lowLimit = t.getValue(); } // if here, doesn't match and then the last threshold is returned return ((LinkedList) thresholds).getLast(); } // default threshold is returned return DefaultThreshold.NORMAL.getThreshold(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy