How to change barchart color based on frequency value

ga4696 :

I have generated this chart but I don't know how to create the color scale based on frequency

chart=ChartFactory.createXYBarChart(plotTitle,xaxis , true, yaxis, dataset);


plot = chart.getXYPlot();
    plot.getRangeAxis().setLabel("");
            plot.getDomainAxis().setLabel("");
            ValueAxis y_axis = plot.getRangeAxis();     // Y
            ValueAxis x_axis = plot.getDomainAxis();
            Font font = new Font("Veranda", Font.PLAIN, 12);
                y_axis.setTickLabelFont(font);
                x_axis.setTickLabelFont(font);
                x_axis.setTickLabelPaint(Color.black);
                y_axis.setTickLabelPaint(Color.black);
    plot.getDomainAxis().setAxisLineVisible(false);

    XYBarRenderer barRndr = (XYBarRenderer) plot.getRenderer();
    barRndr.setBarPainter(new StandardXYBarPainter());
    barRndr.setSeriesPaint(0, Color.blue);

Here is a picture of how I visualize it now with blue color: enter image description here

and here is an example of how I want it to look:

enter image description here

I am not very familiar with jfreechart so I don't know if there is such a posibility. Any help would be very much appreciated. Thanks!

trashgod :

Override getItemPaint() in your XYBarRenderer, as suggested here. Use Color.getHSBColor() to generate the desired colors, as shown here and here. Starting from this example, the XYBarRenderer shown in this initial variation simply creates a static palette of blue shades having varying saturation. The updated version below dynamically scales the color saturation to reflect the dataset frequency values passed to the renderer's constructor, as shown here.

image

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Paint;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.Range;
import org.jfree.data.general.DatasetUtils;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Year;
import org.jfree.data.xy.IntervalXYDataset;

/**
 * @see https://stackoverflow.com/a/54680803/230513
 * @see https://stackoverflow.com/a/54362133/230513
 * @see https://stackoverflow.com/a/42612723/230513
 */
public class Test {

    private static final int N = 5;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }

    private void display() {
        JFrame f = new JFrame("Data");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        IntervalXYDataset dataset = createDataset();
        JFreeChart chart = ChartFactory.createXYBarChart("Data", "Time", true,
            "Value", dataset, PlotOrientation.VERTICAL, false, false, false);
        XYPlot plot = chart.getXYPlot();
        XYBarRenderer r = new MyXYBarRenderer(dataset);
        r.setDrawBarOutline(true);
        r.setBarPainter(new StandardXYBarPainter());
        plot.setRenderer(r);
        f.add(createPanel(chart));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private class MyXYBarRenderer extends XYBarRenderer {

        private final IntervalXYDataset dataset;

        public MyXYBarRenderer(IntervalXYDataset dataset) {
            this.dataset = dataset;
        }

        @Override
        public Paint getItemPaint(int row, int col) {
            double value = dataset.getYValue(row, col);
            Range r = DatasetUtils.findRangeBounds(dataset);
            double range = r.getUpperBound() - r.getLowerBound() + 1;
            float scaled = (float) (value / range);
            return Color.getHSBColor(2f / 3, 1 - scaled, 1);
        }
    }

    private ChartPanel createPanel(JFreeChart chart) {
        final XYPlot plot = chart.getXYPlot();
        final DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
        domainAxis.setTickUnit(new DateTickUnit(DateTickUnitType.YEAR, 1));
        domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
        domainAxis.setDateFormatOverride(new SimpleDateFormat("yyyy"));
        return new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(500, 250);
            }
        };
    }

    private IntervalXYDataset createDataset() {
        TimeSeries series = new TimeSeries("Value");
        Calendar c = Calendar.getInstance();
        for (int i = 0; i < N; i++) {
            series.add(new Year(c.getTime()), i + 1);
            c.add(Calendar.YEAR, 1);
        }
        for (int i = 1; i < N; i++) {
            series.add(new Year(c.getTime()), N - i);
            c.add(Calendar.YEAR, 1);
        }
        return new TimeSeriesCollection(series);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change color of text based on text-value by using css?

How to color BarChart bar in TornadoFx?

How to change color of a HTML table (dynamically created) cell based on a value?

How to change DataGridView cell color based on value of Combobox?

How to set the color of a barchart / barchartseries?

How to change the background color of table row based on value in table cell?

How to set color of bar in barChart in JasperStudio based on value in dataset?

How to change color for the numbers based on its value using javascript and react?

How to change value of a cell based on cell fill color

How do I change the color of an infobox in shinydashboard based on a string, not a value?

change color based on the string value

How change cell table color in php based on cell value?

How to change font color in Excel based on cell value?

Change color in barchart legend

How to check a number inside a span and change its color based on the value?

How to change Background Color of ListBoxItem based on ListBoxItem Value

How to change text color based on a dynamic value using Angularjs directives?

How to Change the row Background color based on cell value

How to change color of text based on $value

How to change background color of cell based on other cell value by VBA

How to change the text color based on a value using php and html?

How do I change the color of text in an AJAX Barchart?

How do I change every value in a column except certain ones based on their frequency

Change color of a variable based on the value

In react, how to change the color of a range slider based on the value?

How to change color of a table row based on object property value?

How to change the border color of a polygon based on an attribute value in R tmap

How to change background color based on a value with NextJS and TailwindCSS?

How can I change ChartJS line color based on the value?