Skip to content

Commit

Permalink
primefaces#6812 fix horizontal bar chart
Browse files Browse the repository at this point in the history
  • Loading branch information
christophs78 committed Mar 19, 2021
1 parent a2c5e8c commit 12bb010
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import org.primefaces.component.charts.ChartRenderer;
import org.primefaces.model.charts.bar.BarChartOptions;
import org.primefaces.util.LangUtils;
import org.primefaces.util.WidgetBuilder;

public class BarChartRenderer extends ChartRenderer {
Expand Down Expand Up @@ -70,6 +71,10 @@ protected void encodeOptions(FacesContext context, String type, Object options)

writer.write("\"barPercentage\":" + barOptions.getBarPercentage());

if (LangUtils.isNotBlank(barOptions.getIndexAxis())) {
writer.write(",\"indexAxis\":\"" + barOptions.getIndexAxis() + "\"");
}

if (barOptions.getCategoryPercentage() != null) {
writer.write(",\"categoryPercentage\":" + barOptions.getCategoryPercentage());
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/primefaces/model/charts/bar/BarChartOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class BarChartOptions extends ChartOptions {
private boolean offsetGridLines = true;
private CartesianScales scales;

private String indexAxis = "x";

/**
* Gets the barPercentage
*
Expand Down Expand Up @@ -152,4 +154,20 @@ public CartesianScales getScales() {
public void setScales(CartesianScales scales) {
this.scales = scales;
}

/**
* Vertical bar chart (x) or horizontal bar char (y)
* @return
*/
public String getIndexAxis() {
return indexAxis;
}

/**
* Switches between vertical and horizontal Bar Chart
* @param indexAxis x (=vertical bar chart) or y (=horizontal bar chart)
*/
public void setIndexAxis(String indexAxis) {
this.indexAxis = indexAxis;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class HorizontalBarChartDataSet extends BarChartDataSet {
*/
@Override
public String getType() {
return "horizontalBar";
return "bar";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@
package org.primefaces.model.charts.hbar;

import org.primefaces.model.charts.bar.BarChartModel;
import org.primefaces.model.charts.bar.BarChartOptions;

/**
* Defines the horizontalBar model used to create horizontalBar chart component.
* Defines the horizontalBar model used to create bar chart component with indexAxis=y.
*/
public class HorizontalBarChartModel extends BarChartModel {

private static final long serialVersionUID = 1L;

/**
* Gets the type
*
* @return type of current chart
*/
@Override
public String getType() {
return "horizontalBar";
return "bar";
}

@Override
public BarChartOptions getOptions() {
BarChartOptions barChartOptions = super.getOptions();
barChartOptions.setIndexAxis("y");
return barChartOptions;
}
}

0 comments on commit 12bb010

Please sign in to comment.