Gradient color in Bar/Pie

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
zulfikaralib
Posts: 14
Joined: Thu Sep 22, 2005 5:38 am
Location: Mumbai, India
Contact:

Gradient color in Bar/Pie

Post by zulfikaralib » Tue Sep 16, 2008 10:59 am

Hi All,

I've recently ported from ver 0.9.7 to 1.0.10

Earlier my graphs used to have a gradient effect on the facing side of the bar. Now after doing the changes for 1.0.10 the gradient effect changes to a single color

The following is the code that I use to render my Bar chart which renders the bars in single color.

Would really help if any of you could guide me through this. I need the bars to get displayed in gradient effect.

Code: Select all

private CategoryPlot getCategoryPlot( ArrayList a_lineChartData, ChartDisplayOptions a_displayOptions ) throws Exception
{
	DefaultCategoryDataset dataset = new DefaultCategoryDataset();

	String[] w_labels = a_displayOptions.getXAxisLabels();
	for( int i=0; i<a_lineChartData.size(); i++ )
	{
		ChartData w_data = (ChartData)a_lineChartData.get(i);
		double[] w_Y = w_data.getY();
		if( w_labels == null ) w_labels = w_data.getLabels();
		String[] w_links = w_data.getLinks();

		for( int j=0; j<w_Y.length; j++ )
		{
			String w_legendLabel = null;
			if(w_data.getLegendLabel() != null && "".equals(w_data.getLegendLabel().trim())){
				w_legendLabel  = "";
			}else{
				w_legendLabel  = w_data.getLegendLabel();
			}
			dataset.addValue(w_Y[j], w_legendLabel, w_labels[j] );
		}
	}
	boolean isVertical =
		( a_displayOptions.getChartType() == ChartDisplayOptions.BAR_CHART
		  || a_displayOptions.getChartType() == ChartDisplayOptions.VERT_STACK_CHART );

	// create the chart...
	CategoryAxis categoryAxis = null;
	if( isVertical )
	{
		categoryAxis = a_displayOptions.getIsThreeD()
			? new CategoryAxis3D(a_displayOptions.getXAxisLabel())
			: new CategoryAxis(a_displayOptions.getXAxisLabel());

			if(a_displayOptions.getXAxisLabelAngle() == 90)
				((CategoryAxis)categoryAxis).setCategoryLabelPositions(CategoryLabelPositions.UP_90);
	}
	else
	{
		// there is no 3D vertical category
		categoryAxis = new CategoryAxis(a_displayOptions.getXAxisLabel());
	}
	categoryAxis.setLabelFont(a_displayOptions.getAxisTitleFont());
	categoryAxis.setTickLabelFont(a_displayOptions.getAxisLabelFont());
	categoryAxis.setTickLabelPaint(a_displayOptions.getAxisLabelColor());

	ValueAxis valueAxis = null;
	if( isVertical )
	{
		valueAxis = a_displayOptions.getIsThreeD()
		? new NumberAxis3D(a_displayOptions.getYAxisLabel())
		: new NumberAxis(a_displayOptions.getYAxisLabel());
		((NumberAxis)valueAxis).setVerticalTickLabels(a_displayOptions.getIsYAxisTitleToBeRotated());
	}
	else
	{
		valueAxis = a_displayOptions.getIsThreeD()
		? new NumberAxis3D(a_displayOptions.getYAxisLabel())
		: new NumberAxis(a_displayOptions.getYAxisLabel());
	}
	valueAxis.setLabelFont(a_displayOptions.getAxisTitleFont());
	valueAxis.setTickLabelFont(a_displayOptions.getAxisLabelFont());
	valueAxis.setTickLabelPaint(a_displayOptions.getAxisLabelColor());
	if(a_displayOptions.getUpperMargin() > 0)
		valueAxis.setUpperMargin(a_displayOptions.getUpperMargin());
	valueAxis.setVerticalTickLabels(false);

	NumberFormat nf = NumberFormat.getInstance(a_displayOptions.getNumberLocale());
	nf.setMaximumFractionDigits(a_displayOptions.getYAxisLabelPrecision());
	nf.setMinimumFractionDigits(a_displayOptions.getYAxisLabelPrecision());
	((NumberAxis)valueAxis).setNumberFormatOverride(nf);

	NumberFormat w_nfToolTip = NumberFormat.getIntegerInstance(a_displayOptions.getNumberLocale());
	
	BarRenderer renderer = null;
	CategoryToolTipGenerator toolTipGen = new StandardCategoryToolTipGenerator("{1}={2}",w_nfToolTip);
	CategoryURLGenerator urlGen = new DigiteCategoryURLGenerator(a_lineChartData);


	switch( a_displayOptions.getChartType() )
	{
		case ChartDisplayOptions.BAR_CHART :
		case ChartDisplayOptions.HOR_BAR_CHART :
		{
			renderer = a_displayOptions.getIsThreeD()
				? new BarRenderer3D() : new BarRenderer();
		}
		break;
		case ChartDisplayOptions.HOR_STACK_CHART :
		{
			// no 3d for this category
			renderer = new StackedBarRenderer();
		}
		break;
		case ChartDisplayOptions.VERT_STACK_CHART :
		{
			renderer = a_displayOptions.getIsThreeD()
				? (BarRenderer)new StackedBarRenderer3D()
				: (BarRenderer)new StackedBarRenderer();
		}
		break;
	}

	renderer.setItemMargin(0.0);
	renderer.setBaseOutlinePaint(Color.black);
	renderer.setBaseOutlineStroke( new BasicStroke(1) );
	renderer.setToolTipGenerator(toolTipGen);
	renderer.setItemURLGenerator(urlGen);
	renderer.setDrawBarOutline(true);
	DecimalFormat df = new DecimalFormat();
	df.setMaximumFractionDigits(a_displayOptions.getDataLabelPrecision());
	df.setMinimumFractionDigits(a_displayOptions.getDataLabelPrecision());
	renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator(df.toLocalizedPattern(),df));
	
	for( int i=0; i<a_lineChartData.size(); i++ )
		renderer.setSeriesPaint(i, a_displayOptions.getBarColor(i) );
	CategoryPlot cplot = isVertical
		? (CategoryPlot)new CategoryPlot(dataset, categoryAxis, valueAxis, renderer)
		: (CategoryPlot)new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
	
	if(isVertical)
		cplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

	if(a_displayOptions.isShowGoals())
	{
		cplot.setBackgroundPaint(Color.BLACK);
		cplot.setDomainGridlinePaint(Color.GREEN);

		IntervalMarker w_LCL_Marker = new IntervalMarker(a_displayOptions.getLCL(), a_displayOptions.getLCL(), new Color(255, 150, 0, 255), new BasicStroke(1f), new Color(255, 0, 0, 255), new BasicStroke(1f), 1f);
		IntervalMarker w_UCL_Marker = new IntervalMarker(a_displayOptions.getTarget(), a_displayOptions.getTarget(), new Color(0, 255, 0, 255), new BasicStroke(1f), new Color(255, 0, 0, 255), new BasicStroke(1f), 1f);
		IntervalMarker w_target_Marker = new IntervalMarker(a_displayOptions.getUCL(), a_displayOptions.getUCL(), new Color(255, 0, 0, 255), new BasicStroke(1f), new Color(255, 0, 0, 255), new BasicStroke(1f), 1f);
		cplot.addRangeMarker(w_LCL_Marker);
		cplot.addRangeMarker(w_UCL_Marker);
		cplot.addRangeMarker(w_target_Marker);

		if(cplot.getRangeAxis().getRange().getUpperBound() < a_displayOptions.getUCL())
		{
			Range w_newRange = new Range(cplot.getRangeAxis().getRange().getLowerBound(), a_displayOptions.getUCL()+1);
			cplot.getRangeAxis().setRange(w_newRange);
		}
	}

	if( a_displayOptions.getIsGridToBeDisplayed() )
	{
		cplot.setDomainGridlinesVisible(true);
		cplot.setDomainGridlinePaint(a_displayOptions.getGridLineColor());
	}
	else
	{
		cplot.setDomainGridlinesVisible(false);
	}

	cplot.getRangeAxis().setTickLabelsVisible(true);
	cplot.getRangeAxis().setLabelFont(a_displayOptions.getDataLabelFont());
	
	return cplot;
}


public void doChart (ArrayList a_lineChartData, ChartDisplayOptions a_displayOptions) throws Exception
{
	CategoryPlot w_CatPlot = null;
	XYPlot w_XYPlot = null;
	PiePlot w_PiePlot = null;
	String w_PlotType = "XYPlot";

	if( a_displayOptions.getChartType() == ChartDisplayOptions.PIE_CHART )
	{
		a_displayOptions.setIsThreeD(true);
		a_displayOptions.setChartWidth(a_displayOptions.getChartHeight() + (int)(a_displayOptions.getChartHeight()*0.35));
		w_PiePlot = getPiePlot( a_lineChartData, a_displayOptions );
		w_PlotType = "PiePlot";
	}
	else if( a_displayOptions.getChartType() == ChartDisplayOptions.SCATTER_CHART )
	{
		w_XYPlot = getScatterPlot( a_lineChartData, a_displayOptions );
	}
	else if( a_displayOptions.getChartType() == ChartDisplayOptions.LINE_CHART )
	{
		if(!a_displayOptions.getIsTextXAxisLineChart()){
			w_XYPlot = getXYPlot( a_lineChartData, a_displayOptions );
		}else{
			w_CatPlot = getLinePlot(a_lineChartData,a_displayOptions);
			w_PlotType = "Category";
		}
	}
	else if(a_displayOptions.getChartType() == ChartDisplayOptions.BUBBLE_CHART)
	{
		w_XYPlot = getBubblePlot(a_lineChartData,a_displayOptions);
	}
	else {

		// its some kind of bar charts only here.
		// if we are to implement other types of chart the else clause eveidently has to be suitably changed.
		w_CatPlot = getCategoryPlot( a_lineChartData, a_displayOptions );
		w_PlotType = "Category";
	}
	if( a_displayOptions.getChartType() == ChartDisplayOptions.PIE_CHART ) // no w_plot background color for pie
	{
		w_PiePlot.setBackgroundPaint(a_displayOptions.getPlotBackgroundColor());
		w_PiePlot.setBackgroundPaint(null);//a_displayOptions.getBackgroundColor());
		w_PiePlot.setOutlinePaint(null);
	}
	if("XYPlot".equals(w_PlotType))
	{
		w_XYPlot.setBackgroundPaint(a_displayOptions.getPlotBackgroundColor());
		m_chart = new JFreeChart(a_displayOptions.getTitle(), a_displayOptions.getTitleFont(), w_XYPlot, a_displayOptions.getIsLegendToBeDisplayed());
	}
	else if("Category".equals(w_PlotType))
	{
		w_CatPlot.setBackgroundPaint(a_displayOptions.getPlotBackgroundColor());
		m_chart = new JFreeChart(a_displayOptions.getTitle(), a_displayOptions.getTitleFont(), w_CatPlot, a_displayOptions.getIsLegendToBeDisplayed());
	}
	else if("PiePlot".equals(w_PlotType))
	{
		w_PiePlot.setBackgroundPaint(a_displayOptions.getPlotBackgroundColor());
		m_chart = new JFreeChart(a_displayOptions.getTitle(), a_displayOptions.getTitleFont(), w_PiePlot, a_displayOptions.getIsLegendToBeDisplayed());
	}
	if( m_chart.getTitle() != null ) m_chart.getTitle().setPaint(a_displayOptions.getTitleColor());
	String w_subTitle = a_displayOptions.getSubTitle();
	if(  w_subTitle != null && w_subTitle.trim().length() > 0 )
		m_chart.addSubtitle(new TextTitle(w_subTitle, a_displayOptions.getSubTitleFont()));

	// legend related stuff
	if( a_displayOptions.getIsLegendToBeDisplayed() )
	{
		// we have to set the font, color and position. now here we go in a round about way
		// in tryign to figure out where exactly the legend should be placed - just to keep up with vestiges of old code
		if( a_displayOptions.isLegendVertical() )
		{
			m_chart.getLegend().setPosition( a_displayOptions.getLegendLeftBottomY() < 0.5 ? RectangleEdge.LEFT : RectangleEdge.RIGHT );
		}
		else
		{
			m_chart.getLegend().setPosition( a_displayOptions.getLegendLeftBottomX() < 0.5 ? RectangleEdge.BOTTOM : RectangleEdge.TOP );
		}

		//if( a_displayOptions.getLegendLeftBottomX() < 0.5 ) // its west
		if( m_chart.getLegend() instanceof LegendTitle )
		{
			LegendTitle w_legend = (LegendTitle)m_chart.getLegend();
			w_legend.setItemFont(a_displayOptions.getLegendFont());
			w_legend.setBackgroundPaint(Color.WHITE);//a_displayOptions.getLegendBackgroundColor());
			w_legend.setItemPaint(Color.BLACK);// a_displayOptions.getBackgroundColor() ); // no bounding box for legend
		}
	}

	String w_RGB = "255,255,255";
	if(w_RGB == null) w_RGB="255,255,255";
	   StringTokenizer w_st = new StringTokenizer(w_RGB,",");
	   Color w_BckCol1= new Color(Integer.parseInt(w_st.nextToken()),Integer.parseInt(w_st.nextToken()),Integer.parseInt(w_st.nextToken()));
	w_RGB = "255,215,215";
	if(w_RGB == null) w_RGB="255,215,215";
	   w_st = new StringTokenizer(w_RGB,",");
	   Color w_BckCol2= new Color(Integer.parseInt(w_st.nextToken()),Integer.parseInt(w_st.nextToken()),Integer.parseInt(w_st.nextToken()));

	GradientPaint w_GradPaint = new GradientPaint(0,0, w_BckCol1, a_displayOptions.getChartWidth()/5, a_displayOptions.getChartHeight()/5,w_BckCol2, true); // true means to repeat pattern

	m_chart.setBackgroundPaint( w_GradPaint);

	ChartRenderingInfo w_info = new ChartRenderingInfo(new StandardEntityCollection());
	BufferedOutputStream w_fout = new BufferedOutputStream(new FileOutputStream(a_displayOptions.getGifFileName()),5000);

	ChartUtilities.writeChartAsJPEG(w_fout,1.0f ,m_chart,
		a_displayOptions.getChartWidth(), a_displayOptions.getChartHeight(), w_info);
	w_fout.close();
	m_linkMap = getLinkMap( a_displayOptions.getMapName(), w_info );

}
Zulfikarali Barodawala

zulfikaralib
Posts: 14
Joined: Thu Sep 22, 2005 5:38 am
Location: Mumbai, India
Contact:

Post by zulfikaralib » Wed Sep 17, 2008 3:34 pm

Hi All,

Managed to get the gradient effect on 3D Bar Charts... I have implemented the drawItem method of BarRenderer3D and now it works just fine.

Will post the code soon for that too.

Just wish if could have the Gradient effect built in with JFreeChart.


Thanks,
Zulfikarali
Zulfikarali Barodawala

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Thu Sep 18, 2008 2:39 pm

I think it is just the 3D bars that don't have gradient support. I will be interested to see how you implemented it and hopefully we can get something included in a future release.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

zulfikaralib
Posts: 14
Joined: Thu Sep 22, 2005 5:38 am
Location: Mumbai, India
Contact:

Post by zulfikaralib » Thu Sep 18, 2008 3:02 pm

Hi David,

Good day.

I am waiting for the release to happen. As soon as the code is released and the charts come clean without 0 defects, i'll be posting the code.

Don't want to post it now as it is under testing. Will keep you posted regarding this.

Thanks,
Zulfikarali
Zulfikarali Barodawala

sachindevade
Posts: 3
Joined: Thu Jan 29, 2009 3:24 pm
Contact:

please can u tell me how apply Gradient Paint color 3D Chart

Post by sachindevade » Thu Jan 29, 2009 3:57 pm

i am tring to apply Gradient Paint color on 3D Chart So i try with stackedbar3D Chart, bar3D Chart but Gradient Paint color not apply
on it



please can u tell me how apply Gradient Paint color 3D Chart

Locked