Changing the color

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Narasimha Narahari

Changing the color

Post by Narasimha Narahari » Wed Feb 05, 2003 12:45 am

Hi,
I am not able to understand how to use setCategoriesPaint(.. ). Could someone give me an example.

Thanks,
--Narasimha

Arnaud

Re: Changing the color

Post by Arnaud » Wed Feb 05, 2003 7:17 pm

Hello,

Here is an example using an instance of the class VerticalBarRenderer, that we will call myVerticalBarRenderer.

// First, define Your colors
// You can use predefined colors (Color.blue), or create Your owns with
// (r,v,b) values
Paint[] seriesPaint = {
Color.red,
new Color(255, 154, 0),
Color.blue,
Color.darkGray,
Color.magenta,
new Color(156, 207, 0),
new Color(0, 207, 255)
};

// then, assign them to Your renderer
myVerticalBarRenderer.setCategoriesPaint(seriesPaint);

Regards,
Arnaud

Narasimha Narahari

Re: Changing the color

Post by Narasimha Narahari » Wed Feb 05, 2003 8:47 pm

Hi
Thanks for your reply, but i dont find the setCategoriesPaint method in VerticalBarRenderer. Is there anything else i am missing here.

Thanks,
-Narasimha

Arnaud

Re: Changing the color

Post by Arnaud » Thu Feb 06, 2003 12:24 pm

Hello,

The class VerticalBarRenderer inherits from the class BarRenderer which itself inherits from the class AbstractCategoryItemRenderer in which the method setCategoriesPaint() is defined...

Have a look on the javadoc, http://www.object-refinery.com/jfreecha ... derer.html.

Regards,
Arnaud

Narasimha Narahari

Re: Changing the color

Post by Narasimha Narahari » Thu Feb 06, 2003 3:50 pm

Hi,
Thanks for your reply, but i am not able to set the color of the categories. Below is the code in which i have set the categories paint. Please let me know if its correct


chart = ChartFactory.createVerticalBarChart(title,
xtitle,
ytitle,
categoryData, true);
VerticalBarRenderer myVertical = new VerticalBarRenderer();
myVertical.setCategoriesPaint(paint);
chart.setBackgroundPaint(new GradientPaint(0, 0, getColor(color),
1000, 0, getColor(finalGradColor)));
chart.setBackgroundPaint(getColor(color));
plot = chart.getCategoryPlot();
axis = (HorizontalCategoryAxis) plot.getDomainAxis();
axis.setVerticalCategoryLabels(true);
return chart;

Arnaud

Re: Changing the color

Post by Arnaud » Thu Feb 06, 2003 6:21 pm

Hello,

When You write :
myVertical.setCategoriesPaint(paint);

You must define first what paint is.

Try this :

Paint[] seriesPaint = {
Color.red,
new Color(255, 154, 0),
Color.blue,
Color.darkGray,
Color.magenta,
new Color(156, 207, 0),
new Color(0, 207, 255)
};

chart = ChartFactory.createVerticalBarChart(title,
xtitle,
ytitle,
categoryData, true);
VerticalBarRenderer myVertical = new VerticalBarRenderer();
myVertical.setCategoriesPaint(seriesPaint);
chart.setBackgroundPaint(new GradientPaint(0, 0, getColor(color),
1000, 0, getColor(finalGradColor)));
chart.setBackgroundPaint(getColor(color));
plot = chart.getCategoryPlot();
axis = (HorizontalCategoryAxis) plot.getDomainAxis();
axis.setVerticalCategoryLabels(true);
return chart;

Regards,
Arnaud

Narasimha Narahari

Re: Changing the color

Post by Narasimha Narahari » Thu Feb 06, 2003 6:56 pm

Hi,
I am sorry i did not give you the exact code. I have already defined above paint. But if you see the ChartFactory there is a call to the renderer. I am wondering if any change needs to be then there.

Please let me know.

Thanks,
--Narasimha

Arnaud

Re: Changing the color

Post by Arnaud » Thu Feb 06, 2003 7:31 pm

Hello,

I did not look properly at Your code, so my response was false...

Instead of :
VerticalBarRenderer myVertical = new VerticalBarRenderer();
myVertical.setCategoriesPaint(paint);

You should do something like :
((AbstractCategoryItemRenderer) chart.getCategoryPlot().getRenderer()).setCategoriesPaint(paint);

The error is that You didn't get the renderer associated with Your chart instance, but created a new one without using it to update Your chart.

Regards,
Arnaud

Narasimha Narahari

Re: Changing the color

Post by Narasimha Narahari » Thu Feb 06, 2003 8:49 pm

Hi,

Thanks for ypur help but i dont think its working. I have put some more code. Please let me know if i am correctly.

thanks,
--Narasimha

public JFreeChart createChart(int type,Vector respondents, String survey,String title,String xtitle,String ytitle,String[] respArr){
CategoryDataset categoryData = createCategoryDataset(respondents,respArr);


int color=0;
int finalGradColor=1;
Paint[] paint = {Color.red,new Color(255, 154, 0),Color.blue,Color.darkGray,Color.magenta,
new Color(156, 207, 0),new Color(0, 207, 255)};

CategoryPlot plot;
HorizontalCategoryAxis axis;

JFreeChart chart;
try {

switch (type) {
case 1:


chart = ChartFactory.createVerticalBarChart3D(title, xtitle,ytitle,categoryData,
true);

((AbstractCategoryItemRenderer) chart.getCategoryPlot().getRenderer()).setCategoriesPaint(paint);
return chart;

Peter FitzPatrick

Re: Changing the color

Post by Peter FitzPatrick » Fri Feb 07, 2003 7:50 am

I was having the same problem and this thread was very helpful,

However rather than:

((AbstractCategoryItemRenderer) chart.getCategoryPlot().getRenderer()).setCategoriesPaint(paint);

Try:

chart.getCategoryPlot().setCategoriesPaint(paint);

This worked for me.

Peter FitzPatrick

Narasimha Narahari

Re: Changing the color

Post by Narasimha Narahari » Fri Feb 07, 2003 3:57 pm

Hi,
Thanks for your response, but it doesnt work either. The interface CategoryItemRenderer doesnt define setCategoriesPaint. Could you please give me a sample code.

Thanks in advance,
--Narasimha

Peter FitzPatrick

Re: Changing the color

Post by Peter FitzPatrick » Mon Feb 10, 2003 12:59 am

Narasimha

The was a mistake in my reply, sorry about that it should be:-

chart.getCategoryPlot().setSeriesPaint(paint);

Not

chart.getCategoryPlot().setCategoriesPaint(paint);

Try that instead, it should work.

Peter

Arnaud

Re: Changing the color

Post by Arnaud » Tue Feb 11, 2003 1:00 pm

Hello again,

Well... got mixed up with versions 0.9.4 and version 0.9.5... my responses dealt with version 0.9.4, and code I had experimented. As I'm currently upgrading to 0.9.5, I understand Your troubles !

Heres is what I learn, by carefully reading the javadoc - version 0.9.5 :
1) Class AbstractCategoryItemRenderer, setCategoriesPaint(java.awt.Paint[] paint) :
"Sets the paint to be used for categories under special circumstances.

This attribute is provided for the situation where there is just one series, and you want each category item to be plotted using a different color (ordinarily, the series color is used for all the items in the series). "

2) Class AbstractRenderer, setSeriesPaint(int dataset, int series, java.awt.Paint paint) :
"Sets the paint used for a series."

--> therefore, If You have multiple series and one category, You need to use the setSeriesPaint instead of the setCategoriesPaint method. I think it is a bit confusing as version 0.9.4 used the setCategoriesPaint for that case... but we now have the power to use different colors with one series and multiple categories ! So, the code You need may finally be something like this :
renderer.setSeriesPaint(0,new Color(206, 255, 206));
renderer.setSeriesPaint(1,new Color(156, 207, 0));
renderer.setSeriesPaint(2,new Color(255, 207, 156));
renderer.setSeriesPaint(3,new Color(255, 154, 0));
renderer.setSeriesPaint(4,new Color(206, 255, 255));
renderer.setSeriesPaint(5,new Color(156, 206, 255));
renderer.setSeriesPaint(6,new Color(0, 207, 255));

I think it may be interesting to have a setSeriesPaint(int dataset, int series, java.awt.Paint[] paint)... I'll have a look later on that.

Hope that time it will really help You make Your code work !
Regards,
Arnaud

David Gilbert

Re: Changing the color

Post by David Gilbert » Tue Feb 11, 2003 3:01 pm

Hi Arnaud,

What I'm hoping is that you can now override the getItemPaint(...) method in any renderer to use multiple colors within one series. I say "hoping" because I've set the code up so that it should be possible, but I haven't tested it so I could have messed something up.

In my own charts, I use a single color for each series so that it is easy to identify the series...I'm not sure of the purpose of using multiple colors within a series.

Regards,

Dave Gilbert

Locked