Dynamic Chart (or other control) Manipulation

edited April 2016 in Library Questions

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);
}

Answers

  • just discovered cp5.get(), so I'm going to give that a try in a bit.

  • Answer ✓

    solved my own problem :P

    Chart iChart = cp5.get(Chart.class, entry.getKey() + "Chart");
    iChart.addData(entry.getKey() + "DataSet", iDataPoint.Value);
    
Sign In or Register to comment.