i just create a new chart with achartengine and i notice that when i move the chart it go over the x and y axis. thanks to Dan this problem solve by update my achartengine to 1.1.0 ver.
anyway i saw that this problem has been solve at the achartengine demoes but i can't understead why it doesn't fix the problem at my chart.
i guess it something that i do wrong, my xml code is :
<RelativeLayout
android:id="#+id/rlMyChartsGridDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rlMyChartsTopGrid"
android:background="#drawable/weeks_grid_down" >
<LinearLayout
android:id="#+id/chart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
my java code is:
public GraphicalView weightView(Context context) {
manageData = new ManageData(context);
TimeSeries series = new TimeSeries("The day");
TimeSeries maxSeries = new TimeSeries("Max Weight");
TimeSeries minSeries = new TimeSeries("Min Weight");
int count = 0;
float sum = 0;
float max = 0;
int preWeek = 0;
manageData.open();
cursor = manageData.getProfile();
manageData.close();
if ((cursor.moveToFirst()) && (cursor.getCount() != 0)) {
weightUnit = cursor.getString(C_PROFILE_WEIGHT_UNIT);
weight = cursor.getFloat(C_PROFILE_WEIGHT);
bmi = cursor.getFloat(C_PROFILE_BMI);
weightGain();
cursor.close();
minSeries.add(0,weight);
maxSeries.add(0,weight);
minSeries.add(12,weight+1);
maxSeries.add(12,weight+2);
minSeries.add(42,weight+minWeight);
maxSeries.add(42,weight+maxWeight);
}
manageData.open();
Cursor cursor = manageData.getDiaryInfo();
manageData.close();
cursor.moveToFirst();
if (cursor.getCount() > 0) {
preWeek = cursor.getInt(DIARY_WEEK);
}
while (!cursor.isAfterLast()) {
if ((cursor.getInt(DIARY_WEEK) == preWeek)
&& (cursor.getFloat(WEIGHT) != 0)) {
count++;
sum = sum + cursor.getFloat(WEIGHT);
cursor.moveToNext();
} else {
if (count != 0) {
series.add(preWeek, sum / count);
if (max < sum / count) {
max = sum / count;
}
}
sum = 0;
count = 0;
preWeek = cursor.getInt(DIARY_WEEK);
if (cursor.getFloat(WEIGHT) != 0) {
count++;
sum = sum + cursor.getFloat(WEIGHT);
}
cursor.moveToNext();
}
}
if (max < sum / count) {
max = sum / count;
}
series.add(preWeek, sum / count);
cursor.close();
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
dataset.addSeries(minSeries);
dataset.addSeries(maxSeries);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
// Customization Weight line
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(0xff154916);
renderer.setLineWidth(3);
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setFillPoints(true);
// Customization maxWeight line
XYSeriesRenderer rendererMax = new XYSeriesRenderer();
rendererMax.setColor(0xff8EC239);
rendererMax.setLineWidth(2);
// Customization minWeight line
XYSeriesRenderer rendererMin = new XYSeriesRenderer();
rendererMin.setColor(0xffF7931E);
rendererMin.setLineWidth(2);
mRenderer.setMargins(new int[] {20, 35, 20, 20 });
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.TRANSPARENT);
mRenderer.setMarginsColor(color);
mRenderer.addSeriesRenderer(renderer);
mRenderer.addSeriesRenderer(rendererMax);
mRenderer.addSeriesRenderer(rendererMin);
mRenderer.setYLabelsColor(0, Color.BLACK);
mRenderer.setXLabelsColor(Color.BLACK);
mRenderer.setLabelsTextSize(18);
mRenderer.setAxesColor(Color.BLACK);
mRenderer.setLabelsColor(Color.BLACK);
mRenderer.setYLabelsAlign(Align.RIGHT);
// mRenderer.setYAxisAlign(Align.LEFT, 0);
mRenderer.setXTitle("Week");
if(weightUnit.equals("kg")){
mRenderer.setYTitle("Weight (kg)");
} else {
mRenderer.setYTitle("Weight (lb)");
}
mRenderer.setZoomButtonsVisible(true);
mRenderer.setLabelsColor(0xff418614);
mRenderer.setLegendTextSize(15);
mRenderer.setPointSize(5);
// mRenderer.setZoomEnabled(false, false);
// mRenderer.setPanEnabled(true, false);
mRenderer.setXAxisMax(43);
mRenderer.setXAxisMin(0);
if(max>maxWeight + weight){
mRenderer.setYAxisMax(max + 10);
} else {
mRenderer.setYAxisMax(maxWeight + weight + 10);
}
mRenderer.setYAxisMin(0);
return ChartFactory.getLineChartView(context, dataset, mRenderer);
}
my main java code is:
public void onClick(View v) {
switch (v.getId()) {
case R.id.tbMyChartsWeight:
tbMyChartsWeight.setChecked(true);
tbMyChartsWaist.setChecked(false);
tbMyChartsMood.setChecked(false);
line = new LineGraph();
gView = line.weightView(this);
layout = (LinearLayout) findViewById(R.id.chart);
layout.removeAllViews();
layout.addView(gView);
return;
case R.id.tbMyChartsWaist:
tbMyChartsWeight.setChecked(false);
tbMyChartsWaist.setChecked(true);
tbMyChartsMood.setChecked(false);
line = new LineGraph();
gView = line.waistView(this);
layout = (LinearLayout) findViewById(R.id.chart);
layout.removeAllViews();
layout.addView(gView);
return;
case R.id.tbMyChartsMood:
tbMyChartsWeight.setChecked(false);
tbMyChartsWaist.setChecked(false);
tbMyChartsMood.setChecked(true);
line = new LineGraph();
gView = line.mentalView(this);
layout = (LinearLayout) findViewById(R.id.chart);
layout.removeAllViews();
layout.addView(gView);
return;
default:
break;
}
}
i hope someone can help me with that
and i'm sorry for my bad english
thanks for you all :]
You are using a transparent background. You can fix this by using other background type or don't set any background.
Related
I am creating Stacked bar chart using Achartengine library. The values doesn't show properly. I want want to show the chart value like this {20,20,20} at top of the bar and align to center.
Here is my code
dataset.addSeries(JumlahSeries);
dataset.addSeries(JumlahSeries1);
dataset.addSeries(FirstSeries);
final XYSeriesRenderer FirstRenderer = new XYSeriesRenderer();
Log.d("Error Here","29");
FirstRenderer.setColor(Color.BLUE);
FirstRenderer.setFillPoints(false);
FirstRenderer.setLineWidth(5);
FirstRenderer.setChartValuesTextSize(15);
FirstRenderer.setDisplayChartValues(false);
// Creating XYSeriesRenderer to customize expenseSeries
Log.d("Error Here","30");
final XYSeriesRenderer SecondRenderer = new XYSeriesRenderer();
SecondRenderer.setColor(Color.RED);
SecondRenderer.setFillPoints(false);
SecondRenderer.setLineWidth(5);
SecondRenderer.setDisplayChartValues(false);
SecondRenderer.setChartValuesTextSize(15);
Log.d("Error Here","31");
final XYSeriesRenderer ThirdRenderer = new XYSeriesRenderer();
ThirdRenderer.setFillPoints(false);
ThirdRenderer.setColor(Color.GREEN);
ThirdRenderer.setLineWidth(5);
ThirdRenderer.setDisplayChartValues(false);
ThirdRenderer.setChartValuesTextSize(15);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
final XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
//zoomIn = new Zoom((GraphicalView) mChart, true, multiRenderer.getZoomRate());
//multiRenderer.setChartTitle("\n"+"\n"+ title);
multiRenderer.setChartTitleTextSize(20);
//multiRenderer.setXTitle("\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"Laporan Pada :"+ DateToDisplay);
multiRenderer.setShowGridX(false);
multiRenderer.setShowLegend(true);
multiRenderer.setShowLabels(true);
multiRenderer.setAxisTitleTextSize(15);
multiRenderer.setMargins(new int[] { 70,70,25,25});
multiRenderer.setYTitle("Bilangan\n\n"+"\n"+"\n"+"\n");
multiRenderer.setZoomButtonsVisible(true);
multiRenderer.setBarSpacing(spacing);
multiRenderer.setYAxisMax(max);
multiRenderer.setYLabelsColor(0, Color.BLACK);
multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setXLabels(0);
multiRenderer.setPanEnabled(true, false);
double Xmax = 2;
if(StacktotalX.getTotal().size()<5){
Log.d("Error Here","32");
Xmax=StacktotalX.getTotal().size()*1.3;
}
multiRenderer.setXAxisMax(Xmax) ;
multiRenderer.setXAxisMin(-1) ;
multiRenderer.setLabelsTextSize(13);
multiRenderer.setZoomEnabled(true,false);
multiRenderer.setClickEnabled(true);
for (int i = 0; i < StacktotalX.getTotal().size(); i++)
{
Log.d("Error Here","33");
multiRenderer.addXTextLabel(i,StacktotalX.getTotal().get(i).toString());
Log.d("texl",StacktotalX.getTotal().get(i).toString());
}
multiRenderer.addSeriesRenderer(ThirdRenderer);
multiRenderer.addSeriesRenderer(SecondRenderer);
multiRenderer.addSeriesRenderer(FirstRenderer);
Log.d("Error Here","34");
mChart = ChartFactory.getBarChartView(getBaseContext(), dataset, multiRenderer, Pattern);
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setBackgroundColor(Color.TRANSPARENT);
multiRenderer.setMarginsColor(Color.TRANSPARENT);
multiRenderer.setLabelsColor(Color.BLACK);
mChart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChart.getCurrentSeriesAndPoint();
if (seriesSelection != null) {
int seriesIndex = seriesSelection.getSeriesIndex();
String selectedSeries="Bilangan";
Log.d("seriesIndex",String.valueOf(seriesIndex));
// String yobject = [(int)seriesSelection.getXValue()-1];
// Getting the clicked Month
//String month = x[(int)seriesSelection.getXValue()-1];
// Getting the y value
int amount = (int) seriesSelection.getValue();
Log.d("amout",String.valueOf(amount));
Toast.makeText(
getBaseContext(),
selectedSeries + " in " + " : " + amount ,
Toast.LENGTH_SHORT).show();
}
}
});
// Start Activity*/
chartContainer.addView(mChart);
new Thread(new Runnable() {
public void run() {
long time = 0;
time = 9000/max;
Log.d("Error Here","35");
if(time>1.5){
time = (long) 1.5;
}if(time<0.5){
time = (long) 0.5;
}
int animation = 1;
Log.d("Error Here","36");
while(true){
boolean stop = true;
for(int k=0; k< StacktotalX.getTotal().size();k++){
// for(int k=0;k<jum1.size();k++)
// {
// JumlahSeries.add(k,jum1.get(tims));
// tims++;
// }
if(animation<= jum3.get(k)){
Log.d("Error Here","37");
FirstSeries.add(k,animation);
stop=false;
}
if(animation<= jum.get(k)){
Log.d("Error Here","38");
JumlahSeries1.add(k,animation);
stop=false;
}
if(animation<= jum1.get(k)){
Log.d("Error Here","39");
JumlahSeries.add(k,animation);
stop=false;
}
}
try {
Log.d("Error Here","40");
Thread.sleep(time);
Log.d("Error Here","4");
mChart.repaint();
}catch (InterruptedException e) {
e.printStackTrace();
}
animation++;
if(stop){
FirstSeries.clear();
JumlahSeries1.clear();
JumlahSeries.clear();
for(int y=0;y<StacktotalX.getTotal().size();y++)
{
FirstSeries.add(y,jum3.get(y));
JumlahSeries1.add(y, jum.get(y));
JumlahSeries.add(y, jum1.get(y));
}
FirstRenderer.setChartValuesSpacing(10);
FirstRenderer.setChartValuesTextAlign(Align.LEFT);
FirstRenderer.setDisplayChartValues(true);
FirstRenderer.getChartValuesTextAlign();
SecondRenderer.setChartValuesSpacing(10);
SecondRenderer.setChartValuesTextAlign(Align.CENTER);
SecondRenderer.setDisplayChartValues(true);
SecondRenderer.getChartValuesTextAlign();
ThirdRenderer.setChartValuesSpacing(10);
ThirdRenderer.setChartValuesTextAlign(Align.RIGHT);
ThirdRenderer.setDisplayChartValues(true);
ThirdRenderer.getChartValuesTextAlign();
mChart.repaint();
break;
}
}
}
}).start();*
This is my image that now is showing:-
i am drawing a bar chart and pie chart to represent the various application names in it using the achartengine library.
When i instantiate CategorySeries object it requires a series title as :
CategorySeries series = new CategorySeries("title") //IN BAR CHART
CategorySeries series = new CategorySeries("title") //IN PIE CHART
Unlike Pie chart the applications name does not get inflated at the bottom of the bar chart
say, in case of pie chart its as follows :
(1) application 1
(2) application 2
(3) application ... up to
(10) application 10.
But its not showing in case of bar char but instead its showing as :
(1) NUMBER OF APPLICATIONS ---->>> (This is the series title i had
passed in as the parameter while
instantiating the object of CategorySeries)
PLEASE SUGGEST ME TO SHOW THE KEY OF BAR CHART ASWELL,I HAVE ATTACHED A CODE SNIPPET BELOW
if (c.getCount() > 0) {
if (! mPieChart) {
series = new CategorySeries("NUMBER OF APPLICATIONS");
getActivity().startManagingCursor(c);
c.moveToPosition(-1);
//series.clear();
while (c.moveToNext()) {
recievedBytes = c.getLong(ApplicationDataCounterDao.CONTENT_RECEIVED_COLUMN);
sentBytes = c.getLong(ApplicationDataCounterDao.CONTENT_SENT_COLUMN);
totalBytes = (recievedBytes + sentBytes) / (1024 * 1024);
if (totalBytes > 0) {
applicationInfo
.add(new ApplicationDataUsage(
c.getString(ApplicationDataCounterDao.CONTENT_APPLICATION_NAME_COLUMN),
Double.toString(totalBytes)));
}
}
int size = applicationInfo.size();
for (int i = 0; i < size; ++i) {
Log.v("BAR CHART", "data added are " + applicationInfo.get(i).mApplicationName);
double value = Double.parseDouble(applicationInfo
.get(i).mDataUsage);
series.add("BAR", value);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series.toXYSeries());
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setDisplayChartValues(true);
renderer.setChartValuesSpacing((float) 0.5);
renderer.setColor(Color.CYAN);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setBarSpacing(0.5);
mRenderer.getBarSpacing();
mRenderer.setXTitle("Application Number");
mRenderer.setYTitle("Data Usage");
mRenderer.setAxesColor(Color.GREEN);
mRenderer.setZoomButtonsVisible(true);
mRenderer.setInScroll(true);
mRenderer.setLabelsColor(Color.RED);
mRenderer.addSeriesRenderer(renderer);
if (mChartViewBar == null) {
Log.v(LOG_TAG, "chart view is null");
mChartViewBar = ChartFactory
.getBarChartView(getActivity(), dataset,
mRenderer, Type.DEFAULT);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
mChartViewBar
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartViewBar
.getCurrentSeriesAndPoint();
CategorySeries series22 =new CategorySeries("333");
mChartViewBar.repaint();
if (seriesSelection == null) {
Toast.makeText(
getActivity(),
"No chart element was clicked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
getActivity(),
"APPLICATION NAME : "
+ series22.getTitle()
+ 1
+ " was clicked"
+ " DATA USAGE : "
+ seriesSelection
.getXValue(),
Toast.LENGTH_SHORT).show();
}
}
});
mChartLayout.removeAllViews();
mChartLayout.addView(mChartViewBar, new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
} else {
mChartLayout.removeAllViews();
mChartLayout.addView(mChartViewBar, new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mChartViewBar.repaint();
}
} else if (mPieChart) {
CategorySeries seriesPie = new CategorySeries("Pie Graph");
getActivity().startManagingCursor(c);
c.moveToPosition(-1);
while (c.moveToNext()) {
long recievedBytes = c .getLong(ApplicationDataCounterDao.CONTENT_RECEIVED_COLUMN);
Long sentBytes = c.getLong(ApplicationDataCounterDao.CONTENT_SENT_COLUMN);
totalBytes = (recievedBytes + sentBytes)
/ (1024 * 1024);
if (totalBytes > 0) {
applicationInfo
.add(new ApplicationDataUsage(
c.getString(ApplicationDataCounterDao.CONTENT_APPLICATION_NAME_COLUMN),
Double.toString(totalBytes)));
}
}
int size = applicationInfo.size();
for (ApplicationDataUsage data_value : applicationInfo) {
Log.v("PIE CHART", "data added are " + data_value.mApplicationName);
double dataUsage = Double
.parseDouble(data_value.mDataUsage);
seriesPie.add(data_value.mApplicationName, dataUsage);
}
for (int i = 0; i < size; ++i) {
prepareColors();
}
DefaultRenderer renderer = new DefaultRenderer();
for (int fillColor : colorList) {
// Log.d("1111111111",
// "FILL COLOUR TILL SIZE OF COLORLIST");
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(fillColor);
renderer.addSeriesRenderer(r);
}
renderer.setInScroll(true);
renderer.setChartTitle("APPLICATION WISE PIE CHART");
renderer.setChartTitleTextSize(20);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.BLACK);
renderer.setZoomButtonsVisible(true);
if (mChartViewPie == null) {
Log.v(LOG_TAG, "chat view is null");
// series.clear();
mChartViewPie = ChartFactory.getPieChartView(
getActivity(), seriesPie, renderer);
renderer.setClickEnabled(true);
renderer.setSelectableBuffer(10);
mChartViewPie
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartViewPie
.getCurrentSeriesAndPoint();
mChartViewPie.repaint();
if (seriesSelection == null) {
ToastHelper.showToastMessage(
"No item was Clicked",
getActivity());
} else {
ToastHelper.showToastMessage(
"Application number "
+ seriesSelection
.getPointIndex()
+ " was clicked and it's Data Usage is "
+ seriesSelection
.getValue(),
getActivity());
}
}
});
/*if (mChartViewBar != null) {
mChartLayout.removeView(mChartViewBar);
}
if (mChartViewPie != null) {
mChartLayout.removeView(mChartViewPie);
}*/
mChartLayout.addView(mChartViewPie, new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
} else {
/* if (mChartViewBar != null) {
mChartLayout.removeView(mChartViewBar);
}
if (mChartViewPie != null) {
mChartLayout.removeView(mChartViewPie);
}*/
mChartLayout.removeAllViews();
mChartLayout.addView(mChartViewPie, new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mChartViewPie.repaint();
}
}
You are confusing the legend with the X axis labels. The BarChart is an XYChart which means it is getting both legend and x axis labels. The PieChart is not an XYChart, so it is not getting X axis and Y axis.
The series titles go to the legend, as you mentioned.
In the X axis the series values indexes go by default. However, you can hide the default labels and add your own custom ones:
renderer.setXLabels(0);
renderer.addXTextLabel(1, "label);
renderer.addXTextLabel(2, "label);
...
Can u please help me in creating a simple bar chart (for android) where the values are got from sqlite database. for eg consider the following table
year Units Sold
2001 2000
2002 2100
2003 1900
I need to create a bar graph with x-axis as year and y-axis as units sold . Can u Plz consider this as a request . I dont know how to import data from database .
first you have to fetch the values from the data base and stored into array with corresponding data type . Do like this.....
List<int[]> initial;
int[] my_year;
int[] my_units_sold;
public void onCreate()
{
int Column1 = net_db_cur.getColumnIndex("Year");
int Column2 = net_db_cur.getColumnIndex("Units Sold");
cur = sampleDB.rawQuery("SELECT * FROM " + Table_Name , null);
my_year = new int[columncount];
my_units_sold = new int[columncount];
if (cur.moveToFirst())
{
for (int i = 0; i < columncount; i++)
{
my_year [i] = net_db_cur.getInt(Column1);
my_units_sold[i] = net_db_cur.getInt(Column2);
cur.moveToNext();
}
}
initial = new ArrayList<int[]>();
initial1.add(my_year);
initial1.add(my_units_sold);
ll.addView(createBarChart(initial ,Color.BLUE,10,0,14,2000000,20000000,1,x_axis_labels1,x_axis_labels2,y_axis_labels1, y_axis_net_labels2,Color.parseColor("#ffffff")));
}
public View createBarChart(int[] values,int[] colors,int lable_text_size,int xMin,int xMax,int yMin,int yMax,double bar_space,double[] x_axis_lables1,String[] x_axis_lables2,double[] y_axis_lables1,String[] y_axis_lables2, int lable_color)
{
XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
/*for (int i = 0; i <values.length; i++)
{*/
CategorySeries series = new CategorySeries("");
/*double[] v = values.get(i);
int seriesLength = v.length;*/
for (int k = 0; k < values.length; k++)
{
series.add(values[k]);
}
dataset.addSeries(series.toXYSeries());
//}
//mrenderer.setMargins(new int[] { 10, 65, 10, 15 });
int color_length = colors.length;
for (int i = 0; i < color_length; i++)
{
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
r.setChartValuesSpacing(15);
mrenderer.addSeriesRenderer(r);
}
mrenderer.setOrientation(Orientation.HORIZONTAL);
mrenderer.setInScroll(false);
mrenderer.setGridColor(Color.BLACK);
mrenderer.setYLabelsAlign(Align.RIGHT);
mrenderer.setPanEnabled(false, false);
mrenderer.setZoomEnabled(false, false);
mrenderer.setBarSpacing(bar_space);
mrenderer.setLabelsTextSize(lable_text_size);
mrenderer.setAxesColor(Color.BLACK);
mrenderer.setXAxisMin(xMin);
mrenderer.setXAxisMax(xMax);
for(int i=0;i<x_axis_lables2.length;i++)
{
mrenderer.addXTextLabel(x_axis_lables1[i],x_axis_lables2[i]);
}
mrenderer.setYAxisMin(yMin);
mrenderer.setYAxisMax(yMax);
for(int j=0;j<y_axis_lables1.length;j++)
{
mrenderer.addYTextLabel(y_axis_lables1[j],y_axis_lables2[j]);
}
mrenderer.setLabelsColor(lable_color);
return (ChartFactory.getBarChartView(Income.this, dataset, mrenderer, Type.DEFAULT));
}
You will get the bar chart for with data base values. All the best.
I'm using AChartEngine to display averages across several days (2 months if possible). I'd like to provide a comparison between one individual and a group. At this point, I'm working on how to provide dates across the x-axis (ie, 12/15/2011 or Dec. 11, 2011 or even 12/15, Dec. 15).
I'm not sure how to do this in AchartEngine; the documentation is a little hard for me to follow without pictures (sorry). I'd like something like this: Using AChartEngine library for graphs, not able to get value for diffrent x-axis value
Here's my code:
private void setupHistoryChart() {
XYMultipleSeriesRenderer renderer = getBarDemoRenderer();
setChartSettings(renderer);
Intent intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT);
startActivity(intent);
}
public XYMultipleSeriesRenderer getBarDemoRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(15);
renderer.setChartTitleTextSize(18);
renderer.setLabelsTextSize(12);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] {20, 30, 15, 0});
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(Color.BLUE);
renderer.addSeriesRenderer(r);
return renderer;
}
private void setChartSettings(XYMultipleSeriesRenderer renderer) {
renderer.setChartTitle("History");
renderer.setChartTitleTextSize((float) 14.0);
renderer.setXTitle("Day");
renderer.setXLabelsAlign(Align.LEFT);
renderer.setYLabelsAlign(Align.LEFT);
renderer.setBarSpacing(0.5);
renderer.setZoomEnabled(true);
renderer.setZoomRate(0.2f);
renderer.setYTitle("Unit)");
renderer.setXAxisMin(1.0);
if(AppStatus.mDayMap.size() == 0){
renderer.setXAxisMax(3.0);
renderer.setYAxisMax(10.0);
}else{
renderer.setXAxisMax(AppStatus.mDayMap.size());
// Get the max Integer out of myMap
Map.Entry<String, Integer> maxEntry = null;
for (Map.Entry<String, Integer> entry : AppStatus.mDayMap.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
}
}
renderer.setYAxisMax(maxEntry.getValue()/1000 + maxEntry.getValue()/1000);
}
renderer.setYAxisMin(0);
}
private XYMultipleSeriesDataset getBarDemoDataset() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
HashMap<String, Integer> myMap = AppStatus.mDayMap;
CategorySeries series = new CategorySeries ("Your History " + 1);
try{
if(AppStatus.mDayMap.size() == 0){
series.add(3.0);
series.add(10.0);
dataset.addSeries(series.toXYSeries());
}else{
Log.v(LOG_TAG, "Size of Map = " + myMap.size());
float sum = (float) 0.0;
if(myMap.size() > 0){
Iterator it = myMap.entrySet().iterator();
float j= (float)0.0;
for (int i = 0; i < 1 /*2*/; i++) {
//CategorySeries series = new CategorySeries("Your History " + (i + 1));
while(it.hasNext()){
Map.Entry pairs = (Map.Entry)it.next();
sum = sum + (float) (1.0 + j);
series.add((((Integer) pairs.getValue()).doubleValue())/1000);
}
dataset.addSeries(series.toXYSeries());
}
}
}
}catch(IllegalArgumentException e){
Log.e(LOG_TAG, "IllegalArgumentException " + e.toString());
series.add(3.0);
series.add(10.0);
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
You can set custom text for the X axis labels:
renderer.addXTextLabel(x, text);
renderer.setXLabels(0);
In order to have multiple color bars in the bar chart, you will have to add multiple series.
There are some instructions here on how to download and run the demo application which includes a lot of good code to demonstrate the AChartEngine usage.
I'm following an example in this link and
created a class as below
public class aChartExample {
public Intent execute(Context context) {
int[] colors = new int[] { Color.RED, Color.YELLOW, Color.BLUE };
DefaultRenderer renderer = buildCategoryRenderer(colors);
CategorySeries categorySeries = new CategorySeries("Vehicles Chart");
categorySeries.add("cars ", 30);
categorySeries.add("trucks", 20);
categorySeries.add("bikes ", 60);
return ChartFactory.getPieChartIntent(context, categorySeries, renderer, null);
}
protected DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}
}
and I'm calling it when my app starts on my starting activity in the onCreate.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
Intent achartIntent = new aChartExample().execute(this);
startActivity(achartIntent);
}
I then included the achartengine-0.6.0.jar in my project.
When I run the app it crashes on the startActivity line.
Not sure where to go from here.
Did you add this below line in AndroidManifest.xml. If so can you post the error message, I tried the same example 2 days ago and worked for me.
<activity android:name="org.achartengine.GraphicalActivity">
startActivity takes only activities. Not for all classes.
final CategorySeries series = new CategorySeries("pie");
for (int i = 0; i < Xaxes_value.size(); i++) {
series.add(Xaxes_value.get(i) + "",
Long.parseLong(yaxes_value.get(i)));
}
List<Integer> colors = new ArrayList<Integer>();
int[] colorr = { Color.parseColor("#56ADDB"),
Color.parseColor("#F59331"), Color.parseColor("#73C83C"),
Color.parseColor("#948D8B"), Color.parseColor("#E54273"),
Color.parseColor("#FFD34E"), Color.parseColor("#2A86D6"),
Color.parseColor("#00BFC0"), Color.parseColor("#95AC5F"),
Color.parseColor("#FF8497") };
for (int i = 0; i < yaxes_value.size(); i++) {
colors.add(colorr[i]);
}
renderer = new DefaultRenderer();
for (int color = 0; color < colors.size(); color++) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors.get(color));
r.setDisplayBoundingPoints(false);
// r.setDisplayChartValuesDistance(20);
r.setDisplayChartValues(false);
r.setChartValuesTextSize(18);
r.setStroke(BasicStroke.SOLID);
renderer.addSeriesRenderer(r);
}
// renderer.isInScroll();
renderer.setZoomEnabled(false);
renderer.setPanEnabled(false);
renderer.setZoomButtonsVisible(false); // set zoom button in Graph
renderer.setApplyBackgroundColor(false);
renderer.setBackgroundColor(Color.WHITE); // set background color
renderer.setChartTitle("Pie Chart");
renderer.setLabelsColor(Color.BLACK);
renderer.setMargins(new int[] { 20, 100, 20, 15 });
renderer.setLabelsTextSize(20);
renderer.setChartTitleTextSize((float) 30);
renderer.setShowLabels(false);
renderer.setLegendTextSize(25);
renderer.setDisplayValues(true);
renderer.setStartAngle(90);
GraphicalView mChart = ChartFactory.getPieChartView(getApplicationContext(), series,
renderer);
layout_graph.addView(mChart);