Skip to content

Gradient fill? #730

Open
Open

Description

@robm99x

More of a question/request ... is there a way to gradient fill CategoryChart for bar charts? TIA!

Activity

  1. timmolter commented on Jun 22, 2026

    @timmolter
    Member

    Re-labeling this as a Feature Request — gradient fill for category bar charts isn't supported today (bars are painted with a single flat series.getFillColor()), so I'm tracking it as an enhancement rather than a how-to.

    How it could be implemented in the future

    The bar fill happens in one place, PlotContent_Category (and its sibling PlotContent_HorizontalBar):

    g.setColor(series.getFillColor());
    g.fill(barPath);

    Since Graphics2D.fill() accepts any java.awt.Paint, a vertical gradient is a small, localized change — swap the flat color for a GradientPaint:

    Color fill = series.getFillColor();
    g.setPaint(new GradientPaint(
        (float) xOffset, (float) zeroOffset, fill,           // bar base
        (float) xOffset, (float) yOffset,    fill.brighter() // bar top
    ));
    g.fill(barPath);

    To do it properly as an opt-in, backward-compatible feature, the work would be roughly:

    1. Add a setting to CategoryStyler (e.g. setBarFillGradientEnabled(boolean), optionally with a configurable end color).
    2. Gate the GradientPaint branch behind that flag in PlotContent_Category, keeping the current flat fill as the default.
    3. Mirror the same logic in PlotContent_HorizontalBar for orientation parity, and update the legend marker fill so the legend matches the bars.
    4. Add a demo to the bar-chart examples in xchart-demo.

    Note that XChart already has gradient interpolation infrastructure for heat maps (Styler.getRangeColors() + the gradient color logic in PlotContent_HeatMap), which could serve as a reference if multi-stop gradients are ever wanted here.

    In the meantime, the GradientPaint snippet above can be applied in a custom build/fork to achieve the effect today.

    PRs welcome!

  2. mvanhorn commented on Oct 13, 2026

    @mvanhorn
    Contributor

    CategoryStyler and HorizontalBarStyler add setBarFillGradientEnabled, initialized false so existing charts keep the flat fill. When the flag is on, PlotContent_Category and PlotContent_HorizontalBar fill each bar with a non-cyclic GradientPaint from the series fill at the segment base to fill.brighter() at the value end; negative bars reverse those ends, coincident endpoints stay flat, and the Graphics2D paint from before the gradient is restored.

    #1037

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions