I am new to AchartEngine. I am using AchartEngine with android to create a Barchart. I have looked at the aChartEngine API and have created a Barchart, its working fine.
When I want to see my actual view I have to decrease the zoom rate by clicking zoom button which is in right bottom. I need to show a compleate Barchart what I exactly declared in my program with out using zoom button.
I need to navigate from one view to another. So, I created one graphical view with ontouchlistner, but it shows error.
Any ideas would be greatly appreciated. Am I missing something here?
public class GraphicViewExample extends Activity {
private String[] mMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int[] x = { 0, 1, 2, 3, 4, 5, 6, 7 };
int[] income = { 2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 };
public static final String TYPE = "type";
private XYMultipleSeriesDataset mDataset = getDemoDataset();
private XYMultipleSeriesRenderer mRenderer = getDemoRenderer();
private GraphicalView mChartView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.graphicviewexample);
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getBarChartView(this, mDataset,
mRenderer, Type.DEFAULT);
mRenderer.setSelectableBuffer(100);
layout.addView(mChartView, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
mChartView.repaint();
}
mChartView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
#SuppressWarnings("unused")
/*SeriesSelection seriesSelection = mChartView
.getCurrentSeriesAndPoint();*/
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
mRenderer.removeAllRenderers();
r.setColor(Color.RED);
mChartView.repaint();
return true;
}
});
/*mChartView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
* LinearLayout slayout = (LinearLayout)
* findViewById(R.id.chart); ChartView =
* ChartFactory.getBarChartView( getApplicationContext(),
* sDataset, smRenderer, Type.DEFAULT);
* slayout.addView(ChartView, new LayoutParams(
* LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
*
* ChartView.repaint();
mChartView.repaint();
}
});*/
}
private XYMultipleSeriesDataset getDemoDataset() {
XYSeries incomeSeries = new XYSeries("Income");
for (int i = 0; i < x.length; i++) {
incomeSeries.add(i, income[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(incomeSeries);
return dataset;
}
private XYMultipleSeriesRenderer getDemoRenderer() {
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.rgb(130, 130, 230));
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setBarSpacing(0.3f);
multiRenderer.setBarWidth(30);
multiRenderer.setChartTitle("Income Chart");
multiRenderer.setXTitle("Year 2013");
multiRenderer.setYTitle("Amount in Dollars");
//multiRenderer.setZoomLimits(1.0,0.7,1.0,3000.0);
multiRenderer.setZoomButtonsVisible(true);
//multiRenderer.setZoomEnabled(true, true);
//multiRenderer.setPanEnabled(true, true);
multiRenderer.setInScroll(true);
multiRenderer.setXAxisMin(0);
multiRenderer.setXAxisMax(7);
multiRenderer.setYAxisMin(0);
multiRenderer.setYAxisMax(4000);
multiRenderer.setClickEnabled(true);
multiRenderer.setShowGridX(true);
for (int i = 0; i < x.length; i++) {
multiRenderer.addXTextLabel(i, mMonth[i]);
}
multiRenderer.addSeriesRenderer(incomeRenderer);
return multiRenderer;
}
}
Logs:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at org.achartengine.renderer.DefaultRenderer.getSeriesRendererAt(DefaultRenderer.java:189)
at org.achartengine.chart.XYChart.draw(XYChart.java:240)
at org.achartengine.GraphicalView.onDraw(GraphicalView.java:168)
This code works for my line graph,
v.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
SeriesSelection seriesSelection = ((GraphicalView) v)
.getCurrentSeriesAndPoint();
Log.d("sreedhu", String.valueOf(seriesSelection));
if (seriesSelection == null) {
Log.d("sreedhu", "Nothing Selected");
} else {
//your code}
}
}
use this codes to zoom your barchart...!
renderer.setScale((float) 1);
Related
I want to use graph in widget. Like This! I want to make graph using AchartEngine. Graph using AchartEngine is working in normal app but how to implement it in widget?
Here is My code:
public class MainActivity extends ActionBarActivity {
private XYMultipleSeriesDataset mDataset = getDemoDataset();
private XYMultipleSeriesRenderer mRenderer = getDemoRenderer();
private GraphicalView mChartView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRendererStyling();
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getLineChartView(MainActivity.this,
mDataset, mRenderer);
mRenderer.setSelectableBuffer(100);
layout.addView(mChartView, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
} else {
mChartView.repaint();
}
}
private void setRendererStyling() {
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
mRenderer.setAxisTitleTextSize(16);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setPointSize(8);
}
private XYMultipleSeriesRenderer getDemoRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setMargins(new int[] { 20, 30, 15, 0 });
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(Color.BLUE);
r.setPointStyle(PointStyle.SQUARE);
// r.setFillBelowLine(true);
// r.setFillBelowLineColor(Color.WHITE);
r.setFillPoints(true);
renderer.addSeriesRenderer(r);
r = new XYSeriesRenderer();
r.setPointStyle(PointStyle.CIRCLE);
r.setColor(Color.GREEN);
r.setFillPoints(true);
renderer.addSeriesRenderer(r);
renderer.setAxesColor(Color.DKGRAY);
renderer.setLabelsColor(Color.LTGRAY);
return renderer;
}
private XYMultipleSeriesDataset getDemoDataset() {
double[] seriesFirstY = { 20, -20, 63, 150, -49, 24, 91, -34, -8 };
double[] seriesSecondY = { 10, 70, -40, -20, 130, 24, 167, -34, 80 };
double[] seriesFirstX = { 0, 8, 14, 20, 35, 40, 56, 80, 109 };
double[] seriesSecondX = { 0, 24, 67, 75, 95, 134, 149, 168, 180 };
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
XYSeries firstSeries = new XYSeries("series One");
for (int i = 0; i < 9; i++)
firstSeries.add(seriesFirstX[i], seriesFirstY[i]);
dataset.addSeries(firstSeries);
XYSeries secondSeries = new XYSeries("series Two");
for (int j = 0; j < 9; j++)
secondSeries.add(seriesSecondX[j], seriesSecondY[j]);
dataset.addSeries(secondSeries);
return dataset;
}
I want to use Above code in widget app. Please help me.!!
public class MainActivity extends AppWidgetProvider {
private XYMultipleSeriesDataset mDataset = getDemoDataset();
private XYMultipleSeriesRenderer mRenderer = getDemoRenderer();
private GraphicalView mChartView;
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int currentWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
setRendererStyling();
if (mChartView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
View rowView = inflater.inflate(R.layout.second, null, true);
LinearLayout layout = (LinearLayout) rowView
.findViewById(R.id.chart);
mChartView = ChartFactory.getLineChartView(context, mDataset,
mRenderer);
mRenderer.setSelectableBuffer(100);
layout.addView(mChartView, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// views.apply(context, layout);
views.setImageViewBitmap(R.id.img_view,
layout.getDrawingCache());
appWidgetManager.updateAppWidget(currentWidgetId, views);
Toast.makeText(context, "widget added", Toast.LENGTH_SHORT)
.show();
} else {
mChartView.repaint();
}
}
}
}
I tried this code, but its does not work... Please help me.
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 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();
I m trying to set doughnut chart inner circle radius and outter circle radius in aChartengine, below is my code :
public class MainActivity extends Activity {
GraphicalView gv;
RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<double[]> values1 = new ArrayList<double[]>();
values1.add(new double[] { 15, 5 });
gv = createIntent(values1);
rl = (RelativeLayout) findViewById(R.id.rel);
rl.addView(gv);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public GraphicalView createIntent(List<double[]> values1) {
List<String[]> titles = new ArrayList<String[]>();
titles.add(new String[] { " ", " " });
int[] colors = new int[] { Color.BLUE, Color.GREEN };
DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setApplyBackgroundColor(true);
renderer.setShowLegend(false);
renderer.setShowLabels(false);
renderer.setStartAngle(270);
renderer.setBackgroundColor(Color.rgb(222, 222, 200));
renderer.setLabelsColor(Color.GRAY);
return ChartFactory.getDoughnutChartView(MainActivity.this,
buildMultipleCategoryDataset("Project budget", titles, values1),
renderer);
}
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;
}
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;
}
But I have browser this query of setting radius , in many search I have found radius on canvas but in my case I don't have canvas.
Can anyone tell me how to set radius here in my code ?
renderer.setScale((flaot)1.3) solved my problem but the inner and outer radius is approx not prefect.
You have to just chage value of decCoef (variable in DoughnutChart class).
In DefaultRenderer add your own radius variable and create getter and setter.
now, use this variable in DoughnutChart.
for example ,
DefaultRenderer defaultRenderer = new DefaultRenderer();
defaultRenderer.setmWidth(0.1f);
In DoughnutChart class :
public class DoughnutChart extends RoundChart {
private float mWidth; // make your own variable and initialize in constructor
public DoughnutChart(MultipleCategorySeries dataset, DefaultRenderer renderer) {
super(null, renderer);
mDataset = dataset;
mWidth = renderer.getmWidth();
}
double decCoef = mWidth / cLength; // change value of decCoef by using our variable.
I want to display graph using Achartengine in Fragment.
In my layout file I have used empty linear layout to add graph returned by createIntent() method
but it only shows blank screen
I am using following code
My Fragment activity is as below
pls help
public class Analytics_Screen extends Fragment implements OnClickListener,SlideActivity.Callbacks{
View checkanalytics_view;
SlideActivity activity;
LinearLayout graph_container;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
activity = (SlideActivity) getActivity();
metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
if (checkanalytics_view == null)
{
checkanalytics_view = inflater.inflate(R.layout.checkanalytics_screen, null, false);
}
activity.OpenLeftmenu(0);
SlideActivity.registerCallback(this);
graph_container = (LinearLayout)checkanalytics_view.findViewById(R.id.graph_container);
final GraphicalView gv =createIntent();
graph_container.addView(gv,new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
return checkanalytics_view;
}
public GraphicalView createIntent()
{
// List<double[]> values = new ArrayList<double[]>();
System.out.println("==============create Intent Start==========================");
List<Double> dValues=new ArrayList<Double>();
dValues.add((double) 5000);
dValues.add((double) 6000);
dValues.add((double) 7000);
dValues.add((double) 8000);
dValues.add((double) 10000);
dValues.add((double) 5000);
dValues.add((double) 25000);
dValues.add((double) 50000);
dValues.add((double) 50000);
// values.add(new double[] { 5000, 7300, 9240,10230, 11300, 10040,14230, 12300, 14240});
int[] colors = new int[] { Color.parseColor("#77c4d3")};
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
renderer.setOrientation(Orientation.VERTICAL);
setChartSettings(renderer, "Analitics", "", "",0.1,
12.5, 0,50000, Color.BLACK, Color.BLACK);
renderer.setXLabels(1);
renderer.setYLabels(10);
renderer.addXTextLabel(1, "Male");
renderer.addXTextLabel(2, "Female");
renderer.addXTextLabel(3, "Age 0-20");
renderer.addXTextLabel(4, "Age 21-30");
renderer.addXTextLabel(5, "Age 31-40");
renderer.addXTextLabel(6, "Age 41-50");
renderer.addXTextLabel(7, "Age 50+");
renderer.addXTextLabel(8, "Penciled");
renderer.addXTextLabel(9, "Attended");
/* renderer.addXTextLabel(10, "Oct");
renderer.addXTextLabel(11, "Nov");
renderer.addXTextLabel(12, "Dec");*/
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
seriesRenderer.setDisplayChartValues(true);
}
final GraphicalView grfv = ChartFactory.getBarChartView(activity, buildBarDataset(dValues), renderer,Type.DEFAULT);
System.out.println("===============Graph View=================="+grfv);
return grfv;
}
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(10);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(10);
renderer.setLegendTextSize(5);
renderer.setBarSpacing(1);
renderer.setMarginsColor(Color.parseColor("#EEEDED"));
renderer.setXLabelsColor(Color.BLACK);
renderer.setYLabelsColor(0,Color.BLACK);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.parseColor("#FBFBFC"));
renderer.setInScroll(true);
int length = colors.length;
Log.d("Color Len",""+length);
for (int i = 0; i < length; i++) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
// r.setChartvalueAngle(-90);
r.setChartValuesSpacing(25);
renderer.addSeriesRenderer(r);
}
return renderer;
}
protected XYMultipleSeriesDataset buildBarDataset(List<Double> values) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
CategorySeries series = new CategorySeries("Amount");
// Double v = values.get(0);
int seriesLength = values.size();
for (int k = 0; k < seriesLength; k++) {
series.add(values.get(k));
}
dataset.addSeries(series.toXYSeries());
return dataset;
}
protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
int labelsColor) {
renderer.setChartTitle(title);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setMargins(new int[] {15,50,50,50 });
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
}
Use this instead
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors)
{
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(10);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(10);
renderer.setLegendTextSize(5);
renderer.setBarSpacing(1);
renderer.setMarginsColor(Color.parseColor("#EEEDED"));
renderer.setXLabelsColor(Color.BLACK);
renderer.setYLabelsColor(0, Color.BLACK);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.parseColor("#FBFBFC"));
renderer.setInScroll(true);
int length = colors.length;
Log.d("Color Len", "" + length);
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors[0]);
// r.setChartvalueAngle(-90);
// r.setChartValuesSpacing(25);
renderer.addSeriesRenderer(r);
return renderer;
}
I am not sure if my answer can solve your problem, but I was successful in displaying a graph in a fragment. This is what I did, following is my fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4DDBFF">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graphLayout"
android:background="#000000" />
</LinearLayout>
I created 2 classes (a)SideFragment.java (b) LineGraph.java
LineGraph.java generates the graph and the following method return the view in this class
public GraphicalView getView(Context context){
GraphicalView view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}
In SideFragment.java I have a GraphicalView variable which is initialised in onCreate() by calling LineGraph.getView() as follows
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity().getApplicationContext();
line = new LineGraph();
graphview = line.getView(context);
}
And I add this Graphview is attached to the fragment Layout in onStart()
public void onStart() {
super.onStart();
Log.i(TAG, "onStart");
LinearLayout fragmentLayout = (LinearLayout) view.findViewById(R.id.graphLayout);
fragmentLayout.addView(graphview);
}
Since I am generating the graph dynamically I keep repainting the GraphView on a background thread (AsyncTask) which I have initiated in onActivityCreated()
protected void onProgressUpdate(String... values) {
Log.i(TAG, "onProgressUpdate");
graphview.repaint();
super.onProgressUpdate(values);
}
I hope this could be of some help to you.