Is it possible to disable a line of a linechart by default? I want a line to be part of the legend, but not enabled by default, so the user can chooses when to activate it.
It is supported in the new version released now. Version 2.0.1
LineChart chart = ...
soChart.getDefaultLegend().hide(chart);
thanks for this quick update!
disabling a line works as expected, but it seems enabling them again goes wrong. they are enabled for a very short time, but get disabled again.
I tried such a scenario and it is working for me. Do you have a code snippet that demonstrate your issue?
Problem seems to be caused because I call soChart.update();
from a thread to load and add data to the chart. When disabling this line, I can indeed activate/deactive lines.
AsyncManager.register(this, task -> {
while (true) {
Data data = service.getData()
task.push(() -> {
xValues.add(System.currentTimeMillis() - timestampOffset);
yValues[0]
.add(data.getValue1());
yValues[1]
.add(data.getValue2());
yValues[2]
.add(data.getValue3());
yValues[3]
.add(data.getValue4());
try {
soChart.update();
} catch (Exception ex) {
// log
}
}
Thread.sleep(200);
}
});
Yes, when you are invoking the soChart.update()
asynchronously, the initial state of the Legend
is restored. This can be solved by removing the Legend
immediately after the first soChart.update()
call.
Outside the async part (initial update):
LineChart chart = ...
soChart.getDefaultLegend().hide(chart);
...
...
soChart.update();
soChart.disableLegend(); // No legend info will be send in the further updates
If you have customized the Legend
before the initial call:
LineChart chart = ...
soChart.disableLegend(); // Default legend is disabled
Legend myLegend = new Legend(); // Creating my own legend
myLegend.hide(chart);
soChart.add(myLegend); // Adding
...
...
soChart.update();
soChart.remove(myLegend); // No legend info will be send in the further updates
Again a perfect solution! You really rock!!!
Small note, I had to use (case 1):
soChart.disableDefaultLegend();
This method is not available:
soChart.disableLegend();
Yes, it should be: soChart.disableDefaultLegend()
Nice to know that the solution worked for you.
Frank Delporte:
AsyncManager.register(this, task → {
while (true) {
Data data = service.getData()
task.push(() → {
xValues.add(System.currentTimeMillis() - timestampOffset);
yValues[0]
.add(data.getValue1());
yValues[1]
.add(data.getValue2());
yValues[2]
.add(data.getValue3());
yValues[3]
.add(data.getValue4());
try {
soChart.update();
} catch (Exception ex) {
// log
}
}
Thread.sleep(200);
}
});
In the new version 2.0.2, there is new feature called “data channel”. So, your code can be written as follows and is more efficient, because it sends only newly added data to the client:
// After your first update
soChart.update();
// Create a data channel to push further data later
DataChannel dc = new DataChannel(soChart,
xValues, yValues[0]
, yValues[1]
, yValues[2]
, yValues[3]
);
...
AsyncManager.register(this, task -> {
while (true) {
Data data = service.getData()
task.push(() -> {
try {
// You can also use push(...) instead of append(...)
dc.append(System.currentTimeMillis() - timestampOffset,
data.getValue1(), data.getValue2(),
data.getValue3(), data.getValue4());
} catch (Exception ex) {
// log
}
}
Thread.sleep(200);
}
});
Also, there is no need to disable the legend etc.
Hi Syam, this is indeed a very nice addition which makes the code a lot cleaner! Thanks a lot.
I think there is even another possible improvement ;-)
A dc.reset()
would be very usefull to clean the existing data from the chart.
Available in version 2.0.3
And working like a charm!!!