0

Bar Graph with NO X valueI am able to plot MP Android chart successfully but If I click the particular bar then its not showing the X axis value. I am using kotlin and below is the part of the sample code.

Input:

[{"date":"2018-09-15","count":"5"},{"date":"2018-09-16","count":"10"}]

Working Code:

val entries = ArrayList<BarEntry>()
    val labels = ArrayList<String>()
    val arr = JSONArray(jsonStringArray)
    for (i in 0 until arr.length()) {
        entries.add(BarEntry(arr.getJSONObject(i).getInt("count").toFloat(), i))
        labels.add(arr.getJSONObject(i).getString("date"))
           }
    val barDataSet = BarDataSet(entries, "Test Data")
    barDataSet.setValueTextSize(10f)
    val data = BarData(labels, barDataSet)
    barChart.data = data 
    val colors = intArrayOf(Color.rgb(132, 101, 122))
    barDataSet.setColors(colors)
  • Do you mean X axis value or Y axis value? X axis value should be shown below the axis automatically when there is enough space. – Ricky Mo Oct 12 '18 at 02:39
  • Thanks @RickyMo for your response. If I point to the bar then I want the value to be displayed at the top. –  Oct 12 '18 at 04:08
  • I added Sample Bar Chart in this discussion. –  Oct 12 '18 at 04:16
  • That is the Y value not the X. – Ricky Mo Oct 12 '18 at 04:21
  • That's right. If I click one bar then I can see only Y value but not the X Value. My goal here is to If I click the bar then I need to see both the X and Y values. –  Oct 12 '18 at 04:29
  • You can use `barDataSet.setValueFormatter()` and implement your custom `ValueFormatter` to show whatever text you want. – Ricky Mo Oct 12 '18 at 04:30
  • I tried ValueFormatter, Its rounding the Y value but its not showing X value in the graph. –  Oct 12 '18 at 04:43

1 Answers1

0

You can implement your own ValueFormatter to show whatever text you want. In getFormattedValue(), you get access to entry which you can call getXIndex()

private class MyValueFormatter implements ValueFormatter
{

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        return String.format("%d,%f",entry.getXIndex(),value);
    }
}

In kotlin you can do it like this:

barDataSet.valueFormatter = object : ValueFormatter{
    fun getFormattedValue(value: Float, entry: Entry, dataSetIndex: Int, viewPortHandler: ViewPortHandler): String {
        return String.format("%d,%f", entry.xIndex, value)
    }
}
Ricky Mo
  • 6,285
  • 1
  • 14
  • 30
  • Below is the sample code. Somehow the formatter is not working properly for me. [{"date":"2018-09-15","count":"5"},{"date":"2018-09-16","price":"10"}] val entries = ArrayList() val labels = ArrayList() val arr = JSONArray(jsonStringArray) for (i in 0 until arr.length()) { entries.add(BarEntry(arr.getJSONObject(i).getInt("count").toFloat(), i)) labels.add(arr.getJSONObject(i).getString("date")) } –  Oct 12 '18 at 05:03
  • Please add your relevant code in your post properly. – Ricky Mo Oct 12 '18 at 05:08
  • sorry about it. Added the complete code in the post. –  Oct 12 '18 at 05:09
  • Can you show your implementation of your `ValueFormatter` and the complete initialization of your bar chart? – Ricky Mo Oct 12 '18 at 05:25
  • barDataSet.valueFormatter ----I am using Kotlin –  Oct 12 '18 at 05:56
  • Please show your code. You should have created a class like `class MyValueFormatter : ValueFormatter` and do `barDataSet.valueFormatter = MyValueFormatter()` somewhere. – Ricky Mo Oct 12 '18 at 06:01
  • I am getting data from the JSON output and the complete code is in the post. var jsonStringArray="[{"date":"2018-09-15","count":"5"},{"date":"2018-09-16","count":"10"}]" –  Oct 12 '18 at 06:44
  • You didn't show your implementation of `ValueFormatter`. I added kotlin code to do so. – Ricky Mo Oct 12 '18 at 07:44
  • Thanks but now its showing 25*value with six digits of zero after the value. I am not sure how its getting the value. barDataSet.valueFormatter= ValueFormatter { value, entry, dataSetIndex, viewPortHandler -> String.format("%d,%f", entry.xIndex, value) } –  Oct 12 '18 at 15:30