java - 如何更改 CSS 第 7 系列上方的 javaFX LineChart 中的图标?

标签 java css javafx linechart scenebuilder

我正在绘制包含多个系列的图表,但我只能使用 css 对前 7 个系列进行个性化设置。从第 8 系列开始,它返回到 default-color0 设置。有人可以帮助我吗?

CSS 代码:

.default-color0.chart-series-line {
    -fx-stroke: transparent;
}
.default-color0.chart-line-symbol { 
    -fx-background-color: transparent, transparent;
}
.default-color3.chart-series-line {
    -fx-stroke: transparent;
}
.default-color3.chart-line-symbol{
    -fx-shape: "M0,1 L0,2 L1,2 L1,3 L2,3 L2,2 L3,2 L3,1 L2,1 L2,0 L1,0 L1,2 z"; /* <= Esse é para cruz */
    -fx-background-color: blue, blue;
}
.default-color4.chart-series-line {
    -fx-stroke: transparent;
}
.default-color4.chart-line-symbol{
    -fx-shape: "M0,4 L4,4 L4,0 L0,0 Z"; /* <= esse é para quadrado*/
}
.default-color5.chart-series-line {
    -fx-stroke: transparent;
}
.default-color5.chart-line-symbol{
    -fx-shape: "M0,4 L2,0 L4,4 Z"; /* <= esse é para triangulo*/
}
.default-color6.chart-series-line {
    -fx-stroke: transparent;
}
.default-color6.chart-line-symbol{
    -fx-shape: "M0,1 L0,2 L1,2 L1,3 L2,3 L2,2 L3,2 L3,1 L2,1 L2,0 L1,0 L1,2 z"; /* <= Esse é para cruz */
    -fx-background-color: red, red;
}
.default-color7.chart-series-line {
    -fx-stroke: transparent;
}
.default-color7.chart-line-symbol{
    -fx-shape: "M1,3 L0,1 L0,0 L1,0 L1,1 L2,1 L2,0 L3,0 L3,1 L2,3 z"; /* <= Esse é para coracao */
    -fx-border-color: rosybrown;
    -fx-background-color: rosybrown, rosybrown;
}
.default-color8.chart-series-line {
    -fx-stroke: transparent;
}
.default-color8.chart-line-symbol{
    -fx-shape: "M1,3 L0,1 L0,0 L1,0 L1,1 L2,1 L2,0 L3,0 L3,1 L2,3 z"; /* <= Esse é para coracao */
    -fx-border-color: green;
    -fx-background-color: green, green;
}

Java代码:

 private void putDataChart(String cns){
  try{
    // Atention: the first serie is 'base' (dont show in the chart),
    // the second and the third ones shows the lines and de plots (Max and Min Artherial Pressure (AP))
    // after that all the lines are transparent and shows only the plots.
    // I could't go after the 7th serie becouse its repeats from the zero 
    // i dont know why.
    // to change this settings you have to change the chart.css
    chartPA.getData().clear();
    List<Document> eventos = eADO.getAllEvents(cns);
    if (eventos == null) return;      // get out if there is no events to show.
    XYChart.Series paMaxSerie = new XYChart.Series<>();
    XYChart.Series paMinSerie = new XYChart.Series<>();
    XYChart.Series consultas = new XYChart.Series<>();
    XYChart.Series grupos = new XYChart.Series<>();
    XYChart.Series exames = new XYChart.Series<>();
    XYChart.Series emergencias = new XYChart.Series<>();
    XYChart.Series base = new XYChart.Series<>();
    XYChart.Series risco = new XYChart.Series<>();
    XYChart.Series teste = new XYChart.Series<>(); //this series isnt showing .default-color8.chart-line-symbol

    paMinSerie.setName("PA min");
    paMaxSerie.setName("PA max");
    consultas.setName("Consulta");
    grupos.setName("Grupo");
    exames.setName("Exames");
    emergencias.setName("Emergências");
    base.setName("Legenda:");
    risco.setName("Risco CardioVascular");
    teste.setName("8th Serie");

    LocalDateTime oldestDate = LocalDateTime.now().minusDays(1);

    for (Document ev : eventos) {
        LocalDateTime ldt = LocalDateTime.from(ev.getDate("date").toInstant().atOffset(ZoneOffset.UTC));
        Duration dura = Duration.between(oldestDate,ldt);
        if (dura.toDays() < 0){
            oldestDate = LocalDateTime.from(ev.getDate("date").toInstant().atOffset(ZoneOffset.UTC));
        }
        String tipoEvento = ev.getString("tipo");
        LocalDate date = LocalDate.from(ev.getDate("date").toInstant().atOffset(ZoneOffset.UTC));
        if (tipoEvento.equals("CONSULTA")) {  // CONSULTA = Medical Appointment in portuguese
            consultas.getData().add(new XYChart.Data<>(date.toString(),10));
        } else if (tipoEvento.equals("GRUPOHAS")) { // = Hypertension group
            grupos.getData().add(new XYChart.Data<>(date.toString(),20));
        } else if (tipoEvento.equals("EXAMES")) {
            exames.getData().add(new XYChart.Data<>(date.toString(),30));
        } else if (tipoEvento.equals("CVRisk")) { 
            risco.getData().add(new XYChart.Data<>(date.toString(),200));
        } else if (tipoEvento.equals("EMERGENCIA")) {
            emergencias.getData().add(new XYChart.Data<>(date.toString(),50));
        } else if (tipoEvento.contains("PA")) { // = AP
            paMinSerie.getData().add(new XYChart.Data<>(date.toString(),ev.getInteger("MIN")));
            paMaxSerie.getData().add(new XYChart.Data<>(date.toString(),ev.getInteger("MAX")));
        }
    }

    Long baseDays = Duration.between(oldestDate, LocalDateTime.now()).toDays();
    for (long i = 0; i < baseDays; i++){
        LocalDate nextDay = oldestDate.plusDays(i).toLocalDate();
        base.getData().add(new XYChart.Data<>(nextDay.toString(),0));
    }

    chartPA.setVerticalGridLinesVisible(false);
    chartPA.getData().add(base);
    chartPA.getData().addAll(paMaxSerie, paMinSerie, consultas, grupos, 
            exames, emergencias, risco, teste); 

  } catch (Exception putz){
      ApoiosMongoADO.arquivaErro("Erro em HasFXMLController.putDataChart(cns)", putz);
  }
}

最佳答案

在挖掘之后我在这里找到了答案: http://tiwulfx.panemu.com/2013/01/07/provide-more-colors-for-chart-series/

这是我添加到代码中(在初始化图表后)工作的内容:

// Changing limit to 10 series
    int i=0;
    for (Node node : chartPA.lookupAll(".chart-legend-item")) {
        if (node instanceof Label && ((Label) node).getGraphic() != null) {
            ((Label) node).getGraphic().getStyleClass().remove("default-color" + (i % 8));
            ((Label) node).getGraphic().getStyleClass().add("default-color" + (i % 10));
        }
        i++;
    }
    for (i=0; i < 10; i++){
        for (Node node : chartPA.lookupAll(".series" + i)) {
            node.getStyleClass().remove("default-color" + (i % 8));
            node.getStyleClass().add("default-color" + (i % 10));
        }
    }

关于java - 如何更改 CSS 第 7 系列上方的 javaFX LineChart 中的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43294879/

相关文章:

java - 多线程和匿名类和对象

java - reduce() 不适用于 lightcouch

java - 什么是NullPointerException,我该如何解决?

css - z-index 重叠不同的父元素与不同的父元素的第 n 个子元素

javafx - 使用父类(super class)扩展 javafx.application.Application。为什么子类中的 Override 会被忽略?

javascript - 如何在 nashorn 中转换 java 类?

java - 将 postgresql 数据库添加到 travis

html - 对齐 Div 在 Firefox 中有效,但在 IE 大于 7 时无效

jquery - 如何使用 CSS 为多个元素设置动画

java - 需要从 eclipse 插件代码访问 JavaFX