Am using AChartEngine library to displaying data's in PieChart view in my project. Initially I created Dataset, Render, View and Chart Factory to create PieChart and its working fine. For eg PieChart for Gender
OnCreate(){
malecount = 3;
femalecount = 1;
createpiechart(); // Creating PieChart initially
}
This code is working fine. PieChart with male & Female with 2 different colors that mentioned as static.
Now I add Spinner for filters, add event for that Spinner OnItemSelectedListener.
String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Last Week"))
{
malecount = 2; femalecount = 1; // clear existing values from temp memory and add new values
mSeries.clear(); // clear the dataset
createpiechart(); // recreate piechart
}
in that create piechart method again execute the same code as before.
private void createpiechart() {
VALUES = new int[] { malecount, femalecount };
mRenderer.setChartTitle("Gender");
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsColor(0xff000000); // Black color
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setStartAngle(90);
for (int i = 0; i < VALUES.length; i++) {
mSeries.add(NAME_LIST[i] + " " + VALUES[i], VALUES[i]);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[(mSeries.getItemCount() - 1)
% COLORS.length]);
mRenderer.addSeriesRenderer(renderer);
}
if (mChartView != null) {
mChartView.repaint();
}
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
mChartView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// nothing
}
});
layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
else {
mChartView.repaint();
}
}
After executing the same code Green and Blue color of Male & Female in Bottom changes to white color, I need same color before it shown. Check this image below and help me how to change that color back or whats wrong with this code ?
Just clear Dataset and Renderer and then pass new values.
To clear dataset
int size = dataset.getSeriesCount();
for (int i = 0; i < size; i++) {
dataset.removeSeries(0);
}
To clear Renderer renderer.removeAllRenderers();
Related
In my android program I am using achartengine library for showing a pie chart.
The basic of the program is I have an activity,where I have 2 edit text fields and a button.after fill up the edit text field ,when click the button it will show a popup window with piechart..
here,I am using NAME_LIST as a string type array. where value of edit text will store and after we can fetch..
the code is given below:---
public class AndroidPopupWindowActivity111_new extends Activity {
private static int[] COLORS = new int[] { Color.MAGENTA, Color.CYAN };
LinearLayout layout;
private CategorySeries mSeries = new CategorySeries("");
private DefaultRenderer mRenderer = new DefaultRenderer();
private GraphicalView mChartView;
EditText name1,name2;
private int[] VALUES = { 40,60 };
String x1,y1;
String[] NAME_LIST ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_main);
name1=(EditText) findViewById(R.id.ext1);
name2=(EditText) findViewById(R.id.ext2);
x1=name1.getText().toString();
y1=name2.getText().toString();
NAME_LIST = new String[] { x1 , y1 };
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setStartAngle(90);
final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext() .getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.main_piechart, null);
final PopupWindow popupWindow = new PopupWindow( popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layout = (LinearLayout)popupView.findViewById(R.id.chart);
layout.addView(mChartView);
for (int i = 0; i < VALUES.length; i++) {
mSeries.add(NAME_LIST[i] + "(" + VALUES[i]+"%)", VALUES[i]);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);
mRenderer.addSeriesRenderer(renderer);
}
if (mChartView != null) {
mChartView.repaint();
}
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
}});
}
}
But the problem is the name is not fetching from edittext..I am giving an example what error is occuring.:--
Click the picture to know the problem...in this picture A,B,C,D is the edit text values..in my output A,B,C,D is showing as "null"
where is the problem??????????thanks in advance
When you're initializing
String[] NAME_LIST = new String[] { x1,y1 };
x1 and y1 are null. Changing the values of x1 and y1 later will not change the values in NAME_LIST.
Add the line
NAME_LIST = new String[] { x1,y1 };
just before the for loop in OnCreate(). It will work.
EDIT:
Move these five lines
name1=(EditText) findViewById(R.id.ext1);
name2=(EditText) findViewById(R.id.ext2);
x1=name1.getText().toString();
y1=name2.getText().toString();
NAME_LIST = new String[] { x1 , y1 };
into the onClick() method, just before the for loop begins.
I am trying to use ACHartEngine in my Android Application. Everything works fine when start the app with API 14 but as soon as i use the LineChart or BarChart below API 14 it doesn`t show any Graph.
Maybe someone has experienced the same problem?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setupView_1 = inflater.inflate(R.layout.layout_setup1, container, false);
startPowerGraph();
return setupView_1;
}
private void startPowerGraph() {
setupChart();
//Checking if i am connected via Bluetooth
if (BT.isConnected()) {
// Start plotting chart
new ChartTask().execute();
}
}
public void setupChart() {
// Creating an XYSeries for Visits
visitsSeries = new XYSeries("Charging Power");
// Creating a dataset to hold each series
dataset = new XYMultipleSeriesDataset();
// Adding Visits Series to the dataset
dataset.addSeries(visitsSeries);
// Creating XYSeriesRenderer to customize visitsSeries
visitsRenderer = new XYSeriesRenderer();
visitsRenderer.setColor(Color.BLUE);
visitsRenderer.setPointStyle(PointStyle.CIRCLE);
visitsRenderer.setFillPoints(true);
visitsRenderer.setLineWidth(2);
visitsRenderer.setDisplayChartValues(false);
// visitsRenderer.setFillBelowLine(true);// needed for area chart??
// Creating a XYMultipleSeriesRenderer to customize the whole chart
multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setInScroll(true);// to fit in ScrollVIew
multiRenderer.setChartTitle(setupView_1.getResources().getString(R.string.chartTitle));
multiRenderer.setXTitle(setupView_1.getResources().getString(R.string.chartXAchse));
multiRenderer.setYTitle(setupView_1.getResources().getString(R.string.chartYAchse));
multiRenderer.setZoomButtonsVisible(false);
multiRenderer.setMargins(new int[] { 20, 30, 15, 0 });
// multiRenderer.setXAxisMin(0);
// multiRenderer.setXAxisMax(100);
//
// multiRenderer.setYAxisMin(0);
// multiRenderer.setYAxisMax(10);
multiRenderer.setBarSpacing(2);
// Adding visitsRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to
// multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(visitsRenderer);
// Getting a reference to LinearLayout of the SetUpCharging_Page1 Layout
LinearLayout chartContainer = (LinearLayout) setupView_1.findViewById(R.id.Power_LinearLayout_Graph);
mChart = (GraphicalView) ChartFactory.getLineChartView(mFragmentActivity, dataset, multiRenderer);
// mChart = (GraphicalView) ChartFactory.getBarChartView(
// mFragmentActivity, dataset, multiRenderer, Type.STACKED);
// Adding the Line Chart to the LinearLayout
chartContainer.addView(mChart);
}
public class ChartTask extends AsyncTask<Void, String, Void> {
// Generates dummy data in a non-ui thread
#Override
protected Void doInBackground(Void... params) {
int i = 0;
int pulse = 100;
try {
do {
String[] values = new String[2];
// get Actual chargepower
int visits = getPowerKW();
if (getPowerKW() > multiRenderer.getYAxisMax()) {
multiRenderer.setYAxisMax(getPowerKW() + 2);
}
if (getPowerKW() < multiRenderer.getYAxisMax()) {
multiRenderer.setYAxisMin(getPowerKW());
}
values[0] = Integer.toString(i);
values[1] = Integer.toString(visits);
double maxX = visitsSeries.getMaxX();
double minX = maxX - i; // deltaX is your required
// x-range
double maxY = visitsSeries.getMinY();
double minY = visitsSeries.getMaxY();
multiRenderer.setRange(new double[] { -minX, maxX, -maxY, minY });
if (BT.isConnected()) {
publishProgress(values);
Thread.sleep(pulse);
i++;
} else {
Toast.makeText(mFragmentActivity, "Chart is waiting for Bluetooth Connection", Toast.LENGTH_SHORT).show();
}
} while (pause);
} catch (Exception e) {
}
return null;
}
// Plotting generated data in the graph
#Override
protected void onProgressUpdate(String... values) {
visitsSeries.add(Integer.parseInt(values[0]), Integer.parseInt(values[1]));
mChart.repaint();
}
}
Okay new Update!
Its not about the ScrollView which doesnt work with the Chart. For API > 10 u can edit
android:fillViewport="true"
in your .xml File or do it in your java code
DialRenderer dialRenderer;
dialRenderer.setInScroll(true);
But for API 10 this doesn`t work in my case. So
i fixed it by using following code:
mChart.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 250));
Note that i have used 250 or any other fix int value. If i use
mChart.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
it won`t work again .
Maybe somebody could explain me this weird problem :P
I'm trying to use a seekbar that when you move it, a new graph will be shown in achartengine, but the problem is that paints over the old graphic, instead of repaint the chartview with new values. I've tried everything I've read in other questions but nothing has worked for me. Can anyone help me? Thanks. This is my code:
int length2 = titles.length;
XYSeries series = new XYSeries(titles[0], 0);
double[] xV = x.get(0);
double[] yV = values.get(0);
int seriesLength = xV.length;
for (int k = 0; k < seriesLength; k++) {
series.add(xV[k], yV[k]);
}
mDataset.addSeries(series);
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
if (mChartView == null) {
mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
layout.addView(mChartView);
} else {
layout.removeAllViews();
layout.addView(mChartView);
}
This is the kind of repaint issue that you could fix using:
renderer.setInScroll(true);
I am using aChartEngine Library to draw donut in my android application.I want to make two donut - one inside another with two different color. But I am unable to make them with two different color.This is my donut class which draw donut in LinearLayout.
public class PunchStatGraph {
private GraphicalView mChartView2;
static int count = 2;
int[] Mycolors = new int[] { Color.parseColor("#D4272D"),
Color.parseColor("#6F1717") };
String[] average = { "SPEED", "FORCE"};
String[] max = { "SPEED", "FORCE"};
public Intent execute(Context context, LinearLayout parent, double avg_values[],double max_values[]) {
parent.removeAllViews();
int[] colors = new int[count];
for (int i = 0; i < count; i++) {
colors[i] = Mycolors[i];
}
DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setShowLabels(false);
renderer.setBackgroundColor(Color.BLACK);
renderer.setPanEnabled(false);// Disable User Interaction
renderer.setScale((float) 1.4);
renderer.setInScroll(true); //To avoid scroll Shrink
renderer.setStartAngle(90);
renderer.setShowLegend(false);
MultipleCategorySeries categorySeries = new MultipleCategorySeries(
"Punch Graph");
categorySeries.add(average, avg_values);
categorySeries.add(max, max_values);
mChartView2 = ChartFactory.getDoughnutChartView(context,
categorySeries, renderer);
parent.addView(mChartView2);
return ChartFactory.getDoughnutChartIntent(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;
}
}
Check Image also.Thanks in advance.
We are creating bar chart for file manager application, we have to display different color for each bar.
Written following code globally.
GraphicalView chartView = null;
private CategorySeries series = new CategorySeries("Space");
private XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> sizes = new ArrayList<String>();
Written following code to display different colors.
public void getBarChart(){
for(int i = 0;i < name.length; i++){
series.add(name[i],values[i]);
multiRenderer.addXTextLabel(i, name[i]);
}
dataset.addSeries(series.toXYSeries());
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.rgb(0, 255, 255));
renderer.setFillPoints(true);
renderer.setLineWidth(2);
renderer.setDisplayChartValues(true);
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Chart Used Space");
multiRenderer.setXTitle("Name of Directory");
multiRenderer.setAxisTitleTextSize((float)18);
multiRenderer.setChartTitleTextSize((float)20);
multiRenderer.setYTitle("Amount of Space in MB");
multiRenderer.setXLabelsAngle((float)35);
multiRenderer.setLabelsTextSize((float)15);
multiRenderer.setXLabelsAlign(Align.LEFT);
multiRenderer.setZoomButtonsVisible(true);
multiRenderer.setBarSpacing((float) 1.0);
multiRenderer.addSeriesRenderer(renderer);
chartView = ChartFactory.getBarChartView(getBaseContext(), dataset, multiRenderer, org.achartengine.chart.BarChart.Type.DEFAULT);
}
The bar color in a bar chart is given by its renderer. Each series has a renderer. In order to have a separate color for each bar you will have to add several series, each one having only one bar.
Hi Have a look at this code.
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
LinearLayout linearChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearChart = (LinearLayout) findViewById(R.id.linearChart);
int colerloop[] = { 1, 2, 2, 2, 3, 3, 3, 3, 1, 1 };
int heightLoop[] = { 300, 200, 200, 200, 100, 100, 100, 100, 300, 300 };
for (int j = 0; j < colerloop.length; j++) {
drawChart(1, colerloop[j], heightLoop[j]);
}
}
public void drawChart(int count, int color, int height) {
System.out.println(count + color + height);
if (color == 3) {
color = Color.RED;
} else if (color == 1) {
color = Color.BLUE;
} else if (color == 2) {
color = Color.GREEN;
}
for (int k = 1; k <= count; k++) {
View view = new View(this);
view.setBackgroundColor(color);
view.setLayoutParams(new LinearLayout.LayoutParams(25, height));
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view
.getLayoutParams();
params.setMargins(3, 0, 0, 0); // substitute parameters for left,
// top, right, bottom
view.setLayoutParams(params);
linearChart.addView(view);
}
}
}
For more details please visit the below link.
http://www.androidhub4you.com/2013/05/custom-bar-chart-in-android-dynamic.html
If you follow above link your bar chart will be look like below image.
http://4.bp.blogspot.com/-V7SwZZIRPo8/UaH3pR99oSI/AAAAAAAAAhY/N8etB_mAzec/s1600/device-2013-05-26-171036.png