Multiple Sorted Series with String Labels on Domain

A discussion forum for JFreeChart (a 2D chart library for the Java platform).

Multiple Sorted Series with String Labels on Domain

Postby dhall » Tue Dec 19, 2017 2:10 am

We have been using jfreechart for many years for simple charting. In our old charts, we have been charting the speed one direction down a roadway by cross street.

ie, the data we have been using would be:

Street A, mile marker 123.3, speed 10
Street B, mile marker 123.8, speed 11
Street C, mile marker 124.2, speed 12

This used a DefaultCategoryDataSet and we were fine with it drawing in the order the labels were entered.

Now we have a requirement to overlay the opposite direction speed line on the same chart. There is very little match between the location data one way versus another--it will be the same mile marker range but not the exact same road names or mile markers.

So, the opposite direction roadway may have:
Street A2, mile marker 123.1, speed 20
Street B2, mile marker 123.5, speed 21
Street C2, mile marker 124.5, speed 10

We need these two sets of data as separate series, but the data intermingled so they show up in mile marker order. I would want based on the data above, for it to create an axis with the labels:
Street A2
Street A
Street B2
Street B
Street C
Street C2

I had originally hoped by creating a Comparable object with both mile marker and street name (the compareTo sorting by mile marker and the toString outputting street name) that the category labels would end up in the correctly sorted order. They do not. Am I missing anything here? Is there a way to force this to occur?

I also experimented with changing to a XYPlot and using a SymbolAxis. This does not work for several reasons. The SymbolAxis appears to only put labels on integers, if you look at the data above, we may have multiple streets and data points between 123 and 124. It steadily mislocates the names I put ["Street A2", "Street A", "Street B2"] as the SymbolAxis array.

Also, control over the label display on a SymbolAxis seems much more limited than control over the label display with a DefaultCategoryDataset. Currently, our labels are often multiple rows long, at a 45 degree angle, and the end user has a method to control which labels are shown or not.

Is there a solution here that I'm missing? I've even thought about exploding those mile markers to integer values (aka, 123.1 becomes 1231 and "Street A" goes into index 1231 in a sparse String array for the SymbolAxis) but there also appear to be significant issues with this solution (and back to the issue of control over the label display).
Posts: 2
Joined: Tue Dec 19, 2017 1:41 am

Re: Multiple Sorted Series with String Labels on Domain

Postby paradoxoff » Wed Dec 20, 2017 8:24 pm

One of your problems seems to be the category order that you see in a CategoryPlot. In short: the categories are not ordered by default. The categories should appear along the CategoryAxis in the order in which the categories were added to the dataset, regardless of the type of Comparable. In the absence of any code, I really can't imagine what exactly is wrong with what you are seeing.
Did I get it right that "Street A" and "Street A2" are the same street, and only differ in the driving direction?
In that case, I would suggest the following: Use an XYPlot, where the mile marker is used as x coordinate, and the speed as y coordinate. Create one series with an individual color for each street/direction combination. To indicate the direction, you can use a custom series shape, e. g. a triangle tat is pointing either left or right.
Alternatively, you can use a VectorXYDataset and use a custom VectorRenderer that, in contrast to a normal VectorRenderer that is drawing arrows of constant length, regardless of the value range of the domain and range axis.
Posts: 1623
Joined: Sat Feb 17, 2007 1:51 pm

Re: Multiple Sorted Series with String Labels on Domain

Postby dhall » Thu Jan 04, 2018 11:55 pm

No, the problem is that Street A and Street A2 don't always have the same name.

So southbound might have streets
Ash at 123.3
Oak at 123.8
Main at 124.5

And northbound may have streets
Brown at 123.1
Green at 123.5
Pink at 124.2

The client wants to see an x-axis with labels in the order:
Brown, Ash, Green, Oak, Pink, Main
and a y-axis with speed.

The problem we have with the XYPlot (which I have experimented with) is that the control over the x-axis label display is not present. The SymbolAxis type does not solve the problem--there is no way to associate the right labels with the right data points. It only associates the labels with whole number points. So, point 123 can only have one label and it will show the data between 123 and 124 but not other labels.
Posts: 2
Joined: Tue Dec 19, 2017 1:41 am

Re: Multiple Sorted Series with String Labels on Domain

Postby paradoxoff » Mon Jan 08, 2018 4:49 pm

if you need to show the street names in a particular order, then simply add the the categories in the same order. For example:
dataset.addValue(speedForBrown, "Speed", "Brown");
dataset.addValue(speedForAsh, "Speed", "Ash");
and so on.
Posts: 1623
Joined: Sat Feb 17, 2007 1:51 pm

Return to JFreeChart

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests