2016-06-14 11 views
1

Gibt es eine Möglichkeit, Padding \ Offset nach oben hinzuzufügen? Siehe Bildschirm. Die Ziffern werden abgeschnitten, wenn LimitLine oben ist:MPAndroidChart - Offset oben hinzufügen

enter image description here


EDIT: Quellcode

public class ChartManager implements 
    OnChartGestureListener, OnChartValueSelectedListener { 

private LineChart mChart; 
private Context mContext; 
private float lastYValue; 

int blueColor; 
int tealColor; 
int whiteColor; 

public ChartManager(Context context, LineChart mChart) { 
    this.mContext = context; 

    tealColor = mContext.getResources().getColor(R.color.graph_stroke); 
    blueColor = mContext.getResources().getColor(R.color.primary_blue); 
    whiteColor = mContext.getResources().getColor(R.color.primary_white); 

    mChart.setTop(50); 
    mChart.setOnChartGestureListener(this); 
    mChart.setOnChartValueSelectedListener(this); 
    mChart.setDrawGridBackground(false); 
    mChart.setDescription(null); 
    mChart.setNoDataTextDescription("You need to provide data for the chart."); 
    mChart.setTouchEnabled(false); 
    mChart.setDragEnabled(false); 
    mChart.setScaleEnabled(false); 
    mChart.setPinchZoom(true); 
    mChart.setBorderColor(blueColor); 
    mChart.getLegend().setEnabled(false); 

    XAxis xAxis = mChart.getXAxis(); 
    xAxis.setAxisLineColor(blueColor); 
    xAxis.setGridColor(blueColor); 
    xAxis.setTextColor(blueColor); 
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); 

    mChart.getAxisRight().setEnabled(true); 
    mChart.getAxisLeft().setEnabled(false); 

    this.mChart = mChart; 
} 

public void setLimitLine(){ 
    YAxis yAxis = mChart.getAxisRight(); 
    yAxis.removeAllLimitLines(); 
    yAxis.setAxisLineColor(blueColor); 
    yAxis.setGridColor(blueColor); 
    yAxis.setTextColor(blueColor); 


    LimitLine ll = new LimitLine(lastYValue, SingleAsset.round(lastYValue, 2).toString()); 
    ll.setLineColor(tealColor); 
    ll.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP); 
    ll.setLineWidth(2f); 
    ll.setTextColor(whiteColor); 
    ll.setTextSize(16f); 
    yAxis.addLimitLine(ll); 

    Legend l = mChart.getLegend(); 
    l.setForm(Legend.LegendForm.LINE); 
} 

public void removeAllLimitLines(){ 
    YAxis yAxis = mChart.getAxisRight(); 
    yAxis.removeAllLimitLines(); 
} 

public String parseHours(long millis){ 
    return new SimpleDateFormat("hh:mm").format(new Date(millis)); 
} 

public void setData(LineChart mChart, List<SingleAsset.Timestamp> timestamps) { 
    ArrayList<String> xVals = new ArrayList<>(); 
    for (SingleAsset.Timestamp timestamp : timestamps){ 

     xVals.add(parseHours(timestamp.getTimestamp())); 
    } 

    ArrayList<Entry> yVals = new ArrayList<>(); 
    for (int i = 0; i < timestamps.size(); i++){ 

     float value = (float) timestamps.get(i).getValue(); 
     yVals.add(new Entry(value, i)); 
     lastYValue = value; 
    } 

    // create a dataset and give it a type 
    LineDataSet lineDataSet = new LineDataSet(yVals, null); 
    lineDataSet.enableDashedHighlightLine(10f, 5f, 0f); 
    lineDataSet.setColor(mContext.getResources().getColor(R.color.primary_green)); 
    lineDataSet.setLineWidth(1f); 
    lineDataSet.setCircleRadius(3f); 
    lineDataSet.setDrawCircleHole(false); 
    lineDataSet.setValueTextSize(9f); 

    Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.fade_green); 
    lineDataSet.setFillDrawable(drawable); 

    lineDataSet.setDrawFilled(true); 
    lineDataSet.setDrawValues(false); 
    lineDataSet.setDrawCircles(false); 
    lineDataSet.setDrawCubic(false); 

    ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>(); 
    dataSets.add(lineDataSet); // update the datasets 

    // create a data object with the datasets 
    LineData lineData = new LineData(xVals, dataSets); 

    // set data 
    mChart.setData(lineData); 
} 

@Override 
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { 
    Log.i("Gesture", "START, x: " + me.getX() + ", y: " + me.getY()); 
} 

@Override 
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { 
    Log.i("Gesture", "END, lastGesture: " + lastPerformedGesture); 

    // un-highlight values after the gesture is finished and no single-tap 
    if (lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP) 
     mChart.highlightValues(null); // or highlightTouch(null) for callback to onNothingSelected(...) 
} 

@Override 
public void onChartLongPressed(MotionEvent me) { 
    Log.i("LongPress", "Chart longpressed."); 
} 

@Override 
public void onChartDoubleTapped(MotionEvent me) { 
    Log.i("DoubleTap", "Chart double-tapped."); 
} 

@Override 
public void onChartSingleTapped(MotionEvent me) { 
    Log.i("SingleTap", "Chart single-tapped."); 
} 

@Override 
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) { 
    Log.i("Fling", "Chart flinged. VeloX: " + velocityX + ", VeloY: " + velocityY); 
} 

@Override 
public void onChartScale(MotionEvent me, float scaleX, float scaleY) { 
    Log.i("Scale/Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY); 
} 

@Override 
public void onChartTranslate(MotionEvent me, float dX, float dY) { 
    Log.i("Translate/Move", "dX: " + dX + ", dY: " + dY); 
} 

@Override 
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) { 
    Log.i("Entry selected", e.toString()); 
    Log.i("LOWHIGH", "low: " + mChart.getLowestVisibleXIndex() + ", high: " + mChart.getHighestVisibleXIndex()); 
    Log.i("MIN MAX", "xmin: " + mChart.getXChartMin() + ", xmax: " + mChart.getXChartMax() + ", ymin: " + mChart.getYChartMin() + ", ymax: " + mChart.getYChartMax()); 
} 

@Override 
public void onNothingSelected() { 
    Log.i("Nothing selected", "Nothing selected."); 
} 

} 
+0

Können Sie bitte posten Sie Ihre Code? – Dhruv

+0

@Dhruv, sorry. Bitte schauen Sie auf bearbeiten – AnZ

+1

http://StackOverflow.com/Q/28563051/3817374 Sie können dies beziehen – Beena

Antwort

2

Ich hatte das gleiche Problem und nach viel dort dachte suchen ist keine direkte Weg, um diesen zusätzlichen Raum zu bekommen. Hier ist, wie ich es tat:

//dont start at zero 
yaxis.setStartAtZero(false); 
//look at your dataset and set the min and max values of the y-axis at runtime 
//keep the range with some extra space - as you want 
yaxis.setAxisMinValue(4); 
yaxis.setAxisMaxValue(8); 

-Code nicht verwendet werden, wie sie ist: 4 und 8 oben sind Zahlen, die meinen Zweck geeignet - die Werte ändern, wie pro Ihre Datenpunkte