Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/knowm/XChart into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
timmolter committed Apr 25, 2024
2 parents 6119de5 + 5ab11e9 commit 446ca07
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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
*
* <p>Demonstrates the following:
*
* <ul>
* <li>bar series are stacked
* <li>line series is overlapped
*/
public class BarChart12 implements ExampleChart<CategoryChart> {

public static void main(String[] args) {

ExampleChart<CategoryChart> exampleChart = new BarChart12();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}

private static List<String> getMonths() {
return Arrays.asList(
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
}

private static List<Double> getRandomValues(int count) {

List<Double> values = new ArrayList<>(count);
Random rand = new Random();
for (int i = 0; i < count; i++) {
values.add(rand.nextDouble() * 1000);
}
return values;
}

@Override
public CategoryChart getChart() {

// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Month")
.yAxisTitle("Consumption")
.build();

// Customize Chart
chart.getStyler().setPlotGridVerticalLinesVisible(false);
chart.getStyler().setLegendVisible(true);
chart.getStyler().setStacked(true);
chart
.getStyler()
.setSeriesColors(
new Color[] {
Color.decode("#2133D0"),
Color.decode("#FF3B47"),
Color.decode("#FFBD00"),
Color.DARK_GRAY
});

List<String> months = getMonths();
List<Double> period1Values = getRandomValues(12);
List<Double> period2Values = getRandomValues(12);
List<Double> period3Values = getRandomValues(12);
List<Double> averageValues = new ArrayList<>();
for (int i = 0; i < 12; i++) {
averageValues.add((period1Values.get(i) + period2Values.get(i) + period3Values.get(i)) / 3);
}

// Series
CategorySeries staked1 = chart.addSeries("Period 1", months, period1Values);
CategorySeries staked2 = chart.addSeries("Period 2", months, period2Values);
CategorySeries staked3 = chart.addSeries("Period 3", months, period3Values);
CategorySeries overlappedLine = chart.addSeries("Average", months, averageValues);
overlappedLine.setOverlapped(true);
overlappedLine.setChartCategorySeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Line);

return chart;
}

@Override
public String getExampleChartName() {

return getClass().getSimpleName() + " - Stacked Bars with Overlapped Line Chart";
}
}
11 changes: 11 additions & 0 deletions xchart/src/main/java/org/knowm/xchart/CategorySeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 446ca07

Please sign in to comment.