robinhood / spark

A simple Android sparkline chart view.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DataSet with one value isnt being drawn

zoltish opened this issue · comments

Whenever all items in the adapter supply one Y value, nothing gets drawn. I expected (and I think the "correct" behavior would be) a straight line to be drawn.

Also, as an extra; I do believe that having one point with a value should also draw a straight line, or at least a dot to indicate that there is something. The former can be achieved by adding two points, still thought Id mention it though.

Sounds like there are two cases here:

  1. Multiple points in your adapter, but they all return the same Y value.

For this case, we should definitely be drawing a straight line, so this sounds like a bug for sure.

  1. A single point in your adapter.

This is something of an undefined edge-case. A line isn't appropriate, since it implies there are at least two points in the graph. A dot is generally more correct, but leaves us in an odd position. Spark tries to shrink the bounds of the graph as tightly as it can to the Sparkline. In the case of a dot, it really can't since a single point has no size. It leaves a lot of ambiguity in where the dot should be placed.

An easy work-around in your adapter would be to just duplicate the single point, if you wish to get a line. But that's assuming that problem 1 above is fixed.

  1. Yup, thats what I meant.

  2. I can understand that, its a lot of logic for such a "minor" thing. Personally I dont mind, Ill work around it.

Thanks for the quick answers. Much appreciated!

The root issue is that the scale we calculate is infinity, due to a straight line technically having 0 height. A quick and dirty workaround until I can get the real fix out would be to add this to your SparkAdapter:

        @Override
        public RectF getDataBounds() {
            RectF bounds = super.getDataBounds();

            if (bounds.top == bounds.bottom) {
                bounds.top -= 1;
                bounds.bottom += 1;
            }

            return bounds;
        }

It's basically just enforcing a height on the data bounds, leaving your line directly in the center. Hoping to get a real fix in for you later this week.

Thanks a lot Dan! Im so thankful for finding your library, both for its functionality and for your quick responses. Will patiently wait for your fix as I have a few other things to tackle in the meanwhile myself. Thanks again!

You're welcome! Very glad you like it!

Just released 1.1.2, which should contain this fix!