我想在条形图中画一条线。该行反映了与其他值进行比较的特定值。我将solution for a LineChart应用于我的情况(参见代码)。但是,没有画线。
public XYChart buildBarChart ()
{
Line valueMarker = new Line ();
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();
BarChart<String,Number> barChart = new BarChart<String,Number> (xAxis,yAxis);
barChart.setBarGap (-10);
barChart.setCategoryGap (0);
barChart.getYAxis().setTickLabelsVisible(false);
barChart.getYAxis().setOpacity(0);
yAxis.setLabel ("Score");
// update marker
Node chartArea = barChart.lookup (".chart-plot-background");
Bounds chartAreaBounds = chartArea.localToScene (chartArea.getBoundsInLocal ());
// remember scene position of chart area
yShift = chartAreaBounds.getMinY();
// set x parameters of the valueMarker to chart area bounds
valueMarker.setStartX (chartAreaBounds.getMinX());
valueMarker.setEndX (chartAreaBounds.getMaxX());
// find pixel position of that value
double displayPosition = yAxis.getDisplayPosition (valNederland);
// update marker
valueMarker.setStartY (yShift + displayPosition);
valueMarker.setEndY (yShift + displayPosition);
BarChart.Series<String,Number> series1 = new BarChart.Series<String,Number> ();
BarChart.Series<String,Number> series2 = new BarChart.Series<String,Number> ();
BarChart.Series<String,Number> series3 = new BarChart.Series<String,Number> ();
BarChart.Series<String,Number> series4 = new BarChart.Series<String,Number> ();
series1.getData ().add (new XYChart.Data<String,Number> (sRegio, valRegio_2015));
series2.getData ().add (new XYChart.Data<String,Number> (sOmgeving, valOmgeving));
series3.getData ().add (new XYChart.Data<String,Number> (sRest, valRest));
series4.getData ().add (new XYChart.Data<String,Number> (sNl, valNederland));
barChart.getData().addAll (series1, series2, series3, series4);
return barChart;
} /*** buildBarChart ***/
这种情况必不可少的是拥有ChartArea的坐标。当我在调试器中检查ChartArea的属性时,我发现_geomBounds的所有值都是&#39; unlogic&#39;,即maxX / Y = -1和minX / Y = -1。在为chartASreaBounds分配值后,我只检查一行。
对我来说,似乎barChart查找失败,因为之前我遇到过类似的问题。有没有人建议如何纠正这种情况?
修改
BarChart在构造函数中创建如下:
public Charter (int nTimes, int nCategories, float [] years, String [] titles, String label, Indicator ind, float [] past)
{
this ();
// all kind of code to do somethong with the parameters omitted
// ...
// Setting up the BarChart. Note that past == null in my experiments
HBox hBox = new HBox ();
XYChart bar = buildBarChart ();
bar.setPrefHeight (height);
bar.setPrefWidth (width);
VBox buttons = buildButtonView ();
if (past != null)
{
XYChart line = buildLineChart ();
line.setPrefHeight (height);
line.setPrefWidth (width);
hBox.getChildren ().addAll (bar, line);
} else
{
hBox.getChildren ().addAll (bar);
} // if
Label dsecription = new Label (label);
this.getProperties ().put ("--Indicator", ind.getName ());
this.getChildren ().addAll (dsecription, hBox);
} /*** Charter ***/
创建宪章后,它会被添加到VBox中,并随着VBox已添加到场景中而显示在屏幕上。
答案 0 :(得分:0)
看起来您想在将条形图添加到场景之前计算条形图在屏幕上的位置。从那时起,屏幕上还没有位置为-1或未定义。
我倾向于在fxml中制作我的布局,因此不会出现这样的问题。如果您需要在代码中执行此操作,我认为首先将其添加到hBox的子项中,然后绘制该行可以解决问题。