如何使用XChart对Y轴进行限制?

时间:2013-02-08 07:22:45

标签: java charts plot

我试图绘制一系列相似的函数。域为[0,1],范围始终为[-3,3]。

我希望每个图表的Y轴上的限制相同。试图设置y min和y max系列似乎不起作用。

有没有办法让Y轴上的限制在每个图表上都相同?

import java.math.BigDecimal;
import java.util.*;
import com.xeiam.xchart.*;
public class Bug {
    static void plot(Chart chart,int n) {
        chart.setTitle("title");
        chart.setXAxisTitle("X");
        chart.setYAxisTitle("Y");
        List<Number> x=new ArrayList<Number>();
        List<Number> y=new ArrayList<Number>();
        String seriesName=addOneSeries(chart,n,x,y);
    }
    private static String addOneSeries(Chart chart,int n,List<Number> x,List<Number> y) {
        for(int i=0;i<=10;i++) {
            x.add(i/10.);
            y.add(i*n/10.);
        }
        String seriesName="series "+n;
        Series series=chart.addSeries(seriesName,x,y);
        series.xMin=BigDecimal.ZERO;
        series.xMax=BigDecimal.ONE;
        series.yMin=new BigDecimal(-5);
        series.yMax=new BigDecimal(5);
        return seriesName;
    }
    public static void main(String[] args) {
        for(int i=0;i<4;i++) {
            Chart chart=new Chart(700,500);
            plot(chart,i);
            new SwingWrapper(chart).displayChart();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

现在可以使用XChartChart上设置最小值和最大值。下面是OP的Bug类的返工,用于演示这一点。此时,由于它是一个全新的功能,您必须获得xchart-2.0.0-SNAPSHOT jar here才能看到此功能。请随意ask for help or request more features

public class NoBug {

  static void plot(Chart chart, int n) {

    chart.setChartTitle("title");
    chart.setXAxisTitle("X");
    chart.setYAxisTitle("Y");
    List<Number> x = new ArrayList<Number>();
    List<Number> y = new ArrayList<Number>();
    String seriesName = addOneSeries(chart, n, x, y);
  }

  private static String addOneSeries(Chart chart, int n, List<Number> x, List<Number> y) {

    for (int i = 0; i <= 10; i++) {
      x.add(i / 10.);
      y.add(i * n / 10.);
    }
    String seriesName = "series " + n;
    Series series = chart.addSeries(seriesName, x, y);
    chart.getStyleManager().setxAxisMin(0);
    chart.getStyleManager().setxAxisMax(1);
    chart.getStyleManager().setyAxisMin(-5);
    chart.getStyleManager().setyAxisMax(5);
    return seriesName;
  }

  public static void main(String[] args) {

    for (int i = 0; i < 4; i++) {
      Chart chart = new LineChart(700, 500);
      plot(chart, i);
      new SwingWrapper(chart).displayChart();
    }
  }

}

答案 1 :(得分:0)

更新2020-10-15

这些方法现在使用大写的//create-account.component.html <html> <mat-grid-list cols="1" rowHeight="500px"> <mat-grid-tile> <div class="example-container"> <mat-form-field appearance="fill" class="width-50"> <mat-label>First Name</mat-label> <input matInput> </mat-form-field> <mat-form-field appearance="fill" class="width-50 ml-2"> <mat-label>Surname</mat-label> <input matInput> </mat-form-field> <br> <mat-form-field appearance="fill" class="width-100"> <mat-label>Email address</mat-label> <input matInput placeholder="Ex. pat@example.com"> </mat-form-field> <br> <mat-form-field appearance="fill" class="width-100"> <mat-label>Enter password</mat-label> <input matInput [type]="hide ? 'password' : 'text'"> <button mat-icon-button matSuffix (click)="hide = !hide" [attr.aria-label]="'Hide password'" [attr.aria-pressed]="hide"> <mat-icon>{{hide ? 'visibility_off' : 'visibility'}}</mat-icon> </button> </mat-form-field> <br> <label id="example-radio-group-label">Gender: </label> <br><br> <mat-radio-group aria-label="Select an option"> <mat-radio-button value="1">Male</mat-radio-button> <mat-radio-button class="example-radio-button" value="2">Female</mat-radio-button> <mat-radio-button class="example-radio-button" value="3">Custom</mat-radio-button> </mat-radio-group> <br><br> <label id="example-radio-group-label">Date of birth: </label> <br><br> <mat-form-field appearance="fill" class="width-25"> <mat-label>Day</mat-label> <mat-select> <mat-option [value]="6"> 26 </mat-option> </mat-select> </mat-form-field> <mat-form-field appearance="fill" class="width-25 ml-1"> <mat-label>Month</mat-label> <mat-select> <mat-option [value]="6"> 26 </mat-option> </mat-select> </mat-form-field> <mat-form-field appearance="fill" class="width-25 ml-1"> <mat-label>Year</mat-label> <mat-select> <mat-option [value]="6"> 26 </mat-option> </mat-select> </mat-form-field> </div> </mat-grid-tile> </mat-grid-list> </html> X

Y

请参见https://knowm.org/javadocs/xchart/org/knowm/xchart/style/AxesChartStyler.html#setXAxisMin(java.lang.Double)