2

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 ?

userx
  • 1,083
  • 5
  • 18
  • 36

2 Answers2

5

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.

wye
  • 316
  • 4
  • 7
2

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

Wingzero
  • 9,644
  • 10
  • 39
  • 80
  • Thanks for your answer @Winzergo . I have added some more details on what I am trying to do. Please see my edits. – userx Jul 14 '15 at 14:34
  • I tried setting the customAxisMax for both x and y axis but that did not fully effect. It increased upto 1200 (previously it was 1000). Since I have a small rectangle for the graph view, could it be that I need to scale it to fit ? – userx Jul 14 '15 at 14:45
  • I tried with the ios-charts demo code, customAxisMin and customAxisMax works fine on my side. Have you changed the rendering code? Do you mind provide a sample project? You can try the demo horizontal bar chart code: ` _chartView.leftAxis.startAtZeroEnabled = NO; _chartView.rightAxis.startAtZeroEnabled = NO; _chartView.leftAxis.customAxisMax = 100; _chartView.leftAxis.customAxisMin = 10; _chartView.data = data;` it will limit the leftAxis to 10~100 – Wingzero Jul 15 '15 at 01:18
  • BTW, you can try to debug the swift code, it will give you more information. – Wingzero Jul 15 '15 at 01:20
  • `setVisibleYRangeMaximum` will not limit your axis min or max value. use customAxisMax instead. I have uploaded the screenshot on my side, is it what you want? You can try from the demo code, much easier to find out where goes wrong. – Wingzero Jul 15 '15 at 01:23
  • Thanks for getting back to me. I changed the code as per your suggestion, please see the code in the edited question, also, I am adding what I am getting and what I want. Your help is appreciated. – userx Jul 15 '15 at 05:38
  • I am not sure what you mean by `only one bar with values upto 3000 and a bar chart coming upto 900`. what 3000 and 900 are, explicitly? But I have update my answer, is it what you want? There is no magic, just simple configuration. – Wingzero Jul 15 '15 at 05:54
  • I got it ! I just copy pasted your code and converted it to Swift and it worked! I am not sure why it was not working before. Thank you so much for your help!! – userx Jul 15 '15 at 16:48
  • 1
    glad it works. You can check where's the difference. What I guess is you are using right axis while I use left one. It should be consistent – Wingzero Jul 15 '15 at 22:58
  • This does not seem to work for me. On a horizontal bar chart,I set a customAxisMax to 600 and a customAxisMin to 0, startAtZeroEnabled = NO, and I have values equal to 33, some 0s and a 50. The issue is that the chart does not scale using my custom maximum value. It scales using the maximum value of my data set, which is 50. – Konrad Dec 06 '15 at 16:33
  • 1
    @Konrad I tried with latest ChartsDemo 2.1.6, I can have a customAxisMax=600 effect without problem. Which axis you are setting? right or left? you have to make sure it's the same y axis when you set it, also update your code maybe. – Wingzero Dec 07 '15 at 01:18
  • Thanks Wingzero. I do use the right axis, and I set it for the right axis. But setting customAxisMax for the LEFT axis even though I disabled it (leftAxis.enabled = NO) made things work. – Konrad Dec 07 '15 at 16:01
  • really? have you set dataSet axis dependency? – Wingzero Dec 07 '15 at 22:25