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);
...
Related
We are using AChart engine to draw bar graph. As per design requirement have to place an image on top of the tallest bar in the graph. We know the index of largest value in the XY series. Is there any method to find the coordinates of the tallest bar to position the image? Below is the helper class we are using to generate the bar graph. When user taps on the bar we have to place an image on top of the bar.
Issues :
Not able to get the click event correctly
How to position the image on top of the bar
public class BarGraphHelper {
private Context mContext;
private BarGraph mBarGraph;
private DisplayMetrics mMetrics;
private XYMultipleSeriesDataset mDataset;
private XYMultipleSeriesRenderer renderer;
private View barChartView = null;
private BarChart chart;
public BarGraphHelper(Context context, BarGraph barGraph){
mContext = context;
mBarGraph = barGraph;
init();
}
private void init() {
renderer = new XYMultipleSeriesRenderer();
mDataset = new XYMultipleSeriesDataset();
mMetrics = mContext.getResources().getDisplayMetrics();
}
public View getBarGraphView(){
if(renderer == null){
return null;
}
getBarGraphDataset();
renderer = getBarRenderer();
setBarGraphSettings();
chart = new BarChart(mDataset, renderer, Type.DEFAULT);
barChartView = new GraphicalView(mContext, chart);
barChartView.setOnClickListener(onBarClick);
return barChartView;
}
private void setBarGraphSettings() {
if(renderer != null){
renderer.setXAxisMin(0.5);
renderer.setYAxisMin(0);
renderer.setYAxisMax(110);
renderer.setXLabelsColor(Color.WHITE);
renderer.setYLabelsColor(0, Color.WHITE);
renderer.setXTitle("mins");
renderer.setYTitle("Tweets");
renderer.setShowAxes(true);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setBarSpacing(0.2);
renderer.setBarWidth(15);
renderer.setShowGrid(false);
renderer.setYLabels(0);
renderer.setXLabels(0); // sets the number of integer labels to appear
int margin[] = renderer.getMargins();
renderer.setMargins(new int[] { (int)convertDiptoPixel(margin[0]), (int)convertDiptoPixel(margin[1]), (int)convertDiptoPixel(margin[2]), (int)convertDiptoPixel(margin[3]) });
renderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
renderer.setLabelsColor(Color.WHITE);
renderer.setAxisTitleTextSize(convertDiptoPixel(renderer.getAxisTitleTextSize()));
renderer.setShowLegend(false);
renderer.setLabelsTextSize(convertDiptoPixel(renderer.getLabelsTextSize()));
renderer.setZoomButtonsVisible(false);
renderer.setPanEnabled(true, true);
renderer.setZoomEnabled(false, false);
renderer.setClickEnabled(true);
renderer.setSelectableBuffer(10);
}
}
private void getBarGraphDataset() {
if(mBarGraph != null){
CategorySeries positiveSeries = new CategorySeries("Positive");
ArrayList<Sentiment> sentimentList = mBarGraph.getSentimentList();
if(sentimentList != null && !sentimentList.isEmpty()){
Sentiment sentiment = null;
for(int i=0; i< sentimentList.size(); i++){
sentiment = sentimentList.get(i);
positiveSeries.add(sentiment.getTotalTweetCount());
}
mDataset.addSeries(positiveSeries.toXYSeries());
}
}
}
public XYMultipleSeriesRenderer getBarRenderer() {
XYSeriesRenderer seriesRenderer = new XYSeriesRenderer();
seriesRenderer.setColor(Color.parseColor("#5FB356"));
seriesRenderer.setDisplayChartValues(true);
renderer.addSeriesRenderer(seriesRenderer);
return renderer;
}
private float convertDiptoPixel(float pixel){
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, pixel, mMetrics);
}
OnClickListener onBarClick = new OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = ((GraphicalView) barChartView).getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(mContext, "No chart element was clicked", Toast.LENGTH_SHORT)
.show();
} else {
double[] screenPoint = chart.toScreenPoint(new double[] {
seriesSelection.getXValue(),seriesSelection.getValue() },0);
Toast toast = Toast.makeText(
mContext,
"Chart element in series index " + seriesSelection.getSeriesIndex()
+ " data point index " + seriesSelection.getPointIndex() + " was clicked"
+ " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue()
+ " clicked point value X=" + (int)screenPoint[0] + ", Y=" + (int)screenPoint[1], Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.LEFT, (int)screenPoint[0],(int)screenPoint[1]);
toast.show();
}
}
};
}
Hi i am using achartengine's pie-chart to represent my actual sales, now as my app starts one of the chart element in the piechart should get highlighted, like when we do the onclick on pie chart.
Below is the code for my piechart
final DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setPanEnabled(false);// Disable User Interaction
renderer.setLabelsColor(Color.BLACK);
renderer.setShowLegend(true);
renderer.setLegendTextSize(20);
renderer.setInScroll(true);
renderer.setStartAngle(180);
renderer.setChartTitle("Sales By Market Segment- Month");
renderer.setLabelsTextSize(22);
final CategorySeries categorySeries = new CategorySeries("Sales");
categorySeries.add("Craft1", cursor.getInt(1));
categorySeries.add("product1", cursor.getInt(2) );
categorySeries.add("product2", cursor.getInt(3) );
categorySeries.add("product", cursor.getInt(4));
categorySeries.add("product1", cursor.getInt(5));
mChartView2=ChartFactory.getPieChartView(context, categorySeries,renderer);
parent.addView(mChartView2);
renderer.setClickEnabled(true);
mChartView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView2.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(getActivity(), "No chart element selected", Toast.LENGTH_SHORT)
.show();
}else {
for (int i = 0; i < categorySeries.getItemCount(); i++) {
renderer.getSeriesRendererAt(i).setHighlighted(i == seriesSelection.getPointIndex());
}
mChartView2.repaint();
Toast.makeText(
getActivity(),
"Chart data point index " + seriesSelection.getPointIndex() + " selected"
+ " point value=" + seriesSelection.getValue(), Toast.LENGTH_SHORT).show();
}
}
});
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;
}
products are getting highlighted when clicking but I want to highlight one product, when app starts.
Found the solution:
mChartView2=ChartFactory.getPieChartView(context, categorySeries,renderer);
parent.addView(mChartView2);
renderer.getSeriesRendererAt(0).setHighlighted(true);
mChartView2.repaint();
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 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.
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.