package com.example.a401st_35.weather2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private LineChart lineChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lineChart = (LineChart)findViewById(R.id.chart);
ArrayList<Entry> val = new ArrayList<Entry>();
val.add(new Entry(100.0f,0));
val.add(new Entry(100.0f,1));
val.add(new Entry(100.0f,2));
val.add(new Entry(100.0f,3));
LineDataSet setComp1 = new LineDataSet(val,"Company 1");
setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
dataSets.add(setComp1);
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("1.Q");
xVals.add("2.Q");
xVals.add("3.Q");
xVals.add("4.Q");
**LineData data = new LineData(xVals,dataSets);**
lineChart.setData(data);
lineChart.invalidate();
}
}
我试过其他Mpandroid LineCharts,但总是会遇到与此相同的错误。
LineData中的LineData(com.github.mikephill.charting.interfaces.datasets.ILineDataSet ...)无法应用于(java.util.ArrayLIst,java.util.ArrayList)
Error:(53, 25) error: constructor LineData in class LineData cannot be
applied to given types;
required: ILineDataSet[]
found: ArrayList<String>,ArrayList<LineDataSet>
reason: varargs mismatch; ArrayList<String> cannot be converted to
ILineDataSet
特别,
[LineData数据=新的LineData(xVals,dataSets); ]这条线是问题所在。
已经设置了build.gradle并且还导入了jar文件。我怎样才能解决这个问题?
最佳答案
要提供多个行数据集,请使用以下示例,并同时使用mpcharts版本3.0.3
ArrayList<Entry> lineEntries = new ArrayList<Entry>();
lineEntries.add(new Entry(0, 6));
lineEntries.add(new Entry(1, 9));
lineEntries.add(new Entry(2, 4));
lineEntries.add(new Entry(3, 8));
lineEntries.add(new Entry(4, 12));
lineEntries.add(new Entry(5, 10));
LineDataSet lineDataSet1 = new LineDataSet(lineEntries, "Line1");
lineDataSet1.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet1.setColors(getColor("defaultGreen"));
lineDataSet1.setLineWidth(3);
lineDataSet1.setHighlightEnabled(true);
lineDataSet1.setCircleColor(getColor("defaultOrange"));
lineDataSet1.setCircleRadius(6);
lineDataSet1.setCircleHoleRadius(3);
lineDataSet1.setDrawHighlightIndicators(true);
lineDataSet1.setHighLightColor(Color.RED);
lineDataSet1.setValueTextSize(defaultValueTextSize);
lineDataSet1.setValueTextColor(getColor("primaryDark"));
lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
ArrayList<Entry> lineEntries2 = new ArrayList<Entry>();
lineEntries2.add(new Entry(0, 8));
lineEntries2.add(new Entry(1, 11));
lineEntries2.add(new Entry(2, 6));
lineEntries2.add(new Entry(3, 10));
lineEntries2.add(new Entry(4, 14));
lineEntries2.add(new Entry(5, 12));
LineDataSet lineDataSet2 = new LineDataSet(lineEntries2, "Line2");
lineDataSet2.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet2.setColors(getColor("defaultOrange"));
lineDataSet2.setLineWidth(3);
lineDataSet2.setHighlightEnabled(true);
lineDataSet2.setCircleColor(getColor("defaultGreen"));
lineDataSet2.setCircleRadius(6);
lineDataSet2.setCircleHoleRadius(3);
lineDataSet2.setDrawHighlightIndicators(true);
lineDataSet2.setHighLightColor(Color.RED);
lineDataSet2.setValueTextSize(defaultValueTextSize);
lineDataSet2.setValueTextColor(getColor("primaryDark"));
lineDataSet2.enableDashedLine(8,16,4);
lineDataSet2.setMode(LineDataSet.Mode.CUBIC_BEZIER);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(lineDataSet1);
dataSets.add(lineDataSet2);
LineData data = new LineData(dataSets);
lineChart.setData(data);
lineChart.animateY(1000);
lineChart.getDescription().setText("Line Comparison Chart");
Legend legend = lineChart.getLegend();
legend.setStackSpace(5);
legend.setTextColor(Color.BLACK);
// xAxis customization
XAxis xAxis = lineChart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
xAxis.setCenterAxisLabels(false);
xAxis.setDrawGridLines(true);
xAxis.setTextColor(Color.BLACK);
xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
YAxis leftAxis = lineChart.getAxisLeft();
leftAxis.setTextColor(Color.BLACK);
YAxis rightAxis = lineChart.getAxisRight();
rightAxis.setTextColor(Color.BLACK);
要给出一个行数据集,请使用以下示例:
lineEntries = new ArrayList<Entry>();
lineEntries.add(new Entry(0, 10));
lineEntries.add(new Entry(1, 11));
lineEntries.add(new Entry(2, 12));
lineEntries.add(new Entry(3, 14));
lineEntries.add(new Entry(4, 18));
lineEntries.add(new Entry(5, 31));
lineDataSet = new LineDataSet(lineEntries, "Line");
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setHighlightEnabled(true);
lineDataSet.setLineWidth(3);
lineDataSet.setColor(getColor("defaultBlue"));
lineDataSet.setCircleColor(getColor("defaultOrange"));
lineDataSet.setCircleRadius(6);
lineDataSet.setCircleHoleRadius(3);
lineDataSet.setDrawHighlightIndicators(true);
lineDataSet.setHighLightColor(Color.RED);
lineDataSet.setValueTextSize(defaultValueTextSize);
lineDataSet.setValueTextColor(getColor("primaryDark"));
LineData lineData = new LineData(lineDataSet);
lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);
lineChart.animateY(1000);
lineChart.setData(lineData);
有关自定义xAxis值,请参阅以下内容;
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(getXAxisValues()));
private ArrayList<String> getXAxisValues()
{
ArrayList<String> labels = new ArrayList<String> ();
labels.add( "JAN");
labels.add( "FEB");
labels.add( "MAR");
labels.add( "APR");
labels.add( "MAY");
labels.add( "JUN");
return labels;
}