4

I am using the pie chart view from achartengine's tutorial. Here is what i want.

enter image description here

I want the legends i.e. pass/fail to be displayed to the right of the pie chart as shown in the figure. In the demo examples of achartengine, they are bottom aligned. How to get them to the right? Please help!

Shafi
  • 1,368
  • 10
  • 24
  • I added a transparent frameLayout on top of it, and got what i achieved. – Shafi Jan 18 '13 at 08:44
  • How did you do that? Can you be more specific?some sample code?? – oikonomopo Jan 20 '13 at 16:46
  • If you want post your solution in my question in order to have it in StackOverflow.Many users may want it... – oikonomopo Jan 20 '13 at 16:57
  • @ Shafi could please post a sample code for where to add the frame layout. – ARP Mar 11 '15 at 05:19
  • Unfortunately, I do not have access to the code right now. But from what I remember, I did the following: In the layout file, add a framelayout, make it transparent (our piechart will be behind this layout) and add the legends to this layout via code. Add the piechart as a view(not as intent). Hope this helps! – Shafi Mar 11 '15 at 13:16

1 Answers1

0

This code below worked for me. But I think it's better creating a RecyclerView as a legend, instead of using the one provided by MPAndroidChart class. What I did:

  1. Create the RecyclerView and celda_recycler_legend.xml:
  2. Create an AdapterLegend.java script, which extents RecyclerView.Adapter class, to be able to use a RecyclerView with the legends. On this script, I added a LegendViewHolder instance, which extents RecyclerView.ViewHolder, with the celda_recycler_legend.xml configured.

AdapterLegend

 class AdapterLegend extends RecyclerView.Adapter {

    private ArrayList<String> legends;
    private ArrayList<Integer> colors;

    public AdapterLegend(ArrayList legends, ArrayList<Integer> colors) {
        this.legends = legends;
        this.colors = colors;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new LegendViewHolder(layoutInflater.inflate(R.layout.celda_recycler_legend, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        LegendViewHolder legendViewHolder = (LegendViewHolder) holder;
        legendViewHolder.LoadLegend(legends.get(position), colors.get(position));
    }

    @Override
    public int getItemCount() { return legends.size(); }
  }

LegendViewHolder

class LegendViewHolder extends RecyclerView.ViewHolder {

    private final TextView legendText;
    private final ImageView legendColorLabel;

    public LegendViewHolder(@NonNull View itemView) {
        super(itemView);

        legendText = itemView.findViewById(R.id.legendTextView);
        legendColorLabel = itemView.findViewById(R.id.legendLabelColor);
    }

    public void LoadLegend(String legend, int legendColor){
        legendText.setText(legend);
        legendColorLabel.setBackgroundColor(legendColor);
    }
}
  1. I set the RecyclerView with an AdapterLegend instance

       legendRecyclerView = recountDataLayout.findViewById(R.id.chartLegendRecycler);
       legendRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    

https://github.com/PhilJay/MPAndroidChart/issues/2478#issuecomment-378566623

Hope this works for you!

  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 04 '21 at 22:34
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – M123 Sep 07 '21 at 11:25
  • 1
    Hello @Mansi I didn't know that. Now I'm going to correct my answer thanks! – Patito00Game Sep 07 '21 at 17:08