Charts: ์‚ฌ์šฉ์ž ์ •์˜ Y ๊ฐ’ ํฌ๋งทํ„ฐ

์— ๋งŒ๋“  2016๋…„ 04์›” 12์ผ  ยท  11์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: danielgindi/Charts

์šฐ์„ , ์ด IOS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ต‰์žฅํ•ฉ๋‹ˆ๋‹ค! ๊ฐ์‚ฌ ํ•ด์š”.

์‹œ๊ฐ„ ํ˜•์‹(mn:ss)์œผ๋กœ BarChart์— Y ๊ฐ’์„ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

IOS ๋ฌธ์„œ์—์„œ "ChartDefaultXAxisValueFormatter"๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋ณด์•˜์ง€๋งŒ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ์ผ์„ ์„ฑ์ทจํ•œ ์‚ฌ๋žŒ์ด ์žˆ์Šต๋‹ˆ๊นŒ?

์•Œ๋ ‰์‹œ์Šค

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

IAxisValueFormatter ์ƒ์†ํ•˜์—ฌ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

import Foundation
import Charts
class YAxisValueFormatter: NSObject, IAxisValueFormatter {

    let numFormatter: NumberFormatter

    override init() {
        numFormatter = NumberFormatter()
        numFormatter.minimumFractionDigits = 1
        numFormatter.maximumFractionDigits = 1

        // if number is less than 1 add 0 before decimal
        numFormatter.minimumIntegerDigits = 1 // how many digits do want before decimal
        numFormatter.paddingPosition = .beforePrefix
        numFormatter.paddingCharacter = "0"
    }

    /// Called when a value from an axis is formatted before being drawn.
    ///
    /// For performance reasons, avoid excessive calculations and memory allocations inside this method.
    ///
    /// - returns: The customized label that is drawn on the axis.
    /// - parameter value:           the value that is currently being drawn
    /// - parameter axis:            the axis that the value belongs to
    ///

    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return numFormatter.string(from: NSNumber(floatLiteral: value))!
    }
}

YAxisValueFormatter ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ์ฒด๋ฅผ ๊ทธ๋ž˜ํ”„ leftAxis์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

    // Number formatting of YAxis
    chartView.leftAxis.valueFormatter = YAxisValueFormatter()

๋ชจ๋“  11 ๋Œ“๊ธ€

๋ˆ„์  ๋ง‰๋Œ€ ์ฐจํŠธ ๋ณด๊ธฐ ์ปจํŠธ๋กค๋Ÿฌ์˜ ์˜ˆ๋ฅผ ๋ณด์…จ์Šต๋‹ˆ๊นŒ? ๋‚˜๋Š” ๋‹น์‹ ์ด ํฌ๋งทํ„ฐ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ๊ณณ๊ณผ ๋น„์Šทํ•˜๋‹ค๊ณ  ์ƒ์ƒํ•ฉ๋‹ˆ๊นŒ?

        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.maximumFractionDigits = 1;
        formatter.negativeSuffix = @" $";
        formatter.positiveSuffix = @" $";

        BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];
        [data setValueFormatter:formatter];

        _chartView.data = data;

@dxclancy๊ฐ€ ์ง€์ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ์ˆซ์ž ํฌ๋งทํ„ฐ์ด๋ฏ€๋กœ ์ˆซ์ž์™€ ๋‚ ์งœ ํฌ๋งทํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์•ˆ๋…•ํ•˜์„ธ์š”,

๋‹ต๋ณ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋ถˆํ–‰ํžˆ๋„ leftAxis.valueformatter๋Š” NSNumberFormatter๋งŒ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

์‹ ์†ํ•œ ํŒŒ์ผ์—์„œ์™€ ๊ฐ™์ด ChartDefaultXAxisValueFormatter๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜์ง€๋งŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

ํ•„์š”ํ•œ ๊ฒƒ์€ ์™ผ์ชฝ(Y) ์ถ•์„ ์‹œ๊ฐ„ ex = 1:02:58๋กœ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์•Œ๋ ‰์‹œ์Šค

์•ˆ๋…•ํ•˜์„ธ์š”, ๋‹น์‹ ์ด ์›ํ•˜๋Š” ๊ฒƒ์€ NSDateFormatter๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๊นŒ?

๋‚˜๋Š” ์ด๊ฒƒ์„ ์ •์˜์—์„œ ๋ณธ๋‹ค:
๊ณต๊ฐœ var valueFormatter: ChartXAxisValueFormatter?

๋”ฐ๋ผ์„œ ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜์—์„œ ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<strong i="9">@interface</strong> MyChartViewController: < ChartXAxisValueFormatter>

๋˜๋Š”

<strong i="13">@interface</strong> MyCustomFormatter: NSObject < ChartXAxisValueFormatter>

๋˜๋Š” ์‹ ์† ๋“ฑ๊ฐ€๋ฌผ.

๊ทธ๋Ÿฌ๋‚˜ yVals๋ฅผ ํฌ๋งทํ•˜๋ ค๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋“ค๋ฆฝ๋‹ˆ๋‹ค. ์ฆ‰, NSNumberFormatter์—๋งŒ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ?

์‹ค์ œ๋กœ #742์—์„œ ์ด์™€ ์œ ์‚ฌํ•œ ๋ฌธ์ œ์— ๋Œ€ํ•ด ๋…ผ์˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ˜„์žฌ @danielgindi ์˜ ์ž…์žฅ์€ ์ด๊ฒƒ์ด ์ž˜๋ชป๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋‚˜๋Š” ๋‹น์‹ ์—๊ฒŒ 3 ๊ฐ€์ง€ ์˜ต์…˜์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

  • ChartXAxisValueFormatter๊ฐ€ ์ถฉ๋ถ„ํ•˜๋ฉด ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•˜์‹ญ์‹œ์˜ค. ๋„์™€๋“œ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ stringFromNumber๋ฅผ ์žฌ์ •์˜ํ•˜๋Š” NSNumberFormatter์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํ•ด๋‹น ๊ฐ’์„ NSDateFormatter์— ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ๊ณ ์œ ํ•œ ์‚ฌ์šฉ์ž ์ง€์ • ์„œ์‹์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ํŽธํ•˜๋‹ค๋ฉด #742์˜ pull ์š”์ฒญ์„ ์ž์‹ ์˜ ํฌํฌ์— ๋ณ‘ํ•ฉํ•˜๊ณ  NSDateFormatter๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์›ํ•˜๋Š” ๋Œ€๋กœ ์‹œ๊ฐ„์„ ํ˜•์‹ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ChartXAxisValueFormatter์—์„œ ๋ชจ๋ธ๋ง๋œ ChartYAxisValueFormatter๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‚˜๋Š” ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ํ›Œ๋ฅญํ•œ ์˜ต์…˜์ด ๋งŽ์ด ์žˆ๋Š” ์ด ๋ฉ‹์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฒ˜์Œ ์ ‘ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„๋ช…ํ•œ ๊ฒƒ์„ ๋†“์น˜๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ท€ํ•˜์˜ ์ œ์•ˆ์— ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์ถ• ํฌ๋งทํ„ฐ๋ฅผ ํฌ๋งทํ„ฐ๋กœ ์˜ฎ๊ธฐ๋ ค๋ฉด ์‹ ์ค‘ํ•˜๊ฒŒ ์ƒ๊ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ˆซ์ž๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ˆซ์ž ํฌ๋งทํ„ฐ๊ฐ€ ์‚ฌ์šฉํ•  ํฌ๋งทํ„ฐ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๊ฐ’๊ณผ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ํ‘œ์‹œํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํฌ๋งทํ„ฐ์˜ ์ถœ์ฒ˜๋Š” ํ™•์‹คํ•œ ์ˆซ์ž์ž…๋‹ˆ๋‹ค.

์˜ค๋ฒ„๋ผ์ด๋“œ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ๋งค๋ ฅ์ฒ˜๋Ÿผ ์ž‘๋™ํ–ˆ์Šต๋‹ˆ๋‹ค!
๊ณ ๋งˆ์›Œ ์–˜๋“ค์•„, ๋„ˆ๋Š” ์ตœ๊ณ ์•ผ :)

NSNumberFormatter ๋Š” ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์ด _ํ•ญ์ƒ_ ์ˆซ์ž๋ผ๋Š” ์‚ฌ์‹ค ๋•Œ๋ฌธ์— NSFormatter ๋ณด๋‹ค ๋” ์ •ํ™•ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ NSFormatter ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ป๋Š” ๊ฒƒ์€ ์—†์Šต๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ์ถœ๋ ฅ์„ ์›ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์€ ์ž…๋ ฅ์ด ์ˆซ์ž๋ผ๋Š” ์‚ฌ์‹ค์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฉฐ ๋งค์šฐ ํŠน๋ณ„ํ•œ ๋ฐฉ์‹์œผ๋กœ ํ•ด๋‹น ์ˆซ์ž์˜ ํ˜•์‹์„ ์ง€์ •ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๋Š” ๊ฐ’๊ณผ ํ•จ๊ป˜ ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์ž์ฒด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์•ž์œผ๋กœ ๋‚˜์•„๊ฐˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค( ChartYAxisValueFormatter ์ ‘๊ทผ ๋ฐฉ์‹).

์ด ๋ฌธ์ œ๋Š” Y์ถ• ๊ฐ’ ํฌ๋งทํ„ฐ์— ๋Œ€ํ•œ ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•  ๋•Œ๊นŒ์ง€ ์—ด๋ ค ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Charts 3.0(v3 ๋ถ„๊ธฐ)์—์„œ ๊ตฌํ˜„๋จ

IAxisValueFormatter ์ƒ์†ํ•˜์—ฌ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

import Foundation
import Charts
class YAxisValueFormatter: NSObject, IAxisValueFormatter {

    let numFormatter: NumberFormatter

    override init() {
        numFormatter = NumberFormatter()
        numFormatter.minimumFractionDigits = 1
        numFormatter.maximumFractionDigits = 1

        // if number is less than 1 add 0 before decimal
        numFormatter.minimumIntegerDigits = 1 // how many digits do want before decimal
        numFormatter.paddingPosition = .beforePrefix
        numFormatter.paddingCharacter = "0"
    }

    /// Called when a value from an axis is formatted before being drawn.
    ///
    /// For performance reasons, avoid excessive calculations and memory allocations inside this method.
    ///
    /// - returns: The customized label that is drawn on the axis.
    /// - parameter value:           the value that is currently being drawn
    /// - parameter axis:            the axis that the value belongs to
    ///

    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return numFormatter.string(from: NSNumber(floatLiteral: value))!
    }
}

YAxisValueFormatter ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ์ฒด๋ฅผ ๊ทธ๋ž˜ํ”„ leftAxis์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

    // Number formatting of YAxis
    chartView.leftAxis.valueFormatter = YAxisValueFormatter()

์œ„์—์„œ ์„ค๋ช…ํ•œ ๋Œ€๋กœ IAxisValueFormatter๋ฅผ ์ƒ์†ํ•˜๋ฉด ๋‚ด ProjectName-Swift.h ํŒŒ์ผ์— "'IChartAxisValueFormatter'๋ผ๋Š” ํ˜•์‹ ๋˜๋Š” ํ”„๋กœํ† ์ฝœ์ด ์—†์Šต๋‹ˆ๋‹ค."๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰