Agung
Agung

Reputation: 49

Linechart x-axis value reapiting in library MPAndroidchart

I have a problem when i set value from sqlite to x-axis label. this is the picture And my code of X-axis forrmated is

Axis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            String selectQueryz = "SELECT  * FROM table_palembang";
            db = new DBHelper(getApplicationContext());
            SQLiteDatabase dbz = db.getWritableDatabase();
            Cursor cursorz = dbz.rawQuery(selectQueryz, null);
            countz = cursorz.getCount();
            String[] datez = new String[countz];
            ArrayList<String> arral = new ArrayList<>();
            for (int k = 0; k < countz; k++) {
                cursorz.moveToNext();
                datez[k] = cursorz.getString(2);
                arral.add(datez[k]);
            }
            return datez[countz % arral.size()];
        }
    });

can anyone help me? thanks ...

Upvotes: 0

Views: 293

Answers (1)

Vygintas B
Vygintas B

Reputation: 1694

ValueFormatter is used to format your data which you set with chart.setData() not to set data itself.

Here is sample code how to format dates

xAxis.setValueFormatter(new IAxisValueFormatter() {

    private final SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH);

    @Override
    public String getFormattedValue(float value, AxisBase axis) {

        long millis = TimeUnit.HOURS.toMillis((long) value);
        return mFormat.format(new Date(millis));
    }
});

EDIT

I just took a look that you store indices of date array to chart data. If so, you just need to return date from dates array.

String[] datez;
String selectQueryz = "SELECT  * FROM table_palembang";
db = new DBHelper(getApplicationContext());
SQLiteDatabase dbz = db.getWritableDatabase();
Cursor cursorz = dbz.rawQuery(selectQueryz, null);
countz = cursorz.getCount();
datez = new String[countz];
for (int k = 0; k < countz; k++) {
    cursorz.moveToNext();
    datez[k] = cursorz.getString(2);
}
xAxis.setValueFormatter(new IAxisValueFormatter() {

    @Override
    public String getFormattedValue(float value, AxisBase axis) {

        return datez[(int) value];
    }
});

P.S. DataBase calls should be in background thread.

Upvotes: 4

Related Questions