Come posso creare un bar-chart con JFreeChart, che accorcia troppo lungo bar con un suggerimento visibile?
-
21-09-2019 - |
Domanda
Voglio creare un bar-chart, ma alti valori straordinari dovrebbe essere abbreviato. Un esempio è questa immagine:
(fonte: epa.gov )
Spero che sia chiaro quello che voglio.
La mia domanda è: Come posso fare questo con JFreeChart . Se non è possibile con JFreeChart si può eventualmente raccomandare open-source Java-librerie alternative per la produzione di tale uscita.
Soluzione
Si potrebbe farlo con un CombinedDomainCategoryPlot
o CombinedDomainXYPlot
. Impostare l'asse gamma di primo piano per il vostro taglio off valore e quindi fare qualcosa di simile con la seconda trama. Poi aggiungerli ad una trama combinato.
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CombinedDomainCategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class PlayChart {
public static void main(String[] args) {
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.addValue(100, "A", "A");
ds.addValue(200, "A", "B");
ds.addValue(400, "A", "C");
ds.addValue(500, "A", "D");
ds.addValue(2000, "A", "E");
JFreeChart bc = ChartFactory.createBarChart("My Bar Chart", "Things", "Counts", ds, PlotOrientation.VERTICAL, true, false, false);
JFreeChart bcTop = ChartFactory.createBarChart("My Bar Chart", "Things", "Counts", ds, PlotOrientation.VERTICAL, true, false, false);
CombinedDomainCategoryPlot combinedPlot = new CombinedDomainCategoryPlot();
CategoryPlot topPlot = bcTop.getCategoryPlot();
NumberAxis topAxis = (NumberAxis) topPlot.getRangeAxis();
topAxis.setLowerBound(1500);
topAxis.setUpperBound(2000);
combinedPlot.add(topPlot, 1);
CategoryPlot mainPlot = bc.getCategoryPlot();
combinedPlot.add(mainPlot, 5);
NumberAxis mainAxis = (NumberAxis) mainPlot.getRangeAxis();;
mainAxis.setLowerBound(0);
mainAxis.setUpperBound(600);
JFreeChart combinedChart = new JFreeChart("Test", combinedPlot);
ChartFrame cf = new ChartFrame("Test", combinedChart);
cf.setSize(800, 600);
cf.setVisible(true);
}
}
Le trame condivideranno lo stesso asse X. Avrai bisogno di giocare con i renderer per impostare i colori e le etichette.
rimosso morti collegamento ImageShack
Altri suggerimenti
Non sono sicuro che si può fare in JFreeChart.
Una soluzione (che non è bello) è rendere il grafico in un'immagine e poi manipolarlo (tritarlo etc.) come immagine, utilizzando RenderedImage , e non come un JFreeChart. Purtroppo che sta per essere un po 'di dolore in quanto si sarebbe probabilmente desidera tagliare in un determinato posto sulla asse y etc.