customize Legend in MPAndroidChart

元气小坏坏 提交于 2020-02-24 11:35:27

问题


I am working in project in project that needs implement chart , I decide use MPAndroidChart its work just fine but I need some things done to be perfect to me

First can I change bars to specific image or it takes just images , as I have 3D image as bars in app design .

Second Can I put the legend to the right of chart in two line like image below and change legend text color

my java code

mChart = (BarChart) view.findViewById(R.id.chart);
        mChart.setDescription("");
        Legend mLegend = mChart.getLegend();
        //mLegend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER);
FillBarChart(mChart);
}


private void FillBarChart(BarChart barChart) {
        ArrayList<BarEntry> entries = new ArrayList<>();
        entries.add(new BarEntry(87f, 0));
        entries.add(new BarEntry(90f, 1));


        ArrayList<String> labels = new ArrayList<>();
        labels.add("Omeprazole 20 mg");
        labels.add("Esomeprazole 40 mg");

        BarDataSet dataSet = new BarDataSet(entries, " ");
        dataSet.setBarSpacePercent(40f);
        BarData barData = new BarData(labels, dataSet);
        dataSet.setColors(new int[]{R.color.omeprazole_color , R.color.esomeprazole_color} , getActivity());
        barChart.setData(barData);
        barChart.animateY(3000 , Easing.EasingOption.EaseOutBack );
    }


回答1:


You can do that if you create a custom Chart class and overwrite the init(). In this method, the Chart initializes a LegendRenderer and this is the point you need to get in and make changes.

Create a custom LegendRenderer and make sure that all the measurements are in place for those drawable. drawForm() method will allow you to draw the images instead of the default forms (Circle, Square, Line).



来源:https://stackoverflow.com/questions/35197312/customize-legend-in-mpandroidchart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!