将CategoryAxis的标签设置为barChart javafx

时间:2017-01-20 12:16:28

标签: javafx bar-chart axis

我在javaFX中创建了一个barChart,我遇到了categoryAxis的问题。练习我想将CategoryAxis的数量除以1000.目前我得到的图是这样的: first img 当我将值除以1000时,图表会更改y轴。 second img

如何使用第一张图片的barChart将xaxis值作为第二个图形?

填充并显示图表的类。

    package application;

    import java.io.File;
    import java.math.RoundingMode;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import javafx.scene.Scene;
    import javafx.scene.chart.Axis;
    import javafx.scene.chart.BarChart;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.LineChart;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.ScatterChart;
    import javafx.scene.chart.XYChart;
    import javafx.scene.chart.XYChart.Series;
    import javafx.stage.Stage;
    import spectrogram.SpectrogramExtractor;
    public class ControllerSpectrogram {

        private final static double MINSPECTROGRAMHZ = 20.0; 
        static DecimalFormat df = new DecimalFormat("#.##");            

        @SuppressWarnings("unchecked")
        public static void plotSpectrogramFull(File file, Stage stage) {
            df.setRoundingMode(RoundingMode.CEILING);
            final CategoryAxis xAxisSpectrogram = new CategoryAxis();
            final NumberAxis yAxisSpectrogram = new NumberAxis();

             final BarChart<String,Number> scatterChartSpectrogram = 
                        new BarChart<String,Number>(xAxisSpectrogram,yAxisSpectrogram);
            ArrayList<Double> extractedData; 
            ArrayList<Double> extractedTime; 

            scatterChartSpectrogram.getData().clear();
            scatterChartSpectrogram.setOpacity(0.8);
            scatterChartSpectrogram.setAnimated(true);
            xAxisSpectrogram.setLabel("Time(s)");
            yAxisSpectrogram.setLabel("Spectrogram(Hz)");

            try {

                extractedData = SpectrogramExtractor.extractionSpectrum();
                extractedTime = SpectrogramExtractor.pointTime();
                Series<String, Number> series = new XYChart.Series<String, Number>();
                for (int i = 0; i < extractedData.size(); i=i+2) {
                    if (extractedData.get(i)> MINSPECTROGRAMHZ) {
                        extractedTime.set(i, extractedTime.get(i)/1000);
                        series.getData().add(new XYChart.Data<String, Number>(df.format(extractedTime.get(i)), extractedData.get(i)));
                    }
                }
                Scene scene = new Scene(scatterChartSpectrogram, 1000, 600);
                scatterChartSpectrogram.getData().add(series);
                stage.setScene(scene);
                stage.show();
                extractedData.clear();
            } catch (java.lang.Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

这是方法(在其他类中)

public class SpectrogramExtractor {
    static ArrayList<Double> pointTime = new ArrayList<>();

public static ArrayList<Double> pointTime(){    
    Double countTime = 0;   
    for(int i=0; i<100; i++){
    countTime = countTime + 0.0196356952; 
    pointTime.add(countTime);
    return pointTime;
    }
public static ArrayList<Double>extractionSpectrum(){
//and you can try to populate extractedData whit 100 elements (what you want)
}
}

0 个答案:

没有答案