2017-07-19 3 views
0

Ich möchte X-Achsen-Etiketten auf meinem Liniendiagramm haben (mit MPAndroidChart), aber egal was ich tue, kann ich nicht bekommen sie anzuzeigen. Ich habe eine Methode setupChart genannt, die für diese Grafik alles behandelt, und das ist, wie es aussieht:X-Achsen-Etiketten werden nicht angezeigt, mit MPAndroidChart

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

    ((LineDataSet) data.getDataSetByIndex(0)).setCircleColorHole(Color.WHITE); 
    ((LineDataSet) data.getDataSetByIndex(0)).setCircleColor(color); 
    ((LineDataSet) data.getDataSetByIndex(0)).setColors(dataColors); 
    data.getDataSetByIndex(0).setAxisDependency(YAxis.AxisDependency.LEFT); 
    ((LineDataSet) data.getDataSetByIndex(0)).setDrawCircles(false); 

    // no description text 
    chart.getDescription().setEnabled(false); 

    // mChart.setDrawHorizontalGrid(false); 
    // 
    // enable/disable grid background 
    chart.setDrawGridBackground(false); 

    // enable touch gestures 
    chart.setTouchEnabled(true); 

    // disable scaling and dragging 
    chart.setDragEnabled(false); 
    chart.setScaleEnabled(false); 
    chart.setAutoScaleMinMaxEnabled(false); 
    // if disabled, scaling can be done on x- and y-axis separately 
    chart.setPinchZoom(false); 
    chart.setBackgroundColor(Color.parseColor("#dddddd")); 

    // 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(false); 

    chart.getAxisLeft().setEnabled(false); 

    chart.getAxisRight().setEnabled(false); 
    chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM); 
    chart.getXAxis().setEnabled(false); 
    chart.getXAxis().setDrawLabels(true); 
    chart.getXAxis().setTextColor(context.getResources().getColor(R.color.colorPrimary)); 
    chart.getXAxis().setTypeface(Typeface.SANS_SERIF); 
    YAxis yAxis = chart.getAxisLeft(); 
    yAxis.setAxisMinimum(0f); 
    yAxis.setAxisMaximum(100f); 

    final String[] weeks = new String[52]; 

    for(int i = 0; i < weeks.length; i++) weeks[i] = "Week " + (i+1); 

    IAxisValueFormatter formatter = new IAxisValueFormatter() { 
     @Override 
     public String getFormattedValue(float value, AxisBase axis) { 
      return weeks[(int) value]; 
     } 
    }; 

    XAxis xAxis = chart.getXAxis(); 
    xAxis.setGranularity(1f); 
    xAxis.setValueFormatter(formatter); 
    // animate calls invalidate()... 
    chart.animateXY(2000,2500); 
} 

Ich bin nicht sicher, was ich falsch mache.

+0

, welche Version der Bibliothek sind Sie mit? – santalu

+0

@ santalu Version 3.0.1 –

+0

https://stackoverflow.com/a/41499401/7461132 könnte das Problem lösen. – Abhi

Antwort

0

Ich löste dies meine Offsets durch die Erhöhung:

chart.setViewPortOffsets(60, 0, 50, 60); 
Verwandte Themen