I have a Time Stack Chart whose data set can be changed by clicking on a radio button. While the model is running, the chart instantaneously updates its appearance when a radio button is clicked. When the model is paused, however, the chart's area becomes blank when a radio button is clicked; the legend, in contrast, updates automatically. How do I manually force the chart area to redraw itself?
3 Answers
When you pause a model, only a part of Anylogic is actually paused. The thread that handles the GUI keeps running, which is why you can navigate around in the model while it is paused. This also means that if you try to update a chart's data while the simulation is paused, the appearance will refresh but -- as your data is being updated in another thread that is currently paused -- it will not have received that data.
If you want to pause the simulation and still be able to switch the data being displayed in a chart, you could take a look at the Airport example model. It provides a good method to switch between different charts by making them visible/invisible and adjusting the width, height, x and y. Essentially, you make all of the charts you need, overlap them perfectly, and then make visible the one that is currently of interest to you.
If you want to create the charts programmatically, on top of creating the chart with, e.g., new TimeStackChart(...)
, you also have to add it to the top level presentation group with main.presenation.add(...)
. If you don't do this, the chart will never appear in the model as the model won't have anything to display! To find more information on how to create a chart programmatically, make a chart in Main
and then open Main
in the Java editor. Find the chart you created, take a look at it's constructor (there are a lot of arguments!), and use it as rough template for the charts you wish to create. The Help documentation will further make sense of the parameters you see.

- 2,726
- 1
- 26
- 52

- 707
- 3
- 20
-
We have clients that want to see visualizations for various populations when the model is complete or paused. For instance, let's say the model finishes and they were looking at the number of males in the Martian population. Now they want to see the number of females in the Neptunian population. The chart won't refresh when I switch the data set. I understand that the chart normally updates its own data, but when I switch the data set programmatically, shouldn't the chart see that there's new data, or is that logic in the thread too? – Dylan Knowles Nov 04 '14 at 16:42
-
1if you are in paused mode i don't think the chart sees the new data set. but you can try the .update() or .updateData() [link] http://www.anylogic.com/anylogic/help/index.jsp?topic=/com.xj.anylogic.help/html/analysis/Bar%20Chart.html if that doesn't work. I suggest taking a look at the Airport example. they use a different approach, where you exchange the graph instead of the dataset. So the graph is already drawn... – Nikolaj Klitlund Børty Nov 05 '14 at 08:36
-
Neither `update()` nor `updateData()` work, which is a bit counterintuitive. I'll try the airport example. – Dylan Knowles Nov 05 '14 at 17:14
-
The airplane model provides a great example: several charts occupy the same position, but only the selected one is shown. The problem I have is *slightly* different. Instead of knowing what graphs I need before the simulation runs, I have to dynamically create them. I can instantiate the graphs easily enough, but I can't figure out how to make them appear. I.e., I can say `TimeStackChart nextChart = new TimeStackChart(...)`, but `nextChart` never appears in the model. What function call am I missing to "register" the chart with the model? (I've encountered this hurdle in other models, too.) – Dylan Knowles Nov 05 '14 at 17:50
-
okay i think i see what you mean... i've also tried to programmatically add anylogic objects to the model, and it is not easy. Usually i look at the autogenerated java-code, and look for something like a create method. the object needs to be added to the top level presentation group. – Nikolaj Klitlund Børty Nov 06 '14 at 09:54
-
Same here -- being able to see that autogen'd code is a godsend. If adding it to the top level presentation group is what I'm missing, I'll be very happy to have that resolved. I can't check now, but you wouldn't happen to remember what the "proper" way to do that is, would you? – Dylan Knowles Nov 06 '14 at 16:46
-
PERFECT! That did it! (`presentation.add(chart)` for those who need it.) Nikolaj, can you write up an answer so I can accept it? Something along the lines of "See the Airplane model to start. Make several charts programmatically (using Main's Java code for chart creation as a template), give each chart the same (x,y,width,height), make only the selected one visible, and make sure to call `presentation.add(chart)` so the newly added charts appear." You helped me through this -- you deserve the credit. (If need be, I can edit your answer to polish it :) ) – Dylan Knowles Nov 06 '14 at 17:58
-
1great... i've edited the answer. if you have anything further to add, you are welcome to reedited it. – Nikolaj Klitlund Børty Nov 07 '14 at 07:52
have you tried the chart.refresh();
method?
Also try to update the embedding agent using agent.onChange()
.
hope that helps

- 10,603
- 3
- 16
- 28
-
I've tried chart.refresh() but that didn't work. I forgot about agent.onChange(). I'll give that a shot! – Dylan Knowles Nov 04 '14 at 16:43
-
`onChange()` does not seem to work, unfortunately. It doesn't exist for charts and invoking `onChange()` on the `Main` agent doesn't do anything, either. – Dylan Knowles Nov 04 '14 at 17:16
I encountered the exact same problem, and came up with workaround.
Try this, which I know works in AnyLogic v7:
if (getEngine.getState() == getEngine().PAUSED ){
dynChart.setSelectedItemIndices( new int[]{0} );
dynChart.setSelectedItemIndices( null );
}
I was able to get the chart to refresh manually when paused or finished by selecting/deselecting one of the legend items. I spent a few hours try out API variations to no avail, so I just emulated what was actually working in the UI and it worked.
I am dynamically generating a text item as the chart title, but I have not had the same luck getting the text to refresh when paused like the chart. Any ideas on that one?

- 14,400
- 15
- 47
- 66
-
I can't say that I've tried refreshing text in a chart. Maybe @Nikolaj can help? You may want to ask a new question to get feedback from the community. Asking a question in an answer is less likely to get you the answers you seek. :) – Dylan Knowles Jan 16 '15 at 18:32