Android Open Source - GraphView Static Labels Formatter






From Project

Back to project page GraphView.

License

The source code is released under:

GNU General Public License

If you think the Android project GraphView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jjoe64.graphview.helper;
/*from www .j a va2  s . c o m*/
import com.jjoe64.graphview.DefaultLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.LabelFormatter;
import com.jjoe64.graphview.Viewport;

/**
 * Use this label formatter to show static labels.
 * Static labels are not bound to the data. It is typical used
 * for show text like "low", "middle", "high".
 *
 * You can set the static labels for vertical or horizontal
 * individually and you can define a label formatter that
 * is to be used if you don't define static labels.
 *
 * For example if you only use static labels for horizontal labels,
 * graphview will use the dynamicLabelFormatter for the vertical labels.
 */
public class StaticLabelsFormatter implements LabelFormatter {
    /**
     * reference to the viewport
     */
    protected Viewport mViewport;

    /**
     * the vertical labels, ordered from bottom to the top
     * if it is null, the labels will be generated via the #dynamicLabelFormatter
     */
    protected String[] mVerticalLabels;

    /**
     * the horizontal labels, ordered form the left to the right
     * if it is null, the labels will be generated via the #dynamicLabelFormatter
     */
    protected String[] mHorizontalLabels;

    /**
     * the label formatter that will format the labels
     * for that there are no static labels defined.
     */
    protected LabelFormatter mDynamicLabelFormatter;

    /**
     * reference to the graphview
     */
    protected final GraphView mGraphView;

    /**
     * creates the formatter without any static labels
     * define your static labels via {@link #setHorizontalLabels(String[])} and {@link #setVerticalLabels(String[])}
     *
     * @param graphView reference to the graphview
     */
    public StaticLabelsFormatter(GraphView graphView) {
        mGraphView = graphView;
        init(null, null, null);
    }

    /**
     * creates the formatter without any static labels.
     * define your static labels via {@link #setHorizontalLabels(String[])} and {@link #setVerticalLabels(String[])}
     *
     * @param graphView reference to the graphview
     * @param dynamicLabelFormatter     the label formatter that will format the labels
     *                                  for that there are no static labels defined.
     */
    public StaticLabelsFormatter(GraphView graphView, LabelFormatter dynamicLabelFormatter) {
        mGraphView = graphView;
        init(null, null, dynamicLabelFormatter);
    }

    /**
     * creates the formatter with static labels defined.
     *
     * @param graphView reference to the graphview
     * @param horizontalLabels  the horizontal labels, ordered form the left to the right
     *                          if it is null, the labels will be generated via the #dynamicLabelFormatter
     * @param verticalLabels    the vertical labels, ordered from bottom to the top
     *                          if it is null, the labels will be generated via the #dynamicLabelFormatter
     */
    public StaticLabelsFormatter(GraphView graphView, String[] horizontalLabels, String[] verticalLabels) {
        mGraphView = graphView;
        init(horizontalLabels, verticalLabels, null);
    }

    /**
     * creates the formatter with static labels defined.
     *
     * @param graphView reference to the graphview
     * @param horizontalLabels  the horizontal labels, ordered form the left to the right
     *                          if it is null, the labels will be generated via the #dynamicLabelFormatter
     * @param verticalLabels    the vertical labels, ordered from bottom to the top
     *                          if it is null, the labels will be generated via the #dynamicLabelFormatter
     * @param dynamicLabelFormatter     the label formatter that will format the labels
     *                                  for that there are no static labels defined.
     */
    public StaticLabelsFormatter(GraphView graphView, String[] horizontalLabels, String[] verticalLabels, LabelFormatter dynamicLabelFormatter) {
        mGraphView = graphView;
        init(horizontalLabels, verticalLabels, dynamicLabelFormatter);
    }

    /**
     * @param horizontalLabels  the horizontal labels, ordered form the left to the right
     *                          if it is null, the labels will be generated via the #dynamicLabelFormatter
     * @param verticalLabels    the vertical labels, ordered from bottom to the top
     *                          if it is null, the labels will be generated via the #dynamicLabelFormatter
     * @param dynamicLabelFormatter     the label formatter that will format the labels
     *                                  for that there are no static labels defined.
     */
    protected void init(String[] horizontalLabels, String[] verticalLabels, LabelFormatter dynamicLabelFormatter) {
        mDynamicLabelFormatter = dynamicLabelFormatter;
        if (mDynamicLabelFormatter == null) {
            mDynamicLabelFormatter = new DefaultLabelFormatter();
        }

        mHorizontalLabels = horizontalLabels;
        mVerticalLabels = verticalLabels;
    }

    /**
     * Set a label formatter that will be used for the labels
     * that don't have static labels.
     *
     * For example if you only use static labels for horizontal labels,
     * graphview will use the dynamicLabelFormatter for the vertical labels.
     *
     * @param dynamicLabelFormatter     the label formatter that will format the labels
     *                                  for that there are no static labels defined.
     */
    public void setDynamicLabelFormatter(LabelFormatter dynamicLabelFormatter) {
        this.mDynamicLabelFormatter = dynamicLabelFormatter;
        adjust();
    }

    /**
     * @param horizontalLabels  the horizontal labels, ordered form the left to the right
     *                          if it is null, the labels will be generated via the #dynamicLabelFormatter
     */
    public void setHorizontalLabels(String[] horizontalLabels) {
        this.mHorizontalLabels = horizontalLabels;
        adjust();
    }

    /**
     * @param verticalLabels    the vertical labels, ordered from bottom to the top
     *                          if it is null, the labels will be generated via the #dynamicLabelFormatter
     */
    public void setVerticalLabels(String[] verticalLabels) {
        this.mVerticalLabels = verticalLabels;
        adjust();
    }

    /**
     *
     * @param value raw input number
     * @param isValueX  true if it is a value for the x axis
     *                  false if it is a value for the y axis
     * @return
     */
    @Override
    public String formatLabel(double value, boolean isValueX) {
        if (isValueX && mHorizontalLabels != null) {
            double minX = mViewport.getMinX(false);
            double maxX = mViewport.getMaxX(false);
            double range = maxX - minX;
            value = value-minX;
            int idx = (int)((value/range) * (mHorizontalLabels.length-1));
            return mHorizontalLabels[idx];
        } else if (!isValueX && mVerticalLabels != null) {
            double minY = mViewport.getMinY(false);
            double maxY = mViewport.getMaxY(false);
            double range = maxY - minY;
            value = value-minY;
            int idx = (int)((value/range) * (mVerticalLabels.length-1));
            return mVerticalLabels[idx];
        } else {
            return mDynamicLabelFormatter.formatLabel(value, isValueX);
        }
    }

    /**
     * @param viewport the used viewport
     */
    @Override
    public void setViewport(Viewport viewport) {
        mViewport = viewport;
        adjust();
    }

    /**
     * adjusts the number of vertical/horizontal labels
     */
    protected void adjust() {
        mDynamicLabelFormatter.setViewport(mViewport);
        if (mVerticalLabels != null) {
            if (mVerticalLabels.length < 2) {
                throw new IllegalStateException("You need at least 2 vertical labels if you use static label formatter.");
            }
            mGraphView.getGridLabelRenderer().setNumVerticalLabels(mVerticalLabels.length);
        }
        if (mHorizontalLabels != null) {
            if (mHorizontalLabels.length < 2) {
                throw new IllegalStateException("You need at least 2 horizontal labels if you use static label formatter.");
            }
            mGraphView.getGridLabelRenderer().setNumHorizontalLabels(mHorizontalLabels.length);
        }
    }
}




Java Source Code List

com.jjoe64.graphview.DefaultLabelFormatter.java
com.jjoe64.graphview.GraphView.java
com.jjoe64.graphview.GridLabelRenderer.java
com.jjoe64.graphview.LabelFormatter.java
com.jjoe64.graphview.LegendRenderer.java
com.jjoe64.graphview.SecondScale.java
com.jjoe64.graphview.ValueDependentColor.java
com.jjoe64.graphview.Viewport.java
com.jjoe64.graphview.compat.OverScrollerCompat.java
com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter.java
com.jjoe64.graphview.helper.GraphViewXML.java
com.jjoe64.graphview.helper.StaticLabelsFormatter.java
com.jjoe64.graphview.series.BarGraphSeries.java
com.jjoe64.graphview.series.BaseSeries.java
com.jjoe64.graphview.series.DataPointInterface.java
com.jjoe64.graphview.series.DataPoint.java
com.jjoe64.graphview.series.LineGraphSeries.java
com.jjoe64.graphview.series.OnDataPointTapListener.java
com.jjoe64.graphview.series.PointsGraphSeries.java
com.jjoe64.graphview.series.Series.java