我需要用SQLite
数据库中的数据填充pieChart,但是我不知道如何执行此操作。我目前是android开发的初学者。
我希望此查询成为我将添加到pieChart中的内容的来源:
Cursor cursor = db.rawQuery("SELECT COUNT(label), label, SUM(amount) FROM " + ItemEntry.TABLE_NAME + " GROUP BY Label ORDER BY Amount DESC;", null);
标签当然是标签,它们的值GROUPED BY他们的标签的总和就是我要放在饼图中的数量。
问题是我不知道该怎么做/怎么做..请有人帮助。 顺便说一句,我正在使用AnimatedPieView ...请帮助
这是我的图表JAVA代码
package com.example.admin.test2;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ListView;
import android.widget.TextView;
import com.razerdp.widget.animatedpieview.AnimatedPieView;
import com.razerdp.widget.animatedpieview.AnimatedPieViewConfig;
import com.razerdp.widget.animatedpieview.data.IPieInfo;
import com.razerdp.widget.animatedpieview.data.SimplePieInfo;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class Charts extends Fragment {
public SQLiteDatabase mDatabase;
AnimatedPieView mAnimatedPieView;
ListView itemList;
ArrayList <ChartItems> arrayList;
ChartItemAdapter chartItemAdapter;
ItemDBHelper dbHelper;
TextView expense;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_charts, container, false);
expense = (TextView) view.findViewById(R.id.totalExpns);
expenseSum();
mAnimatedPieView = view.findViewById(R.id.animatedPieChart);
drawPie();
dbHelper = new ItemDBHelper(getActivity());
itemList = (ListView) view.findViewById(R.id.itemListView);
loadDataInListView();
return view;
}
public void loadDataInListView() {
arrayList = dbHelper.getChartData();
chartItemAdapter = new ChartItemAdapter(getContext(), arrayList);
itemList.setAdapter(chartItemAdapter);
chartItemAdapter.notifyDataSetChanged();
Helper.getListViewSize(itemList);
}
public void expenseSum() {
ItemDBHelper dbHelper = new ItemDBHelper(getActivity());
mDatabase = dbHelper.getWritableDatabase();
Cursor cur = mDatabase.rawQuery("SELECT SUM(" + ItemContract.ItemEntry.COLUMN_AMOUNT + ") as Total FROM " + ItemContract.ItemEntry.TABLE_NAME
, null);
if (cur.moveToFirst()) {
int total = cur.getInt(cur.getColumnIndex("Total"));// get final total
}
int total = cur.getInt(cur.getColumnIndex("Total"));
expense.setText("-₱ " + total);
}
public void drawPie() {
AnimatedPieViewConfig config = new AnimatedPieViewConfig();
config.startAngle(-90)// Starting angle offset
.addData(new SimplePieInfo(30, Color.parseColor("#77dd77"), "Sample lang"))//Data (bean that implements the IPieInfo interface)
.addData(new SimplePieInfo(18.0f, Color.parseColor("#ff6961"), "Sample ulet"))
.duration(3000);// draw pie animation duration
config.floatShadowRadius(18f);
config.floatUpDuration(500);
config.interpolator(new DecelerateInterpolator(4f));
// The following two sentences can be replace directly 'mAnimatedPieView.start (config); '
mAnimatedPieView.applyConfig(config);
mAnimatedPieView.start();
}
}
用于图表的XML文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:background="#f6f6f6"
tools:context=".Charts">
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/pastelPink"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView18"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:fontFamily="@font/montserrat"
android:text="Distribution Chart"
android:textColor="#fefefe"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/view"
app:layout_constraintEnd_toEndOf="@+id/view"
app:layout_constraintStart_toStartOf="@+id/view"
app:layout_constraintTop_toTopOf="@+id/view" />
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view9"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/sections"
app:layout_constraintBottom_toBottomOf="@+id/itemListView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="@font/montserrat"
android:text="Total Expenses"
android:textSize="13dp"
app:layout_constraintEnd_toEndOf="@+id/view9"
app:layout_constraintStart_toStartOf="@+id/view9"
app:layout_constraintTop_toTopOf="@+id/view9" />
<TextView
android:id="@+id/totalExpns"
android:layout_width="250dp"
android:layout_height="28dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:autoSizeTextType="uniform"
android:fontFamily="@font/montserrat"
android:text="₱ 0"
android:textAlignment="center"
android:textColor="@color/pastelRed"
android:textSize="18dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/view9"
app:layout_constraintStart_toStartOf="@+id/view9"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
<com.razerdp.widget.animatedpieview.AnimatedPieView
android:id="@+id/animatedPieChart"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="@+id/view9"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/view9"
app:layout_constraintTop_toBottomOf="@+id/totalExpns" />
<ListView
android:id="@+id/itemListView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="60dp"
android:background="@android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/view9"
app:layout_constraintStart_toStartOf="@+id/view9"
app:layout_constraintTop_toBottomOf="@+id/animatedPieChart" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
我的DBHELPER:使用我用于在图表布局中填充列表视图的代码。
package com.example.admin.test2;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.admin.test2.ItemContract.*;
import java.util.ArrayList;
public class ItemDBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "itemlist.db";
public static final int DATABASE_VERSION = 4;
public ItemDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE_ITEMLIST_TABLE = "CREATE TABLE " + ItemEntry.TABLE_NAME + " (" +
ItemEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
ItemEntry.COLUMN_LABEL + " TEXT NOT NULL, " +
ItemEntry.COLUMN_DETAIL + " TEXT, " +
ItemEntry.COLUMN_AMOUNT + " INTEGER NOT NULL, " +
ItemEntry.COLUMN_DATE + " TEXT " +
");";
db.execSQL(SQL_CREATE_ITEMLIST_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + ItemEntry.TABLE_NAME);
onCreate(db);
}
public ArrayList<ChartItems> getChartData() {
ArrayList<ChartItems> arrayList = new ArrayList <>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT COUNT(label), label, SUM(amount) FROM " + ItemEntry.TABLE_NAME +
" GROUP BY Label ORDER BY Amount DESC;", null);
while (cursor.moveToNext()) {
String label = cursor.getString(1);
double amount = cursor.getDouble(2);
int count = cursor.getInt(0);
ChartItems chartItems = new ChartItems(count,label,amount);
arrayList.add(chartItems);
}
return arrayList;
}
}
答案 0 :(得分:1)
从Sqlite获取数据后,调用 drawPie()函数,而不是调用 onCreate()方法
fun drawPie() {
val config = AnimatedPieViewConfig()
for(chartItem in arrayList){ // arrayList -> List from sqlite
config.addData( SimplePieInfo(
chartItem.count,
Color.parseColor("//colour you wish"),
chartItem.label
))
}
config.startAngle(-90f)// Starting angle offset
.duration(2000)// draw pie animation duration
config.floatShadowRadius(18f)
config.floatUpDuration(500)
config.interpolator(DecelerateInterpolator(4f))
config.drawText(true)
config.textSize(36f)
// The following two sentences can be replace directly
mAnimatedPieView.start (config)
mAnimatedPieView.applyConfig(config)
mAnimatedPieView.start()
}