2016-09-28 64 views
0

我坚持使用MPAndroidChart在图表上添加图例。我想这是因为提供了多个图表。 我已经使用了Google。我已经尝试过这样的东西:MPAndroidChart:无法将图例添加到具有多个图表的lineChart

Legend l = chart.getLegend(); 
l.setEnabled(true); 

寻求你的帮助。 在此先感谢。

这里是ChartActivity

public class StatisticActivity extends BaseActivity { 

private LineChart[] mCharts = new LineChart[4]; 
private Date mCurrDay; 

private Date getStartDay() { 
return DateHelper.getStartDay(Calendar.getInstance().getTime()); 
} 


@Override 
public void initViews() { 
mCharts[0] = (LineChart) findViewById(R.id.chart1); 
mCharts[1] = (LineChart) findViewById(R.id.chart2); 
mCharts[2] = (LineChart) findViewById(R.id.chart3); 
mCharts[3] = (LineChart) findViewById(R.id.chart4); 
for (int i = 0; i < mCharts.length; i++) { 

LineData data = getData(i); 

// add some transparency to the color with "& 0x90FFFFFF" 
setupChart(mCharts[i], data, mColors[i % mColors.length]); 
} 
} 


private LineData getData(int category) { 
mCurrDay = getStartDay(); 

ArrayList<Entry> yVals = new ArrayList<>(); 
int c = Integer.valueOf(DateHelper.convertToString(mCurrDay).substring(11)) + 7; 
for (int i = Integer.valueOf(DateHelper.convertToString(mCurrDay).substring(11)); i < c; i++) { 
float val = (float) DbHelper.getInstance(context).getDailyStatistic(DateHelper.convertToString(mCurrDay), String.valueOf(category)); 
yVals.add(new Entry(i, val)); 
mCurrDay = DateHelper.goToNextDay(mCurrDay); 
} 

// create a dataset and give it a type 
LineDataSet set1 = new LineDataSet(yVals, "DataSet 1"); 
set1.setFillAlpha(110); 
set1.setFillColor(Color.RED); 
set1.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER); 
// set1.setLineWidth(1.75f); 
set1.setDrawCircles(false); 
set1.setDrawCircleHole(false); 
set1.setDrawFilled(true); 
if (Utils.getSDKInt() >= 18) { 
// fill drawable only supported on api level 18 and above 
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red); 
set1.setFillDrawable(drawable); 
} else { 
set1.setFillColor(Color.BLACK); 
} 
set1.setColor(Color.WHITE); 
set1.setHighLightColor(Color.WHITE); 
set1.setDrawValues(false); 
set1.setLabel("Label"); 


// create a data object with the datasets 
LineData data = new LineData(set1); 
return data; 
} 

private int[] mColors = new int[]{ 
Color.rgb(137, 230, 81), 
Color.rgb(240, 240, 30), 
Color.rgb(89, 199, 250), 
Color.rgb(250, 104, 104) 
}; 

private void setupChart(LineChart chart, LineData data, int color) { 

((LineDataSet) data.getDataSetByIndex(0)).setCircleColorHole(color); 

// no description text 
chart.setDescription("Descr"); 
chart.setDescriptionColor(Color.WHITE); 
chart.setNoDataTextDescription("You need to provide data for the chart."); 
chart.setDrawGridBackground(false); 
chart.setTouchEnabled(true); 
chart.setDragEnabled(true); 
chart.setScaleEnabled(true); 
chart.setPinchZoom(true); 

chart.setBackgroundColor(color); 

// set custom chart offsets (automatic offset calculation is hereby disabled) 
// chart.setViewPortOffsets(10, 0, 10, 0); 

// add data 
chart.setData(data); 

// get the legend (only possible after setting data) 
// Legend l = chart.getLegend(); 
// l.setEnabled(true); 
// l.setForm(Legend.LegendForm.LINE); 
// l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); 
// 
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); 
// l.setOrientation(Legend.LegendOrientation.HORIZONTAL); 
// l.setDrawInside(false); 
// l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT); 
// l.setWordWrapEnabled(true); 
// l.setYEntrySpace(30); 
// l.setXEntrySpace(30); 
// l.setFormSize(30); 

Legend l = chart.getLegend(); 
l.setFormSize(10f); // set the size of the legend forms/shapes 
l.setForm(Legend.LegendForm.CIRCLE); // set what type of form/shape should be used 
l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT); 
l.setTextSize(12f); 
l.setTextColor(Color.BLACK); 
l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis 
l.setYEntrySpace(5f); // set the space between the legend entries on the y-axis 

// set custom labels and colors 
l.setCustom(new int[]{Color.WHITE}, new String[] {"LEGEND"}); 


chart.getAxisLeft().setEnabled(true); 
// chart.getAxisLeft().setSpaceTop(80); 
chart.getAxisLeft().setAxisLineColor(Color.WHITE); 
// chart.getAxisLeft().setSpaceBottom(80); 
chart.getAxisLeft().setDrawAxisLine(false); 
chart.getAxisLeft().setGridColor(Color.WHITE); 
chart.getAxisLeft().setTextColor(Color.WHITE); 
chart.getAxisLeft().setLabelCount(4, false); 
chart.getAxisLeft().setAxisMinValue(0); 
chart.getAxisRight().setEnabled(false); 

XAxis xAxis = chart.getXAxis(); 
xAxis.setEnabled(true); 
xAxis.setPosition(XAxis.XAxisPosition.TOP); 
xAxis.setDrawGridLines(false); 
xAxis.setDrawAxisLine(false); 
xAxis.setAxisLineColor(Color.WHITE); 
xAxis.setTextColor(Color.WHITE); 
xAxis.setValueFormatter(new AxisValueFormatter() { 
@Override 
public String getFormattedValue(float value, AxisBase axis) { 
switch ((int) value) { 
case 1: 

default: 
return "0"; 
} 
} 

@Override 
public int getDecimalDigits() { 
return 0; 
} 
}); 

// animate calls invalidate()... 
chart.animateX(2500); 

} 
} 

回答

0

我发现什么问题。 所以要显示图例我不要需要使用setEnabled(true)并使用chart.invalidate()

这里是我的代码:

Legend legend = chart.getLegend(); 
     legend.setYOffset(40); 
     legend.setForm(Legend.LegendForm.CIRCLE); 
     legend.setTextColor(Color.WHITE); 
     legend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT); 
     legend.setTextSize(200); 
     chart.invalidate();