Question:
In my chart I only have one serie, and I would like -according to the value- to set a different color for the bar. For exemple :
- CategoryPlot plot = chart.getCategoryPlot();
- CategoryDataset dataset = plot.getDataset(0);
- Number value = dataset.getValue(dataset.getRowKey(0), dataset.getColumnKey(0));
- Double val = value.doubleValue();
- if (val <= 20.0) {
- BarRenderer renderer = (BarRenderer) plot.getRenderer();
- Paint tmp = renderer.getItemPaint(row, column);
- /*
- ** Help Please
- */
- }
- return chart;
1. How do I set a color to a single Bar ?
2. How do I apply that to my chart ?
Answer:
You'll need to create your own subclass of BarRenderer and override getItemPaint(). Instead of choosing a color based on column, choose it based on your value. Here's an outline to show how the existing BarRenderer works.
- plot.setRenderer(new MyRender());
- ...
- class MyRender extends BarRenderer {
- @Override
- public Paint getItemPaint(int row, int col) {
- System.out.println(row + " " + col + " " + super.getItemPaint(row, col));
- return super.getItemPaint(row, col);
- }
- }
底下範例將 Bar Chart 定義下面的數值範圍使用不同的顏色顯示:
- BarChartDemo5.java
- package charts.bar.category;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Paint;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JPanel;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.ChartPanel;
- import org.jfree.chart.JFreeChart;
- import org.jfree.chart.axis.AxisLocation;
- import org.jfree.chart.axis.CategoryAxis;
- import org.jfree.chart.axis.NumberAxis;
- import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
- import org.jfree.chart.plot.CategoryPlot;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.chart.renderer.category.BarRenderer;
- import org.jfree.chart.title.TextTitle;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.category.DefaultCategoryDataset;
- import org.jfree.ui.ApplicationFrame;
- import org.jfree.ui.RefineryUtilities;
- // https://code.google.com/p/test-chart/source/browse/trunk/src/main/java/es/efor/plandifor/demo/BarChartDemo5.java?r=2
- public class BarChartDemo5 extends ApplicationFrame
- {
- public static List
Values = new ArrayList(); - public BarChartDemo5(String s)
- {
- super(s);
- JPanel jpanel = createDemoPanel();
- jpanel.setPreferredSize(new Dimension(600, 400));
- setContentPane(jpanel);
- }
- private static CategoryDataset createDataset()
- {
- DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
- String s = "Prison Population Rates";
- defaultcategorydataset.addValue(59D, s, "Norway"); Values.add(59D);
- defaultcategorydataset.addValue(69D, s, "Switzerland"); Values.add(69D);
- defaultcategorydataset.addValue(85D, s, "France"); Values.add(85D);
- defaultcategorydataset.addValue(93D, s, "Syria"); Values.add(93D);
- defaultcategorydataset.addValue(96D, s, "Germany"); Values.add(96D);
- defaultcategorydataset.addValue(111D, s, "China"); Values.add(111D);
- defaultcategorydataset.addValue(116D, s, "Australia"); Values.add(116D);
- defaultcategorydataset.addValue(121D, s, "Egypt"); Values.add(121D);
- defaultcategorydataset.addValue(129D, s, "England & Wales"); Values.add(129D);
- defaultcategorydataset.addValue(157D, s, "New Zealand"); Values.add(157D);
- defaultcategorydataset.addValue(205D, s, "Chile"); Values.add(205D);
- defaultcategorydataset.addValue(229D, s, "Iran"); Values.add(229D);
- defaultcategorydataset.addValue(359D, s, "Singapore"); Values.add(359D);
- defaultcategorydataset.addValue(404D, s, "South Africa"); Values.add(404D);
- defaultcategorydataset.addValue(406D, s, "Ukraine"); Values.add(406D);
- defaultcategorydataset.addValue(686D, s, "USA"); Values.add(686D);
- return defaultcategorydataset;
- }
- private static JFreeChart createChart(CategoryDataset categorydataset)
- {
- JFreeChart jfreechart = ChartFactory.createBarChart("Prison Population Rates - Selected Countries", "Country", "Prisoners Per 100,000 National Population", categorydataset, PlotOrientation.HORIZONTAL, true, true, false);
- jfreechart.addSubtitle(new TextTitle("Source: http://www.homeoffice.gov.uk/rds/pdfs2/r188.pdf", new Font("Dialog", 2, 10)));
- jfreechart.setBackgroundPaint(Color.white);
- CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
- categoryplot.setBackgroundPaint(Color.lightGray);
- categoryplot.setRangeGridlinePaint(Color.white);
- categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
- MyRender barrenderer = new MyRender(Values);
- //BarRenderer or = (BarRenderer)categoryplot.getRenderer();
- categoryplot.setRenderer(barrenderer);
- //barrenderer.setBaseItemLabelsVisible(true);
- barrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
- CategoryAxis categoryaxis = categoryplot.getDomainAxis();
- categoryaxis.setCategoryMargin(0.0D);
- categoryaxis.setUpperMargin(0.02D);
- categoryaxis.setLowerMargin(0.02D);
- NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
- numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
- numberaxis.setUpperMargin(0.10000000000000001D);
- return jfreechart;
- }
- public static JPanel createDemoPanel()
- {
- JFreeChart jfreechart = createChart(createDataset());
- return new ChartPanel(jfreechart);
- }
- public static void main(String args[])
- {
- BarChartDemo5 barchartdemo5 = new BarChartDemo5("Bar Chart Demo 5");
- barchartdemo5.pack();
- RefineryUtilities.centerFrameOnScreen(barchartdemo5);
- barchartdemo5.setVisible(true);
- }
- public static class MyRender extends BarRenderer {
- List
values; - public MyRender(List
values){ this.values = values;} - @Override
- public Paint getItemPaint(int row, int col) {
- double value = values.get(col);
- int h = ((int)value)/100;
- if(value<100) return Color.GREEN;
- else if(h<3) return Color.yellow;
- else if(h<5) return Color.orange;
- else return Color.red;
- }
- }
- }
沒有留言:
張貼留言