MPAndroidChart - Why BarData constructor not work?

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

My code:

 public BarData getBarData(String fieldName) {         ArrayList<BarEntry> entries = new ArrayList<>();         entries.add(new BarEntry(this.house.Population, 0));         entries.add(new BarEntry(this.currentStore.Population, 1));          ArrayList<String> labels = new ArrayList<String>();         labels.add("house");         labels.add("store" + this.currentStore.StoreName);          List<BarDataSet> dataSets = new ArrayList<>();         dataSets.add(new BarDataSet(entries, fieldName));          return new BarData(labels, dataSets); }

According to the document, the BarData constructor is like the code above. But why Android Studio always tell me there is something wrong?

Error message:

Error:(97, 16) error: constructor BarData in class BarData cannot be applied to given types; required: IBarDataSet[] found: List<String>,List<BarDataSet> reason: varargs mismatch; List<String> cannot be converted to IBarDataSet

Please give me some information. I really need someone's help.

Thanks.

回答1:

Using the newest version (3.0.0-beta1) of MPAndroidChart?

The constructor of this class has changed:

public BarData(List<IBarDataSet> dataSets) {     super(dataSets); }

This commit has removed X-axis labels.

Look into this example as it provides a new way to use a library.



回答2:

It's working version of Barchart with new BarData(datasets); here is code with xml. you needto edit tools:context= in xml.

import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View;  import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; import com.github.mikephil.charting.utils.ColorTemplate;  import java.util.ArrayList;  public class SubActivity_05 extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_sub_05);          BarChart bchart = (BarChart) findViewById(R.id.chart1);          ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();          for (int i = (int) 0; i < 10 + 1; i++) {             float val = (float) (Math.random());             yVals1.add(new BarEntry(i, val));         }          BarDataSet set1;          set1 = new BarDataSet(yVals1, "The year 2017");         set1.setColors(ColorTemplate.MATERIAL_COLORS);          ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();         dataSets.add(set1);          BarData data = new BarData(dataSets);          data.setValueTextSize(10f);         data.setBarWidth(0.9f);          bchart.setTouchEnabled(false);         bchart.setData(data);      }

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"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="com.example.administrator.jjikmeok.SubActivity_05">      <com.github.mikephil.charting.charts.BarChart         android:id="@+id/chart1"         android:layout_width="fill_parent"         android:layout_height="fill_parent" />  </RelativeLayout>

It's good to check out tutorial page. https://github.com/PhilJay/MPAndroidChart/wiki/Getting-Started



回答3:

For version 3 you must use like this

// the labels that should be drawn on the XAxis final String[] quarters = new String[] { "Q1", "Q2", "Q3", "Q4" };  IAxisValueFormatter formatter = new IAxisValueFormatter() {      @Override     public String getFormattedValue(float value, AxisBase axis) {         return quarters[(int) value];     }      // we don't draw numbers, so no decimal digits needed     @Override     public int getDecimalDigits() {  return 0; } };  XAxis xAxis = mLineChart.getXAxis(); xAxis.setGranularity(1f); // minimum axis-step (interval) is 1 xAxis.setValueFormatter(formatter);


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!