JavaFx 2.x:XYChart属性 [英] JavaFx 2.x: XYChart properties

查看:331
本文介绍了JavaFx 2.x:XYChart属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的XY LineChart,我想在不使用CSS的情况下将网格和线条描边(宽度),样式(点划线,虚线等)和颜色设置为rgb以及背景颜色.

Having a simple XY LineChart, I would like to set grid and line stroke (width), style (dotted, dashed and so on) and color as rgb as well as background color without using css.

这可能吗?如果是这样,该怎么办?我找不到任何合适的方法.

Is this possibile? And if so, how to? I can't find any suitable method.

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class XyChart extends Application {

@Override
public void start(Stage stage) {
   stage.setTitle("Line plot");

   final CategoryAxis xAxis = new CategoryAxis();
   final NumberAxis yAxis = new NumberAxis(1, 21,0.1);

   yAxis.setTickUnit(1);
   yAxis.setPrefWidth(35);
   yAxis.setMinorTickCount(10);

   yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis){
        @Override
    public String toString(Number object){
            String label;
            label = String.format("%7.2f", object.floatValue());
            return label;
    }
});
final LineChart<String, Number>lineChart = new LineChart<String, Number>(xAxis, yAxis);

   lineChart.setCreateSymbols(false);
   lineChart.setAlternativeRowFillVisible(false);
   lineChart.setLegendVisible(false);

   XYChart.Series series1 = new XYChart.Series();

    series1.getData().add(new XYChart.Data("Jan", 1));
    series1.getData().add(new XYChart.Data("Feb", 4));
    series1.getData().add(new XYChart.Data("Mar", 2.5));
    series1.getData().add(new XYChart.Data("Apr", 5));
    series1.getData().add(new XYChart.Data("May", 6));
    series1.getData().add(new XYChart.Data("Jun", 8));
    series1.getData().add(new XYChart.Data("Jul", 12));
    series1.getData().add(new XYChart.Data("Aug", 8));
    series1.getData().add(new XYChart.Data("Sep", 11));
    series1.getData().add(new XYChart.Data("Oct", 13));
    series1.getData().add(new XYChart.Data("Nov", 10));
    series1.getData().add(new XYChart.Data("Dec", 20));


    BorderPane pane = new BorderPane();
    pane.setCenter(lineChart);          
    Scene scene = new Scene(pane, 800, 600);
    lineChart.setAnimated(false);
    lineChart.getData().addAll(series1);       

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

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

推荐答案

是的,您的案例在逻辑上比

Yes, your case is simpler in logic than How to dynamically change line style in JavaFX 2.0 line chart? and you are also asking for a couple more features. Marking as duplicate was just an easy way to point to a similar question and answer.

我认为答案是相同的(使用CSS查找)-JavaFX 2.2中没有像lineChart.setStroke这样的api.

I think the answer is the same (use css lookups) - there is no such api in JavaFX 2.2 as lineChart.setStroke.

最终,某些只能通过css查找访问的内容可以通过Java API获得.例如,某些区域背景资料是JavaFX 8中的API,因此,一旦获得对它的引用,就可以通过api对其进行修改-尽管即使那样,我仍然不认为有一种方法可以获取对它的引用.类似于没有CSS查找的图表背景,或者getChildren().get(idx)调用中某些难以想象的丑陋序列,或者侵入了图表源代码.在这些选项中,我认为在大多数情况下,css查找方法是最可取的.

Eventually some of the stuff which is only accessible via css lookups might be made available via Java API. For instance some of the region background stuff is API in JavaFX 8 so, once you get a reference to it, you could modify it via api - though, even then, I still don't think there is a way to get a reference to something like a chart background without a css lookup or some unthinkably ugly sequence of getChildren().get(idx) calls or hacking into the chart source code. Of those options, I think the css lookup approach is the most preferable most of the time.

请注意,您实际上并没有避免使用css文件,因为JavaFX 2.2附带了示例解决方案不会提供用户样式表-所有用户样式是通过代码完成的.

Note that you are not really avoiding a css file, as JavaFX 2.2 ships with a default css file which is used to style charts. Also note that the linked sample solution to the dynamic line style question does not supply a user stylesheet - all user styling is done in code.

这篇关于JavaFx 2.x:XYChart属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆