JFreeChart XY图:更改域范围

时间:2014-03-17 12:57:14

标签: jfreechart

我创建了以下XY图表:

enter image description here

在这个例子中,我有两个轴,每个轴有两个系列。每个系列都有39分。

我想知道如何更改域轴的比例,例如代替0-39,它会显示0-3.9。

如何实现这一目标?我的图表代码如下:

private final static int SERIES_MIN = 0;
private final static int SERIES_MAX = 1;

private JFreeChart createXYLineChart(String title) {
    XYDataset voltageDataset = createXYVoltageDataset();
    XYDataset currentDataset = createXYCurrentDataset();

    XYLineAndShapeRenderer rVoltage = new XYLineAndShapeRenderer();
    rVoltage.setSeriesPaint(SERIES_MIN, new Color(0xAA, 0xAA, 0xFF)); 
    rVoltage.setSeriesPaint(SERIES_MAX, new Color(0x00, 0x00, 0xAA)); 
    rVoltage.setSeriesShapesVisible(SERIES_MIN,  false);
    rVoltage.setSeriesShapesVisible(SERIES_MAX,  false);
    float dashVoltage[] = {1.0f, 5f}; // on, off
    rVoltage.setSeriesStroke(SERIES_MIN, new BasicStroke(2.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 5, dashVoltage, 0));
    rVoltage.setSeriesStroke(SERIES_MAX, new BasicStroke(1f));

    XYLineAndShapeRenderer rCurrent = new XYLineAndShapeRenderer();
    rCurrent.setSeriesPaint(SERIES_MIN, new Color(0x66, 0xAA, 0x66)); 
    rCurrent.setSeriesPaint(SERIES_MAX, new Color(0x00, 0x44, 0x00)); 
    rCurrent.setSeriesShapesVisible(SERIES_MIN,  false);
    rCurrent.setSeriesShapesVisible(SERIES_MAX,  false);
    float dashCurrent[] = {1.0f, 5f}; // on, off
    rCurrent.setSeriesStroke(SERIES_MIN, new BasicStroke(2.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 5, dashCurrent, 0));
    rCurrent.setSeriesStroke(SERIES_MAX, new BasicStroke(1));

    JFreeChart chart = ChartFactory.createXYLineChart("Profile", "Set Current", "Voltage", null);
    XYPlot plot = (XYPlot) chart.getPlot(); 

    plot.setDataset(SERIES_MIN, voltageDataset);
    plot.setRenderer(SERIES_MIN, rVoltage);

    plot.setDataset(SERIES_MAX, currentDataset);
    plot.setRenderer(SERIES_MAX, rCurrent);

    plot.setRangeAxis(SERIES_MAX, new NumberAxis("Actual Current")); 
    plot.mapDatasetToRangeAxis(SERIES_MAX, SERIES_MAX); //2nd dataset to 2nd y-axi

    plot.setBackgroundPaint(new Color(0xFF, 0xFF, 0xFF));
    plot.setDomainGridlinePaint(new Color(0x00, 0x00, 0xff));
    plot.setRangeGridlinePaint(new Color(0xff, 0x00, 0x00));

    return chart;
}

private  XYDataset createXYVoltageDataset() {
    final XYSeries s1 = new XYSeries("Min Voltage");
    final XYSeries s2 = new XYSeries("Max Voltage");
    for (int i = 0; i < profile.getNumSteps(); i++) s1.add(i, profile.getStepMinVoltage(i));
    for (int i = 0; i < profile.getNumSteps(); i++) s2.add(i, profile.getStepMaxVoltage(i));
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(s1);
    dataset.addSeries(s2);
    return dataset;
}
private  XYDataset createXYCurrentDataset() {
    final XYSeries s1 = new XYSeries("Min Current");
    final XYSeries s2 = new XYSeries("Max Current");
    for (int i = 0; i < profile.getNumSteps(); i++){
        s1.add(i, profile.getStepMinCurrent(i));
    }
    for (int i = 0; i < profile.getNumSteps(); i++) s2.add(i, profile.getStepMaxCurrent(i));
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(s1);
    dataset.addSeries(s2);
    return dataset;
}

1 个答案:

答案 0 :(得分:2)

您应该可以在域轴上使用setRange()

NumberAxis domainAxis = new NumberAxis("Set Current");
domainAxis.setRange(0, 3.9);
plot.setDomainAxis(SERIES_MAX, domainAxis);