From b3046c091bfc87f2ce36adb855a4921c9d3b3efa Mon Sep 17 00:00:00 2001 From: "Jose M. Palomar" Date: Mon, 5 Feb 2024 18:33:59 +0100 Subject: [PATCH 1/3] Allow having stacked and overlapped category series --- .../main/java/org/knowm/xchart/CategorySeries.java | 11 +++++++++++ .../internal/chartpart/PlotContent_Category_Bar.java | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/xchart/src/main/java/org/knowm/xchart/CategorySeries.java b/xchart/src/main/java/org/knowm/xchart/CategorySeries.java index 0b767e8e1..d284e8ec9 100644 --- a/xchart/src/main/java/org/knowm/xchart/CategorySeries.java +++ b/xchart/src/main/java/org/knowm/xchart/CategorySeries.java @@ -9,6 +9,8 @@ /** A Series containing category data to be plotted on a Chart */ public class CategorySeries extends AxesChartSeriesCategory { + private boolean isOverlapped = false; + private CategorySeriesRenderStyle chartCategorySeriesRenderStyle = null; /** @@ -42,6 +44,15 @@ public CategorySeries setChartCategorySeriesRenderStyle( return this; } + public boolean isOverlapped() { + return isOverlapped; + } + + public CategorySeries setOverlapped(boolean overlapped) { + isOverlapped = overlapped; + return this; + } + @Override public LegendRenderType getLegendRenderType() { diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Category_Bar.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Category_Bar.java index 97ab1120a..df101410e 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Category_Bar.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Category_Bar.java @@ -174,7 +174,7 @@ public void doPaint(Graphics2D g) { } else { yBottom = y; } - if (stylerCategory.isStacked()) { + if (stylerCategory.isStacked() && !series.isOverlapped()) { yTop += accumulatedStackOffsetPos[categoryCounter]; yBottom += accumulatedStackOffsetPos[categoryCounter]; accumulatedStackOffsetPos[categoryCounter] += (yTop - yBottom); @@ -190,7 +190,7 @@ public void doPaint(Graphics2D g) { // yBottom. } yBottom = y; - if (stylerCategory.isStacked()) { + if (stylerCategory.isStacked() && !series.isOverlapped()) { yTop -= accumulatedStackOffsetNeg[categoryCounter]; yBottom -= accumulatedStackOffsetNeg[categoryCounter]; accumulatedStackOffsetNeg[categoryCounter] += (yTop - yBottom); From d9c96bd4158715fe857e93361c910d1848f2d96b Mon Sep 17 00:00:00 2001 From: "Jose M. Palomar" Date: Wed, 7 Feb 2024 10:22:39 +0100 Subject: [PATCH 2/3] Add example for stacked bars with overlapped lines --- .../xchart/demo/charts/bar/BarChart12.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart12.java diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart12.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart12.java new file mode 100644 index 000000000..0edef8e7c --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart12.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2024 EnergĂ­a Plus. All rights reserved. + */ + +package org.knowm.xchart.demo.charts.bar; + +import org.knowm.xchart.CategoryChart; +import org.knowm.xchart.CategoryChartBuilder; +import org.knowm.xchart.CategorySeries; +import org.knowm.xchart.SwingWrapper; +import org.knowm.xchart.demo.charts.ExampleChart; + +import java.awt.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +/** + * Stacked Bars with Overlapped Line Chart + * + *

Demonstrates the following: + * + *