当轴位于轴上时,轴线符号被剪裁

时间:2015-07-18 07:24:12

标签: java charts javafx

我有一个带有定义轴范围的折线图,并且我将点绘制为位于轴上的线的一部分。默认情况下,位于轴上的点和线段将被剪切,如下面的屏幕截图所示;

enter image description here

我的代码如下;

package com.jtech;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class ShowSymbol extends Application {

    @Override
    public void start(Stage stage) {
        final NumberAxis xAxis = new NumberAxis(0,4,1);
        final NumberAxis yAxis = new NumberAxis(0,10,2);

        final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
        XYChart.Series series = new XYChart.Series();

    final XYChart.Data d1 = new XYChart.Data(0.0, 0.0);
    final XYChart.Data d2 = new XYChart.Data(0.5, 2.0);
    final XYChart.Data d3 = new XYChart.Data(1.0, 2.5);
    final XYChart.Data d4 = new XYChart.Data(1.5, 3.5);
    final XYChart.Data d5 = new XYChart.Data(2.0, 10.0);
    final XYChart.Data d6 = new XYChart.Data(2.5, 10.0);
    final XYChart.Data d7 = new XYChart.Data(3.0, 0.0);
    final XYChart.Data d8 = new XYChart.Data(3.5, 0.0);
    final XYChart.Data d9 = new XYChart.Data(4.0, 8.0);

        series.getData().addAll(d1,d2,d3,d4,d5,d6,d7,d8,d9);
        lineChart.getData().add(series);
        lineChart.setLegendVisible(false);

        final Scene scene = new Scene(lineChart, 400, 300);
        scene.getStylesheets().add(getClass().getResource("/css/show.css").toExternalForm());

        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}  

样式表包含;

.chart-series-line { -fx-stroke: grey; -fx-stroke-width: 4.0px; -fx-effect: null; }

.chart-line-symbol {
     -fx-background-color: #860061, white;
     -fx-background-insets: 0, 2;
     -fx-background-radius: 0;
     -fx-padding: 4px;
 }

我想保持指定的轴范围(0-> 4和0-> 10),但显示位于它们上的符号和线的完整段。这可能吗?

1 个答案:

答案 0 :(得分:3)

您面临的问题是绘图区域被裁剪。来自XYChart.java的代码摘录:

private final Group plotArea = new Group(){
  @Override public void requestLayout() {} // suppress layout requests
};
private final Group plotContent = new Group();
private final Rectangle plotAreaClip = new Rectangle();

public XYChart(Axis<X> xAxis, Axis<Y> yAxis) {
...
getChartChildren().addAll(plotBackground,plotArea,xAxis,yAxis);
...
plotArea.setClip(plotAreaClip);
...
}

解决方案可能只是禁用剪辑。但这很可能会给你带来其他麻烦(线重叠等)。尽管如此,由于没有适当的API,这里有一个快速的&amp;关于如何禁用裁剪的肮脏方式:

Region chartContent = (Region) lineChart.lookup(".chart-content");
for( Node node: chartContent.getChildrenUnmodifiable()) {
    if( node instanceof Group) {
        Group plotArea= (Group) node;
        plotArea.setClip(null);
    }
}

它看起来就像这样:

enter image description here

结论:您最好实现自己的图表(例如,使用并更改LineChart和XYChart的源代码)以支持此功能。或者你file a change request寻求合适的解决方案。