PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.67k stars 9.02k forks source link

All data appearing at one x-value (LineChart) #3138

Open EXJUSTICE opened 7 years ago

EXJUSTICE commented 7 years ago

I've previously and successfully implemented a linechart featuring an x-axis by using the method recommended by PhilJay, where a set of index integers are shared between the y and x-datasets.

I have heard that it is possible now with ver. 3+ to directly use timestamps on the x-axis, with an IAxisValueFormatter converting them to string dates. However, I am experiencing a strange situation where the data is all displayed at x=0, with no plotting or x-axis labels whatsoever.

screenshot_2017-05-24-22-46-45_resized_20170524_104652780

The system works as this: SQLite creates a datetime value, which I convert into a date, and then into a long timestamp. I've checked the validity of the timestamps by having them printed, theyre solid.

This is my method for making LineDataSets

public LineDataSet makeDataIntoGraph(ArrayList<Double> lists, String name) {
        List<Entry> dataEntries = new ArrayList<Entry>();
        Entry valEntry;

        float value = 0;
        long date =0;

        for (int i = 0; i < lists.size(); i++) {

            //TODO changed back to DateStamps
            value =lists.get(i).floatValue() ;

            date = LongStamps.get(i);

            valEntry = new Entry(date, value);
            dataEntries.add(valEntry);

        }
        //Turn into data
        LineDataSet indicatorData = new LineDataSet(dataEntries, name);
        //indicatorData.setAxisDependency(YAxis.AxisDependency.LEFT);

        return indicatorData;
    }

Any advice would be appreciated. I am running version 3.0.2.

Dashing-Daniel-Li commented 7 years ago

I have the same issue. The date is a long but the Entry class only accepts float so you get an inaccurate reading when drawing the graph.

My solution was to get the first item in the array time as a long. then minus that from all the points before putting them into the graph. if(points.size() >0){ long firstTimeStamp = points.get(0).getTimeStamp().getTime(); for(MoodLog point : points) { Date date = point.getTimeStamp(); long time = date.getTime()- firstTimeStamp; float x = time; float y = point.getScore(); entries.add(new Entry(x,y)); xAxisLabels.add(point.getDateShort()); }

timusus commented 6 years ago

If you're using a unix timestamp, the problem is when it gets converted to Float, it loses precision, so lots of timestamps that actually represent different times end up being rounded and represented as the same x value. See #2891