2

我想在顶部或每个条的主体上添加带有条数值的标签BarChart

我在 API 和 CSS 参考指南中都进行了搜索,但找不到任何定义它的选项。

当前版本的 JavaFX 图表是否支持此功能?

4

2 回答 2

3

这个怎么样:

import javafx.scene.chart.Axis;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;

class CustomStackedBarChart extends StackedBarChart<String, Number> {

    public CustomStackedBarChart(CategoryAxis xAxis, Axis<Number> yAxis) {
        super(xAxis, yAxis);
    }

    @Override
    protected void layoutPlotChildren() {
        super.layoutPlotChildren();

        for (Series<String, Number> series : getData()) {
            for (Data<String, Number> data : series.getData()) {
                StackPane bar = (StackPane) data.getNode();

                final Text dataText = new Text(data.getYValue() + "");
                bar.getChildren().add(dataText);

            }
        }
    }
}
于 2016-09-12T14:39:41.283 回答
1

我正在尝试做同样的事情。到目前为止,这就是我所拥有的。

class CustomStackedBarChart extends StackedBarChart<String, Number> {

    public CustomStackedBarChart( CategoryAxis xAxis, Axis<Number> yAxis ) {
        super( xAxis, yAxis );

        setAnimated( false );
        setLegendVisible( false );
        setTitleSide( Side.TOP );
        setTitle( "SSPP Diurnal Symbol Rate" );
        setMinHeight( 500 );
        setMinWidth( 1000 );
    }

    @Override
    protected void layoutPlotChildren() {
        super.layoutPlotChildren();

        // Each individual Data element has its own bar.
        // TODO add label to each bar.
        for ( Series<String, Number> series : getData() ) {
            for ( Data<String, Number> data : series.getData() ) {
                StackPane bar = (StackPane) data.getNode();

                Label label = null;

                for ( Node node : bar.getChildrenUnmodifiable() ) {
                    LOGGER.debug( "Bar has child {}, {}.", node,
                        node.getClass() );
                    if ( node instanceof Label ) {
                        label = (Label) node;
                        break;
                    }
                }

                if ( label == null ) {
                    label = new Label( series.getName() );
                    label.setRotate( 90.0 );
                    bar.getChildren().add( label );
                }
                else {
                    label.setText( series.getName() );
                }
            }
        }
    }
}
于 2013-03-28T00:36:18.107 回答