我想在上滑面板上显示条形图。我是一个完全的乞丐。任何人都可以解释如何做到这一点。 代码是:
package com.digitaldesigner.adish.apptracker;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.couchbase.lite.Database;
import com.couchbase.lite.DatabaseOptions;
import com.couchbase.lite.Document;
import com.couchbase.lite.Emitter;
import com.couchbase.lite.Manager;
import com.couchbase.lite.Mapper;
import com.couchbase.lite.android.AndroidContext;
import com.couchbase.lite.internal.InterfaceAudience;
import com.couchbase.lite.replicator.Replication;
import com.digitaldesigner.adish.apptracker.lockscreen.UnlockCountService;
import com.digitaldesigner.adish.apptracker.usagetracker.appUsage.PagerAdapter;
import com.digitaldesigner.adish.apptracker.usagetracker.barchart.BarChartView;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements Replication.ChangeListener {
public static String TAG = "GrocerySync";
public static int flag=0;
public static int unlock=0;
public static final String DATABASE_NAME = "sync_gateway";
public static final String designDocName = "grocery-local";
public static final String byDateViewName = "byDate";
public static final String SYNC_URL = "http://172.16.143.150:4985/sync_gateway";
protected static Manager manager;
public static Database database;
SlidingUpPanelLayout slidingUpPanel, slidingUpPanel1;
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences preferences=getSharedPreferences("anyname", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.putInt("unlock",unlock);
editor.commit();
if(preferences.getBoolean("firstTime", false)){
editor.putLong("savedTime",System.currentTimeMillis());
editor.commit();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slidingUpPanel = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
slidingUpPanel1 = (SlidingUpPanelLayout) findViewById(R.id.main_content);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Detailed View"));
tabLayout.addTab(tabLayout.newTab().setText("Pie-Chart View"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
Intent myIntent = new Intent(this,UnlockCountService.class);
this.startService(myIntent);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
try {
startCBLite();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error Initializing CBLIte, see logs for details", Toast.LENGTH_LONG).show();
com.couchbase.lite.util.Log.e("TAG", "Error initializing CBLite", e);
}
}
protected void onDestroy() {
if(manager != null) {
manager.close();
}
super.onDestroy();
}
public void startCBLite() throws Exception {
manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
DatabaseOptions options = new DatabaseOptions();
options.setCreate(true);
database = manager.openDatabase(DATABASE_NAME, options);
com.couchbase.lite.View viewItemsByDate = database.getView(String.format("%s/%s", designDocName, byDateViewName));
viewItemsByDate.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
Object createdAt = document.get("created_at");
if (createdAt != null) {
emitter.emit(createdAt.toString(), null);
}
}
}, "1.0");
startSync();
}
public void startSync() {
URL syncUrl;
try {
syncUrl = new URL(SYNC_URL);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
Replication pullReplication = database.createPullReplication(syncUrl);
pullReplication.setContinuous(true);
Replication pushReplication = database.createPushReplication(syncUrl);
pushReplication.setContinuous(true);
pullReplication.start();
pushReplication.start();
pullReplication.addChangeListener(this);
pushReplication.addChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void changed(Replication.ChangeEvent event) {
}
}
条形图的代码:
package com.digitaldesigner.adish.apptracker.usagetracker.barchart;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Document;
import com.couchbase.lite.replicator.Replication;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
//import com.github.mikephil.charting.charts.BarChart;
import com.digitaldesigner.adish.apptracker.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static com.digitaldesigner.adish.apptracker.MainActivity.database;
import static com.digitaldesigner.adish.apptracker.MainActivity.flag;
public class BarChartView extends Fragment implements Replication.ChangeListener {
public static int i = 0;
public static String TAG = "GrocerySync";
public static ArrayList<String> ydata = new ArrayList<String>();
public static ArrayList<Float> xdata = new ArrayList<Float>();
public static ArrayList<String> myDocId = new ArrayList<String>();
public static ArrayList<Float> cpyf = new ArrayList<Float>();
public static PieChart pieChart;
@Override
public void onStop() {super.onStop();}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_bar_chart, container, false);
}
@Override
public void onPause() {super.onPause();}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
pieChart = (PieChart) rootView.findViewById(R.id.idPieChart);
pieChart.setRotationEnabled(true);
pieChart.setHoleRadius(55f);
pieChart.setEntryLabelColor(Color.BLACK);
pieChart.setTransparentCircleAlpha(0);
pieChart.setCenterText("APP USAGE");
pieChart.setCenterTextSize(20);
}
public static void addDataSet() {
float others = 0; int flag2=0;
ArrayList<PieEntry> xEntrys = new ArrayList<>();
ArrayList<String> yEntrys = new ArrayList<>();
for (int i = 0; i < xdata.size(); i++) {
if (xdata.get(i) < 5.0) {
others += xdata.get(i);
xdata.remove(i);
ydata.remove(i);
}
}
Log.e("test", "size" + xdata.size());
for (int i = 0; i < ydata.size(); i++) {
String value;
value = ydata.get(i);
for (int j = i + 1; j < ydata.size(); j++) {
if (value.equals(ydata.get(j))) {
ydata.remove(j);
xdata.remove(j);
}
}
}
for (int i = 0; i < xdata.size(); i++) {
Log.e("test", "percentage" + xdata.get(i));
Log.e("test", "application name" + ydata.get(i));
String str = ydata.get(i);
float x = xdata.get(i);
if(flag==0) {
myDocId.add(createDocument(xdata.get(i), ydata.get(i)));
flag++;
}
else
{
Log.e("updation","updation");
for(int j=0;j<myDocId.size();j++) {
Document doc = database.getDocument(myDocId.get(j));
// We can directly access properties from the document object:
String percent=(String)doc.getProperty("percent");
String owner = (String) doc.getProperty("Applicationn name");
Log.e("String","String obtained"+owner);
if(owner.equals(ydata.get(i))){
flag2=1;
x=x+100;
String xs=x+"%";
Log.e("for checking percent 1"+xs,"percent 2"+percent);
if(!(percent.equals(x+"%")))
{
Document doc1 = database.getDocument(myDocId.get(j));
Map<String, Object> properties1 = new HashMap<String, Object>();
properties1.putAll(doc1.getProperties());
properties1.put("percent", x + "%");
properties1.put("Applicationn name", str);
try {
doc.putProperties(properties1);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
}
else{
continue;
}
}
}
if(flag2==0){
Log.e("not matching excitsing","adding"+ydata.get(i));
myDocId.add(createDocument(xdata.get(i), ydata.get(i)));
}
flag2=0;
}
Log.e("test", "index" + i);
xEntrys.add(new PieEntry(xdata.get(i), ydata.get(i)));
}
for (int i = 0; i < ydata.size(); i++) {
yEntrys.add(ydata.get(i));
}
PieDataSet pieDataSet = new PieDataSet(xEntrys, " ");
pieDataSet.setSliceSpace(4);
pieDataSet.setValueTextSize(10);
ArrayList<Integer> colors = new ArrayList<>();
colors.add(Color.GREEN);
colors.add(Color.RED);
colors.add(Color.CYAN);
colors.add(Color.BLUE);
colors.add(Color.MAGENTA);
colors.add(Color.YELLOW);
colors.add(Color.rgb(255,127,80));
pieDataSet.setColors(colors);
PieData pieData = new PieData(pieDataSet);
pieChart.setData(pieData);
pieChart.invalidate();
}
@Override
public void changed(Replication.ChangeEvent event) {
Replication replication = event.getSource();
com.couchbase.lite.util.Log.d(TAG, "Replication : " + replication + " changed.");
if (!replication.isRunning()) {
String msg = String.format("Replicator %s not running", replication);
com.couchbase.lite.util.Log.d(TAG, msg);
} else {
int processed = replication.getCompletedChangesCount();
int total = replication.getChangesCount();
String msg = String.format("Replicator processed %d / %d", processed, total);
com.couchbase.lite.util.Log.d(TAG, msg);
}
}
private static String createDocument(float x, String s) {
Log.e("test", "entered document creation");
Document document = database.createDocument();
String documentId = document.getId();
Log.e("oru valya test", "document id:" + document.getId());
Map<String, Object> map = new HashMap<String, Object>();
map.put("percent", x + "%");
map.put("Applicationn name", s);
try {
// Save the properties to the document
document.putProperties(map);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error putting", e);
}
return documentId;
}
}
主要活动的XMl布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoDragView="@+id/dragView"
sothree:umanoOverlay="true"
sothree:umanoPanelHeight="68dp"
sothree:umanoShadowHeight="4dp"
android:elevation="6dp">
<!-- MAIN CONTENT -->
<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?android:attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?android:attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
tools:targetApi="lollipop" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?android:attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?android:attr/actionBarSize"
tools:targetApi="lollipop" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/toolbar"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<!-- SLIDING LAYOUT -->
<fragment android:name="com.digitaldesigner.adish.apptracker.usagetracker.barchart.BarChartView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/idPieChart"
>
</fragment>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
</RelativeLayout>
BAR CHART的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.github.mikephil.charting.charts.PieChart
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/idPieChart"
>
</com.github.mikephil.charting.charts.PieChart>
</RelativeLayout>
我是一个完全的乞丐。我一直在单独的标签中显示饼图。有人可以帮帮我吗?