JavaFX BarChart 实现 - 为什么使用 StackPanes 作为条形图?

JavaFX BarChart implementation - why use StackPanes for bars?

我最近一直在深入研究 JavaFX 中图表的实现,并注意到在 BarChart.java 中,StackPanes 用于各个条形图,如下所示:

bar = new StackPane();
item.setNode(bar);
bar.getStyleClass().addAll("chart-bar", "series" + seriesIndex, "data" + itemIndex);

稍后(在 layoutPlotChildren(...)):

bar.resizeRelocate(xPos, bottom, barWidth, top - bottom);

在我看来,更合乎逻辑的选择是使用矩形,特别是因为 JavaFX Peformance Tips and Tricks 声明使用形状 classes(即使必须重新创建它们,因为它们不能直接调整大小比使用 Region/Pane) 更快。

我问这个问题的主要原因是因为我有一个自定义图表实现(蜡烛图),它使用矩形作为蜡烛,我希望条形图的样式相同。为矩形制作样式 class 使用不同的 CSS 属性(-fx-fill-fx-smooth-fx-stroke 等),而 Region/Pane 使用 -fx-background-color-fx-border-color,甚至没有指定 -fx-smooth 的选项,所以我不能对蜡烛图和柱状图使用一种主要样式 class。

BarChart 使用 StackPane 的事实给我留下了几个问题:

why use StackPanes for bars?

我不了解 JavaFX 团队的想法,但我怀疑选择 是因为 CSS 支持。因为 StackPane 继承自 Region, it supports a very rich set of styleable properties, such as the background, border, and even shape. So in a BarChart, you could set arbitrary background images on the bars just using an external style sheet, for example. By contrast, Shapes 支持一组更有限的可设置样式的属性。无论如何,理论上,直接使用 StackPane 而不是 Region 将允许向条形图添加更多节点的可能性,尽管我认为任何此类代码都非常难看。

The color of the bars in BarChart are specified with the -fx-bar-fill attribute... how does this actually work?

-fx-bar-fill 是一个 looked-up color that is defined in the default stylesheet, modena.css。一般来说,默认样式 sheet 的文档很差:除了查看上面链接的源代码之外,没有真正的方法知道为哪些节点类型定义了哪些查找颜色(尽管,公平地说,它是很难看出任何此类文档以任何形式携带正确信息并且比 css 文件更容易访问)的样子。

In the CandleStickChart implementation in Ensemble8... again is this choice arbitrary or are there good reasons for using these classes as opposed to 2 Rectangles or one Rectangle and 1 Line, say.

鉴于烛台图表节点看起来像一条线和一个矩形,使用包含一条线和其他内容的 Group 是有意义的。如果您接受有关条形图 CSS 的论点,那么使用某种 RegionRegion 子类对我来说是有意义的。