mmislam101 / SimpleBarChart

A simple animated bar chart class
The Unlicense
41 stars 14 forks source link

bar color different for negative values #2

Open sgs1 opened 10 years ago

sgs1 commented 10 years ago

Hello!

My compliments for the project. Two questions: is possible to change bar color only for negative values? How i can insert negative values for Y column? Thanks

mmislam101 commented 10 years ago

Unfortunately I didn't design the graph to handle negative values. I probably should've. As for the bar color, there's a delegate method so that you can set the color of each bar individually by returning the color you want it to be.

sgs1 commented 10 years ago

Thanks for the reply. I suppose that we can develop it by "cloning" the same function that you've used for positive values, but for negative. What do you think about this idea?

mmislam101 commented 10 years ago

If you have a graph that is specifically just for negative values then yah, you can probably just flip some logic in the methods where it'll draw the bars going down (or still up but with negative values). But I thought you meant something like where the x-axis is in the center and you can have both positive and negative values. If you can help by reformatting the logic to be flexible for both negative and positive that'd be great. There are some gotchya's though. Dealing with floating-point numbers and comparisons of them is difficult. You'll come across some bad code around detecting whether a number is NEAR zero instead of equal to zero.

sgs1 commented 10 years ago

Thanks for the reply. Can i set the width of Y label?

mmislam101 commented 10 years ago

The width of the Y label I made so that it's flexible (sort of) to whatever text is going there. Probably not the best choice for proper charting methods. Again you're totally invited to go in there and fix it up and make a Pull Request to me. The codes open to change.

sgs1 commented 10 years ago

Thanks for the reply.

I could fix your code but i don't understand what are the methods that you've used for y label's width and for Y values. Can you help me?

mmislam101 commented 10 years ago

Sure. I can try. In SimpleBarChart.m you'll see a method reloadData. Inside reloadData you'll see where I set the _yLabelView.frame That frame is based on yLabelSize.width (and whether there are yLabels at all). The yLabelSize is determined above that code using sizeWithFont for the LARGEST value of the maximum value in the set of data that is received. Probably not the best idea considering the y-axis labels could and do go above the max, I should've done it better.

Anyways. If you want to set your own custom widths, what you could do is create a property in SimpleBarChart.h that says something like customMaxYLabelWidth. And you can initialize it as 0 on the initWithFrame in SimpleBarChart.m. Then that line where you set the _yLabelView.frame, you can check if the customMaxYLabelWidth is greater than 0 and if it is then replace the yLabelSize.width to customMaxYLabelWidth.

Would that help you? It'd be cool if you could branch off the current SimbleBarChart master, and then create a pull request so that we keep this up to date for everybody.

sgs1 commented 10 years ago

Sorry but in reloadData i don't see _yLabelView.frame...:(

sgs1 commented 10 years ago

I've uploaded a screenshot that shows how the Y label appears with values major of 999. example

mmislam101 commented 10 years ago

It's in https://github.com/mmislam101/SimpleBarChart/blob/master/SimpleBarChart/SimpleBarChart.m Line 155.

And by the way. If you're doing this just so that you can fit a larger data sets, have you tried setting incrementValue property to a large number? The incrementValue determines the increments in which the y-axis labels are calculated. For data sets un the thousands, you should set the incrementValue to 100 or more depending on the maximum value in your data set.

sgs1 commented 10 years ago

last

Hi and thanks for the reply!

Yes it works, i attach here another screenshot, but i'll try with modify _yLabelView.frame.

About the values on Y-axis labels, it could be very interesting to see only some values of _values array (like the first, the third and the last)..

I've tried to modify _topValue and i've seen some negative values. Can you explain me, how you assign to Y-axis label the values? Ideally, i could "clone" this method and assign to the value a negative sign.....

mmislam101 commented 10 years ago

Actually there is a known bug with that where it'll show -1.0 or something instead of 0. I think that's mainly due to the problem with floating point comparison. I tried putting in some extra logic around it but I think the way I did all this just needs to be refactored honestly. If you're willing to do it that'd be super awesome. Check out - (void)setupYAxisLabels for the logic on how those labels are calculated and placed

sgs1 commented 10 years ago

What are you talking about, in this words:

"Actually there is a known bug with that where it'll show -1.0 or something instead of 0. I think that's mainly due to the problem with floating point comparison. I tried putting in some extra logic around it but I think the way I did all this just needs to be refactored honestly. If you're willing to do it that'd be super awesome."

mmislam101 commented 10 years ago

Oh don't worry about that, I mis-read what you wrote.

sgs1 commented 10 years ago

No problem!

I've another question. Can i modify the frame of bar if they have a negative values? thanks

mmislam101 commented 10 years ago

Absolutely, do whatever you need to do.

What would be nice is if you could make the x-axis flexible so that (based on what kind of values are in the data set) it will move up and down so that it can account for negative values with bars going down from the x-axis line and bars going the usual up from the x-axis line. So it can look like this: http://www.andypope.info/charts/invert_10.gif

sgs1 commented 10 years ago

thanks for the reply,and good idea... But the question is if in the code there is some part where i can change the position of the bar only with negative values.

If you see the previous image that i've uploaded, you can see that the first label is on first bar...and it could be better if the bar is under the value label...

mmislam101 commented 10 years ago

hmm. That would require you to do a lot of refactoring that includes positioning the start of the rectangle for each bar be at a different value for negative numbers than positive numbers. That would require you to become familiar with how I draw the bars themselves. You can look at (void)setupBars to see how I set the rects for each bar in the data set. In there you'll have to add logic to shift the y-positions of bars for negative values. Is that what you wanted to do?

mmislam101 commented 10 years ago

And I apologize that I didn't comment this code properly the first time. I'll make sure to do that from now on so that collaboration can be easier.

sgs1 commented 10 years ago

Yes, you've understand my problem.

Please let me know when you have commented the code... Thanks for the update!