Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legend with NSAttributedString and multiple lines on Vertical #1193

Closed
soniacasas opened this issue Jun 29, 2016 · 5 comments
Closed

Legend with NSAttributedString and multiple lines on Vertical #1193

soniacasas opened this issue Jun 29, 2016 · 5 comments

Comments

@soniacasas
Copy link

Hi!
First of all thanks for this amazing job!

Is there a way to use NSAttributedString with the legend to have multiple colors and font sizes? I was trying to use setCustoms with [NSObjects] but reading the doc it says it's just a bridge, and it doesn't work.

let mutable = NSMutableAttributedString(string: "Testing Mutable")
mutable.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(20), range:  NSMakeRange(0, 5))
mutable.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSMakeRange(2, 3))
chartView.legend.setCustom(colors: [UIColor.redColor(),UIColor.orangeColor()], labels: [mutable,mutable])

Also i'm using the legend in a pie chart and is vertically positioned, the documentation says that word wrap is just usefull on horizontal, what can I do?

this is the sample code

private func configChartView(){

        chartView.drawSliceTextEnabled = false
        chartView.setDescriptionTextPosition(x: 0, y: 0)
        chartView.drawSlicesUnderHoleEnabled = false
        chartView.setExtraOffsets(left: 0, top: 0, right: 0, bottom: 0)
        chartView.userInteractionEnabled = false

        customizeLegendWithColors()

    }

    private func customizeLegendWithColors() {

        chartView.legend.enabled = true
        chartView.legend.wordWrapEnabled = true
        chartView.legend.direction = .LeftToRight
        chartView.legend.form = .Circle
        chartView.legend.horizontalAlignment = .Right
        chartView.legend.verticalAlignment = .Center
        chartView.legend.orientation = .Vertical
        chartView.legend.font = UIFont.extraBigLargeFontBold()
        chartView.legend.textColor = UIColor(red: 74, green: 74, blue: 74)
        chartView.legend.calculatedLabelBreakPoints = [true, true]
        chartView.legend.maxSizePercent = 0.3

    }

    private func updateChart() {

        let data = [ChartDataEntry(value: 0.75, xIndex: 0),
                    ChartDataEntry(value: 0.25, xIndex: 1)]

        let pieChartDataSet = PieChartDataSet(yVals: data, label: "")

        pieChartDataSet.drawValuesEnabled = false

        let pieChartData = PieChartData(xVals: ["962962 Total listened articles","271604 Total read articles"], dataSet: pieChartDataSet)
        chartView.data = pieChartData
        pieChartDataSet.colors = chartColors as! [NSUIColor]
    }

I'm getting this

image

@liuxuan30
Copy link
Member

well, it seems like the legend canno't satisfy your needs... Would you please take a look at ChartLegend code to know how it renders? I want to help but it's a lot of work to answer your questions.

@soniacasas
Copy link
Author

soniacasas commented Jun 30, 2016

this is what i want to achieve, the legend is in 2 lines, and, as it has 2 colors an NSAttributedString could work
image

I'll take a look to ChartLegend, any help would be great!

Thanks

@chuynadamas
Copy link
Contributor

@soniacasas
Hi :) I've similar problem some time ago, I hope this help you.

NSattributedString support multiple formats, please check the example below:

_labelns = NSMutableAttributedString()

for index in 0 ..< arrayData.count {

                    let word = arrayData[index] as! String
                    let newAttString = NSMutableAttributedString(string: "\(word)\n")

                    if index % 2 != 0 {
                        newAttString.addAttribute(NSForegroundColorAttributeName, value: self.textColor, range: NSMakeRange(0, word.characters.count))
                    } else {
                        newAttString.addAttribute(NSForegroundColorAttributeName, value: UIColor.grayColor(), range: NSMakeRange(0, word.characters.count))
                    }

                    _labelns!.appendAttributedString(newAttString)

                }

arrayData is an array with all the labels that you want to apply format, as you can see on my example this just change the color between the odd and even, in your case you need a different way to determine the case for every label, but you can add as many attributes as you want for every string, and then append to the legend.

@nicard
Copy link

nicard commented May 22, 2017

Hello, I'm answering in this post, because actually LegendEntry doesn't support custom font.

I want add custom font in my legend, using attributed string, but it seem's allow us do it.

Someone can help me?

@mayckonx
Copy link

The same problem here.

I tried to add custom font using LegendEntry, but it accept only string.

@jjatie jjatie closed this as completed Apr 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants