How to format y-axis to show values as 5*10^5, 1*10^6, 2*10^6... instead of 500,000,1,000,000, 2,000,000... and which are divisible by 5 or 10?
Asked
Active
Viewed 2,295 times
1 Answers
1
A LogAxis
with the default tick units seems to work. This related example uses integer tick units.
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* @see https://stackoverflow.com/a/22450677/230513
* @see https://stackoverflow.com/a/10353270/230513
*/
public class Test {
private static final int N = 7;
private void display() {
XYSeries series = new XYSeries("Series");
for (int i = 0; i <= N; i++) {
series.add(i, Math.pow(10, i));
}
NumberAxis xAxis = new NumberAxis("X");
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
LogAxis yAxis = new LogAxis("Y");
XYPlot plot = new XYPlot(new XYSeriesCollection(series),
xAxis, yAxis, new XYLineAndShapeRenderer(true, false));
JFreeChart chart = new JFreeChart(
"Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(480, 240);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().display();
}
});
}
}
-
thanks for your answer, is it possible to show the power value as 10 power 3 instead of 10^3? – Vijay Vennapusa Mar 17 '14 at 13:14
-
1If you mean like 10³, you might try [this](http://www.jfree.org/forum/viewtopic.php?f=3&t=116638)? – trashgod Mar 17 '14 at 23:09
-
could you please update the code to attributedString, I am not able to figure out, how to use it. Thanks! – Vijay Vennapusa Mar 20 '14 at 16:13
-
Sorry, I've not used it either, you might look [here](http://www.jfree.org/forum/search.php?keywords=AttributedString) or [here](http://stackoverflow.com/search?q=AttributedString+%5Bjfreechart%5D). You might also pose a new question with an [mctre](http://stackoverflow.com/help/mcve) that show your intended usage. – trashgod Mar 20 '14 at 16:52
-
1@user3215852: I found this [example[](http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=116592#p176675) for the axis label, but I'm not sure about the tick labels themselves. – trashgod Mar 20 '14 at 18:15
-
its only for axis labels, I am not able to set it for tick labels. – Vijay Vennapusa Mar 20 '14 at 19:20