MPAndroidChart - Why doesn't the BarData constructor work?

My code is:

 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 similar to the code above. But why does Android Studio always tell me that something is 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 some help.

Thank.

+4
source share
4 answers

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

The constructor of this class has changed:

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

This commit removed the X axis labels.

Check out this example as it provides a new way to use the library.

+9
source

Barchart BarData ( ); xml. : 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>

. https://github.com/PhilJay/MPAndroidChart/wiki/Getting-Started

+2

3

// 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);
+1

, . : 'com.github.PhilJay: MPAndroidChart: v2.2.4'

0

All Articles