1

This is my code at the moment (updated):

public DynamicTimeSeriesCollection dataset;
private static final String TITLE = "Stripchart";
private static final int COUNT = 3 * 60;
private static final int TEMP_MIN = -10;
private static final int TEMP_MAX = 50;
private static final int AIR_MIN = 0;
private static final int AIR_MAX = 20;
private static final int INSO_MIN = 0;
private static final int INSO_MAX = 1;
public void draw(Data data) {
    float[] newData = new float[3];
    newData[0] = (float) data.getTemp();
    newData[1] = (float) data.getAir();
    newData[2] = (float) data.getInso();
    dataset.advanceTime();
    dataset.appendData(newData);
}

private ChartPanel createChart() {
    dataset = new DynamicTimeSeriesCollection(3, COUNT, new Second());
    dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2016));
    dataset.addSeries(new float[1], 0, "Temperature");
    dataset.addSeries(new float[1], 1, "Air");
    dataset.addSeries(new float[1], 2, "Insolation");
    final JFreeChart result = ChartFactory.createTimeSeriesChart(
            TITLE, "hh:mm:ss", " ", dataset, true, true, false);
    final XYPlot plot = result.getXYPlot();
    ValueAxis domain = plot.getDomainAxis();
    domain.setAutoRange(true);
    NumberAxis temp = new NumberAxis("Temperature");
    NumberAxis air = new NumberAxis("Air");
    NumberAxis inso = new NumberAxis("Insolation");

    plot.setRangeAxis(0, temp);
    plot.setRangeAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT);
    plot.setRangeAxis(1, air);
    plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
    plot.setRangeAxis(2, inso);
    plot.setRangeAxisLocation(2, AxisLocation.BOTTOM_OR_LEFT);
    List<Integer> axes = Arrays.asList(0, 1, 2);
    plot.mapDatasetToRangeAxes(0, axes);
    temp.setRange(TEMP_MIN, TEMP_MAX);
    air.setRange(AIR_MIN, AIR_MAX);
    inso.setRange(INSO_MIN, INSO_MAX);

    ChartPanel chartPanel = new ChartPanel(result);

    return chartPanel;
}

The units are °C , m/s and lux respectively.

Everything works fine but it does not map the series as exactly as I want.

enter image description here

As you can see, the blue line and green line are not mapped with the second and third axis.

Any ideas to get it working?

Your help will be appreciated.

// Updated :

I have tried using this but the result is the same:

List<Integer> axes = Arrays.asList(0, 1, 2);
plot.mapDatasetToRangeAxes(0, axes);
Forrest
  • 723
  • 2
  • 8
  • 24

1 Answers1

3

In your fragment, I see three axes and one dataset with three series. Your calls to mapDatasetToRangeAxis() appear to assume three distinct datasets. As suggested in this related example, you may want something like this:

List<Integer> axes = Arrays.asList(0, 1, 2);
plot.mapDatasetToRangeAxes(0, axes);

The approach assumes that the three series have linearly dependent scales, and you'll have to scale the individual axes accordingly, as shown here. You could try separate datasets, but I haven't tried it.

Addendum: Based on your update, it appears that the three datasets are not commensurate. Instead, create three individual datasets. Use the first in your chart factory, use setDataset() to establish the other two and map them accordingly:

final JFreeChart result = ChartFactory.createTimeSeriesChart(
    TITLE, "hh:mm:ss", " ", createDatasetTemp(), true, true, false);
…
plot.setDataset(1, createDatasetAir());
plot.setDataset(2, createDatasetInso());
plot.setRangeAxis(0, temp);
plot.setRangeAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT);
plot.setRangeAxis(1, air);
plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
plot.setRangeAxis(2, inso);
plot.setRangeAxisLocation(2, AxisLocation.BOTTOM_OR_LEFT);
plot.mapDatasetToRangeAxis(0, 0);
plot.mapDatasetToRangeAxis(1, 1);
plot.mapDatasetToRangeAxis(2, 2);
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • `List axes = Arrays.asList(0, 1, 2);` `plot.mapDatasetToRangeAxes(0, axes);` It does not work as I want. What should I do when the three series have independent scales? – Forrest Aug 29 '16 at 09:33
  • I see °C and w/m^2. What are the units of "Air"? For more specific guidance, , please edit your question to include a [mcve] that shows your revised approach. – trashgod Aug 29 '16 at 09:44
  • Edited. Btw, is there a way to reset all the series in the dataset? – Forrest Aug 29 '16 at 10:00
  • You can just set the upper and lower bounds of each axis to the corresponding defined values. I don't see a reset method in `DynamicTimeSeriesCollection`. – trashgod Aug 29 '16 at 14:11
  • updated with more information. Your help will be appreciated, trashgod – Forrest Aug 30 '16 at 01:58
  • Ah, I see what you mean; try separate datasets, as outlined above. – trashgod Aug 30 '16 at 07:16