在我的项目中,我在同一帧中使用barchartlinechart来显示相同的数据.然而,由于某些原因,我得到的输出在barchartlinechart中都没有 colored颜色 .

For example: enter image description here In this image, the linechart has color but the barchart doesn't.

我使用的代码是:

FXML文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.chart.BarChart?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="401.0" prefWidth="802.0" style="-fx-background-color: white;" stylesheets="@stylesheet.css" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication26.FXMLDocumentController">
   <children>
      <AnchorPane layoutX="1.0" layoutY="14.0" prefHeight="303.0" prefWidth="801.0" AnchorPane.bottomAnchor="46.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="14.0">
         <children>
            <AnchorPane layoutX="342.0" layoutY="-2.0" prefHeight="244.0" prefWidth="419.0" style="-fx-border-color: #4E6172; -fx-background-color: white;" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="-2.0">
               <children>
                  <LineChart fx:id="linechart" layoutX="69.0" layoutY="11.0" prefHeight="353.0" prefWidth="380.0">
                    <xAxis>
                      <CategoryAxis side="BOTTOM" />
                    </xAxis>
                    <yAxis>
                      <NumberAxis side="LEFT" />
                    </yAxis>
                  </LineChart>
               </children>
            </AnchorPane>
            <AnchorPane layoutX="8.0" layoutY="-2.0" prefHeight="367.0" prefWidth="392.0" style="-fx-border-color: #4E6172; -fx-background-color: white;" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="399.0" AnchorPane.topAnchor="-2.0">
               <children>
                  <BarChart fx:id="barchart" layoutX="3.0" layoutY="3.0" prefHeight="363.0" prefWidth="391.0" AnchorPane.bottomAnchor="1.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="2.0">
                    <xAxis>
                      <CategoryAxis side="BOTTOM" />
                    </xAxis>
                    <yAxis>
                      <NumberAxis side="LEFT" />
                    </yAxis>
                  </BarChart>
               </children>
            </AnchorPane>
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

Java控制器:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxapplication26;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

/**
 *
 * @author param
 */
public class FXMLDocumentController implements Initializable {
    
  
    
    @FXML
    private LineChart<String, Number> linechart;
    @FXML
    private BarChart<String, Number> barchart;
    
   
    @Override
    public void initialize(URL url, ResourceBundle rb) {
         XYChart.Series<String, Number> series= new  XYChart.Series<String, Number>();
        series.getData().add(new  XYChart.Data<String, Number>("Jan",12));
        series.getData().add(new  XYChart.Data<String, Number>("Feb",20));
        series.getData().add(new  XYChart.Data<String, Number>("March",10));
        series.getData().add(new  XYChart.Data<String, Number>("April",14));
    
      
        linechart.getData().add(series);
        barchart.getData().add(series);
        
      
        // TODO
    }      
} 
}

如图所示,barchartlinechart中只有一台能够显示 colored颜色 .我试着使用-fx-bar-fill方法,但即使这样也不起作用.

推荐答案

正如@Kleopatra和@Slaw提到的那样,问题是我对barchartlinechart都使用了单一的Series.这就是错误的原因.

解决方案是使用两个不同的Series,分别名为series1series2barchartlinechart.

更正后的代码:

package javafxapplication26;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

/**
 *
 * @author param
 */
public class FXMLDocumentController implements Initializable {
    
  
    
    @FXML
    private LineChart<String, Number> linechart;
    @FXML
    private BarChart<String, Number> barchart;
    
   
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        XYChart.Series<String, Number> series= new  XYChart.Series<String, Number>(); //For the barchart
        series.getData().add(new  XYChart.Data<String, Number>("Jan",12));
        series.getData().add(new  XYChart.Data<String, Number>("Feb",20));
        series.getData().add(new  XYChart.Data<String, Number>("March",10));
        series.getData().add(new  XYChart.Data<String, Number>("April",14));
    
        XYChart.Series<String, Number> series2= new  XYChart.Series<String, Number>(); //For the linechart
        series2.getData().add(new  XYChart.Data<String, Number>("Jan",12));
        series2.getData().add(new  XYChart.Data<String, Number>("Feb",20));
        series2.getData().add(new  XYChart.Data<String, Number>("March",10));
        series2.getData().add(new  XYChart.Data<String, Number>("April",14));
        
        
        barchart.getData().add(series);   
        linechart.getData().add(series2);

    }     
}

正如@Slaw提到的,问题是每个barchartlinechart needs都有自己的SeriesData.这是因 for each XYChart.SeriesXYChart.Data都提供与其相关联的Node,这就是图表中显示的内容.

更正后的输出:

enter image description here

Java相关问答推荐

android Document File. isDirector()返回意外结果

Saxon 9:如何从Java扩展函数中的net.sf.saxon.expr. XPathContent中获取声明的变量

如果给定层次 struct 级别,如何从其预序穿越构造n元树

如何审查Java dtos中的自定义注释字段?

Javascript更新alert 可扩展内容样式与CSS—按钮更多/更少

Javascript在边界中心调整ImageView大小

SQlite for Android无法使用json_group_array/json_object

neo4j java驱动程序是否会在错误发生时自动回滚事务?

Jlink选项&-strie-ative-Commands";的作用是什么?

流迭代列表<;对象>;上的NoSuchElementException

连接Quarkus中的两个异步操作

如何解释Java中for-each循环中对Iterable的强制转换方法引用?

Java-动态绑定-问题-了解

匹配一组字符或另一组字符

在settings.gradle.kts和Build.gradle.kts中使用公共变量

无法播放音频:从资源加载库GStreamer-Lite失败

将stringBuilder + forloop转换为stream + map

使用for循环时出现堆栈溢出错误,但如果使用if块执行相同的操作,则不会产生错误

在缺少字段时使用Jackson With Options生成Optional.Empty()

Java HashMap保留所有时间复杂性