• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting unveven intervals in JFreechart

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anyone tell me if we can set uneven interval for range axis in JFreechart? If yes hw can I do that?

deeps
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don' think JFC can do that out of the box. A slightly hackish way to do this would be to provide a custom NumberFormat (which you'd need to implement) through the axis' setNumberFormatOverride(NumberFormat) method. That can return a blank string for any values that you don't want to display. The Axis objects can be obtained from the Plot object (which in turn is obtained from the JFreeChart object).
 
Deeps Prav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much for that reply.

Actually I want the scaling to be something like 0,10,20,30,35,40,45 50,60,70,80 ..
Thats in the beginning and at the end the scale interval is 10,but in the midrange it is 5.Something like this is my requirement.Can this be attained in the way you said?
If yes can you please explain it a little more?

Thanks,
deeps
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want the chart to be unevenly spaced, or just the axis labels? The former is not possible, while the latter can be accomplished in the way I suggested. Read the javadocs of the JFreeChart to find methods to get the underlying Plot (or XYPlot) object. If you don't have an XYPlot, then you need to cast the Plot object to your plot type before you can call the method to get the range axis. You'll then need to cast the ValueAxis to NumberAxis so that you can call the setNumberFormatOverride method.
 
Deeps Prav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thaks for such fast replies. Everything else is fine.But my doubt is how shall I override the Number Format to get the resullt?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd "override" the NumberFormat by calling the method I mentioned. Did you find it in the javadocs?

Or are you asking about what a NumberFormat is, and how to create your own? I'm sure searching for something like "java extend numberformat" will find examples.
 
Deeps Prav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes... its working. Thaks so much for the support.
 
Deeps Prav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But again the thing is , this is just a matter of displaying the range axis label. But my requirement is to change the scale itself.
Is there any way to get that done?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would requires some serious math to get right. The only non-evenly spaced axes generally used are logarithmic axes (which JFreeChart supports). Anything else is quite out of the ordinary.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:You'd "override" the NumberFormat by calling the method I mentioned. Did you find it in the javadocs?

Or are you asking about what a NumberFormat is, and how to create your own? I'm sure searching for something like "java extend numberformat" will find examples.



I wanted to display only the bolded numbers on my chart.
For Eg : 0.0 0.5 1.0 1.5 ..... 6.0 6.5 7.0
I am trying to avoid decimal numbers in the Range Axis (Y Axis).
I have written the below mentioned code for avoiding the same.
But still the decimal numbers are getting displayed.

NumberAxis na =((NumberAxis)(itr.next()).getCategoryPlot().getRangeAxis(0));
NumberFormat nf = new DecimalFormat("#");
nf.setMaximumFractionDigits(0);
((DecimalFormat) nf).setDecimalSeparatorAlwaysShown(false);
na.setNumberFormatOverride(nf);

Aternately as suggested I have tried to extend NumberFormat Class and tried to format the numbers.

public class UtilNumberFormat extends NumberFormat {
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
if(number>=100)
return toAppendTo.append(number);
else
return toAppendTo.append("");
}
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
if(number>=100)
return toAppendTo.append(number);
else
return toAppendTo.append("");
}
public Number parse(String source, ParsePosition parsePosition) {
return null;
}
}

The above code is not to avoid the decimal numbers, but i have tried inserting SOP inside the format function.
And the SOP doesnt get displayed.
Kindly guide as to where I am taking the wrong turn.
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anup Hege and welcome to Javaranch!

Could I get you to please edit your post with the tags as shown in our UseCodeTags article?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you insert print statements? How are you using this code? Post the actual code.
 
Anup Hege
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Where did you insert print statements? How are you using this code? Post the actual code.



Hi,

The code with the print statements is as mentioned below.




I have noticed one more thing in the below mention URL which has the source code for Jfree Chart.
Number Axis Code

The Charts use the format() function for converting the double number into String.
How can we make the charts use our NumberFormat's Overloaded format (long number, StringBuffer toAppendTo, FieldPosition pos) function.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The Charts use the format() function for converting the double number into String.
How can we make the charts use our NumberFormat's Overloaded format (long number, StringBuffer toAppendTo, FieldPosition pos) function.


Why don't you override format(double) instead, since that's what's being used?
 
Anup Hege
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

The Charts use the format() function for converting the double number into String.
How can we make the charts use our NumberFormat's Overloaded format (long number, StringBuffer toAppendTo, FieldPosition pos) function.


Why don't you override format(double) instead, since that's what's being used?



Hi,
Thanks for the reply.
This is where I am facing the difficulty.
The format(double) is a final function in NumberFormat Class.
Hence I am not able to Override the same.
Kindly let me knwo if I am overlooking / missing something.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anup Hege wrote:The format(double) is a final function in NumberFormat Class.


Oh, right, I forgot about that. But a look at the source code of the NumberFormat class shows that it calls one of the overridden methods. Where and how are you using this class?
 
Anup Hege
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

Anup Hege wrote:The format(double) is a final function in NumberFormat Class.


Oh, right, I forgot about that. But a look at the source code of the NumberFormat class shows that it calls one of the overridden methods. Where and how are you using this class?



I am using the NumberFormat class as suggested by you in one of the earlier posts.



I have used the function setNumberFormatOverride for calling my Customised NumberFormat Class.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post the code that does that for all range axes.
 
Anup Hege
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Post the code that does that for all range axes.





 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so the NumberFormat is only used if lower bound != upper bound, but I'm guessing you already knew that.

But this is a problem:

(itr.next()).getCategoryPlot().getRangeAxis(0).setRangeWithMargins(dLowerBoundRange, dUpperBoundRange);

NumberAxis naxis =((NumberAxis)(itr.next()).getCategoryPlot().getRangeAxis(0));


itr.next() retrieves a new element from the iterator each time it is called, so these two statements work on different objects. This ensures that the NumberFormat is only used for every other iterator element - most likely not what you want.
 
Anup Hege
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:OK, so the NumberFormat is only used if lower bound != upper bound, but I'm guessing you already knew that.

But this is a problem:

(itr.next()).getCategoryPlot().getRangeAxis(0).setRangeWithMargins(dLowerBoundRange, dUpperBoundRange);

NumberAxis naxis =((NumberAxis)(itr.next()).getCategoryPlot().getRangeAxis(0));


itr.next() retrieves a new element from the iterator each time it is called, so these two statements work on different objects. This ensures that the NumberFormat is only used for every other iterator element - most likely not what you want.



You are correct Ulf.
I have used a different technique for resolving this issue.
Below is the code for the same
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic