How to remove a series from DynamicTimeSeriesCollection? - jfreechart

Anyone can guide me on how to remove a series from DynamicTimeSeriesCollection?
In this example, I want to delete series S1
DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(50, 120, new Second());
dataset.addSeries(gaussianData(), 0, "S1");
dataset.addSeries(gaussianData(), 1, "S2");
I don't know how to do it.
Thank you very much.

DynamicTimeSeriesCollection stores values in an array of ValueSequence. By default a ValueSequence can't be deleted but as ValueSequence is protected you can subclass DynamicTimeSeriesCollection wrtie your own removeSeries method and see if that solves your problem.
Take a look at DynamicTimeSeriesCollection#addSeries and reverse the steps, remember to call fireSeriesChanged() at the end.

Thanks GrahamA for your answer.
Cause i need to touch seriesCount is private, so i must create a new class named DynamicTimeSeriesCollectionModified (copy-paste from DynamicTimeSeriesCollection) and write my own removeSeries and it works.
public void deleteSeries(int seriesNumber) {
//remove item in valueHistory array
List<ValueSequence> listValueHistory = new ArrayList<ValueSequence>(Arrays.asList(valueHistory));
listValueHistory.remove(seriesNumber);
valueHistory = listValueHistory.toArray(valueHistory);
//remove item in seriesKeys array
List<Comparable> listSeriesKeys = new ArrayList<Comparable>(Arrays.asList(seriesKeys));
listSeriesKeys.remove(seriesNumber);
seriesKeys = listSeriesKeys.toArray(seriesKeys);
//update seriesCount
seriesCount--;
fireSeriesChanged();
}

Related

Remove random item from array and push to new array

Thank you in advance! I'm new to coding and learning how to deal with Arrays. I am trying to remove random items from an array (deck of cards), and populate a new array (called hand). The problem I always seem to have with arrays is taking the results of one and creating a new function/array/ etc.. Currently, I am outputting 2 separate arrays and I can't seem to push them into one.
let deck = ["dA","dQ","dK","dJ","d10","d09","d08",
"d07","d06","d05","d04","d03","d02","hA","hQ","hK",
"hJ","h10","h09","h08","h07","h06","h05","h04","h03"];
var hand = deck.splice(Math.floor(Math.random()*deck.length),1);
console.log(hand)
var hand = deck.splice(Math.floor(Math.random()*deck.length),1);
console.log(hand);
In your code, just you need to push the value returned from splice method rather than directly assigning it.
By this way every time a new value that gets deleted and will be added to the new array called hand. Hope this helps. :-)
let deck = ["dA","dQ","dK","dJ","d10","d09","d08",
"d07","d06","d05","d04","d03","d02","hA","hQ","hK",
"hJ","h10","h09","h08","h07","h06","h05","h04","h03"];
var hand = [];
const getSelectedCard = () => deck.splice(Math.floor(Math.random()*deck.length),1)
let selectedCard = getSelectedCard();
hand.push(...selectedCard) //or hand.push(selectedCard[0])
console.log(hand);
selectedCard = getSelectedCard();
hand.push(...selectedCard)
console.log(hand);

Put all maps inside one list

My problem with Flutter is when I'm converting an array to a list, there is every item is a separated list and I want all the items in just one list.
Example output when putting 2 items:
[{title: ITEM1}, {title: ITEM2}]
I want to put like this:
[{title: ITEM1, title: ITEM2}]
I hope you guys understand the problem
void _addPecas() {
if ((_pecasController.text.isEmpty) ||
((_pecasController.text.trimLeft() == ("")))) {
print("Campo Vazio");
} else {
setState(() {
Map<String, dynamic> newPeca = Map();
newPeca["title"] = _pecasController.text.trimLeft();
_pecasController.text = "";
_pecasList.add(newPeca);
print(_pecasList);
});
}
}
if [{title: ITEM1, title: ITEM2}] is what you want to achieve,
The problem here is that you are creating a new Map called newPeca every time your function runs. .
Map<String, dynamic> newPeca = Map();
So by the time you set your values you call _pecasList.add(newPeca); that new map will be appended to your List thus you get [{title: ITEM1}, {title: ITEM2}]
Working with the assumption that you want only one Map in _pecaList, _newPeca should always reference that one Map
Map<String, dynamic> newPeca = _pecaList[0]
then you can add your desired values newPeca["title"] = _pecasController.text.trimLeft();
One more problem you'll run into, you want to have duplicate keys in the Map, which is not possible.
The newer value in the map will overwrite the existing one.
E.g
newPeca[title] = "Item 1"
newPeca[title] = "Item 2"
newPeca[title] will end up being Item 2
As I understood, you are asking for one Map inside List and add values into that Map and finally add it into a List. Here is how you can do it:
Map<String, dynamic> newPeca = Map();
if (_pecasController.text.isEmpty ||
_pecasController.text.trimLeft() == ("")) { // you had too much unwanted brackets here
print("Campo Vazio");
} else {
setState(() {
newPeca["title"] = _pecasController.text.trimLeft(); // you have to have unique key if you want to keep in one Map. See the dart pad example I have provided a link below
_pecasController.text = "";
// _pecasList = [newPeca]; // this is also working without outside .add() but if the list is long, im not recommending this.
print(_pecasList);
});
}
// outside the iteration
_pecasList.add(newPeca);
Edit simple dart example in dartpad
Update:
Make sure this: Map<String, dynamic> newPeca = Map(); outside the iteration. This way you are not creating Map for each and every iteration. You are adding to same map and that map you should add after each iteration to the List or just create empty list assign as a new list in each and every iteration(Btw, not the performant way).
Secondly, when you add to the list inside iteration each and every time map will add to the list.
Finally, Even if you make those right, your map will not take same value as title because the reason is your key of the map entry is title and you cannot have duplicate keys inside map and the list should be outside the iteration.

using a variable name to load movie clip to stage using As3

I have created a Spelling Puzzle in AS3 that loads a list of words from an XML file into an Array.
As the code loops through the array, it assigns each word to a variable called "current_word", then scrambles the letters of "current_word" and displays them on stage.
I would like to add an animated MovieClip as a visual aid with the class name that matches the value of current word.
For example if the current word is 'bear', then a MovieClip with the class name 'bear' is loaded from library to stage.
I was trying to create an empty movie clip called "tempItemClip" and overwrite its value with the value of the var current_word.
No errors, but it's not working. I am new to ActionScript. Can someone advise me on the best solution?
public function getWord()
{
current_word=pWord[ques_num];
setTiles(current_word.length);
ques_num++;
trace(current_word);
var tempItemClip:MovieClip = new MyItem();
Puzzle_screen.addChildAt(tempItemClip,4);
tempItemClip.x=380;
tempItemClip.y=130;
//var myClip:Object = getDefinitionByName(current_word);
var myClip:MovieClip = new MovieClip();
tempItemClip[current_word] = myClip;
tempItemClip.addChild(myClip);
}
It's not clear what you are asking, but I believe this is what you want :
var clipClass:Class = getDefinitionByName(current_word) as Class;
var myClip:MovieClip = new clipClass();

KnockoutJS Accessing an array of objects in a separate class

So i have 2 classes, users and health readings. i made a array of user objects with an array of readings inside it.i just want access to the date and weight in the reading array, i have been trying for a long time to figure this out! please help this simple problem is driving me nuts!
// Class to represent a row in the seat reservations grid
function Reading(theDate,theWeight)
{
self.theDate=ko.observable(theDate);
self.theWeight=ko.observable(theWeight);
}
function User(name,weight,date) {
var self = this;
self.name = name;
self.date = date;
self.weight = ko.observable(weight);
self.theReadings = ko.observableArray([
new Reading(12,13)
]);
}
// Editable data
self.users = ko.observableArray([
new User("George",1,2012),
new User("Bindu",2,2012)
]);
/this alerts an object but i dont know how to access the weight/date variable
alert(self.users()[0].theReadings()[0]);
self.readings = ko.observableArray([
new Reading(12,13)
Just missed a few things on this one.
Here ya go.
http://jsfiddle.net/EN7y4/3/
Namely. You had "self.theWeight" in function Reading instead of "this."...
Happy Coding!
alert(self.users()[0].theReadings()[0].theWeight();
I would recommend removing the 'thes'. That's an uncommon style.

How to convert SET to array in APEX?

I have map with key and value and my goal is to get list of 'key'.
I am thinking to get it to the array or List.
Got to the point where I have key values in the SET but haven't figure out
how to convert to the array.
below is my code:
Map<String, String> mmm = new Map<String, String>();
mmm.put('one', 'oneee');
mmm.put('two', 'twooo');
mmm.put('three', 'threeee');
mmm.put('four', 'fourff');
//outputs values in the map
system.debug('=======values()==========>' + mmm.values());
//outputs key in the map
system.debug('=======keyset()===========>' + mmm.keyset());
//get keys in the type SET
SET<string> s = mmm.keyset();
//returns 4
system.debug('------------------------------------' + s.size());
s.arrayTo() //this method does not exist :(
Use List.addAll method?
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_list.htm?SearchType=Stem
If not - you could always manually loop through the set...
Could you use:
Set keys = mmm.keySet();
List keyList = new List(keys);
You should always used generics for type safety.
Map<String, String> mmm = new Map<String, String>();
mmm.put('one', 'oneee');
mmm.put('two', 'twooo');
mmm.put('three', 'threeee');
mmm.put('four', 'fourff');
List<String> lstKeys = new List<String>(mmm.keyset());
System.debug('Output : '+lstKeys);
As per link : https://salesforce.stackexchange.com/questions/5447/is-there-a-difference-between-an-array-and-a-list-in-apex .
This solution will work.
A quick and simple way to do this would also be:
new List<String>(mmm.keySet());
//or
new String[mmm.keySet()];
In some code I wrote recently I've got a Set<String> and am passing it into a method that takes a List<String> with methodName( new List<String>(setVariable) ); or methodName(new String[setVariable] );
Yes I know the post is 11+ years old... but it is also what comes up when searching so I put my answer here.

Resources