Question

I create the following XY chart:

enter image description here

In this instance, I have two axes, each with two series. And each series has 39 points.

I would like to know how to change the scale of the domain axis, so for example instead of 0-39, it would show 0-3.9.

How can this be achieved? My code for the graph is below:

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;
}
Was it helpful?

Solution

You should be able to use setRange() on the domain axis.

NumberAxis domainAxis = new NumberAxis("Set Current");
domainAxis.setRange(0, 3.9);
plot.setDomainAxis(SERIES_MAX, domainAxis);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top