无法解析构造函数 ‘BarEntry(java.lang.String, java.lang.Float)’。

huangapple go评论84阅读模式
英文:

Cannot resolve constructor 'BarEntry(java.lang.String, java.lang.Float)

问题

以下是翻译好的内容:

public void onResponse(@NotNull Call<List<MonthlySales>> call, @NotNull Response<List<MonthlySales>> response) {
    // 检查响应体是否为空
    if (response.body() != null) {
        List<BarEntry> barEntries = new ArrayList<>();
        for (MonthlySales monthlySales : response.body()) {
            barEntries.add(new BarEntry(monthlySales.getMonth(), monthlySales.getTotal()));
        }
        BarDataSet dataSet = new BarDataSet(barEntries, "月销售额");
        dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
        BarData data = new BarData(dataSet);
        data.setBarWidth(10f);
        chart.setVisibility(View.VISIBLE);
        chart.animateXY(2000, 2000);
        chart.setData(data);
        chart.setFitBars(true);
        Description description = new Description();
        description.setText("每月销售额");
        chart.setDescription(description);
        chart.invalidate();
    }
}
英文:

I would like to create agraph from the json data bellow

    [{&quot;month&quot;:&quot;August 2020&quot;,&quot;total&quot;:&quot;4587175.08&quot;},{&quot;month&quot;:&quot;July 2020&quot;,&quot;total&quot;:&quot;9151128.27&quot;},{&quot;month&quot;:&quot;June 2020&quot;,&quot;total&quot;:&quot;10859553.16&quot;},{&quot;month&quot;:&quot;May 2020&quot;,&quot;total&quot;:&quot;2600435.33&quot;}]

That data is from a db to my graph but I get the error above
My code for inserting the entires is as follows

      public void onResponse(@NotNull Call&lt;List&lt;MonthlySales&gt;&gt; call, @NotNull Response&lt;List&lt;MonthlySales&gt;&gt; response) {
            //check if the response body is null
            if(response.body()!=null){
                List&lt;BarEntry&gt; barEntries = new ArrayList&lt;&gt;();
                for (MonthlySales monthlySales : response.body()) {
                    barEntries.add(new BarEntry(monthlySales.getMonth(),monthlySales.getTotal()));
                }
                BarDataSet dataSet = new BarDataSet(barEntries,&quot;Monthly Sales&quot;);
                dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
                BarData data = new BarData(dataSet);
                data.setBarWidth(10f);
                chart.setVisibility(View.VISIBLE);
                chart.animateXY(2000, 2000);
                chart.setData(data);
                chart.setFitBars(true);
                Description description = new Description();
                description.setText(&quot;Sales per month&quot;);
                chart.setDescription(description);
                chart.invalidate();

What could I be doing wrong? Note that the month should be the xaxis label while the total the yaxis, how coud I achieve this?

答案1

得分: 2

> Java中的构造函数是一种特殊的方法,用于初始化对象。当创建类的对象时,构造函数会被调用。

import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

请查看[**`BarEntry`**][1]

/**
 * 正常柱状条的构造函数(非堆叠)。
 *
 * @param x
 * @param y
 */
public BarEntry(float x, float y) {
    super(x, y);
}

public BarEntry(float x, float y, Object data) {
    super(x, y, data);
}

public BarEntry(float x, float y, Drawable icon) {
    super(x, y, icon);
}

public BarEntry(float x, float y, Drawable icon, Object data) {
    super(x, y, icon, data);
}

public BarEntry(float x, float[] vals) {
    super(x, calcSum(vals));
    this.mYVals = vals;
}

public BarEntry(float x, float[] vals, Object data) {
    super(x, calcSum(vals), data);
    this.mYVals = vals;
}

public BarEntry(float x, float[] vals, Drawable icon) {
    super(x, calcSum(vals), icon);
    this.mYVals = vals;
}

public BarEntry(float x, float[] vals, Drawable icon, Object data) {
    super(x, calcSum(vals), icon, data);
    this.mYVals = vals;
}

**DEMO**

ArrayList&lt;BarEntry&gt; barEntries = new ArrayList&lt;&gt;();
             
barEntries.add(new BarEntry(1f, 0));
barEntries.add(new BarEntry(2f, 1));

[1]: https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.java
英文:

> A constructor in Java is a special method that is used to initialise
> objects. The constructor is called when an object of a class is
> created.

import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

Check BarEntry class.

/**
* Constructor for normal bars (not stacked).
*
* @param x
* @param y
*/
public BarEntry(float x, float y) {
super(x, y);
}
public BarEntry(float x, float y, Object data) {
super(x, y, data);
}
public BarEntry(float x, float y, Drawable icon) {
super(x, y, icon);
}
public BarEntry(float x, float y, Drawable icon, Object data) {
super(x, y, icon, data);
}
public BarEntry(float x, float[] vals) {
super(x, calcSum(vals));
this.mYVals = vals;
}
public BarEntry(float x, float[] vals, Object data) {
super(x, calcSum(vals), data);
this.mYVals = vals;
}
public BarEntry(float x, float[] vals, Drawable icon) {
super(x, calcSum(vals), icon);
this.mYVals = vals;
}
public BarEntry(float x, float[] vals, Drawable icon, Object data) {
super(x, calcSum(vals), icon, data);
this.mYVals = vals;
}

DEMO

 ArrayList&lt;BarEntry&gt; barEntries = new ArrayList&lt;&gt;();
barEntries.add(new BarEntry(1f, 0));
barEntries.add(new BarEntry(2f, 1));

huangapple
  • 本文由 发表于 2020年8月20日 20:34:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63505219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定