Spark
Sparkline: a very small line chart, typically drawn without axes or coordinates. It presents the general shape of the variation (typically over time) in some measurement, such as temperature or stock market price, in a simple and highly condensed way.
Spark is a simple Android library that takes a series of x,y points at any scale and draws them as a
sparkline chart.
Usage
Spark is setup with reasonable default values out of the box. Just add a SparkView
to your layout:
Then, just give it a SparkAdapter
to graph your data:
See spark-sample for a complete sample app.
Theming
Spark is very theme-friendly! It has default styles set for you, and welcomes any overrides:
In your Activity
/Fragment
/View
:
In your layout xml:
Set a default style for all SparkView
s in your app's theme:
Scrubbing
Scrubbing is when the user taps and drags their finger along the sparkline chart. It is very useful
to display additional detail information about the point the user is currently scrubbing over.
Enable scrubbing via xml:
or programatically:
and then add a SparkView.OnScrubListener
to get callbacks:
Base Line
It's frequently useful to show a "base line" against which the rest of the sparkline chart will be
compared. In your SparkAdapter
, override hasBaseLine()
to return true
and then return the
appropriate base line value in getBaseline()
.
X Values
Spark assumes that your graph's points are evenly distributed across the x-axis. If that's not true,
just override getX(int index)
in your SparkAdapter
to give SparkView
the correct value.
Animation
To animate sparkline changes, set an animator with sparkView.setSparkAnimator(sparkAnimator)
.
There are two built-in animators: LineSparkAnimator (default) and MorphSparkAnimator. Pass your own
implementation to achieve custom effects.
Data Boundaries
By default, Spark will calculate the min and max of your data set, and draw the sparkline as large as
possible within the View boundaries. If you want different behavior, such as "zooming in" on a portion
of your data, or "zooming out" to leave space between the sparkline and the side of the view, you
can override SparkAdapter.getDataBounds()
:
Vision
Spark is a very simple library and cannot possibly meet everyone's use-cases. A more robust charting
library (such as MP Android Chart) may be a better fit
if you're looking for things like axes or advanced touch gestures. Spark aims to be lightweight
alternative for showing simple sparklines. Spark will prioritize simplicity over new use-cases the
vast majority of the time.
Download
Gradle: