In case you want to learn more about the axis without reading too much, you can check out our tutorial-video explaining the axis in detail.
The following methods mentioned below can be applied to both axes.
The axis classes allow specific styling and consist (can consist) of the following components/parts:
- The labels (drawn in vertical (y-axis) or horizontal (x-axis) alignment), which contain the axis description values
- A so called “axis-line” that is drawn directly next to and parallel to the labels
- The “grid-lines”, each originating from an axis-label in horizontal direction
LimitLines, that allow to present special infomation, like borders or constraints
Control which parts (of the axis) should be drawn
setEnabled(boolean enabled): Sets the axis enabled or disabled. If disabled, no part of the axis will be drawn regardless of any other settings.
setDrawLabels(boolean enabled): Set this to true to enable drawing the labels of the axis.
setDrawAxisLine(boolean enabled): Set this to true if the line alongside the axis (axis-line) should be drawn or not.
setDrawGridLines(boolean enabled): Set this to true to enable drawing the grid lines for the axis.
Customizing the axis range (min / max)
setAxisMaximum(float max): Set a custom maximum value for this axis. If set, this value will not be calculated automatically depending on the provided data.
resetAxisMaximum(): Call this to undo a previously set maximum value. By doing this, you will again allow the axis to automatically calculate it’s maximum.
setAxisMinimum(float min): Set a custom minimum value for this axis. If set, this value will not be calculated automatically depending on the provided data.
resetAxisMinimum(): Call this to undo a previously set minimum value. By doing this, you will again allow the axis to automatically calculate it’s minimum.
setStartAtZero(boolean enabled): Deprecated – Use
setInverted(boolean enabled): If set to true, this axis will be inverted which means the highest value will be on the bottom, the lowest value on top.
setSpaceTop(float percent): Sets the top spacing (in percent of the total axis-range) of the highest value in the chart in comparison to the highest value on the axis.
setSpaceBottom(float percent): Sets the bottom spacing (in percent of the total axis-range) of the lowest value in the chart in comparison to the lowest value on the axis.
setShowOnlyMinMax(boolean enabled): If enabled, this axis will only show it’s minimum and maximum value. This will ignore/override the defined label-count (if not forced).
setLabelCount(int count, boolean force): Sets the number of labels for the y-axis. Be aware that this number is not fixed (if force == false) and can only be approximated. If force is enabled (true), then the exact specified label-count is drawn – this can lead to uneven numbers on the axis.
setPosition(YAxisLabelPosition pos): Sets the position where the axis-labels should be drawn. Either INSIDE_CHART or OUTSIDE_CHART.
setGranularity(float gran): Sets the minimum interval between the y-axis values. This can be used to avoid value duplicating when zooming in to a point where the number of decimals set for the axis no longer allow to distinguish between two axis values.
setGranularityEnabled(boolean enabled): Enables the granularity feature that limits the interval of the y-axis when zooming in. Default: false
Styling / modifying the axis
setTextColor(int color): Sets the color of the axis labels.
setTextSize(float size): Sets the text-size of the axis labels in dp.
setTypeface(Typeface tf): Sets a custom
Typefacefor the axis labels.
setGridColor(int color): Sets the color of the grid-lines of this axis.
setGridLineWidth(float width): Sets the width of the grid-lines of this axis.
setAxisLineColor(int color): Sets the color of the axis-line of this axis.
setAxisLineWidth(float width): Sets the width of the axis-line of this axis.
enableGridDashedLine(float lineLength, float spaceLength, float phase): Enables the grid line to be drawn in dashed mode, e.g. like this “- – – – – -“. “lineLength” controls the length of the line pieces, “spaceLength” controls the space between the lines, “phase” controls the starting point.
Formatting axis values
Both axes support so called
LimitLines that allow to present special information, like borders or constraints.
LimitLines added to the
YAxis are drawn in horizontal direction, and in vertical direction when added to the
XAxis. This is how you add and remove
LimitLines from the axis:
addLimitLine(LimitLine l): Adds a new
LimitLineto this axis.
removeLimitLine(LimitLine l): Removes the specified
LimitLinefrom this axis.
- More methods for adding / removing available as well.
setDrawLimitLinesBehindData(boolean enabled): Allows to control the z-order between the
LimitLinesand the actual data. If this is set to true, the
LimitLinesare drawn behind the actual data, otherwise on top. Default: false
Limit lines (class
LimitLine) are (as the name might indicate) plain and simple lines can be used to provide additional information for the user.
As an example, your chart might display various blood pressure measurement results the user logged with an application. In order to inform the user that a systolic blood pressure of over 140 mmHg is considered to be a health risk, you could add a
LimitLine at 140 to provide that information.
YAxis leftAxis = chart.getAxisLeft(); LimitLine ll = new LimitLine(140f, "Blood Pressure High"); ll.setLineColor(Color.RED); ll.setLineWidth(4f); ll.setTextColor(Color.BLACK); ll.setTextSize(12f); // .. and more styling options leftAxis.addLimitLine(ll);