userx
userx

Reputation: 1121

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

I am using ios-charts to display a Horizontal BarChart with one value like this:

 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()

I have tried to set the maximum value for the YAxis but its not effecting the graph. Edit: Please see the screenshot here : http://postimg.org/image/ogoj2t02f/ What I need is the bar should show the upper limit upto 3000 (or an amount specified dynamically) instead of the calculated upper limit.

I changed some code to make it, here is what I have currently:

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)

Please let me know if I am unclear and you need more details. This has been more challenging that I thought it would be! Any help here would be much appreciated.

Second Edit: I have the following code now :

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

I am still getting the same thing : http://postimg.org/image/ogoj2t02f/ What I need is only one bar with values upto 3000 and a bar chart coming upto 900. Could you please help ?

Upvotes: 2

Views: 10448

Answers (2)

wye
wye

Reputation: 346

If the upper and lower y-axis values are not shown as expected, make sure,

  1. you set the customAxisMax and customAxisMin on both left and right of the y-axis.
  2. you set the customAxisMax and customAxisMax before you set the .data of the chart view.
  3. you set both x and y-axis' startAtZeroEnabled to false. And you set startAtZeroEnabled before you set the .data of the chart view.

It took me a few hours to figure those out. Hope it could save you some.

Upvotes: 5

Wingzero
Wingzero

Reputation: 9754

customAxisMax should be able to help you. It works fine on my side.

What you might be missing is, you are setting leftAxis, but you should know there is rightAxis as well. I am not sure which you are using.

If you take a look BarLineChartViewBase.swift', there is code in

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)

It reads customAxisMax or customAxisMin once you set it. You need to double check which axis you are using.

Edit for your second edit:

I just change the demo code, is it what you want?

Code in ios-charts demo, try it in your demo code:

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

screenshot: enter image description here

Upvotes: 2

Related Questions