Dynamically Adding Dataset to Chart Customizer

I am working on pulling data from a database and separating it based on a timestamp before displaying it on a Classic Chart. The only thing is, I need to dynamically determine how many different timestamps there are when the script runs. Is there a way to dynamically add or delete datasets from the chart customizer so that each dataset has it’s own curve?

You can add additional series to a chart in scripting, though that isn't the same as adding them to the chart customizer.

Here is an example that should give an idea of where to start.

If you need some help working through that, let us know what you have tried and where it didn't work.

Thank you for the suggestion. I believe that once everything gets set up, this may be what I’m looking for. One thing that I didn’t understand, though, is where the chart that has the getPlot() method called on it is coming from in the sample.

The sample is intended to be placed in the configureChart extension function of a Classic Chart component. However, you can modify the script to work outside of that function you just have to get a reference to the chart.

I have used the following code (that was referenced in the first reply) to set up the legend for the chart.

legItems = plot.getLegendItems()
newLegItem = LegendItem(newDs.getSeriesKey(0), newDs.getSeriesKey(0), '', '', legItems.get(0).getLine(), legItems.get(0).getLineStroke(), Color.red)
legItems.add(newLegItem)
plot.setFixedLegendItems(legItems)

I understand what it is doing, but the odd thing is that no matter what I do the first dataset that I create and add to the chart has two entries in the legend. There is only one line plotted on the chart, but the first and only the first dataset ends up getting duplicated on the legend. Does anyone have any idea why this might be?

Update: I discovered some extra dataset properties on the chart I was using for testing. Removing them solved my issue.