首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建条形图并将数字更改为字符串值?

如何创建条形图并将数字更改为字符串值?
EN

Stack Overflow用户
提问于 2016-04-23 05:18:43
回答 1查看 361关注 0票数 0

我在ViewController中使用SwiftCharts框架创建了条形图,并在下面得到了图表和条形图,

图X值显示数字,但我需要更改字符串值(例如:1改为BMW,3更改为Audi,4更改为Bens,6更改为Maruthi),我的代码如下,

代码语言:javascript
复制
import UIKit
import SwiftCharts

class BarViewController: UIViewController {

    private var chart: Chart?

    let sideSelectorHeight: CGFloat = 50

    private func barsChart(horizontal horizontal: Bool) -> Chart {
        let tuplesXY = [(1, 8), (3, 2), (4, 4), (6, 6)]

        func reverseTuples(tuples: [(Int, Int)]) -> [(Int, Int)] {
            return tuples.map{($0.1, $0.0)}
        }

        let chartPoints = (horizontal ? reverseTuples(tuplesXY) : tuplesXY).map{ChartPoint(x: ChartAxisValueInt($0.0), y: ChartAxisValueInt($0.1))}

        let labelSettings = ChartLabelSettings(font: ExamplesDefaults.labelFont)

        let (axisValues1, axisValues2) = (
            0.stride(through: 10, by: 2).map {ChartAxisValueDouble(Double($0), labelSettings: labelSettings)},
            0.stride(through: 7, by: 1).map {ChartAxisValueDouble(Double($0), labelSettings: labelSettings)}
        )
        let (xValues, yValues) = horizontal ? (axisValues1, axisValues2) : (axisValues2, axisValues1)

        let xModel = ChartAxisModel(axisValues: xValues, axisTitleLabel: ChartAxisLabel(text: "Product Details", settings: labelSettings))
        let yModel = ChartAxisModel(axisValues: yValues, axisTitleLabel: ChartAxisLabel(text: "Sales Percentage", settings: labelSettings.defaultVertical()))

        let barViewGenerator = {(chartPointModel: ChartPointLayerModel, layer: ChartPointsViewsLayer, chart: Chart) -> UIView? in
            let bottomLeft = CGPointMake(layer.innerFrame.origin.x, layer.innerFrame.origin.y + layer.innerFrame.height)

            let barWidth: CGFloat = Env.iPad ? 60 : 30

            let (p1, p2): (CGPoint, CGPoint) = {
                if horizontal {
                    return (CGPointMake(bottomLeft.x, chartPointModel.screenLoc.y), CGPointMake(chartPointModel.screenLoc.x, chartPointModel.screenLoc.y))
                } else {
                    return (CGPointMake(chartPointModel.screenLoc.x, bottomLeft.y), CGPointMake(chartPointModel.screenLoc.x, chartPointModel.screenLoc.y))
                }
            }()
            return ChartPointViewBar(p1: p1, p2: p2, width: barWidth, bgColor: UIColor.blueColor().colorWithAlphaComponent(0.6))
        }

        let frame = ExamplesDefaults.chartFrame(self.view.bounds)
        let chartFrame = self.chart?.frame ?? CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height - sideSelectorHeight)
        let coordsSpace = ChartCoordsSpaceLeftBottomSingleAxis(chartSettings: ExamplesDefaults.chartSettings, chartFrame: chartFrame, xModel: xModel, yModel: yModel)
        let (xAxis, yAxis, innerFrame) = (coordsSpace.xAxis, coordsSpace.yAxis, coordsSpace.chartInnerFrame)

        let chartPointsLayer = ChartPointsViewsLayer(xAxis: xAxis, yAxis: yAxis, innerFrame: innerFrame, chartPoints: chartPoints, viewGenerator: barViewGenerator)

        let settings = ChartGuideLinesDottedLayerSettings(linesColor: UIColor.blackColor(), linesWidth: ExamplesDefaults.guidelinesWidth)
        let guidelinesLayer = ChartGuideLinesDottedLayer(xAxis: xAxis, yAxis: yAxis, innerFrame: innerFrame, settings: settings)

        return Chart(
            frame: chartFrame,
            layers: [
                xAxis,
                yAxis,
                guidelinesLayer,
                chartPointsLayer]
        )
    }

    private func showChart(horizontal horizontal: Bool) {
        self.chart?.clearView()

        let chart = self.barsChart(horizontal: horizontal)
        self.view.addSubview(chart.view)
        self.chart = chart
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.showChart(horizontal: false)

    }

提前谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-25 10:43:58

我没有使用过这个库,但是我认为作为元组输入的数据可以更改。像这样

代码语言:javascript
复制
("BMW":8), ("AUDI": 4)

元组的类型由它的值决定。

希望能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36806959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档