Coloring of certain Data points / data nodes in chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
vatamian
Posts: 1
Joined: Tue Jun 16, 2015 2:36 pm
antibot: No, of course not.

Coloring of certain Data points / data nodes in chart

Post by vatamian » Tue Jun 16, 2015 3:00 pm

Hello everybody,

I'm a newby in the library JFreeChart, therefore I would need some assistance for the following problem:

As an exercise, I have to create a Chart, where some of the data points (at first place) have to be with different than the stroke color.

What I use for my task: XYDifferenceRender, as the first part was coloring the difference between two of the series.

So far I have found some threads on the forum with advice on this toppic. I managed to override my render, and the method getItemPaint. The problem is, I seem to get painted not only a data point, but also the section that is connected to him.

This is what I get now:
Image

And this is what I am aiming for: (See the yellow and the white triangle on the right side)

Image

Some code snippets from my side:

I create the dataset manually, within sepaate method:

Code: Select all

private XYDataset createDataset() {

XYSeries test1 = new XYSeries("MinValue");
test1.add(1.0, 1.0);
test1.add(2.0, 2.0);
...

XYSeries test2 = new XYSeries("MaxValue");
test2.add(1.0, 5.0);
test2.add(2.0, 1.0);
.....

XYSeriesCollection dataset = new XYSeriesCollection();
		
//all the points in a specific series are added to the collection
dataset.addSeries(test1);
dataset.addSeries(test2);

return dataset;
}


class MyRenderer extends XYDifferenceRenderer {
		
	    public MyRenderer(Paint positivePaint, Paint negativePaint, boolean shapes) {
	        super(positivePaint, negativePaint, shapes);
	    }
	    
	    @Override
	    public Paint getItemPaint(int row, int col) {
    
       //Colors not only the points, but also the lines connected to them
	        if (col == centroidColumn) {
	            return centroidColor;
	        } else {
	            return super.getItemPaint(row, col);
	        }
	    }
	    
	}

And this is how I call for the render:

Code: Select all


....

JFreeChart xyGraphics = ChartFactory.createXYLineChart(chartTitle, "KW", "Werte", createDataset());

ChartPanel chartPanel = new ChartPanel(xyGraphics);

XYPlot plot = xyGraphics.getXYPlot();

....

MyRenderer renderer = new MyRenderer(Color.BLACK, Color.CYAN, true);

renderer.setSeriesPaint(0, Color.BLUE);
renderer.setSeriesPaint(1, Color.RED);
....

renderer.setSeriesShape(0, ShapeUtilities.createUpTriangle(5));
renderer.setSeriesShape(1, ShapeUtilities.createDiamond(10));
.....

// Determine how thick the line will be, how round/square it will be at the edges, what the distance betweeen the dots will be if dotted etc
renderer.setSeriesStroke(0, new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 4.0f, new float[] {14.0f, 16.0f}, 0.0f));
		
renderer.setSeriesStroke(1, new BasicStroke(3.0f));

plot.setRenderer(renderer);
Any help on my issue will be deeply appreciated, how to color only the datapoints without the series or strokes connecting them.

Thank you

Greetings
vatamian

Locked