【问题标题】:JavaFX cannot draw line in BarChartJavaFX 无法在 BarChart 中画线
【发布时间】:2017-04-01 08:13:00
【问题描述】:

我想在条形图中画一条线。该线反映了与其他值进行比较的某个值。我将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 的所有值都是“不合逻辑的”,即 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 ***/

Charter 创建完成后,它被添加到 VBox 中,并在屏幕上显示 VBox 已经添加到场景中。

【问题讨论】:

  • 该方法何时调用?也许在条形图布局之前?
  • 可能是这样,我不知道BarChart什么时候布局。我对我的问题添加了编辑

标签: javafx bar-chart


【解决方案1】:

您似乎想在将条形图添加到场景之前计算其在屏幕上的位置。因为此时屏幕上还没有位置为 -1 或未定义。

我倾向于在 fxml 中进行布局,因此不会出现此类问题。如果您需要在代码中执行此操作,我认为在绘制线之前先将其添加到 hBox 的子项中可以解决问题。

【讨论】:

  • 谢谢,我会试试的。我认为我应该首先将 hBox 添加到场景中才能使其工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多