I'm using an XYPlot in JFreeChart. All the lines on it are XYSeries objects. Both axes are NumberAxis objects. The Y-Axis range is from 0-1, with ticks every .1. Along with displaying the numbers though, I'd like to display text on the Y-Axis, like High/Medium/Low. High would cover .7-1, etc. What is the best way to go about doing this?
Java – JFreeChart XYPlot with labels
javajfreechart
Related Solutions
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
and you run it with
mvn clean compile assembly:single
Compile goal should be added before assembly:single or otherwise the code on your own project is not included.
See more details in comments.
Commonly this goal is tied to a build phase to execute automatically. This ensures the JAR is built when executing mvn install
or performing a deployment/release.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
You can manually create the JFreeChart object instead of using ChartFactory. First generate the datasets and Plot, setting each dataset to an index. Then you can customize the Plot with the necessary Axis and Renderer. Here's an example for doing so with dummy data that has two datasets, each with different magnitude y-values:
//create the series - add some dummy data
XYSeries series1 = new XYSeries("series1");
XYSeries series2 = new XYSeries("series2");
series1.add(1000, 1000);
series1.add(1150, 1150);
series1.add(1250, 1250);
series2.add(1000, 111250);
series2.add(1150, 211250);
series2.add(1250, 311250);
//create the datasets
XYSeriesCollection dataset1 = new XYSeriesCollection();
XYSeriesCollection dataset2 = new XYSeriesCollection();
dataset1.addSeries(series1);
dataset2.addSeries(series2);
//construct the plot
XYPlot plot = new XYPlot();
plot.setDataset(0, dataset1);
plot.setDataset(1, dataset2);
//customize the plot with renderers and axis
plot.setRenderer(0, new XYSplineRenderer());//use default fill paint for first series
XYSplineRenderer splinerenderer = new XYSplineRenderer();
splinerenderer.setSeriesFillPaint(0, Color.BLUE);
plot.setRenderer(1, splinerenderer);
plot.setRangeAxis(0, new NumberAxis("Series 1"));
plot.setRangeAxis(1, new NumberAxis("Series 2"));
plot.setDomainAxis(new NumberAxis("X Axis"));
//Map the data to the appropriate axis
plot.mapDatasetToRangeAxis(0, 0);
plot.mapDatasetToRangeAxis(1, 1);
//generate the chart
JFreeChart chart = new JFreeChart("MyPlot", getFont(), plot, true);
chart.setBackgroundPaint(Color.WHITE);
JPanel chartPanel = new ChartPanel(chart);
Best Answer
I have some JFreeChart experience, and after a little research I don't have an answer for adding the three labels to the axis.
However, as an alternative approach, you should be able to delineate these three areas on the plot with colors by setting a MarkerAxisBand for the NumberAxis (using this method).
You could then add interval markers to the MarkerAxisBand to highlight the three areas.