Kaitlyn Hanrahan
Kaitlyn Hanrahan

Reputation: 789

How can I have a multiple line Label in a PieChart in AChartEngine?

I have a pie chart using AChartEngine that I'm very happy with. I've chosen to style it without a Legend and with Labels (the text connected to its corresponding Pie wedge with a little line) formatted like "Example Data Point - 25%"

I get this Label string via:

mSeries.add(name + " - " + Double.toString(value), value);

But often the name is too long so the value doesn't display. I get "...". Since the value is important, I want to put it on a new line so it will always show.

I'm picturing Labels that look something like:

Short Name
10%

Too Long of A Na...
25%

Also fine:

Too Long of A
Name
25%

Or:

Too Long of A
Name - 25%

If I could wrap long names to new lines, that would be even better! But I really am mostly concerned with getting the Value to always display.

I tried to add a new line with \n or System.getProperty("line.separator") but the PieChart still renders the Label as one line. The \n is not getting displayed as text, it's like it's not even there. According to other Questions, this supposedly works on other Chart types, but no one is claiming it works for Pies, which it doesn't seem to based on my efforts.

I've also played with other rendering options built into AChartEngine. Such as 'setDisplayValues(true)', which puts the values right over the pie wedges. I don't like how that looks, as my pie has some tiny wedges, so the values get rendered on top of each other.

Any insight on how I can make the Labels on AChartEngine PieChart multi-line (or any other way to force the value to display) would be very appreciated! Thank you so much to anyone who can help!

Upvotes: 1

Views: 1124

Answers (1)

Kaitlyn Hanrahan
Kaitlyn Hanrahan

Reputation: 789

It's not what I really wanted, but since a few months have gone by, I've come up with an alternative. For posterity, I will leave it here as an answer to my own question but not accept it in case someone out there does someday share a real solution.

Since I couldn't find a way to ensure that the labels on my pie chart always showed percent values, I have the info Toast when a wedge is clicked. Here is the simple enough code:

    private void addPieClickListeners(final GraphicalView mChartView){
    mChartView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();

            if (seriesSelection == null) {
                // Do Nothing, Possible Toast:
                // Toast.makeText(context,"No chart element was clicked",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(context, 
                        Integer.toString((int)Math.round(seriesSelection.getValue())) +
                        "% " + 
                        mSeries.getCategory(seriesSelection.getPointIndex()),
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

I call this method, along with my other functions to set up my Pie Chart, in onCreateView(), since this is in a Fragment.

Cheers.

Upvotes: 1

Related Questions