Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bar chart floating y position #947

Closed
nspavlo opened this issue Apr 13, 2016 · 15 comments
Closed

Bar chart floating y position #947

nspavlo opened this issue Apr 13, 2016 · 15 comments

Comments

@nspavlo
Copy link

nspavlo commented Apr 13, 2016

Hello.
I'm using BarChartView with following configuration:

- (BarChartView *)chartView
{
    if (_chartView) {
        return _chartView;
    }
    _chartView = [[BarChartView alloc] initForAutoLayout];
    _chartView.minOffset = 1.0f;
    _chartView.descriptionText = @"";
    _chartView.noDataTextDescription = @"";
    _chartView.maxVisibleValueCount = 20;
    _chartView.drawBarShadowEnabled = NO;
    _chartView.pinchZoomEnabled = NO;
    _chartView.drawGridBackgroundEnabled = NO;
    _chartView.xAxis.enabled = NO;
    _chartView.leftAxis.enabled = NO;
    _chartView.rightAxis.enabled = NO;
    _chartView.legend.enabled = NO;
    _chartView.userInteractionEnabled = NO;
    _chartView.noDataText = @"";    
    return _chartView;
}

BarChartData is updated every second. After a while chart y position is changing. Smth. like +/- 8pt.
Is it configurable?
Thanks in advance.

@liuxuan30
Copy link
Member

Not sure what you want to config. x axis is index based, so depends on how many bars you have, the x value position is calculated then.

@nspavlo
Copy link
Author

nspavlo commented Apr 14, 2016

Always same number of items (bars). Only values is changing. sry. not x position but y.

@liuxuan30
Copy link
Member

then update your data sets

@nspavlo
Copy link
Author

nspavlo commented Apr 14, 2016

Thanks, but I think we're not on the same page here. I will create example project. :)

@liuxuan30
Copy link
Member

maybe screenshots and explaination, demo project is too heavy

@nspavlo nspavlo changed the title Bar chart floating x position Bar chart floating y position Apr 14, 2016
@nspavlo
Copy link
Author

nspavlo commented Apr 14, 2016

Initial state:
img_1906

After a while:
img_1905

Views:
-root
-- chart subview
-- overlay subview (top, middle, bottom) lines

DataSet:

BarChartDataSet *set = [[BarChartDataSet alloc] initWithYVals:yVals label:@"DataSet"];
set.barSpace = 0.4;
set.colors = @[ color ];
set.drawValuesEnabled = NO;

BarChartData *data = [[BarChartData alloc] initWithXVals:items dataSets:@[ set ]];
self.chartView.data = data;

yVals and items always contains same number of items.

@jleach
Copy link

jleach commented Apr 14, 2016

I have the same problem. If I have 10 values along the xAxes, 9 of which are 0 (no bar) and the 10th value is say 20 (anything > 0) then the single bar floats about 10px above the x-axis. If on the other hand, I have 10 values along the xAxes, all of which are > 0 then they all sit right on the x-axis.

You can see this in the image above. In the initial state, the bars are floating ~10 px above the x-axis. However, in the "After a while" graph you can see they're much closer to the x-axis. This happens when some of the bars have a 0 value.

If you can point me to where in the code this might be happening, perhaps I can create a PR with a fix.

@liuxuan30
Copy link
Member

liuxuan30 commented Apr 15, 2016

could you just turn on displaying the y axis values, so we know the y axis range? Also, which version are you using?
I doubt it's related to recent change about deprecating startAtZeroEnabled and moving to axisMinValue.
Please check.
forcing axisMinValue = 0 will make the y axis always started from 0.

@nspavlo
Copy link
Author

nspavlo commented Apr 15, 2016

Initial:
img_1911

After a while:
img_1914

@jleach
Copy link

jleach commented Apr 15, 2016

For me I have 24 values; index 0 = 71, the rest are zeros. Also, I have rightAxis.startAtZeroEnabled = true I walked through the render bar method and it looks like it should be drawing ok. Maybe its the axes that is being draw too low?

fullsizerender

@danielgindi
Copy link
Collaborator

If I understand you correctly, your Y values range is changing. When the max value is getting bigger, the other bars are getting smaller relatively.
You can control the minimum/maximum Y axis values, using axisMinValue.
Or you can use setVisibleYRangeMaximum to limit the Y scale and force the user to pan...

@liuxuan30
Copy link
Member

@jleach seems your issue is different? I tried your 71 and 23 zeroes with ChartsDemo, it's fine. I think you got something wrong with your code, or you modified something. The typical work flow is first configure the chart properties, and then setup the data. Make sure chartView.data = yourData is the last line; or you call notifyDataSetChanged() everytime you changed something about axis ranges.

@jleach
Copy link

jleach commented Apr 19, 2016

@liuxuan30 I tried moving the chartView.data = to the be the last line and tried calling notifyDataSetChated() - no luck. Here is my method:
https://gist.github.com/jleach/ef831910e5dd16a785361533df73113e

I'll go back and see if I'm messing with the layout or something. The only thing I can think of is that I'm embedding the chart in a UIView. Maybe a constraint is causing me grief?

@liuxuan30
Copy link
Member

liuxuan30 commented Apr 20, 2016

@jleach constraints should impact the whole chart view, not just axis or a bar, right? unless you separate them..

        chartView.data = BarChartData(xVals: xVals, dataSets: barChartDataSets)
        chartView.notifyDataSetChanged()

the second chartView.notifyDataSetChanged() is not needed as data setter will call it once.

I mean something like:

chartView.leftAxis.enabeld = false
chartView.data = BarChartData(xVals: xVals, dataSets: barChartDataSets)
chartView.leftAxis.enabeld = true
chartView.notifyDataSetChanged() <--- must call this or the chart will mess up, becaues left axis will impact the space calculation

BTW, from your code, I see you only use rightAxis, but you don't assign data set axis dependency to right, and default is left axis. I also suspect your chartView.leftAxis.startAtZeroEnabled is NOT true(though default is true), that's why it will have some gap, at least I can reproduce it with ChartsDemo under this scenario

you can try set.axisDependency = AxisDependencyRight to see if your problem is gone (which I guess will) and turn on startAtZeroEnabled(deprecated in latest release)

@danielgindi
Copy link
Collaborator

You have not responded to my reply. Have you checked it out?
Also there are dedicated addEntry/removeEntry functions to add/remove entry without calling notifyDataSetChanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants