• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

changing the x-axis labels on JFreeChart

 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope someone has experience with this product because I'm almost out of hair. I'm going to look like Bear by the time I figure out this package.

Anyway, I need to change the x-axis labels and I'm confused by all the classes like SymbolAxis and TimeSeries. The time data from the database is stored as an integer like hhMMss, and the data is sampled every 20 seconds, so I have times like 71234 and 151210. The chart comes out with axis labels like 70,000, 80,000, and 150,000. I need to convert these so 70,000 is "7am" and 150,000 is "3pm". Any idea on which ValueAxis class I should use to achieve this, if it's possible?
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additional info: Here's code that I got right out of the developers guide:

..but line 4 is evaluating false. DateAxis extends ValueAxis so why is it evaluating false?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:
..but line 4 is evaluating false. DateAxis extends ValueAxis so why is it evaluating false?



Is this code being run as part of some web application or in some environment where multiple classloaders are involved? If yes, then this issue could be because of multiple different classloaders loading the same class. Is the JFreeChart jar packaged in more than one place (like within the web app and then within the server wide libraries folder)?
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's in my main web app, added as a jar to the list of libraries, using NetBeans. It's not in the tomcat lib folder; that only contains things like the Apache jars and odbc drivers. I've even removed them, added them back and did a clean and build. Still no joy. I've searched the forums at jfree.org and can't find any similar problem, so I must be doing something wrong or something in my environment is setup wrong.

I think it's time for a SSCE. That's going to be my project for today.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class of getDomainAxis() depends on how the chart is created. It should be created using something like ChartFactory.createTimeSeriesChart(...). Is that the case?
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I have so far:
In the servlet:


Methods in the servlet:

It's finally "trying" to reformat the x-axis labels, but it see's every label as "7 PM" or without the formatter, "19:01:00" to "19:03:00". It doesn't like the fact that I'm trying to format the String "070000" into a time as "7 AM". SimpleDateFormat expects something that it can recognize as a date. I'm thinking that I'll have to write a method to convert my times into Unix-style milliseconds. But my brain is exhausted today. Time to wrap it up and sleep on it. Maybe I'll Dream In Code tonight and figure it out.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the DateAxis does expect values to be unix long timestamps.
Instead of using the generic XYSeries, you can use TimeSeries and TimeSeriesCollection in createDataSet() and add entries using TimeSeries.add(new Hour / new Day /...).
It has the advantage that the framework takes care of converting time period values to unix timestamps, and IMO, also improves readability by making it clear to everybody that the dataset is a time series.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll try that today. I'm brand new to this product, so it's a little overwhelming so far. It sounds like you have a lot of experience with it. I appreciate the help.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:Thanks, I'll try that today. I'm brand new to this product, so it's a little overwhelming so far. It sounds like you have a lot of experience with it. I appreciate the help.


You're welcome! I agree, I too found it complex when working with it. Trial and error are par for the course with it. Then again, it is powerful and full of features, so I guess it's just the nature of the beast.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't figure out how to use TimeSeries. The first parameter to the add() method has to be a RegularTimePeriod object and I can't figure out how to use that with my odd time strings of hhMMss. I don't think I can use any of the concrete subclasses like Hour, Minute, or Second.

I think I'm going to have to go all the way upstream to the DAO and construct a Date (millisecond) object for each record using the date and time fields. But I'm still confused by the RegularTimePeriod.createInstance() method.


createInstance(java.lang.Class c, java.util.Date millisecond, java.util.TimeZone zone)
Creates a time period that includes the specified millisecond, assuming the given time zone.


What is that first parameter? What class?
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hhMMss integer data can be decomposed into hour, minute and second.


Since precision of data is seconds, add data to time series by creating Second instances:

A Second is one of the subclasses of RegularTimePeriod. They can be instantiated directly; there's no need to use
RegularTimePeriod.createInstance(). But if you do want to use it, the argument c should be one of these subclasses (ex: Second.class).

It's not clear to me whether your graph always shows data of less than 1 day or can span larger time periods like days or months (this can also happen when zooming out on the graph if zooming is enabled).
If it's always less than 1 day, then day, month and year can all be zero above. (Edit: well, it looks like zeroes are not valid values according to documentation. They have to be some valid d/m/y, but they can be any arbitrary valid value).
But if it can span longer periods, then you would need the correct day, month, year from your DB.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help in TimeSeries and RegularTime Period. I'm giving you a cow for helping me out of this jam.
I'm posting the code that I ended up using, for future reference. Maybe it will help someone else.
I changed the DAO like so:

The createDate() method:

That put the date into a real Date object that TimeSeries could use:

(I'm not sure why the DAO code didn't indent correctly; it's 4 spaces in my editor, like the other code. Hmm)
But now I have my first JFreeChart. It's a very impressive library and I'm sure I'll like it once I get more familiar with it.
btw, Bear, I looked at HighCharts but the cost is a show stopper for us. However, I did find this list which I'm anxious to go through when I have some time.
Thanks again, Karthik.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to know it worked out, and thank you for that cow!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!