-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/knowm/XChart into develop
- Loading branch information
Showing
3 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart12.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters