BarChart 1.0.19: Bar-Color depending on the value

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
talentfree
Posts: 4
Joined: Wed Feb 11, 2015 9:55 am
antibot: No, of course not.

BarChart 1.0.19: Bar-Color depending on the value

Post by talentfree » Wed Feb 11, 2015 10:25 am

Hi,

I'm trying to colorize the bars in a barchart depending on their value (percentages). The value should be the availibility of a service each day, and if it drops below a threshhold, the bar should go from green to red.
I am trying and just can't get through it.

Maybe someone can push me on the right track?

My code is like:

Code: Select all

public static void main(String arg[])
{
	generateBarChart();
}

	static private File generateBarChart()
			throws IOException {
		System.out.println("generating bar chart");
		File result = File.createTempFile("temp", ".png");

		IntervalXYDataset dataset1 = createDataset1();

		XYBarRenderer renderer1 = new XYBarRenderer(0.0D);
		renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
				"{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"),
				new DecimalFormat("0.00")));
		DateAxis domainAxis = new DateAxis("Date");
		domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
		NumberAxis valueAxis = new NumberAxis("Availability");
		XYPlot plot = new XYPlot(dataset1, domainAxis, valueAxis, renderer1);
		plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
		JFreeChart chart = new JFreeChart("Availibility",
				JFreeChart.DEFAULT_TITLE_FONT, plot, true);

		ChartUtilities.saveChartAsPNG(result, chart, 1200, 600);

		return result;
	}




	private static IntervalXYDataset createDataset1() {
		TimeSeries timeseries = new TimeSeries("availibility");
		timeseries.add(new Day(1, 3, 2002), 99.9D);
		timeseries.add(new Day(2, 3, 2002), 99.4D);
		timeseries.add(new Day(3, 3, 2002), 99.299999999999D);
		timeseries.add(new Day(4, 3, 2002), 99.299999999999D);
		timeseries.add(new Day(5, 3, 2002), 95.4D);
		timeseries.add(new Day(6, 3, 2002), 99.299999999999D);
		timeseries.add(new Day(7, 3, 2002), 95.5D);
		timeseries.add(new Day(8, 3, 2002), 97.299999999999D);
		timeseries.add(new Day(9, 3, 2002), 94.4D);
		timeseries.add(new Day(10, 3, 2002), 49.6D);
		timeseries.add(new Day(11, 3, 2002), 99.299999999999D);
		timeseries.add(new Day(12, 3, 2002), 99.200000000001D);
		timeseries.add(new Day(13, 3, 2002), 99.200000000001D);
		timeseries.add(new Day(14, 3, 2002), 99.200000000001D);
		timeseries.add(new Day(15, 3, 2002), 99.200000000001D);
		return new TimeSeriesCollection(timeseries);
	}

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: BarChart 1.0.19: Bar-Color depending on the value

Post by paradoxoff » Thu Feb 12, 2015 1:39 pm

See the second entry in the FAQ.

talentfree
Posts: 4
Joined: Wed Feb 11, 2015 9:55 am
antibot: No, of course not.

Re: BarChart 1.0.19: Bar-Color depending on the value

Post by talentfree » Thu Feb 12, 2015 2:24 pm

omg, easy as I feared it is... I just didn't see the entry how to do it while i was searching.

Locked