Repaint in achartengine with new values - android

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);

Related

Change height of image button through code in android Studio

I am working in Android studio and using TableLayout. i want to display small imageButtons. and height of each button can be changed programmatically. I tried my best. but does not change the height of imageButton, Please anyone help me, what changes can i make in this code. thanks
public void settable() {
index = 0;
LinearLayout le = (LinearLayout) findViewById(R.id.onofffbutton);
float height = le.getHeight();
height= height/9.0f;
for (int a = 0; a < maxrow; a++) {
TableRow row = new TableRow(this);
for (int b = 0; b < col; b++) {
location[a][b] = new ImageView(this);
/*LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
location[a][b].setLayoutParams(layoutParams);*/
if (isOn[a][b]) {
location[a][b].setBackgroundResource(R.drawable.box1);
} else {
location[a][b].setBackgroundResource(R.drawable.box);
}
location[a][b].setMaxHeight(height);
location[a][b].setOnClickListener(Onclick);
location[a][b].setId(index++);
row.addView(location[a][b]);
}
tableLayout.addView(row, a);
}
}

Refresh graph pie

I modified the sample code because I need to get a pie chart that is updated continuously. I managed to do some work but I can not figure out how to refresh the graph. This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
ButtonInit();
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);
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();
}
}
#Override
protected void onResume() {
super.onResume();
LinearLayout layout = (LinearLayout) findViewById(R.id.Graph);
mChartView = (GraphicalView) ChartFactory.getPieChartView(this, mSeries, mRenderer);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mChartView.repaint();
}
variables a, b​​, c, d are constantly changing because I get them via bluetooth.
Try doing this way
private GraphicalView mChart;
LinearLayout chart_container = (LinearLayout) findViewById(R.id.graphHolder);
.........
.......
mChart = (GraphicalView) ChartFactory.getPieChartView(this, dataset,
mRenderer);
chart_container.addView(mChart);
Now you can use
mChart.repaint();

How can I recreate piechart (AChartEngine) on spinner item click in Android?

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();

How to create bar-chart with different color for each bar?

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

AChartEngine add second series

I have a simple TimeChart in AChartEngine, everything works good but i have no idea how to add second series. Every my try was ended by ANR. Have you got any ideas how to add another series? This is my code:
EDIT: I changed code, i tried add second line but i have ANR:
public class TestgrafActivity extends Activity {
private XYMultipleSeriesDataset mDataset;
private XYMultipleSeriesRenderer mRenderer;
List<double[]> values = new ArrayList<double[]>();
private GraphicalView mChartView;
private TimeSeries time_series,time_series2;
private LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (LinearLayout) findViewById(R.id.chart);
time_series = new TimeSeries("Temperature");
for (int i = 0; i < 50; i++){
long value = new Date().getTime();
time_series.add(new Date(value+(i*1000)), new Random().nextInt(25));
}
time_series2 = new TimeSeries("Temperature2");
for (int i = 0; i < 50; i++){
long value = new Date().getTime();
time_series2.add(new Date(value+(i*1000)), new Random().nextInt(40));
}
mDataset = new XYMultipleSeriesDataset();
mDataset.addSeries(time_series);
mDataset.addSeries(time_series2);
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(Color.GREEN);
r.setPointStyle(PointStyle.CIRCLE);
r.setFillPoints(true);
r.setLineWidth(3);
r.setDisplayChartValues(true);
XYSeriesRenderer r2 = new XYSeriesRenderer();
r2.setColor(Color.RED);
r2.setPointStyle(PointStyle.CIRCLE);
r2.setFillPoints(true);
r2.setLineWidth(3);
r2.setDisplayChartValues(true);
mRenderer = new XYMultipleSeriesRenderer();
mRenderer.addSeriesRenderer(r);
mRenderer.addSeriesRenderer(r2);
mRenderer.setAxisTitleTextSize(16);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setPointSize(3f);
mRenderer.setPanEnabled(true, true);
mRenderer.setZoomEnabled(true, true);
mRenderer.addSeriesRenderer(r2);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(20);
mRenderer.setPanEnabled(true);
mRenderer.setChartTitle("Temperature");
mRenderer.setXTitle("Time");
mRenderer.setYTitle("Degrees C");
mRenderer.setZoomButtonsVisible(true);
mRenderer.setZoomEnabled(true);
mRenderer.setXLabels(12);
mRenderer.setYLabels(10);
mRenderer.setShowGrid(true);
mRenderer.setXLabelsAlign(Align.RIGHT);
mRenderer.setYLabelsAlign(Align.RIGHT);
mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer,
"HH:mm:ss");
layout.addView(mChartView);
}
}
You have only one Series in this code. Where is the code where you try to add another series?
The general approach is this:
mDataset.addSeries(0, time_series);
mRenderer.addSeriesRenderer(0, time_renderer);
//...
//...
mDataset.addSeries(1, some_other_series);
mRenderer.addSeriesRenderer(1, some_other_renderer);
//...
//...

Categories

Resources