Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart - Why BarData constructor not work?

Tags:

android

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.

like image 453
user3087000 Avatar asked Jul 08 '16 08:07

user3087000


2 Answers

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.

like image 155
R. Zagórski Avatar answered Nov 14 '22 23:11

R. Zagórski


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

like image 28
swkim306 Avatar answered Nov 15 '22 01:11

swkim306