Menggunakan library chart di android
Berhubung skripsi ane berhubungan dengan charting. Ane mau berbagi sedikit nih tentang berbagai library chart yang ane cari informasinya. Nah Ada library open source untuk android yang dapat kita gunakan namanya AchartEngine. Nah library ini lumayan lengkap untuk kita gunakan dalam android kita. Langsung saja yah beberapa screenshot yang bisa kita dapatkan dari library ini antara lain.Ini yang AbstractDemoChart.java dari sourcenya ane ambil
/** * Copyright (C) 2009, 2010 SC 4ViewSoft SRL * * 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. */ package org.data.provider.chart; import java.util.Date; import java.util.List; import org.achartengine.chart.PointStyle; import org.achartengine.model.CategorySeries; import org.achartengine.model.MultipleCategorySeries; import org.achartengine.model.TimeSeries; import org.achartengine.model.XYMultipleSeriesDataset; import org.achartengine.model.XYSeries; import org.achartengine.renderer.DefaultRenderer; import org.achartengine.renderer.SimpleSeriesRenderer; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; /** * An abstract class for the demo charts to extend. */ public abstract class AbstractDemoChart implements IChart { /** * Builds an XY multiple dataset using the provided values. * * @param titles the series titles * @param xValues the values for the X axis * @param yValues the values for the Y axis * @return the XY multiple dataset */ protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues, List<double[]> yValues) { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); int length = titles.length; for (int i = 0; i < length; i++) { XYSeries series = new XYSeries(titles[i]); double[] xV = xValues.get(i); double[] yV = yValues.get(i); int seriesLength = xV.length; for (int k = 0; k < seriesLength; k++) { series.add(xV[k], yV[k]); } dataset.addSeries(series); } return dataset; } /** * Builds an XY multiple series renderer. * * @param colors the series rendering colors * @param styles the series point styles * @return the XY multiple series renderers */ protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); renderer.setAxisTitleTextSize(16); renderer.setChartTitleTextSize(20); renderer.setLabelsTextSize(15); renderer.setLegendTextSize(15); renderer.setPointSize(5f); renderer.setMargins(new int[] { 20, 30, 15, 0 }); int length = colors.length; for (int i = 0; i < length; i++) { XYSeriesRenderer r = new XYSeriesRenderer(); r.setColor(colors[i]); r.setPointStyle(styles[i]); renderer.addSeriesRenderer(r); } return renderer; } /** * Sets a few of the series renderer settings. * * @param renderer the renderer to set the properties to * @param title the chart title * @param xTitle the title for the X axis * @param yTitle the title for the Y axis * @param xMin the minimum value on the X axis * @param xMax the maximum value on the X axis * @param yMin the minimum value on the Y axis * @param yMax the maximum value on the Y axis * @param axesColor the axes color * @param labelsColor the labels color */ protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) { renderer.setLabelsTextSize(12); renderer.setChartTitle(title); renderer.setChartTitleTextSize(13); renderer.setXTitle(xTitle); renderer.setYTitle(yTitle); renderer.setXAxisMin(xMin); renderer.setXAxisMax(xMax); renderer.setYAxisMin(yMin); renderer.setYAxisMax(yMax); renderer.setAxesColor(axesColor); renderer.setLabelsColor(labelsColor); } /** * Builds an XY multiple time dataset using the provided values. * * @param titles the series titles * @param xValues the values for the X axis * @param yValues the values for the Y axis * @return the XY multiple time dataset */ protected XYMultipleSeriesDataset buildDateDataset(String[] titles, List<Date[]> xValues, List<double[]> yValues) { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); int length = titles.length; for (int i = 0; i < length; i++) { TimeSeries series = new TimeSeries(titles[i]); Date[] xV = xValues.get(i); double[] yV = yValues.get(i); int seriesLength = xV.length; for (int k = 0; k < seriesLength; k++) { series.add(xV[k], yV[k]); } dataset.addSeries(series); } return dataset; } /** * Builds a category series using the provided values. * * @param titles the series titles * @param values the values * @return the category series */ protected CategorySeries buildCategoryDataset(String title, double[] values) { CategorySeries series = new CategorySeries(title); int k = 0; for (double value : values) { series.add("Project " + ++k, value); } return series; } /** * Builds a multiple category series using the provided values. * * @param titles the series titles * @param values the values * @return the category series */ protected MultipleCategorySeries buildMultipleCategoryDataset(String title, List<String[]> titles, List<double[]> values) { MultipleCategorySeries series = new MultipleCategorySeries(title); int k = 0; for (double[] value : values) { series.add(2007 + k + "", titles.get(k), value); k++; } return series; } /** * Builds a category renderer to use the provided colors. * * @param colors the colors * @return the category renderer */ protected DefaultRenderer buildCategoryRenderer(int[] colors) { DefaultRenderer renderer = new DefaultRenderer(); renderer.setLabelsTextSize(15); renderer.setLegendTextSize(15); renderer.setMargins(new int[] { 20, 30, 15, 0 }); for (int color : colors) { SimpleSeriesRenderer r = new SimpleSeriesRenderer(); r.setColor(color); renderer.addSeriesRenderer(r); } return renderer; } /** * Builds a bar multiple series dataset using the provided values. * * @param titles the series titles * @param values the values * @return the XY multiple bar dataset */ protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); int length = titles.length; for (int i = 0; i < length; i++) { CategorySeries series = new CategorySeries(titles[i]); double[] v = values.get(i); int seriesLength = v.length; for (int k = 0; k < seriesLength; k++) { series.add(v[k]); } dataset.addSeries(series.toXYSeries()); } return dataset; } /** * Builds a bar multiple series renderer to use the provided colors. * * @param colors the series renderers colors * @return the bar multiple series renderer */ protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); renderer.setAxisTitleTextSize(16); renderer.setChartTitleTextSize(20); renderer.setLabelsTextSize(15); renderer.setLegendTextSize(15); int length = colors.length; for (int i = 0; i < length; i++) { SimpleSeriesRenderer r = new SimpleSeriesRenderer(); r.setColor(colors[i]); renderer.addSeriesRenderer(r); } return renderer; } }
//Nah yang ini ane gunain untuk Barchartnya...
package org.data.provider.chart; import java.util.List; import java.util.Map; import org.achartengine.ChartFactory; import org.achartengine.GraphicalView; import org.achartengine.chart.BarChart.Type; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation; import org.data.provider.SettingActivity; import org.data.provider.tools.Tools; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.Paint.Align; import android.preference.PreferenceManager; /** * View viewToBeConverted; Bitmap viewBitmap = Bitmap.createBitmap(viewToBeConverted.getWidth(), viewToBeConverted.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(viewBitmap); viewToBeConverted.draw(canvas); */ /** * <h1>DEKLARASI BARCHART :</h1> Kelas BarChart yang digunakan untuk mengenerate * barChart </br> properti yang wajib dicantumkan adalah</br> * <code><b>legendText</b></code> : <i>text yang akan ditampilkan * dilegend</i></br> <code><b>chartLabel</b></code> : <i>Text yang ada di tiap * barchart yang tampil</i></br> <code><b>xLabel,yLabel, chartTitle </b></code>: * <i>digunakan untuk menuliskan keterangan tiap axis dan judul</i></br> * <code><b>Orientation</b></code> : <i>BarChart mau dalam keadaan horizontal * ataukah vertikal</i></br> <code><b>values</b></code> : <i>nilai dari tiap * barchart */ public class BarChartType extends AbstractDemoChart { /** * Text yang akan ditampilkan di legend */ protected String[] legendText; protected String[] chartLabel; /** * xLabel digunakan untuk menampilkan label pada axis X yLabel digunakan * untuk menampilkan label pada axis Y chartTitle digunakan untuk * menampilkan chartTitle */ protected String xLabel, yLabel, chartTitle; /** * set orientasi dari barchart yang akan ditampilkan defaultnya adalah * HORIZONTAL */ protected Orientation orientation; /** * Set nilai yang akan ditampilkan di barchart */ protected List<double[]> values; protected List<String> textLabelXAxis; protected double maxValue; /** * Warna barChartnya.. */ protected int colors[]; public void setColors(int[] colors) { this.colors = colors; } public String getChartTitle() { return chartTitle; } public void setLegendText(String[] legendText) { this.legendText = legendText; } public void setxLabel(String xLabel) { this.xLabel = xLabel; } public void setyLabel(String yLabel) { this.yLabel = yLabel; } public void setChartTitle(String chartTitle) { this.chartTitle = chartTitle; } public void setOrientation(Orientation orientation) { this.orientation = orientation; } public void setValues(List<double[]> values) { this.values = Tools.convertTo1Digit2Comma(values); this.maxValue = Tools.maxValueFirst(this.values, 0); } public List<String> getTextLabelXAxis() { return textLabelXAxis; } public void setTextLabelXAxis(List<String> textLabelXAxis) { this.textLabelXAxis = textLabelXAxis; } /** * Returns the chart name. * * @return the chart name */ public String getName() { return "Horizontal bar chart"; } /** * Returns the chart description. * * @return the chart description */ public String getDesc() { return "The monthly sales for the last 2 years (horizontal bar chart)"; } /** * Executes the chart demo. * * @param context * the context * @return the built intent */ public Intent execute(Context context) { XYMultipleSeriesRenderer renderer = buildBarRenderer(colors); renderer.setOrientation(this.orientation); setChartSettings(renderer, chartTitle, xLabel, yLabel, 0.5, 12.5, 0, maxValue, Color.GREEN, Color.LTGRAY); renderer.setAxisTitleTextSize(15); renderer.setXLabels(1); renderer.setYLabels(5); renderer.setXLabelsAngle(45); renderer.setChartValuesTextSize(10); // renderer.setMargins(new int []{30,5,5,5}); renderer.setXLabelsAlign(Align.LEFT); int legth = this.textLabelXAxis.size(); for (int i = 0; i < legth; i++) { renderer.addTextLabel(i + 1, this.textLabelXAxis.get(i)); } renderer.setBarSpacing(0.5); renderer.setDisplayChartValues(true); return ChartFactory.getBarChartIntent(context, buildBarDataset(legendText, values), renderer, Type.DEFAULT); } public GraphicalView getView(Context context) { XYMultipleSeriesRenderer renderer = buildBarRenderer(colors); renderer.setOrientation(this.orientation); setChartSettings(renderer, " ", xLabel, yLabel, 0.5, 12.5, 0, maxValue, Color.BLACK, Color.BLACK); renderer.setAxisTitleTextSize(15); renderer.setXLabels(1); renderer.setYLabels(5); renderer.setXLabelsAngle(45); renderer.setChartValuesTextSize(10); renderer.setAxesColor(Color.BLACK); //ambil dari setting SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(context); boolean lock= pref.getBoolean(SettingActivity.LOCK_Y_AXIS,false); renderer.setPanEnabled(true, !lock); // renderer.setMargins(new int []{30,5,5,5}); renderer.setXLabelsAlign(Align.LEFT); int legth = this.textLabelXAxis.size(); for (int i = 0; i < legth; i++) { renderer.addTextLabel(i + 1, this.textLabelXAxis.get(i)); } renderer.setBarSpacing(0.5); //jika digit melebihi 10^6 maka chartvalues tidak titampilkan boolean display=pref.getBoolean(SettingActivity.VIEW_DATA, true); if (Tools.numberDigit(maxValue) < 6) renderer.setDisplayChartValues(display); renderer.setMarginsColor(Color.parseColor("#00FF0000")); return ChartFactory.getBarChartView(context, buildBarDataset(legendText, values), renderer, Type.DEFAULT); } }
/** * Copyright (C) 2009, 2010 SC 4ViewSoft SRL * * 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. */ package org.data.provider.chart; import java.util.List; import org.achartengine.ChartFactory; import org.achartengine.chart.PointStyle; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; import org.data.provider.SettingActivity; import org.data.provider.tools.Tools; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.Paint.Align; import android.preference.PreferenceManager; import android.view.View; /** * Average temperature demo chart. */ public class LineChartType extends AbstractDemoChart { protected String[] legendText; protected List<double[]> xValues; protected List<double[]> values; protected int colors[]; protected PointStyle pointStyle[]; protected String chartTitle; protected String xAxisLabel; protected String yAxisLabel; protected List<String> textLabelAxis; public List<String> getTextLabelAxis() { return textLabelAxis; } public void setTextLabelAxis(List<String> textLabelAxis) { this.textLabelAxis = textLabelAxis; } public String[] getLegendText() { return legendText; } /** * Fungsi untuk mengeset legend Text berapa data yang akan ditampilkan * * @param legendText */ public void setLegendText(String[] legendText) { this.legendText = legendText; } public List<double[]> getxValues() { return xValues; } /** * Mengeset nilai axis x yang menjadi label pada x * * @param xValues */ public void setxValues(List<double[]> xValues) { this.xValues = xValues; } public List<double[]> getValues() { return values; } /** * Mengeset nilai yang akan ditampilkan * * @param values */ public void setValues(List<double[]> values) { this.values = values; } public int[] getColors() { return colors; } /** * Mengeset Warna Garis * * @param colors */ public void setColors(int[] colors) { this.colors = colors; } /** * Mengeset point style yang akan digunakan * * @return */ public PointStyle[] getPointStyle() { return pointStyle; } public void setPointStyle(PointStyle[] pointStyle) { this.pointStyle = pointStyle; } public String getChartTitle() { return chartTitle; } /** * Mengeset judul dari chart yang akan tampil * * @param title */ public void setChartTitle(String title) { this.chartTitle = title; } public String getxAxisLabel() { return xAxisLabel; } /** * Mengeset judul dari x axis * * @param xAxisLabel */ public void setxAxisLabel(String xAxisLabel) { this.xAxisLabel = xAxisLabel; } public String getyAxisLabel() { return yAxisLabel; } /** * Mengeset judul dari y label * * @param yAxisLabel */ public void setyAxisLabel(String yAxisLabel) { this.yAxisLabel = yAxisLabel; } /** * Returns the chart name. * * @return the chart name */ public String getName() { return "Average temperature"; } /** * Returns the chart description. * * @return the chart description */ public String getDesc() { return "The average temperature in 4 Greek islands (line chart)"; } /** * Executes the chart demo. * * @param context * the context * @return the built intent */ public Intent execute(Context context) { XYMultipleSeriesRenderer renderer = buildRenderer(colors, pointStyle); int length = renderer.getSeriesRendererCount(); for (int i = 0; i < length; i++) { ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)) .setFillPoints(true); } setChartSettings(renderer, chartTitle, xAxisLabel, yAxisLabel, Tools.minValueFirst(xValues, 0), Tools.maxValueFirst(xValues, 0), Tools.minValueFirst(values, 0), Tools.maxValueFirst(values, 0), Color.LTGRAY, Color.LTGRAY); renderer.setXLabels(12); renderer.setYLabels(10); renderer.setShowGrid(true); renderer.setXLabelsAlign(Align.RIGHT); renderer.setYLabelsAlign(Align.RIGHT); renderer.setPanLimits(new double[] { -10, 20, -10, 40 }); renderer.setZoomLimits(new double[] { -10, 20, -10, 40 }); Intent intent = ChartFactory .getLineChartIntent(context, buildDataset(legendText, xValues, values), renderer, chartTitle); return intent; } public View getView(Context context) { XYMultipleSeriesRenderer renderer = buildRenderer(colors, pointStyle); int length = renderer.getSeriesRendererCount(); for (int i = 0; i < length; i++) { ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)) .setFillPoints(true); } setChartSettings(renderer, " ", xAxisLabel, yAxisLabel, 0, textLabelAxis.size()+2, Tools.minValue(values), Tools.maxValue(values), Color.BLACK, Color.BLACK); renderer.setXLabels(1); renderer.setXLabelsAngle(45); renderer.setYLabels(5); // renderer.setShowGrid(true); renderer.setGridColor(Color.BLACK); int legth = this.textLabelAxis.size(); for (int i = 0; i < legth; i++) { renderer.addTextLabel(i + 1, this.textLabelAxis.get(i)); } SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(context); boolean lock= pref.getBoolean(SettingActivity.LOCK_Y_AXIS,false); renderer.setPanEnabled(true, !lock); boolean display=pref.getBoolean(SettingActivity.VIEW_DATA, true); if (Tools.numberDigit(Tools.maxValueFirst(xValues, 0)) < 6) renderer.setDisplayChartValues(display); renderer.setXLabelsAlign(Align.LEFT); renderer.setYLabelsAlign(Align.LEFT); renderer.setMarginsColor(Color.parseColor("#00FF0000")); renderer.setPanLimits(new double[] { -10, 20, -10, 40 }); renderer.setZoomLimits(new double[] { -10, 20, -10, 40 }); View v = ChartFactory.getLineChartView(context, buildDataset(legendText, xValues, values), renderer); return v; } }
/** * Copyright (C) 2009, 2010 SC 4ViewSoft SRL * * 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. */ package org.data.provider.chart; import org.achartengine.ChartFactory; import org.achartengine.model.CategorySeries; import org.achartengine.renderer.DefaultRenderer; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.view.View; /** * Class yang akan digunakan untuk membuat Pie Chart * * @author mrhands * */ public class PieChartType extends AbstractDemoChart { private double[] values; private int[] colors; private int textSize = 14; private String title; private String kategori[]; public String[] getKategori() { return kategori; } public void setKategori(String[] kategori) { this.kategori = kategori; } public String getTitle() { return title; } /** * Fungsi untuk mengeset title * * @param title */ public void setTitle(String title) { this.title = title; } /** * Fungsi untuk mendapatkan nilai values dari PieChart * * @return */ public double[] getValues() { return values; } /** * Fungsi untuk memberikan nilai ke Pie Chart * * @param values */ public void setValues(double[] values) { this.values = values; } public int[] getColors() { return colors; } /** * Fungsi untuk mengeset warna yang akan ditampilkan di Pie Chart * * @param colors */ public void setColors(int[] colors) { this.colors = colors; } public int getTextSize() { return textSize; } /** * Fungsi untuk mengeset besarnya text dalam chart * * @param textSize */ public void setTextSize(int textSize) { this.textSize = textSize; } public String getName() { return "Pie chart"; } public String getDesc() { return "Chart yang digunakan untuk PieChart"; } /** * Executes the chart demo. * * @param context * the context * @return the built intent */ public Intent execute(Context context) { CategorySeries categorySeries = new CategorySeries("data"); for (int i = 0; i < values.length; i++) { categorySeries.add(kategori[i], values[i]); } DefaultRenderer renderer = buildCategoryRenderer(colors); renderer.setLabelsTextSize(this.textSize); renderer.setLegendTextSize(10); return ChartFactory.getPieChartIntent(context, categorySeries, renderer, title); } public View getView(Context context) { CategorySeries categorySeries = new CategorySeries("data"); for (int i = 0; i < values.length; i++) { categorySeries.add(kategori[i], values[i]); } DefaultRenderer renderer = buildCategoryRenderer(colors); renderer.setLabelsTextSize(this.textSize); renderer.setLegendTextSize(10); renderer.setLabelsColor(Color.BLACK); return ChartFactory.getPieChartView(context, categorySeries, renderer); } }Karena banyak yang minta project saya kasih link untuk download projectnya..
https://dl.dropbox.com/u/20916268/Data%20Service%20Provider%209%20oktober%202011.zip