Android Charts

Android Charts is a simple library that provides different charting capabilities for use in Android™ applications. Currently it supports pie charts, line charts, and bar charts. I'll post updates here as they come, but I have no plans on keeping old versions of it hosted on here. Hope it's helpful to someone out there!

Screenshots
SS1 SS2 SS3 SS4 SS5 SS6 SS7 SS8
Download
Usage

First, to use any of the charting functionality given by this library, you must include the AAR (or embed the source code) into your Android Studio project. You should be able to find numerous online tutorials on how to do that so I'm not going to go into detail.



Pie Charts

sanderson.androidcharts.PieChart.PieChart is the relevant view you will want to implement in your XML layouts (lets say it's called "mylayout") and in your code. For example, in your layout you may enter something like the following to make a pie chart that is 150 dp by 150 dp:


				<sdanderson.androidcharts.PieChart.PieChart
					android:id="@+id/piechart"
					android:layout_width="150dp"
					android:layout_height="150dp" />
    	

After including the PieChart view in your XML, you can use the pie chart in your code like this:


				import sdanderson.androidcharts.PieChart.PieChart;
    	
				... MORE CODE ...
    	
				View v = inflater.inflate(R.layout.mylayout, container, false);

				PieChart p = (PieChart) v.findViewById(R.id.piechart);
				p.addSlice("First Label", 200, Color.RED);
				p.addSlice("Second Label", 100, Color.DKGRAY);
				p.addSlice("Third Label", 20, Color.BLUE);
				p.percentalize(true);
				p.setBorderWidth(6f);
				p.setBorderColor(Color.WHITE);
    	

PieChart Methods (returns nothing unless otherwise noted)

addSlice(String pLabel, float pValue, int pColor): Adds a 'slice' to the pie chart. addSlice() takes three arguments: a String for the label of the slice, a float for the value of that slice, and an int for the color of that slice.


PieChartItem getSliceByColor(int pColor): Gets the PieChartItem corresponding to the color given.


percentalize(boolean pPercentalize): Notifies the pie chart whether you want the number displayed as a percent in the popup. This method takes a single boolean argument: true if you want the result in percents, false if not. If you don't set this option, the output defaults to the actual values.


boolean isPercentalized(): This method returns a boolean that is true if the numbers displayed are being 'percentalized' and false if not.


setBorderWidth(float pBorderWidth): Dictates how wide the border between each slice and around the entire pie chart will be. It takes a single float as the argument dictating the width.


setBorderColor(int pBorderColor): Sets the color to be used for the border. The only argument this method takes is an int representing the border color.


setDecimalFormatPopupValue(DecimalFormat pDecimalFormat): Set the decimal formatting for the value display in the popup window.


setUseNumberAbbreviationSuffixValue(boolean pUseSuffix): Set whether to use number abbreviations or not in the popup window.


setNumberSuffixes(String pSuffixes): Sets what suffixes should be used. The default is "kmbt" for thousands, millions, billions, trillions.


setNumberGroupSplit(int pGroupSplit): Sets how many of the highest-place numbers to group together and display with the suffixs set in setNumberSuffixes. The default is 3 and works with the default "kmbt" suffixes.


setUseAnimation(boolean pUseAnimation): Sets whether to animate the chart or not.


setAnimationTime(float pAnimationTime): Sets how long the animation should take in milliseconds.


reanimate(): Reruns the animation sequence.


setOnSliceTouchedListener(OnSliceTouchedListener pOnSliceTouchedListener): Assigns the listener for the onSliceTouched callback in the OnSliceTouchedListener interface.

PieChart Interfaces

interface OnSliceTouchedListener { onSliceTouched(int pColor, PieChart pPieChart, float pTouchedX, float pTouchedY) }: onSliceTouched is called only if setOnSliceTouchedListener has assigned a listener and it is called when the PieChart view is tapped (when the user lifts their finger).

Line Charts

sanderson.androidcharts.LineChart.LineChart is the relevant view you want to implement in your XML layouts (lets say it's called "mylayout") and in your code. For example, in your layout you may enter something like the following to make a line chart that is 300dp high and stretches the width of the parent element:


				<sdanderson.androidcharts.LineChart.LineChart
					android:id="@+id/linechart"
					android:layout_width="fill_parent"
					android:layout_height="300dp" />
    	

After including the LineChart view in your XML, you can use the line chart in your code like in the example below. Note that you also need to include the Coordinate class which provides a convenient means of encapsulating the x,y values you'll be plotting.


				import sdanderson.androidcharts.Common.Coordinate;
				import sdanderson.androidcharts.LineChart.LineChart;
    		
				... MORE CODE ...
    		
				View v = inflater.inflate(R.layout.mylayout, container, false);
    		
				LineChart l = (LineChart) v.findViewById(R.id.linechart);
				List<Coordinate<Float, Float>> tempValues = new ArrayList<>();
				tempValues.add(new Coordinate<>(2010f, 100f));
				tempValues.add(new Coordinate<>(2011f, 200f));
				tempValues.add(new Coordinate<>(2012f, 300f));
				tempValues.add(new Coordinate<>(2013f, 400f));
				tempValues.add(new Coordinate<>(2014f, 500f));
				tempValues.add(new Coordinate<>(2015f, 600f));
				l.SetChartTitle("Number of People Eating Cheesecake");
				l.SetXLabel("Year");
				l.SetYLabel("People (1000s)");
				l.AddLine("Cheesecake", tempValues, Color.RED);
    	

LineChart Methods (returns nothing unless otherwise noted)

addLine(String pLabel, List<Coordinate<Float, Float>> pCoordinates, int pColor): Adds a new line to the chart with label pLabel, constituting a list of Coordinates in pCoordinates which will be drawn on the chart in color pColor.


setChartTitle(String pTitle): Sets the chart's title.


setChartTitleColor(int pColor): Sets the color of the chart's title.


setChartBackgroundColor(int pColor): Sets the background color of the chart.


setGridlineWidth(float pGridlineWidth): Sets the width of the horizontal gridlines.


setNumOfGridlines(int pNumOfGridlines): Sets how many horizontal gridlines should be drawn for the chart on each side of the Y = 0 line.


setChartGridlineColor(int pColor): Sets the color of the horizontal gridlines.


setZeroLineWidth(float pWidth): Sets the width the Y = 0 line.


setZeroLineColor(int pColor): Sets the color of the Y = 0 line.


setRoundOutChartYMinMax(boolean pRound): Sets whether to round the Y max and min values away from the mean value to the next number at the highest place value.


setRoundOutChartYMinMax(boolean pRound, int pHighestDigits): Sets whether to round the Y max and min values away from the mean value to the next number at the pHighestDigits highest place value.


setRoundOutChartXMinMax(boolean pRound): Sets whether to round the X max and min values away from the mean value to the next number at the highest place value.


setRoundOutChartXMinMax(boolean pRound, int pHighestDigits): Sets whether to round the X max and min values away from the mean value to the next number at the pHighestDigits highest place value.


setBorderWidth(float pWidth): Sets the width of the border around the chart.


setBorderColor(int pColor): Sets the color of the border around the chart.


setXLabelColor(int pColor): Sets the color of the X Axis label.


setYLabelColor(int pColor): Sets the color of the Y Axis label.


setXValueLabelsColor(int pColor): Sets the color of the X Axis value labels.


setYValueLabelsColor(int pColor): Sets the color of the Y Axis value labels.


setXLabel(String pLabel): Sets the X Axis label.


setYLabel(String pLabel): Sets the Y Axis label.


setDecimalFormatChartX(DecimalFormat pDecimalFormat): Sets the decimal formatting for the X value display on the chart.


setDecimalFormatChartY(DecimalFormat pDecimalFormat): Sets the decimal formatting for the Y value display on the chart.


setDecimalFormatPopupX(DecimalFormat pDecimalFormat): Sets the decimal formatting for the X value display in the popup.


setDecimalFormatPopupY(DecimalFormat pDecimalFormat): Sets the decimal formatting for the Y value display in the popup.


setUseNumberAbbreviationSuffixXValueChart(boolean pUseSuffix): Sets whether to use number abbreviations or not for the X value on the chart.


setUseNumberAbbreviationSuffixYValueChart(boolean pUseSuffix): Sets whether to use number abbreviations or not for the Y value on the chart.


setUseNumberAbbreviationSuffixXValuePopup(boolean pUseSuffix): Sets whether to use number abbreviations or not for the X value in the popup.


setUseNumberAbbreviationSuffixYValuePopup(boolean pUseSuffix): Sets whether to use number abbreviations or not for the Y value in the popup.


setNumberSuffixes(String pSuffixes): Sets what suffixes should be used. The default is "kmbt" for thousands, millions, billions, trillions.


setNumberGroupSplit(int pGroupSplit): Sets how many of the highest-place numbers to group together and display with the suffixs set in setNumberSuffixes. The default is 3 and works with the default "kmbt" suffixes.


setUseAnimation(boolean pUseAnimation): Sets whether to animate the drawing of the chart. The animation is a left to right wipe.


setAnimationTime(float pAnimationTime): Sets how long the animation should take in milliseconds.


reanimate(): Reruns the animation sequence.


setOnChartTouchedListener(OnChartTouchedListener pOnChartTouchedListener): Assigns the listener for the onChartTouched callback in the OnChartTouchedListener interface.


LineChart Interfaces


OnChartTouchedListener{ onChartTouched(List<Coordinate<Integer, Integer>> pTouchedPoints, LineChart pLineChart, float pTouchedX, float pTouchedY); }: onChartTouched is called only if setOnChartTouchedListener has assigned a listener and it is called when the LineChart view is tapped (when the user lifts their finger).

Bar Charts

sanderson.androidcharts.BarChart.BarChart is the relevant view you want to implement in your XML layouts (lets say it's called "mylayout") and in your code. For example, in your layout you may enter something like the following to make a line chart that is 300dp high and stretches the width of the parent element:


				<sdanderson.androidcharts.BarChart.BarChart
					android:id="@+id/barchart"
					android:layout_width="fill_parent"
					android:layout_height="300dp" />
    	

After including the BarChart view in your XML, you can use the bar chart in your code like in the example below.


				import sdanderson.androidcharts.BarChart.BarChart;
    		
				... MORE CODE ...
    		
				View v = inflater.inflate(R.layout.mylayout, container, false);
    		
				BarChart b1 = (BarChart) v.findViewById(R.id.barchart);
				b1.addBar("Bar1", -5450000f, Color.RED);
				b1.addBar("Bar2", -2550000f, Color.YELLOW);
				b1.addBar("Bar3", -1560000f, Color.BLUE);
				b1.addBar("Bar4", 3430000f, Color.GREEN);
				b1.addBar("Bar5", 1570000f, Color.MAGENTA);
				b1.setChartTitle("Bar Chart");
				b1.setXLabel("Some Stuff");
				b1.setYLabel("Random Numbers");
    	

BarChart Methods (returns nothing unless otherwise noted)

addBar(String pLabel, float pValue, int pColor): Adds a new bar to the chart with label pLabel having value pValue that will be drawn on the chart in color pColor.


BarChartItem getBarChartItemByColor(int pColor): Gets the BarChartItem corresponding to the color given.


setAllBarBorderWidths(float pWidth): Sets the width of the borders around each individual bar.


setAllBarBorderColors(int pColor): Sets the color of the borders around each individual bar.


setChartTitle(String pTitle): Sets the chart's title.


setChartTitleColor(int pColor): Sets the color of the chart's title.


setChartBackgroundColor(int pColor): Sets the background color of the chart.


setGridlineWidth(float pGridlineWidth): Sets the width of the horizontal gridlines.


setNumOfGridlines(int pNumOfGridlines): Sets how many horizontal gridlines should be drawn for the chart on each side of the Y = 0 line


setChartGridlineColor(int pColor): Sets the color of the horizontal gridlines.


setZeroLineWidth(float pWidth): Sets the width of the Y = 0 line.


setZeroLineColor(int pColor): Sets the color of the Y = 0 line.


setRoundOutChartYMinMax(boolean pRound): Sets whether to round the Y max and min values away from the mean value to the next number at the highest place value.


setRoundOutChartYMinMax(boolean pRound, int pHighestDigits): Sets whether to round the Y max and min values away from the mean value to the next number at the pHighestDigits highest place value.


setBorderWidth(float pWidth): Sets the width of the border around the chart.


setBorderColor(int pColor): Sets the color of the border around the chart.


setXLabelColor(int pColor): Sets the color of the X Axis label.


setYLabelColor(int pColor): Sets the color of the Y Axis label.


setYValueLabelsColor(int pColor): Sets the color of the Y Axis value labels.


setXLabel(String pLabel): Sets the X Axis label.


setYLabel(String pLabel): Sets the Y Axis label.


setDecimalFormatChartY(DecimalFormat pDecimalFormat): Sets the decimal formatting for the Y value display on the chart.


setDecimalFormatPopupY(DecimalFormat pDecimalFormat): Sets the decimal formatting for the Y value display in the popup.


setUseNumberAbbreviationSuffixYValueChart(boolean pUseSuffix): Sets whether to use number abbreviations or not for the Y value on the chart.


setUseNumberAbbreviationSuffixYValuePopup(boolean pUseSuffix): Sets whether to use number abbreviations or not for the Y value in the popup.


setNumberSuffixes(String pSuffixes): Sets what suffixes should be used. The default is "kmbt" for thousands, millions, billions, trillions.


setNumberGroupSplit(int pGroupSplit): Sets how many of the highest-place numbers to group together and display with the suffixs set in setNumberSuffixes. The default is 3 and works with the default "kmbt" suffixes.


setUseAnimation(boolean pUseAnimation): Sets whether to animate the drawing of the chart. The bars grow from the zero line to their corresponding value.


setAnimationTime(float pAnimationTime): Sets how long the animation should take in milliseconds.


reanimate(): Reruns the animation sequence.


setOnBarTouchedListener(OnBarTouchedListener pOnBarTouchedListener): Assigns the listener for the onBarTouched callback in the OnBarTouchedListener interface.

BarChart Interfaces

OnBarTouchedListener { onBarTouched(BarChartItem pBar, BarChart pPieChart, float pTouchedX, float pTouchedY); }: onBarTouched is called only if setOnBarTouchedListener has assigned a listener and it is called when the BarChart view is tapped (when the user lifts their finger).