【问题标题】:How to auto-scale JavaFX LineChart if an invisible series present?如果存在不可见系列,如何自动缩放 JavaFX LineChart?
【发布时间】:2018-02-07 16:40:40
【问题描述】:

我需要在我的 LineChart 上显示/隐藏系列。现在我正在这样做:

series.getNode().setVisible(false);

但尽管此后系列不可见,但图表轴自动范围仍会考虑系列数据的范围。我也厌倦了设置托管属性:

series.getNode().setVisible(false);
series.getNode().setManaged(false);

但没有成功。那么,是否有某种方法可以隐藏系列并将其从轴自动范围中排除?

【问题讨论】:

    标签: javafx linechart


    【解决方案1】:

    目前看来,这样做的唯一方法是扩展 LineChart 并覆盖 updateAxisRange() 以修改它以仅考虑可见系列:

    public class MLineChart<X, Y> extends LineChart<X, Y> {
    
      public MLineChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis) {
        super(xAxis, yAxis);
      }
    
      @Override
      protected void updateAxisRange() {
        final Axis<X> xa = getXAxis();
        final Axis<Y> ya = getYAxis();
        List<X> xData = null;
        List<Y> yData = null;
        if (xa.isAutoRanging()) xData = new ArrayList<X>();
        if (ya.isAutoRanging()) yData = new ArrayList<Y>();
        if (xData != null || yData != null) {
          for (Series<X, Y> series : getData()) {
            if (series.getNode().isVisible()) { // consider only visible series
              for (Data<X, Y> data : series.getData()) {
                if (xData != null) xData.add(data.getXValue());
                if (yData != null) yData.add(data.getYValue());
              }
            }
          }
          // RT-32838 No need to invalidate range if there is one data item - whose value is zero.
          if (xData != null && !(xData.size() == 1 && getXAxis().toNumericValue(xData.get(0)) == 0)) {
            xa.invalidateRange(xData);
          }
          if (yData != null && !(yData.size() == 1 && getYAxis().toNumericValue(yData.get(0)) == 0)) {
            ya.invalidateRange(yData);
          }
    
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多