Skip to content
Philipp Jahoda edited this page Apr 3, 2016 · 8 revisions

The YAxis is a subclass of AxisBase. This wiki entry only describes the YAxis, not it's superclass.

The YAxis class (in versions older than 2.0.0 YLabels called), is the data and information container for everything related with the vertical-axis. Each Line-, Bar-, Scatter or CandleStickChart has a left and a right YAxis object, responsible for either the left, or the right axis respectively. The RadarChart has only one YAxis. By default, both axes of the chart are enabled and will be drawn.

In order to acquire an instance of the YAxis class, call one of the following methods:

YAxis leftAxis = chart.getAxisLeft();
YAxis rightAxis = chart.getAxisRight();

YAxis leftAxis = chart.getAxis(AxisDependency.LEFT);

YAxis yAxis = radarChart.getYAxis(); // this method radarchart only

At runtime, use public AxisDependency getAxisDependency() to determine the side of the chart this axis represents.

Customizing the axis values

  • setAxisMaxValue(float max): Set a custom maximum value for this axis. If set, this value will not be calculated automatically depending on the provided data.
  • resetAxisMaxValue(): Call this to undo a previously set maximum value. By doing this, you will again allow the axis to automatically calculate it's maximum.
  • setAxisMinValue(float min): Set a custom minimum value for this axis. If set, this value will not be calculated automatically depending on the provided data.
  • resetAxisMinValue(): 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 setAxisMinValue(...) or setAxisMaxValue(...) instead.
  • 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

Customizations that affect the value range of the axis need to be applied before setting data for the chart.

Custom formatting for axis labels

  • setValueFormatter(YAxisValueFormatterf): Sets a custom ValueFormatter for this axis. This interface allows to format/modify the original label text and instead return a customized text. More on the YAxisValueFormatter here.

The zero line

Besides the grid-lines, that originate horizontally alongside each value on the YAxis, there is the so called zeroline, which is drawn at the zero (0) value on the axis, and is similar to the grid-lines but can be configured separately.

  • setDrawZeroLine(boolean enabled): Enables / disables drawing the zero-line.
  • setZeroLineWidth(float width): Sets the line-width of the zero line.
  • setZeroLineColor(int color): Sets the color the zero-line should have.

Zero-line example code:

// data has AxisDependency.LEFT
YAxis left = mChart.getAxisLeft();
left.setDrawLabels(false); // no axis labels
left.setDrawAxisLine(false); // no axis line
left.setDrawGridLines(false); // no grid lines
left.setDrawZeroLine(true); // draw a zero line
mChart.getAxisRight().setEnabled(false); // no right axis

The above code will result in a zero-line like shown in the image below. No axis values are drawn, no grid lines or axis lines are drawn, just a zero line.

More example code

YAxis yAxis = mChart.getAxisLeft();
yAxis.setTypeface(...); // set a different font
yAxis.setTextSize(12f); // set the textsize
yAxis.setAxisMaxValue(100f); // the axis maximum is 100
yAxis.setTextColor(Color.BLACK);
yAxis.setValueFormatter(new MyValueFormatter());
yAxis.setLabelCount(6, true); // force 6 labels
//... and more

The documentation has moved.

Clone this wiki locally