Activity 中未显示来自 slqlite 的 MPAndroidChart 数据
MPAndroidChart data from slqlite is not showing in the Activity
我想使用 sqlite 数据填充 MPAndroidChart,一切似乎都有效,但我不知道为什么图表本身没有显示在 activity 中。
Sqlite 查询中有这样的数据:
----------------------
| Data | Usage |
|------------|-------|
| 2016-04-24 | 7.58 |
| 2016-04-06 | 7.74 |
| 2016-03-13 | 8.46 |
| 2016-03-22 | 8.29 |
----------------------
但即便如此图表也没有显示出来。我在 MPAndroidChart github 页面中查看指南,但似乎我搞砸了一些事情...
ChartActivity.java
public void setData(String car_id){
dbHelper = new DBHelper(context);
Cursor curChartFuelUsage = dbHelper.getChartFuelUsage(car_id);
curChartFuelUsage.moveToFirst();
int count = curChartFuelUsage.getCount();
Float[] x_refuel_data = new Float[count];
Float[] y_fuel_usage = new Float[count];
if (curChartFuelUsage.moveToFirst()){
Log.e("CHART", "moveToFirst OK, count: " + count);
for (int i = 0 ; i < count ; i++){
x_refuel_data[i] = curChartFuelUsage.getFloat(0);
y_fuel_usage[i] = curChartFuelUsage.getFloat(1);
curChartFuelUsage.moveToNext();
}
ArrayList<Entry> values = new ArrayList<Entry>();
for (int i = 0 ; i < count ; i++){
values.add(new Entry(x_refuel_data[i], y_fuel_usage[i]));
}
LineDataSet set1 = new LineDataSet(values,"Test");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1);
LineData lineData = new LineData(dataSets);
chart.setData(lineData);
chart.invalidate();
}
chart_activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="horizontal"-->
android:orientation="vertical"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.asd.app.ChartActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/chart_activity_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/line_chart_fuel_usage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
编辑:
它在将布局方向更改为垂直后有效,但我不知道为什么图表只显示一行中的值。
Char pic
你的父级 LinearLayout
的方向设置为水平,AppBarLayout 的宽度为 match_parent
,因此没有更多 space 来绘制图表。
尝试将父方向更改为 vertical
。
编辑
第 2 期:
您的日期似乎已转换为浮点数 (2016.0),并且您的数据恰好具有相同的前缀 (2016-0...)。
您可以做的是将所有 x-axis 值放入 String[]
中,然后在插入条目时按如下方式操作:values.add(new Entry(i, y_fuel_usage[i]));
然后使用 IAxisValueFormatter
覆盖 X 轴值:
IAxisValueFormatter formatter = new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return yourStringArray[(int)value];
}
};
设置为 X 轴:
yourChart.getXAxis().setValueFormatter(formatter);
虽然我没有测试过上面这个特定的代码,但我过去使用过类似的代码并且运行良好。
我想使用 sqlite 数据填充 MPAndroidChart,一切似乎都有效,但我不知道为什么图表本身没有显示在 activity 中。 Sqlite 查询中有这样的数据:
---------------------- | Data | Usage | |------------|-------| | 2016-04-24 | 7.58 | | 2016-04-06 | 7.74 | | 2016-03-13 | 8.46 | | 2016-03-22 | 8.29 | ----------------------
但即便如此图表也没有显示出来。我在 MPAndroidChart github 页面中查看指南,但似乎我搞砸了一些事情...
ChartActivity.java
public void setData(String car_id){
dbHelper = new DBHelper(context);
Cursor curChartFuelUsage = dbHelper.getChartFuelUsage(car_id);
curChartFuelUsage.moveToFirst();
int count = curChartFuelUsage.getCount();
Float[] x_refuel_data = new Float[count];
Float[] y_fuel_usage = new Float[count];
if (curChartFuelUsage.moveToFirst()){
Log.e("CHART", "moveToFirst OK, count: " + count);
for (int i = 0 ; i < count ; i++){
x_refuel_data[i] = curChartFuelUsage.getFloat(0);
y_fuel_usage[i] = curChartFuelUsage.getFloat(1);
curChartFuelUsage.moveToNext();
}
ArrayList<Entry> values = new ArrayList<Entry>();
for (int i = 0 ; i < count ; i++){
values.add(new Entry(x_refuel_data[i], y_fuel_usage[i]));
}
LineDataSet set1 = new LineDataSet(values,"Test");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1);
LineData lineData = new LineData(dataSets);
chart.setData(lineData);
chart.invalidate();
}
chart_activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="horizontal"-->
android:orientation="vertical"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.asd.app.ChartActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/chart_activity_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/line_chart_fuel_usage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
编辑: 它在将布局方向更改为垂直后有效,但我不知道为什么图表只显示一行中的值。
Char pic
你的父级 LinearLayout
的方向设置为水平,AppBarLayout 的宽度为 match_parent
,因此没有更多 space 来绘制图表。
尝试将父方向更改为 vertical
。
编辑
第 2 期:
您的日期似乎已转换为浮点数 (2016.0),并且您的数据恰好具有相同的前缀 (2016-0...)。
您可以做的是将所有 x-axis 值放入 String[]
中,然后在插入条目时按如下方式操作:values.add(new Entry(i, y_fuel_usage[i]));
然后使用 IAxisValueFormatter
覆盖 X 轴值:
IAxisValueFormatter formatter = new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return yourStringArray[(int)value];
}
};
设置为 X 轴:
yourChart.getXAxis().setValueFormatter(formatter);
虽然我没有测试过上面这个特定的代码,但我过去使用过类似的代码并且运行良好。