In The Above Screen Shot You Can See the First Bar and its corresponding value is cutting from left.I want To put some Margin from left so the first bar will fit in the screen along with its value. I have tried a lot but not able to do that.Please someone help me if you can. thnks.
Following are the set of code Which i have written to achieve this.
CalorieConsumedTrackerActivity.java
public class CalorieConsumedTrackerActivity extends Activity {
private GraphicalView mChart;
JSONArray dailydata = null;
private ProgressDialog progressDialog;
private static String url_display_user ;
JSONParser jsonParser = new JSONParser();
private static final String TAG_SUCCESS = "success";
private static final String TAG_RESPONSE = "getdata";
JSONArray graphdatas = null;
ArrayList<HashMap<String, String>> getdata;
private static final String TAG_MESSAGE = "message";
String userid=null;
String[] mMonth = new String[7] ;
String[] datefromdb = new String[7] ;
int[] line = {1,2,3,4,5,6,7};
int[] target = new int[7];
int[] consumed = new int[7];
int[]showtarget=new int[7];
int[]showconsumed=new int[7];
XYSeries incomeSeries = new XYSeries("Target");
// Creating an XYSeries for Expense
XYSeries expenseSeries = new XYSeries("Consumed");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calories_consumed );
getdata=new ArrayList<HashMap<String,String>>();
DatabaseHandler db=new DatabaseHandler(getApplicationContext());
HashMap<String,String> user = new HashMap<String,String>();
user=db.getUserDetails();
userid=user.get("userid");
TextView lstweek=(TextView) findViewById(R.id.lstweek);
lstweek.setBackgroundColor(Color.parseColor("#FF8000"));
Calendar myCalendar = Calendar.getInstance();
myCalendar.set(myCalendar.getTime().getYear()+1900,myCalendar.getTime().getMonth() , myCalendar.getTime().getDate());
for (int i = 6; i >=0; i--) {
myCalendar.add(Calendar.DAY_OF_YEAR, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(myCalendar.getTime());
System.out.println(date);
mMonth[i]=date;
}
new GetGraphData().execute();
}
private void openChart(){
}
class GetGraphData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(CalorieConsumedTrackerActivity.this, "wait", "Fetching...");
}
@Override
protected String doInBackground(String... params) {
url_display_user = "http://www.bharatwellness.com/getGraphData.php";
// Building Parameters
List<NameValuePair> parametres = new ArrayList<NameValuePair>();
parametres.add(new BasicNameValuePair("userid", userid));
// getting JSON string from URL
JSONObject json = jsonParser.makeHttpRequest(url_display_user,
"GET", parametres);
// Check your log cat for JSON reponse
Log.d("All data : ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
JSONObject c=null;
if (success == 1) {
int i;
int cal=0;
// products found
// Getting Array of Products
graphdatas = json.getJSONArray(TAG_RESPONSE);
// looping through All RESPONSE
for (i = 0; i < graphdatas.length(); i++) {
c = graphdatas.getJSONObject(i);
// Storing each json item in variable
String createddate = c.getString("createddate");
datefromdb[i]=createddate;
String caloriegoal = c.getString("caloriegoal");
String calorieconsumed = c.getString("calorieconsumed");
double cgoal=Double.parseDouble(caloriegoal);
cal=(int) Math.round(cgoal);
target[i]=cal;
double csumed=Double.parseDouble(calorieconsumed);
int calc=(int) Math.round(csumed);
consumed[i]=calc;
}
for(int x=0;x<mMonth.length;x++){
for(int y=0;y<datefromdb.length;y++){
if(mMonth[x].equals(datefromdb[y])){
showconsumed[x]=consumed[y];
showtarget[x]=target[y];
}
if(showtarget[x]==0){
showtarget[x]=cal;
}
}
}
// Creating an XYSeries for Income
// Adding data to Income and Expense Series
for(int j=0;j<line.length;j++){
incomeSeries.add(line[j], showtarget[j]);
expenseSeries.add(line[j],showconsumed[j]);
}
runOnUiThread(new Runnable() {
public void run() {
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Adding Expense Series to dataset
dataset.addSeries(expenseSeries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.parseColor("#04B431"));
incomeRenderer.setPointStyle(PointStyle.SQUARE);
incomeRenderer.setPointStrokeWidth(20);
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setChartValuesTextSize(27);
incomeRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
expenseRenderer.setColor(Color.parseColor("#FF8000"));
expenseRenderer.setPointStyle(PointStyle.CIRCLE);
expenseRenderer.setFillPoints(true);
expenseRenderer.setLineWidth(2);
expenseRenderer.setChartValuesTextSize(27);
expenseRenderer.setDisplayChartValues(true);
for(int z=0;z<mMonth.length;z++){
String createddate=mMonth[z];
String month=createddate.substring(5, 7);
System.out.println(month);
int m=Integer.parseInt(month);
System.out.println(m);
switch (m) {
case 1:
month="Jan";
break;
case 2:
month="Feb";
break;
case 3:
month="Mar";
break;
case 4:
month="Apr";
break;
case 5:
month="May";
break;
case 6:
month="June";
break;
case 7:
month="July";
break;
case 8:
month="Aug";
break;
case 9:
month="Sep";
break;
case 10:
month="Oct";
break;
case 11:
month="Nov";
break;
case 12:
month="Dec";
break;
}
String day=createddate.substring(8, 10);
String finaldate= day+" "+month;
mMonth[z]=finaldate ;
}
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setAxisTitleTextSize(30);
multiRenderer.setBackgroundColor(Color.WHITE);
multiRenderer.setAxesColor(Color.WHITE);
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Calories Consumed Progress Chart");
multiRenderer.setChartTitleTextSize(25);
multiRenderer.setGridColor(Color.LTGRAY);
multiRenderer.setXTitle("");
multiRenderer.setYTitle("Calories");
// multiRenderer.setZoomButtonsVisible(true);
multiRenderer.setBarSpacing(1);
multiRenderer.setPanEnabled(false, false);
multiRenderer.setLabelsTextSize(24);
multiRenderer.setAxesColor(Color.BLACK);
multiRenderer.setXRoundedLabels(true);
multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setYAxisMax(3000);
multiRenderer.setYAxisMin(0);
multiRenderer.setYLabelsColor(0, Color.BLACK);
multiRenderer.setMarginsColor(Color.WHITE);
multiRenderer.setMargins(new int[]{ 30, 30,20, 0 });
multiRenderer.setAxisTitleTextSize(25);
multiRenderer.setGridColor(Color.LTGRAY);
for(int i=0;i<mMonth.length;i++){
multiRenderer.addXTextLabel(i+1, mMonth[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
multiRenderer.addSeriesRenderer(expenseRenderer);
multiRenderer.setYLabels(6);
//multiRenderer.setPanEnabled(false);
// Getting a reference to LinearLayout of the MainActivity Layout
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
chartContainer.setBackgroundColor(Color.WHITE);
chartContainer.setScrollContainer(true);
LinearLayout con = (LinearLayout) findViewById(R.id.linear1);
con.setBackgroundColor(Color.WHITE);
// Specifying chart types to be drawn in the graph
// Number of data series and number of types should be same
// Order of data series and chart type will be same
String[] types = new String[] { LineChart.TYPE, BarChart.TYPE };
// Creating a combined chart with the chart types specified in types array
mChart = (GraphicalView) ChartFactory.getCombinedXYChartView(getBaseContext(), dataset, multiRenderer, types);
mChart.setBackgroundColor(Color.WHITE);
multiRenderer.setClickEnabled(true);
multiRenderer.setSelectableBuffer(10);
multiRenderer.setXLabelsAngle(30.0f);
multiRenderer.setYLabelsAngle(30.0f);
multiRenderer.setBarWidth(10);
multiRenderer.setXLabelsAlign(Align.CENTER);
multiRenderer.setXAxisMax(7.5);
multiRenderer.setXLabelsPadding(10);
multiRenderer.setFitLegend(false);
multiRenderer.setLegendTextSize(19);
multiRenderer.setOrientation(Orientation.HORIZONTAL);
// Adding the Combined Chart to the LinearLayout
chartContainer.addView(mChart);
}});
return json.getString(TAG_MESSAGE);
} else {
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String result) {
// dismiss the dialog after getting all products
progressDialog.dismiss();
// updating UI from Background Thread
//Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
}
}
}