Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Dynamic Chart (or other control) Manipulation

$
0
0

I'm using the controlP5 library to visualize some UDP byte stream data, but ran into an issue with accessing Charts dynamically by their defined names.

The code is working when explicitly referencing Charts by their chart name (commented out at bottom), but I would like to do it all within the for loop down in "receiveUDP()". Below is my code. Is this possible?

class DataPoint {
  int Position;
  float Value;

  public DataPoint(int Position, float Value) {
    this.Position = Position;
    this.Value = Value;
  }
}

public class UDP_DooDad_3000 extends PApplet {
  ControlP5 cp5;

  Map<String, DataPoint> telemetryData = new HashMap<String, DataPoint>();

  Chart speedChart;
  Chart engineSpeedChart;
}

public void setup() {
  telemetryData.put("speed", new DataPoint(28, 0.0f));
  telemetryData.put("engineSpeed", new DataPoint(148, 0.0f));

  cp5 = new ControlP5(this);

  speedChart = cp5.addChart("speedChart", 0, 200, 200, 100)
    .addDataSet("speedDataSet");

  engineSpeedChart = cp5.addChart("engineSpeedChart", 210, 200, 200, 100)
    .addDataSet("engineSpeedDataSet");

  //...


}

public void receiveUDP(byte[] data) {
  for (Map.Entry<String, DataPoint> entry : telemetryData.entrySet()) {
    DataPoint iDataPoint = null;
    iDataPoint = entry.getValue();
    iDataPoint.Value = ripFloatData(data, iDataPoint.Position); //ripFloatData does some bitmashing in another function

    //Possible to reference the name of a chart dynamically?
    //  Chart[entry.getKey() + "Chart"].addData(entry.getKey() + "DataSet", iDataPoint.Value);
    //or
    //  cp5.getChart(entry.getKey() +  "Chart").addData(entry.getKey() + "DataSet", iDataPoint.Value);
  }

  // I want to avoid the below code. I was hoping to do it dynamically in the loop above instead of explicitly adding data points each chart's dataSet
  //DataPoint speedDataPoint;
  //speedDataPoint = telemetryData.get("speed");
  //speedChart.addData("speedDataSet", speedDataPoint.Value);

  //DataPoint engineSpeedDataPoint;
  //engineSpeedDataPoint = telemetryData.get("engineSpeed");
  //engineSpeedChart.addData("engineSpeedDataSet", engineSpeedDataPoint.Value);
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles