Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    modificare i colori di un grafico

    Ciao a tutti,
    ho un problema coi colori di un grafico e non essendo molto pratica di java non so più dove sbattere la testa.
    In pratica sono riuscita a trovare in rete, e poi modificare un pò, una classe che stampi un grafico in formato pdf. Ciò che mi serve adesso è poter assegnare dall'esterno il colore del plot (se fosse possibile vorrei capire anche come si cambia lo stile: tratteggiato, linea continua...), magari quanbdo richiamo la classe. Si può fare? Grazie mille a chi mi risponderà

    Il codice è questo:

    codice:
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.geom.Rectangle2D;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.PlotOrientation;
    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;
    
    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Rectangle;
    import com.lowagie.text.pdf.DefaultFontMapper;
    import com.lowagie.text.pdf.PdfContentByte;
    import com.lowagie.text.pdf.PdfTemplate;
    import com.lowagie.text.pdf.PdfWriter;
    
    public class PdfPlot {
    
    	XYSeriesCollection dataset = new XYSeriesCollection();
    	String ID;
    	private double xInf, xSup;
    
    	public PdfPlot(String ID, double xInf, double xSup) {
    		this.ID = ID;
    		this.xInf = xInf;
    		this.xSup = xSup;
    	}
    
    	public void addPlot(double[] xPlot, double[] yPlot, String title) {
    
    		XYSeries series = new XYSeries(title);
    
    		for (int k = 0; k < xPlot.length; k++) {
    			if (xPlot[k] > xInf && xPlot[k] < xSup) {
    				series.add(xPlot[k], yPlot[k]);
    			}
    		}
    
    		dataset.addSeries(series);
    	}
    
    	
    	public void makePlot() {
    
    		int width = 900;
    		int height = 400;
    
    		Document document = new Document(new Rectangle(width, height));
    		try {
    
    			// create the chart...
    			final JFreeChart chart = ChartFactory.createXYLineChart(ID, // title
    					"Wavelength", // x axis label
    					"Flux", // y axis label
    					dataset, // data
    					PlotOrientation.VERTICAL, // plot oientation
    					true, // include legend
    					true, // tooltips
    					false // urls
    					);
    
    			// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
    			chart.setBackgroundPaint(Color.white);
    
    			// get a reference to the plot for further customisation...
    			final XYPlot plot = chart.getXYPlot();
    			plot.setBackgroundPaint(Color.white);
    		
    			
    			plot.setDomainGridlinePaint(Color.white);
    			plot.setRangeGridlinePaint(Color.white);
    
    			final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    			// renderer.setSeriesLinesVisible(0, true);
    			renderer.setLinesVisible(true);
    			renderer.setShapesVisible(false);
    			// renderer.setSeriesShapesVisible(0, false);
    			// renderer.setSeriesShapesVisible(1, false);
    			plot.setRenderer(renderer);
    
    			// change the auto tick unit selection to integer units only...
    			final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    			rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    			// OPTIONAL CUSTOMISATION COMPLETED.
    
    			
    			// print pdf
    			
    			PdfWriter writer = PdfWriter.getInstance(document,
    					new FileOutputStream("/home/"+ ID+".pdf"));
    
    			document.open();
    			PdfContentByte cb = writer.getDirectContent();
    			PdfTemplate tp = cb.createTemplate(width, height);
    			Graphics2D g2d = tp.createGraphics(width, height,
    					new DefaultFontMapper());
    			Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
    			chart.draw(g2d, r2d);
    			g2d.dispose();
    			cb.addTemplate(tp, 0, 0);
    
    		} catch (DocumentException de) {
    			de.printStackTrace();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    		document.close();
    
    	}
    
    }

  2. #2
    mi spiego meglio, ogni serie viene automaticamente colorata in maniera diversa, ma io voglio stabilire da me quali colori adottare. In rete ho trovato questa istruzione: renderer.setSeriesPaint(0, Color.blue);

    ma così mi devo settare a mano il numero di serie che ho (e non lo so a priori). Per chiarire ulteriormente, ho il mio grafico (di un colore qualsiasi) e poi devo tracciargli sopra delle righe verticali, alcune sono tutte di un tipo (e quindi le voglio ad esempio rosse) e altre di un altro tipo (esempio blu). Al momento vengono stamapte tutte di colore diverso, anche se per ognuna mette la legenda. A me servirebbe che per ogni gruppo di linee si avesse lo stesso colore (e la stessa legenda). E' possibile?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.