2017-07-19 200 views
0

我想在我的折線圖上使用x軸標籤(使用MPAndroidChart),但無論我做什麼,我都無法讓它們顯示。我有一個名爲setupChart方法,可處理一切爲圖表,這是什麼樣子:X軸標籤沒有顯示,使用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); 
} 

我不知道我做錯了。

+0

該庫的版本您使用的? – santalu

+0

@santalu版本3.0.1 –

+0

https://stackoverflow.com/a/41499401/7461132可能會解決問題。 – Abhi

回答

0

我解決了這個通過增加我的補償:

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