jjoe64 / GraphView

Android Graph Library for creating zoomable and scrollable line and bar graphs.
https://github.com/jjoe64/GraphView/wiki
Other
2.76k stars 811 forks source link

No datapoints visible #556

Open Blogshot opened 6 years ago

Blogshot commented 6 years ago

Hello there!

I'm adding datapoints to my graph. Sadly, the graph neither scrolls to the first point, nor seems to at least update its view. I'm trying to take a list of transactions and display a history of total balance.

My code:

final GraphView graph = findViewById(R.id.graph);

new Thread(new Runnable() {
  @Override
  public void run() {
    List<Transaction> transactions = db.transactionDao().getAllTransactions();

    // sorted by newest first, reverse so we have a proper timeline
    Collections.reverse(transactions);

    // generate unique timetamps
    ArrayList<Long> timestamps = new ArrayList<>();

    for (int i = 0; i < transactions.size(); i++) {
      Transaction transaction = transactions.get(i);

      if (!timestamps.contains(transaction.timestamp)) {
        timestamps.add(transaction.timestamp);
      }
    }

    // we just aggregated timestamps, now sum up the balance at each timestamp
    DataPoint[] points = new DataPoint[timestamps.size()];

    for (long timestamp : timestamps) {

      double worthAtTimestamp = 0.0;

      for (int i = 0; i < transactions.size(); i++) {
        Transaction transaction = transactions.get(i);

        if (transaction.timestamp == timestamp) {
          worthAtTimestamp += transaction.buy_price_usd;
        }
      }

      // we have millis as timestamps. divide by 1000 to get seconds
      Log.i("Plotting", "Added plot point at "  + timestamp/1000 + ", worth: " + worthAtTimestamp);
      points[timestamps.indexOf(timestamp)] = new DataPoint(timestamp/1000, worthAtTimestamp);
    }

    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(points);

    series.setDrawBackground(true);
    series.setDrawDataPoints(true);

    graph.addSeries(series);
  }
}).start();

Log:

01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1512120471, worth: 3.43
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1513681418, worth: 3.43
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1513681448, worth: 3.43
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1513681938, worth: 6.41
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1513681945, worth: 6.41
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1514075528, worth: 6.41
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1515269234, worth: 4.47
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1515269643, worth: 6.41
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1515269656, worth: 13.41
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1515388010, worth: 4.47
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1515453946, worth: 4.47
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1515502416, worth: 6.41
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1515523833, worth: 6.41
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1515523833, worth: 6.41
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1515523834, worth: 6.41
01-27 14:59:53.672 23675-23717/? I/Plotting: Added plot point at 1516023042, worth: 12.82

Layout:

[...]

<com.jjoe64.graphview.GraphView
  android:id="@+id/graph"
  android:layout_width="match_parent"
  android:layout_height="100dip" />

[...]

Screenshot: https://image.ibb.co/i9j5ob/device_2018_01_27_150237.png

I can see some blue line at the left end of the graph which doesn't correspond to the data I've put in.

Any ideas what I'm doing wrong?

Yvtq8K3n commented 6 years ago

It's working for me:

Take this example code as base:

            // generate unique timetamps
            long[] timestamps = new long[]{100000000L, 155269656L, 156023040L, 400000003L, 600000000L};
            double worthAtTimestamp = 3.43;
            double worthAtTimestamp2 = 9.23;
            // we just aggregated timestamps, now sum up the balance at each timestamp
            DataPoint[] points = new DataPoint[timestamps.length];
            int index=0;
            for (long timestamp:timestamps) {
                //Adding abit of variation in the chart(if position mod of 5 write 3.43 else 9.23)
                points[index++] = new DataPoint(timestamp, 
                           (timestamp%5==0)?worthAtTimestamp:worthAtTimestamp2);
            }

            LineGraphSeries<DataPoint> series = new LineGraphSeries<>(points);

            series.setDrawBackground(true);
            series.setDrawDataPoints(true);

            graphQuality.addSeries(series);
            graphQuality.getGridLabelRenderer().setHorizontalLabelsAngle(90);
            graphQuality.getGridLabelRenderer().setNumHorizontalLabels(4);
            graphQuality.getViewport().setMaxX(timestamps[timestamps.length-1]);
            graphQuality.getViewport().setMaxY(10);
            graphQuality.getViewport().setMinX(0);

How its look like: image

(I would close this but i dont think i can)