Я создал линейную диаграмму с использованием библиотеки MPandroidchart и хочу заполнить ее пользовательским вводом, сохраненным в комнате и сохраненным в списке liveData.Я создал диаграмму и реализовал класс viewModel, но я не уверен, как добавить liveData в качестве данных диаграммы.
Graph.java (фрагмент, где отображается диаграмма)
public class Graph extends Fragment {
public Graph() {
// Required empty public constructor
}
private LineChart mChart;
private InputViewModel mInputViewModel;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_graph, container, false);
Log.e("create","graph oncreate method reached");
//initialize chart
mChart = view.findViewById(R.id.chart);
//set description
Description description = new Description();
description.setText("Blood glucose levels");
mChart.setDescription(description);
//set description if no data is available
mChart.setNoDataText("No Data Available. Add a blood glucose level input to see your graph");
//enable touch gestures
mChart.setTouchEnabled(true);
//enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
//draw background grid
mChart.setDrawGridBackground(true);
//draw x-axis
XAxis xAxis = mChart.getXAxis();
xAxis.setEnabled(false);
// setting position to TOP and INSIDE the chart
xAxis.setPosition(XAxis.XAxisPosition.TOP_INSIDE);
// setting text size for our axis label
xAxis.setTextSize(10f);
xAxis.setTextColor(Color.BLACK);
// to draw axis line
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
YAxis yAxis = mChart.getAxisLeft();
// setting the count of Y-axis label's
yAxis.setLabelCount(12, false);
yAxis.setTextColor(Color.BLACK);
yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
yAxis.setDrawGridLines(true);
//add upper limit line
LimitLine upper = new LimitLine(8, "Above target range");
upper.setLineColor(Color.parseColor("#bb2d2d"));
upper.setLineWidth(3f);
upper.enableDashedLine(10f, 10f, 0f);
upper.setTextColor(Color.BLACK);
upper.setTextSize(9f);
yAxis.addLimitLine(upper);
//add lower limit line
LimitLine lower = new LimitLine(4, "Below target range");
lower.setLineColor(Color.parseColor("#2c8ec7"));
lower.setLineWidth(3f);
lower.enableDashedLine(10f, 10f, 0f);
lower.setTextColor(Color.BLACK);
lower.setTextSize(9f);
yAxis.addLimitLine(lower);
yAxis.setDrawLimitLinesBehindData(true);
mChart.getAxisRight().setEnabled(false);
mChart.getLegend().setEnabled(false);
// add data
setData();
mChart.notifyDataSetChanged();
mChart.invalidate();
return view;
}
private void setData() {
final ArrayList<Entry> values = new ArrayList<>();
//implement viewModel and liveData
mInputViewModel = ViewModelProviders.of(this).get(InputViewModel.class);
mInputViewModel.getAllInput1s().observe(this, new Observer<List<Input1>>() {
@Override
public void onChanged(@Nullable final List<Input1> input1s) {
}
});
values.add(new Entry(0, (float) 4.8));
Log.e("set", "method reached");
LineDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
set1.setValues(values);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new LineDataSet(values, "Blood Glucose Levels");
set1.setDrawIcons(false);
set1.setLineWidth(4f);
set1.setCircleRadius(5f);
set1.setDrawCircleHole(false);
set1.setValueTextSize(10f);
set1.setFormLineWidth(1f);
set1.setFormSize(15.f);
set1.setColor(Color.BLACK);
set1.setCircleColor(Color.BLACK);
}
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
final LineData data = new LineData(dataSets);
mChart.setData(data);
mChart.notifyDataSetChanged();
mChart.invalidate();
//implement viewModel and liveData
mInputViewModel = ViewModelProviders.of(this).get(InputViewModel.class);
mInputViewModel.getAllInput1s().observe(this, new Observer<List<Input1>>() {
@Override
public void onChanged(@Nullable final List<Input1> input1s) {
//*****insert livedata here?*****//
}
});
mChart.notifyDataSetChanged();
mChart.invalidate();
}
@Override
public void onResume(){
super.onResume();
Log.e("resume", "onResume reached");
setData();
}
}
Tabbed.java (базовая активность фрагмента)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mInputViewModel = ViewModelProviders.of(this).get(InputViewModel.class);
//only add if there is a new input
Log.e("result1","onActivityResult tabbed reacher");
if (requestCode == INPUT_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
//retrieve input from room database
Input1 input1 = new Input1(data.getStringExtra(input.EXTRA_REPLY));
//insert the inputs into the view model of the recyclerView
mInputViewModel.insert(input1);
}
InputViewModel.java
public class InputViewModel extends AndroidViewModel {
private InputRepository mRepository;
private LiveData<List<Input1>> mAllInput1s;
public InputViewModel (Application application) {
super(application);
mRepository = new InputRepository(application);
mAllInput1s = mRepository.getAllInput1s();
}
LiveData<List<Input1>> getAllInput1s() {return mAllInput1s;}
void insert(Input1 input1) {mRepository.insert(input1);}
}
Мне просто нужен способ установить данные диаграммы как LiveData
Спасибо за помощь!