Monday, 21 January 2019

Android stacked column and linechart with MPAndroidChart doesnt center

I have tried the following. Am using the MP android charts specifically the combined chart

  // draw bars behind lines
mChart.setDrawOrder(new CombinedChart.DrawOrder[]{
            CombinedChart.DrawOrder.BAR,
            CombinedChart.DrawOrder.LINE,

    });
        YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setDrawGridLines(false);
    rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setDrawGridLines(false);
    leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
    xAxis.setAxisMinimum(0f);
    xAxis.setGranularity(1f);

    CombinedData data = new CombinedData();
    data.setData(generateLineData());
    data.setData(setnewBarData());

    mChart.setData(data);

So the above generates

enter image description here

As from the image above the values of bar chart are not centralized example the moles first bar is much to the center. Also the line chart should be at the middle of each bar graph but it doesn't move to the middle

Where am i going wrong?

ADDITION methods that set bar data

THe setnewBarData method is

 public BarData setnewBarData() {


    ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

    for (int i = 0; i < itemcount + 1; i++) {
        float mult = (itemcount + 1);
        float val1 = (float) (Math.random() * mult) + mult / 3;
        float val2 = (float) (Math.random() * mult) + mult / 3;

        yVals1.add(new BarEntry(
                i,
                new float[]{val1, val2,},
                getResources().getDrawable(R.drawable.back_arrow_white)));
    }

    BarDataSet set1;

    set1 = new BarDataSet(yVals1, "Statistics Test 2018");
    set1.setDrawIcons(false);
    set1.setColors(new int[]{Color.rgb(61, 165, 255),Color.rgb(128, 133, 233)});

    set1.setStackLabels(new String[]{"Births", "Divorces"});
    set1.setAxisDependency(YAxis.AxisDependency.LEFT);

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

    BarData data = new BarData(dataSets);
    data.setValueTextColor(Color.BLACK);
    data.setBarWidth(0.5f);
    return data;
}

THe method to generate line data is

    private LineData generateLineData() {

    LineData d = new LineData();

    ArrayList<Entry> entries = new ArrayList<Entry>();

    for (int index = 0; index < itemcount; index++)
        entries.add(new Entry(index + 0.5f, getRandom(15, 5)));

    LineDataSet set = new LineDataSet(entries, "Percentage Compliance");
    set.setColor(Color.rgb(240, 238, 70));
    set.setLineWidth(2.5f);
    set.setCircleColor(Color.rgb(240, 238, 70));
    set.setCircleRadius(5f);
    set.setFillColor(Color.rgb(240, 238, 70));
    set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    set.setDrawValues(true);
    set.setValueTextSize(10f);
    set.setValueTextColor(Color.rgb(240, 238, 70));

    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    d.addDataSet(set);

    return d;
}



from Android stacked column and linechart with MPAndroidChart doesnt center

No comments:

Post a Comment