1

I am using the iOS-Charts framework and have the following chart:

enter image description here

I would like to change the position of the numbers (labels) to appear within the circle. Is it possible to achieve this?

José
  • 3,112
  • 1
  • 29
  • 42

1 Answers1

0

Inside LineChartRenderer.swift, increased the value of y when text is being drawn, I increase it by 30 in this example:

 if dataSet.isDrawValuesEnabled {
                    ChartUtils.drawText(
                        context: context,
                        text: formatter.stringForValue(
                            e.y,
                            entry: e,
                            dataSetIndex: i,
                            viewPortHandler: viewPortHandler),
                        point: CGPoint(
                            x: pt.x,
                            y: pt.y - CGFloat(valOffset) - valueFont.lineHeight  + 30),
                        align: .center,
                        attributes: [NSFontAttributeName: valueFont, NSForegroundColorAttributeName: dataSet.valueTextColorAt(j)])
 }

Result:

enter image description here

DevB2F
  • 4,674
  • 4
  • 36
  • 60
  • This looks great thanks, but I can't really change LineChartRenderer.swift since this would affect several charts. What would be the recommended way to achieve this change? – José Jul 05 '17 at 16:59
  • Make a new file called LineChartRendererCustom.swift and add all the code from LineChartRenderer.swift, then add the change I suggested above. Then use that custom renderer to a graph when you want to have the text inside the circle. – DevB2F Jul 05 '17 at 17:03
  • Yeah, I tried that. I created a custom LineChartRenderer in my project and copied over the implementation but got several "_xBounds' is inaccessible due to 'internal' protection level" errors. Using Cocoapods to include Charts and modifying the source is not really an option. – José Jul 05 '17 at 17:09
  • Yes you can make changes, see this: https://stackoverflow.com/questions/39735253/func-is-inaccessible-due-to-private-protection-level But if you are having problems with this I think you should ask a new question, since this is a different problem from what is asked in the original question. – DevB2F Jul 05 '17 at 17:13
  • fork the Charts repo, and edit the LineChartRenderer and add an extra boolean to turn it on and off. – dOM Sep 12 '17 at 13:38