在ios-charts中如何设置y轴的最大值?

时间:2015-07-13 16:52:40

标签: ios swift mpandroidchart ios-charts

我正在使用ios-charts显示一个具有如下值的水平条形码:

 var dataEntries: [BarChartDataEntry] = []
        let dataEntry = BarChartDataEntry(value: self.deductible!.deductiblePaid, xIndex: 0)
        dataEntries.append(dataEntry)
        let chartDataSet =    BarChartDataSet(yVals: dataEntries, label: "Deductible")
        let chartData = BarChartData( xVals: dataPoints, dataSet: chartDataSet)


        self.horizontalBarChart!.data = chartData
        self.horizontalBarChart!.leftAxis.customAxisMax = 3000.0
        self.horizontalBarChart!.leftAxis.startAtZeroEnabled = false

        chartDataSet.colors = ChartColorTemplates.vordiplom()

我试图设置YAxis的最大值但不影响图形。 修改:请在此处查看屏幕截图:http://postimg.org/image/ogoj2t02f/ 我需要的是条形图应该显示高达3000的上限(或动态指定的量)而不是计算的上限。

我改变了一些代码来制作它,这是我目前所拥有的:

self.horizontalBarChart!.noDataText = "You need to provide data for the chart."

        self.horizontalBarChart!.setVisibleYRangeMaximum(3000, axis: ChartYAxis.AxisDependency.Left)
        self.horizontalBarChart!.descriptionText = ""
        var dataEntries: [BarChartDataEntry] = []
        let dataEntry = BarChartDataEntry(value: self.deductible!.deductiblePaid, xIndex: 0)
        dataEntries.append(dataEntry)
        let chartDataSet =    BarChartDataSet(yVals: dataEntries, label: " ")
        self.horizontalBarChart!.noDataText = "You need to provide data for the chart."
        self.horizontalBarChart!.setViewPortOffsets(left: 0, top: 0, right: 0, bottom: 0);
        let chartData = BarChartData( xVals: dataPoints, dataSet: chartDataSet)

        self.horizontalBarChart!.autoScaleMinMaxEnabled = false
        self.horizontalBarChart!.data = chartData
        self.horizontalBarChart!.leftAxis.axisMaximum = 1600
        self.horizontalBarChart!.leftAxis.axisMinimum = 00
        self.horizontalBarChart!.rightAxis.axisMaximum = 1600
        //self.horizontalBarChart!.leftAxis.customAxisMax = 3000.0
        //self.horizontalBarChart!.leftAxis.startAtZeroEnabled = false

        chartDataSet.colors = ChartColorTemplates.vordiplom()
        self.horizontalBarChart!.xAxis.labelPosition = .Top

        self.horizontalBarChart!.backgroundColor = UIColor(red: 189/255, green: 195/255, blue: 199/255, alpha: 1)
        self.horizontalBarChart!.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)

如果我不清楚并且您需要更多详细信息,请告诉我。这比我认为的更具挑战性!这里的任何帮助将不胜感激。

第二次编辑:我现在有以下代码:

 func setHorizontalBarChartData() {

        self.horizontalBarChart!.descriptionText = ""
        var dataEntries: [BarChartDataEntry] = []
        let dataEntry = BarChartDataEntry(value: 900, xIndex: 0)
        dataEntries.append(dataEntry)
        let chartDataSet =    BarChartDataSet(yVals: dataEntries, label: " ")
        self.horizontalBarChart!.noDataText = "You need to provide data for the chart."
        //self.horizontalBarChart!.setViewPortOffsets(left: 0, top: 0, right: 0, bottom: 0);
        let deductible = [" "]
        let chartData = BarChartData( xVals: deductible, dataSet: chartDataSet)
        self.horizontalBarChart!.data = chartData
        self.horizontalBarChart!.leftAxis.startAtZeroEnabled = false
        self.horizontalBarChart!.rightAxis.startAtZeroEnabled = false
        self.horizontalBarChart!.rightAxis.customAxisMax = 3000
        self.horizontalBarChart!.rightAxis.customAxisMin = 10
        chartDataSet.colors = ChartColorTemplates.vordiplom()
        self.horizontalBarChart!.xAxis.labelPosition = .Top
        self.horizontalBarChart!.backgroundColor = UIColor(red: 189/255, green: 195/255, blue: 199/255, alpha: 1)
        self.horizontalBarChart!.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
    }

我仍然得到同样的事情:http://postimg.org/image/ogoj2t02f/ 我需要的只是一个价值高达3000的酒吧和一个高达900的条形图。 你能帮忙吗?

2 个答案:

答案 0 :(得分:4)

如果未按预期显示上下y轴值,请确保

  1. 您在y轴的左右两侧设置了customAxisMaxcustomAxisMin
  2. 在设置图表视图的customAxisMax之前设置了customAxisMax.data
  3. 你设置x和y轴' startAtZeroEnabled为假。在设置图表视图的startAtZeroEnabled之前设置.data
  4. 花了我几个小时来弄明白这些。希望它可以为你节省一些。

答案 1 :(得分:1)

customAxisMax应该可以帮到你。它在我身边很好。

您可能缺少的是,您正在设置leftAxis,但您应该知道还有rightAxis。我不确定你在用什么。

如果您看一下BarLineChartViewBase.swift',则

中有代码

internal override func calcMinMax

_leftAxis.axisMaximum = !isnan(_leftAxis.customAxisMax) ? _leftAxis.customAxisMax : (maxLeft + topSpaceLeft)
_rightAxis.axisMaximum = !isnan(_rightAxis.customAxisMax) ? _rightAxis.customAxisMax : (maxRight + topSpaceRight)
_leftAxis.axisMinimum = !isnan(_leftAxis.customAxisMin) ? _leftAxis.customAxisMin : (minLeft - bottomSpaceLeft)
_rightAxis.axisMinimum = !isnan(_rightAxis.customAxisMin) ? _rightAxis.customAxisMin : (minRight - bottomSpaceRight)

设置后会显示customAxisMaxcustomAxisMin。您需要仔细检查您正在使用的轴。

编辑第二次修改:

我只是更改了演示代码,这是你想要的吗?

ios-charts演示中的代码,请在您的演示代码中尝试:

- (void)setDataCount:(int)count range:(double)range
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];

    [xVals addObject:months[0]];

    NSMutableArray *yVals = [[NSMutableArray alloc] init];

    [yVals addObject:[[BarChartDataEntry alloc] initWithValue:900 xIndex:0]];

    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"DataSet"];
    set1.barSpace = 0.35;

    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];

    BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
    _chartView.leftAxis.startAtZeroEnabled = NO;
    _chartView.rightAxis.startAtZeroEnabled = NO;
    _chartView.rightAxis.enabled = NO;
    _chartView.leftAxis.customAxisMax = 3000;
    _chartView.leftAxis.customAxisMin = 10;

    _chartView.data = data;
}

截图: enter image description here