JavaFX LineChart - 从图表中清除数据

时间:2017-01-31 11:24:08

标签: java javafx linechart

我从折线图中删除数据时遇到问题。我写了一个绘制图表的程序,在操作后单击按钮完成图表数据。

dataSeries1.getData()的removeAll(); < - 不起作用。

代码:

 NumberAxis xAxis = new NumberAxis();
    xAxis.setLabel("Oś Y");
    NumberAxis yAxis = new NumberAxis();
    yAxis.setLabel("Oś X");
    final LineChart lineChart = new LineChart(xAxis, yAxis);
    final XYChart.Series dataSeries1 = new XYChart.Series();

    lineChart.setCreateSymbols(false);
    lineChart.getData().add(dataSeries1);

按钮操作:

 btn.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

            int lewy = Integer.parseInt(kresLewy.getText());
            int prawy = Integer.parseInt(kresPrawy.getText());
            String rownanie = field.getText();

            try {

                String tekst = lewy + "; " + prawy + "; " + rownanie;

                StringReader tekstReader = new StringReader(tekst);

                parsery.interpreter.parser parser_obj
                        = new parsery.interpreter.parser(new parsery.interpreter.MyLexer(tekstReader));

                Object result = parser_obj.parse().value;

                String sWynik = result.toString();

                ZmiennaX zX = new ZmiennaX();
                ArrayList<Double> xArr = new ArrayList<Double>();
                for (double i = lewy; i <= prawy + 0.001; i = i + zX.getDokladnosc()) // +0.001 dla bledow zaokraglenia
                {
                    xArr.add(zX.round2(i));
                }
                String sX = xArr.toString();
                String wartosciX = sX.substring(1, sX.length() - 1);
                String wartosciY = sWynik.substring(1, sWynik.length() - 1);

                String XbezSpacji = wartosciX.replace(" ", "");
                String YbezSpacji = wartosciY.replace(" ", "");

                String[] splitX = XbezSpacji.split(",");
                String[] splitY = YbezSpacji.split(",");
                dataSeries1.getData().removeAll();

                for(int i=0; i<splitX.length; i++){
                double x = Double.parseDouble(splitX[i]);
                double y = Double.parseDouble(splitY[i]);
                dataSeries1.getData().add(new XYChart.Data(x, y));
                }

            } catch (Exception e) {
                System.out.println("Podczs obliczenia wystapil blad. (" + e.getMessage() + ")");
            } catch (Error error) {
                System.out.println("Podczs obliczenia wystapil blad. (" + error.getMessage() + ")");
            }
        }
    });

在绘制新图表后,有人可以帮我删除数据吗?

2 个答案:

答案 0 :(得分:6)

import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { LoginService } from '../services/login.service'; import { User } from '../classes/interfaces/user.interface'; @Component({ moduleId: module.id, selector: 'login-panel', templateUrl: '../templates/login.component.html', styleUrls: [ '../styles/login.component.css' ], providers: [ LoginService ] }) export class LoginComponent implements OnInit { public submitted:boolean = false; public loginForm: FormGroup; constructor( private router: Router, private loginService: LoginService, private fb: FormBuilder ) {} ngOnInit(): void { this.buildForm(); } public onSubmit(): void { if ( ! this.submitted ) { this.submitted = true; this.loginUser( this.loginForm.value ); } } private loginUser( user: User ){ if ( this.loginForm.valid ) { //console.log( user ) this.loginService.login( user ).then( (token) => { localStorage.setItem('id_token', token.id); this.router.navigate(['home']); }).catch( (error) => this.onLoginFailed(error) ); } } private onLoginFailed( error: any ): void { this.submitted = false; /// make form submitable again /// } public signup( event: any ): void { event.preventDefault(); this.router.navigate(['signup']); } } 要求在其参数中传递要删除的元素。既然你没有提供 - 没有任何东西被删除:

removeAll

您希望在上面的行中使用dataSeries1.getData().removeAll();

  

public void clear()

     

从此列表中删除所有元素(可选操作)。此调用返回后列表将为空。

答案 1 :(得分:0)

您可以使用Collections.singleton删除所有数据:

dataSeries1.getData().removeAll(Collections.singleton(barChart.getData().setAll()));

请参阅Collections.singleton https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

的Oracle文档