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

In ios-charts how to set the maximum value for y-axis?

我正在使用 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 的条形图。 你能帮忙吗?

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;
}

截图:

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

  1. 您在 y 轴的左右两侧设置了 customAxisMaxcustomAxisMin
  2. 您在设置图表视图的 .data 之前设置了 customAxisMaxcustomAxisMax
  3. 您将 x 轴和 y 轴的 startAtZeroEnabled 都设置为 false。在设置图表视图的 .data 之前设置 startAtZeroEnabled

我花了几个小时才弄明白这些。希望它可以为您节省一些。