gpt4 book ai didi

java - GWT BarChart 如何减少条形之间的空间并删除起始线

转载 作者:行者123 更新时间:2023-11-30 09:10:10 24 4
gpt4 key购买 nike

在我的项目中我使用 GWT 图表 -

   <inherits name="com.googlecode.gwt.charts.Charts"/>

我想从所有条开始的地方删除垂直线,并减少条之间的空间。是否可以?我已经尝试了几次,但没有成功。这是我的 Java 代码:

  BarChart chart = new BarChart();

BarChartOptions opt = BarChartOptions.create();
opt.setEnableInteractivity(true);
opt.setTitle("Statistic");


DataTable dataTable = DataTable.create();
dataTable.addColumn(ColumnType.STRING, "Name");
dataTable.addColumn(ColumnType.NUMBER, "Stocks!");
dataTable.addRows(3);

dataTable.setValue(0, 0, "Units");
dataTable.setValue(1, 0, "Stock");
dataTable.setValue(2, 0, "Invoices");

dataTable.setValue(0, 1, 743644);
dataTable.setValue(1, 1, 22628);
dataTable.setValue(2, 1, 10);

// Draw the chart
chart.draw(dataTable, opt);

Barchart

I found the way to reduce the size between bars with this:
ChartArea chartArea = ChartArea.create();
chartArea.setHeight(50);
opt.setChartArea(chartArea);

最佳答案

移除垂直轴线

您只能通过将其设置为与背景相同的颜色来移除垂直轴条。在这种情况下,白色:

AxisOptions hAxisOptions = AxisOptions.create();
hAxisOptions.setBaselineColor("#FFF");

opt.setHAxisOptions(hAxisOptions);

enter image description here

如果您还想删除网格线,请将它们也设置为背景色,因为您可以指定的最小网格线数为 2:

opt.setGridlineColor("#FFF");

enter image description here

如果您希望轴线看起来与网格线相同,只需将它们设置为相同的颜色即可:

AxisOptions hAxisOptions = AxisOptions.create();
hAxisOptions.setBaselineColor("#CCC");
opt.setGridlineColor("#CCC");

opt.setHAxisOptions(hAxisOptions);

enter image description here

您可以查看 Google Charts 文档 here指定所有可能的条形图选项以验证这是达到预期效果的唯一方法。这不仅仅是 GWT 可视化 API 的限制。

减少栏之间的空间

GWT Visualization API 似乎不直接支持它,但 Google Charts 支持它。通过添加 native JavaScript 方法,您可以绕过此限制。

首先,扩展 com.google.gwt.visualization.client.visualizations.corechart.Options 类以包含 native 方法:

public class BarChartOptions extends Options {
protected BarChartOptions() {
}

public final native void setGroupWidth(String groupWidth) /*-{
this.bar = { groupWidth: groupWidth }
}-*/;

public static BarChartOptions create() {
return JavaScriptObject.createObject().cast();
}
}

新的 setGroupWidth 方法现在允许您将条形宽度指定为字符串百分比。例如。 “95%”。

BarChartOptions opts = BarChartOptions.create();
opts.setGroupWidth("95%");
// Set other chart options here

// E.g. Removing the axis line
AxisOptions axisOptions = AxisOptions.create();
axisOptions.setBaselineColor("#FFF");
opts.setHAxisOptions(axisOptions);

enter image description here

关于java - GWT BarChart 如何减少条形之间的空间并删除起始线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22659063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com