Search code examples
androidandroid-graphview

How to replace the numbers on the bottom of Bar GraphView with strings?


I'm using jjoe64 GraphView library for creating a bar GraphView in my app.

When I'm creating a new DataPoint, it requires 2 values: X int, and a Y int, like this:

GraphView graph = (GraphView) findViewById(R.id.graph);
BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(new DataPoint[] {
    new DataPoint(0, -1),
    new DataPoint(1, 5),
    new DataPoint(2, 3),
    new DataPoint(3, 2),
    new DataPoint(4, 6)
});
graph.addSeries(series);

What can I do to replace the X int on the bottom with Strings?

EDIT #1

By bottom I mean this part where said 0, 0.5, 1...


Solution

  • What you call "bottom" is "X-axis". To label it use

    StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
    staticLabelsFormatter.setHorizontalLabels(new String[] {"old", "middle", "new"});
    graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
    

    Further reading: Documentation

    Or if you use GraphView 3.x:

    graphView.setHorizontalLabels(new String[] {"0.5", "1", "1.5", "2.0"});