Spark


Source link: https://github.com/robinhoodmarkets/spark

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.

-- en.wikipedia.org/wiki/Sparkline

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:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

<com.robinhood.spark.SparkView

android:id="@+id/sparkview"

android:layout_width="match_parent"

android:layout_height="match_parent" /> </LinearLayout>

Then, just give it a SparkAdapter to graph your data:

SparkView sparkView = (SparkView) findViewById(R.id.sparkview);
 sparkView.setAdapter(new MyAdapter(data));
 ... public class MyAdapter extends SparkAdapter {

  private float[] yData;

public MyAdapter(float[] yData) {

 this.yData = yData;
  
}

@Override
  public int getCount() {

 return yData.length;
  
}

@Override
  public Object getItem(int index) {

 return yData[index];
  
}

@Override
  public float getY(int index) {

 return yData[index];
  
}
 
}

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:

sparkView.setLineColor(getColor(R.color.brand_color_primary));

In your layout xml:

 <com.robinhood.spark.SparkView

android:id="@+id/sparkview"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:spark_lineColor="@color/brand_color_primary"/>

Set a default style for all SparkViews in your app's theme:

<resources>
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<item name="spark_SparkViewStyle">@style/MySparkViewStyle</item>
  </style>

<style name="MySparkViewStyle" parent="@style/spark_SparkView">

<item name="spark_lineColor">@color/line_color</item>

<item name="spark_lineWidth">@dimen/line_width</item>

<item name="spark_cornerRadius">@dimen/corner_radius</item>

<item name="spark_fill">false</item>

 <item name="spark_baseLineColor">@color/base_line_color</item>

<item name="spark_baseLineWidth">@dimen/base_line_width</item>

 <item name="spark_scrubLineColor">@color/scrub_line_color</item>

<item name="spark_scrubLineWidth">@dimen/scrub_line_width</item>

<item name="spark_scrubEnabled">true</item>

 <item name="spark_animateChanges">true</item>
  </style> </resources> 

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:

<com.robinhood.spark.SparkView
  ...
  app:spark_scrubEnabled="true" />

or programatically:

sparkView.setScrubEnabled(true);

and then add a SparkView.OnScrubListener to get callbacks:

sparkView.setScrubListener(new SparkView.OnScrubListener() {

@Override

public void onScrubbed(Object value) {

 scrubInfoTextView.setText(getString(R.string.scrub_format, value));

}

  
}
);

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 path changes, set app:spark_animateChanges="true" or call sparkView.setAnimateChanges(true); .

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():

public class MyAdapter extends SparkAdapter {

  ...

@Override
  public RectF getDataBounds() {

RectF bounds = super.getDataBounds();

// will 'zoom in' to the middle portion of the graph

bounds.inset(bounds.width() / 4, bounds.height() / 4);

return bounds;
  
}
 
}

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:

compile 'com.robinhood.spark:spark:1.1.2'

License

Copyright 2016 Robinhood Markets, Inc.  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

This is a super simple plugin to be used in combination with the new unit testing feature of the Gradle Plugin / Android Studio.

It tries to solve the problem that you have to mock each and every Android SDK class (even SparseArray, TextUtils etc.) by allowing you to use selected classes from a real Android-Jarfile.

NiceSpinner is a re-implementation of the default Android's spinner, with a nice arrow animation and a different way to display its content.

PrismView provides animations for your views, similar to Dragger, but with fragments! You can change the fragment of the PrismView any time and it works like a ViewPager.

Android custom view and progress for music player.

Android service library which uses AAC Player. Ready to use Radio Player Service. (Android Background Player Service)

Features:

  • Play and stop live radio streams on background.
  • Handle incoming and outgoing calls.

Code generator for new Android app permission model.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes