MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

setVisibleXRange() not working

Open nhjariwala opened this issue 11 years ago • 3 comments

I have Bar graph. It MUST display 8 bars. What I have tried but not get desire output. Please have look below code.

protected String[] mMonths = new String[] { "19/03/2015", "18/03/2015",
        "17/03/2015", "16/03/2015", "15/03/2015", "14/03/2015",
        "13/03/2015", "12/03/2015" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mChart = (BarChart) findViewById(R.id.chart1);
    mChart.setDrawBarShadow(true);
    mChart.setDrawValueAboveBar(true);
    mChart.setDescription("");
    mChart.setTouchEnabled(true);
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);
    mChart.setMaxVisibleValueCount(8);
    mChart.setPinchZoom(false);
    mChart.setDrawBarShadow(false);
    mChart.setDrawGridBackground(false);

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setSpaceBetweenLabels(2);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setLabelCount(8);
    leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setDrawGridLines(false);
    rightAxis.setLabelCount(8);

    setData(8, 50);
}

private void setData(int count, float range) {

    ArrayList<String> xVals = new ArrayList<String>();
    for (int i = 0; i < count; i++) {
        xVals.add(mMonths[i % 12]);
    }
    ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

    for (int i = 0; i < count; i++) {
        float mult = (range + 1);
        float val = (float) (Math.random() * mult);
        yVals1.add(new BarEntry(val, i));
    }

    BarDataSet set1 = new BarDataSet(yVals1, "Sale");
    set1.setColor(Color.rgb(247, 90, 83));

    ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
    dataSets.add(set1);

    BarData data = new BarData(xVals, dataSets);
    data.setValueTextSize(10f);

    mChart.setData(data);
    mChart.setVisibleXRange(8);

    Legend l = mChart.getLegend();
    l.setPosition(LegendPosition.BELOW_CHART_CENTER);
    l.setForm(LegendForm.SQUARE);
    l.setFormSize(9f);
    l.setTextSize(11f);
    l.setXEntrySpace(4f);
}

screen shot 2015-04-03 at 5 01 51 pm

All dates are not displaying. I need to display all the dates anyhow or bar graph need to be scrollable so that all the value can display.

Thanks

nhjariwala avatar Apr 03 '15 11:04 nhjariwala

In order to display all x-axis values you can do the following:

  • reduce the text size of your x-axis labels
  • reduce the length of your x-axis labels (e.g. 19/03/15 instead of 19/03/2015)
  • call xAxis.setSpaceBetweenLabels(0)

PhilJay avatar Apr 03 '15 12:04 PhilJay

@PhilJay Thank you for your prompt response ... It works perfect. Is it possible to display the date as it (means 19/03/2015) and chart is scrolling. Same code scrolling for 2 dataSet.

nhjariwala avatar Apr 03 '15 12:04 nhjariwala

Hi @PhilJay... is there any way to wrap words because i have categories name such as "Groceries & Medicine" in xAxis...

hardikamal avatar Aug 22 '15 13:08 hardikamal