ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.6k stars 6k forks source link

How to set different values for data and legend entries #2569

Open SeRG1k17 opened 7 years ago

SeRG1k17 commented 7 years ago

I'm trying to set different values for dataset entry and legend entry, but it does not work.

Need: PieChartDataEntry: number ChartLegendEntry: text

Extra: Also, I can not find how to swap position of percentages and value labels? How to hide the number and percent value in a small slice?

Thank you in advance!

2017-06-29 16 49 21 2017-06-29 16 49 09

for (int i = 0; i < parties.count; i++) {

    float value = (arc4random_uniform(mult) + mult / 5);
    [values addObject:[[PieChartDataEntry alloc] initWithValue:value
                                                         label:[NSString stringWithFormat:@"%.0f", value]]];

    ChartLegendEntry *entry = [[ChartLegendEntry alloc] init];
    entry.label = parties[i];
    [legendEntries addObject:entry];
}

_pieChartView.legend.entries = legendEntries;

PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:values label:@""];

dataSet.colors = colors;

PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
liuxuan30 commented 7 years ago

hiding small slices there is a PR for it, not merged yet. Not quite understand

set different values for dataset entry and legend entry

but check valueFormatter for different values, legend please refer ChartsDemo. If it can't, maybe you need to override them to do your own.

SeRG1k17 commented 7 years ago

i am trying set labels for legend: NSArray * parties = @[ @"Job Abandonment", @"Absenteeism", @"Internal Change", @"Did not like job", @"Other Job", @"Personal Problems", @"Other", @"Moved Away" ];

for (int i = 0; i < _pieChartView.legend.entries.count - 1; i++) {

    ChartLegendEntry *entry = _pieChartView.legend.entries[i];
    entry.label = parties[i];
}

Note: _pieChartView.legend.entries.count - 1 because last entry it`s legend description(name) but in legend showing numbers from:

NSMutableArray *legendEntries = [[NSMutableArray alloc] init];
for (int i = 0; i < parties.count; i++) {

    float value = (arc4random_uniform(mult) + mult / 5);
    [values addObject:[[PieChartDataEntry alloc] initWithValue:value
                                                         label:[NSString stringWithFormat:@"%.0f", value]]];
}

PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:values label:@""];
PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
_pieChartView.data = data;

It`s possible place percent(%) label under value label(number) (see my second picture)?

SeRG1k17 commented 7 years ago

please, reopen it!

liuxuan30 commented 7 years ago

formatter should support prefix and suffix, have you checked? Why you ask to reopen?

SeRG1k17 commented 7 years ago

@liuxuan30 Thanks for all the tips, i set custom legend. But, one more thing(see screenshot). How to swap these numbers? c14c9f965f

SeRG1k17 commented 7 years ago

I found solution, thank you. Need set chartView.usePercentValuesEnabled = NO; and use PieChartDataEntry value and label for manually calculating percents(%)

banagale commented 7 years ago

Sounds like this should be revised as a documentation issue or closed?

SeRG1k17 commented 7 years ago

@banagale @liuxuan30 it`s code swap percent(%) and values labels: Extra: And hide value and percent labels if less then minimum showing value

for (int i = 0; i < self.chartData.count - 1; i++) {

    SPPair *dataPair = self.chartData[i];
    NSInteger value = [((NSNumber *)dataPair.value) integerValue];
    NSString *label = @"";

    if (total > 0 && value * 100 / total >= minimumShowingValue) {

        CGFloat percent = value * 100.0f / total;
        label = [NSString stringWithFormat:@"%ld\n%.0f%%", (long)value, percent];
    }

    [values addObject:[[PieChartDataEntry alloc] initWithValue:value
                                                         label:label]];

    ChartLegendEntry *entry = [[ChartLegendEntry alloc] initWithLabel:dataPair.key
                                                                 form:ChartLegendFormCircle
                                                             formSize:16
                                                        formLineWidth:0
                                                    formLineDashPhase:0
                                                  formLineDashLengths:0
                                                            formColor:self.colors[i]];

    [legendEntries addObject:entry];
}

[_pieChartView.legend setCustomWithEntries: legendEntries];

PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:values label:@""];
dataSet.colors = self.colors;

PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
[data setValueFont:[UIFont systemFontOfSize:13.f]];
[data setValueTextColor:UIColor.whiteColor];
[data setDrawValues:false];

_pieChartView.data = data;
sunilkr123 commented 5 years ago

how to do the same thing with Swift4. and in my case how do I show values in %. Like 20%, `60%.

import UIKit import Charts

class ViewController: UIViewController {

@IBOutlet weak var pieChart: PieChartView!

@IBOutlet weak var iosStepper: UIStepper!
@IBOutlet weak var macStepper: UIStepper!

var iosDataEntry = PieChartDataEntry(value: 100) // 30.0
var macDataEntry = PieChartDataEntry(value: 80)  // 20.0
var androidEntry = PieChartDataEntry(value: 60)  // 30.0
var cEntry = PieChartDataEntry(value: 40)
var cCCEntry = PieChartDataEntry(value: 80)

var numberOfDownloadsDataEntries = [PieChartDataEntry]()

override func viewDidLoad() {
    super.viewDidLoad()

    pieChart.chartDescription?.text = "Novastreams"

    iosDataEntry.label = "iOS"

    macDataEntry.label = "macOS"

    androidEntry.label = "Android"

    cEntry.label = "C#"

    cCCEntry.label = "C####"

    numberOfDownloadsDataEntries = [iosDataEntry, macDataEntry,androidEntry, cEntry, cCCEntry]

    updateChartData()

}

@IBAction func changeiOS( sender: UIStepper) { iosDataEntry.value = sender.value updateChartData() } @IBAction func changeMac( sender: UIStepper) { macDataEntry.value = sender.value updateChartData() }

func updateChartData() {

    let chartDataSet = PieChartDataSet(values: numberOfDownloadsDataEntries, label: nil)
    let chartData = PieChartData(dataSet: chartDataSet)

    let colors = [#colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1),#colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1),#colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1),#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1),#colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)]
    chartDataSet.colors = colors as! [NSUIColor]

    pieChart.data = chartData

}

#