AnZ
AnZ

Reputation: 1060

MPAndroidChart - add offset top

Is there a way to add padding\offset to top? See screen. Digits are cropped if LimitLine is at top:

enter image description here


EDIT: source code

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.");
}

}

Upvotes: 1

Views: 1056

Answers (1)

Viral Patel
Viral Patel

Reputation: 33438

I had the same issue and after a lot of searching figured there is no direct way to get that extra space. Here is how i did it:

//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 not to be used as-is : 4 and 8 above are numbers that suited my purpose - change the values as per your data points

Upvotes: 2

Related Questions