问题是我无法根据屏幕拟合图表。图表的某些部分正在向右切割。我尝试了很多设置右侧的边距,但对我没有任何作用。正如您在附图中看到的最后一个栏被削减了一半。请任何人帮助我。
*layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >
<TableLayout
android:id="@+id/table1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:shrinkColumns="6"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4CC1D2"
android:padding="4dip" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="left"
android:layout_span="1"
android:onClick="gotoback"
android:background="#4CC1D2"
android:src="@drawable/backb" />
<TextView
android:id="@+id/currentMonth"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_span="5"
android:gravity="left"
android:text="How You want to Track"
android:textColor="#ffffff"
android:layout_marginLeft="-18dp"
android:textSize="21sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/table2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:shrinkColumns="9"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E6E6E6"
android:padding="5dip" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="3"
android:background="#FFFFFF"
android:gravity="center"
android:src="@drawable/lstweek" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_span="3"
android:background="#FFFFFF"
android:clickable="true"
android:onClick="getmonth"
android:src="@drawable/lstm" />
<ImageButton
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="10dp"
android:layout_span="3"
android:background="#4CC1D2"
android:gravity="center"
android:onClick="getmonth1"
android:src="@drawable/all" />
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/chart_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical" >
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/logs7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#4CC1D2"
android:onClick="alllogs"
android:text="View Logs"
android:textColor="#FFFFFF"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>*
Activity Java file.
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[] x = {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");
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<x.length;j++){
incomeSeries.add(x[j], showtarget[j]);
expenseSeries.add(x[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.CIRCLE);
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setChartValuesTextSize(20);
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(20);
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.setGridColor(Color.WHITE);
multiRenderer.setXTitle("");
multiRenderer.setYTitle("Calories");
multiRenderer.setZoomButtonsVisible(true);
multiRenderer.setBarSpacing(4);
multiRenderer.setPanEnabled(false, false);
multiRenderer.setLabelsTextSize(20);
multiRenderer.setAxesColor(Color.BLACK);
multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setYAxisMax(3000);
multiRenderer.setYAxisMin(0);
multiRenderer.setYLabelsColor(0, Color.BLACK);
double panLimits[]={20,20,20,20};
multiRenderer.setPanLimits(panLimits);
multiRenderer.setMarginsColor(Color.WHITE);
for(int i=0;i<mMonth.length;i++){
multiRenderer.addXTextLabel(i+1, mMonth[i]);
multiRenderer.setAxisTitleTextSize(20);
int mar[]={20,20,20,20};
multiRenderer.setMargins(mar);
multiRenderer.setGridColor(Color.LTGRAY);
}
// 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.setPanEnabled(false);
// Getting a reference to LinearLayout of the MainActivity Layout
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
chartContainer.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
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.setPanEnabled(false);
mChart.setMinimumWidth(100);
// Adding the Combined Chart to the LinearLayout
chartContainer.addView(mChart,layoutParams);
}});
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();
}
}
}
![enter image description here][2]