nvd3 + discreteBarChart + add a line to chart - nvd3.js

working with discreteBarChart in nvd3 here is the basic example in jsfiddle
I have created this bar chart and I have got my data that feeds it to look like this as is required:
var data = [
{
key: "KPI_NAME",
values: [
{
"label" : "A" ,
"value" : -29.765957771107,
"series" : 0
} ,
{
"label" : "B" ,
"value" : 0,
"series" : 0
} ,
{
"label" : "C" ,
"value" : 32.807804682612,
"series" : 0
} ,
{
"label" : "D" ,
"value" : 196.45946739256,
"series" : 0
} ,
{
"label" : "E" ,
"value" : 0.19434030906893,
"series" : 0
} ,
{
"label" : "F" ,
"value" : -98.079782601442,
"series" : 0
} ,
{
"label" : "G" ,
"value" : -13.925743130903,
"series" : 0
} ,
{
"label" : "H" ,
"value" : -5.138732287,
"series" : 0
}
]
}
]
see the jsfiddle example -> this is with the series added "series: 0" to my data object. This is just how I am creating my object and does not affect the graph.
now i have added another series to my data feed and what i am trying to do is have a constant line in the graph but i get the following in this example(this is with the second series added in my data object)
Is there away I can get the second series to be a line so I would have a constant line accross the top with value 98?
Note: My data that feeds this graph could possibly be formatted better but I am just try to reuse what I already have.

Related

Recursively create hierarchical hash from Mongo collection in Ruby

I am trying to create a hash that looks like this:
{"difficulty"=>{"easy"=>{}, "normal"=>{}, "hard"=>{}}, "terrain"=>{"snow"=>{"sleet"=>{}, "powder"=>{}}, "jungle"=>{}, "city"=>{}}}
From a MongoDB collection which is and enumerable list of hashes that looks like this:
{
"_id" : "globalSettings",
"groups" : [
"difficulty",
"terrain"
],
"parent" : null,
"settings" : {
"maxEnemyCount" : 10,
"maxDamageInflicted" : 45,
"enemyHealthPoints" : 40,
"maxEnemySpeed" : 25,
"maxPlayerSpeed" : 32,
"lightShader" : "diffuse",
"fogDepth" : 12,
"terrainModifier" : 9
}
},
{
"_id" : "difficulty",
"groups" : [
"easy",
"normal",
"hard"
],
"parent" : "globalSettings",
"settings" : {
}
}
{
"_id" : "terrain",
"groups" : [
"snow",
"jungle",
"city"
],
"parent" : "globalSettings",
"settings" : {
}
}
{
"_id" : "snow",
"groups" : [
"sleet",
"powder"
],
"parent" : "terrain",
"settings" : {
"fogDepth" : 4
}
}
{
"_id" : "jungle",
"groups" : [ ],
"parent" : "terrain",
"settings" : {
"terrainModifier" : 6
}
}
{
"_id" : "city",
"groups" : [ ],
"parent" : "terrain",
"settings" : {
"lightShader" : "bumpedDiffuse"
}
}
{
"_id" : "easy",
"groups" : [ ],
"parent" : "difficulty",
"settings" : {
"maxEnemyCount" : 5
}
}
{
"_id" : "normal",
"groups" : [ ],
"parent" : "difficulty",
"settings" : {
}
}
{
"_id" : "hard",
"groups" : [ ],
"parent" : "difficulty",
"settings" : {
"maxEnemyCount" : 20
}
}
{
"_id" : "sleet",
"groups" : [ ],
"parent" : "snow",
"settings" : {
"fogDepth" : 2
}
}
{
"_id" : "powder",
"groups" : [ ],
"parent" : "snow",
"settings" : {
"terrainModifier" : 2
}
}
Every time I try to write the function, I get stuck when setting the parent of the group. How do I recurse, yet keep track of the path of hierarchy?
The closest I've come is with this:
def dbCurse(nodes, parent = nil)
withParent, withoutParent = nodes.partition { |n| n['parent'] == parent }
withParent.map do |node|
newNode={}
newNode[node["_id"]]={}
newNode[node["_id"]].merge(node["_id"] => dbCurse(withoutParent, node['_id']))
end
end
which gives me a crazy mix of arrays and hashes:
{"globalSettings"=>[{"difficulty"=>[{"easy"=>[]}, {"normal"=>[]}, {"hard"=>[]}]}, {"terrain"=>[{"snow"=>[{"sleet"=>[]}, {"powder"=>[]}]}, {"jungle"=>[]}, {"city"=>[]}]}]}
I think the arrays are getting mixed in there from the #map but I'm not sure how to get rid of them to get the clean hash of hashes I show at the top of my question.
Thank you,
David
So looking at your sample input, I'm going to make a core assumption:
The order of the hash objects in the input list are somewhat well-defined. Namely, that if the globalSettings hash refers to the group difficulty, then the next object in the list with _id == 'difficulty' and parent == 'globalSettings' is the correct match.
If this is true, then you can write a function that accepts a description of what you're looking for (i.e., the object with _id == 'difficulty' and parent == 'globalSettings') along with a reference to where you want that data stored, which can then recurse using deeper references.
def doit(obj_list, work = {})
work.each do |key, data|
# fetch the node
node_i = obj_list.index { |n| n['_id'] == key && n['parent'] == data[:parent] } or next
node = obj_list.delete_at(node_i)
# for each group of this node, create a new hash
# under this node's output pointer and queue it for parsing
new_work = {}
node['groups'].each do |group|
data[:output][group] = {}
new_work[group] = { parent: key, output: data[:output][group] }
end
# get the group data for this node
doit(obj_list, new_work)
end
end
input_data = JSON.parse(IO.read './input.json')
output_data = {}
doit( input_data, 'globalSettings' => { parent: nil, output: output_data } )
The trick here is that I'm handing the recursive call to doit the names of the next objects that I'm looking for from the list (using the current object's group list) and pairing each of those desired names with their parent and a reference to where I want the function to put the found data. Each recursive call to doit will use deeper and deeper references into the original output hash.

Update object in array with new fields mongodb

ai have some mongodb document
horses is array with id, name, type
{
"_id" : 33333333333,
"horses" : [
{
"id" : 72029,
"name" : "Awol",
"type" : "flat",
},
{
"id" : 822881,
"name" : "Give Us A Reason",
"type" : "flat",
},
{
"id" : 826474,
"name" : "Arabian Revolution",
"type" : "flat",
}
}
I need to add new fields
I thought something like that, but I did not go to his head
horse = {
"place" : 1,
"body" : 11
}
Card.where({'_id' => 33333333333}).find_and_modify({'$set' => {'horses.' + index.to_s => horse}}, upsert:true)
But all existing fields are removed and inserted new how to do that would be new fields added to existing
Indeed, this command will overwrite the subdocument
'$set': {
'horses.0': {
"place" : 1,
"body" : 11
}
}
You need to set individual fields:
'$set': {
'horses.0.place': 1,
'horses.0.body': 11
}

How to get the result in percentage form using nv.d3.js?

I am using nv.d3.js for generating a simple bar chart, and I'm getting the proper results. But what is want is , to get the result of y axis in percentage instead of the data itself. For ex, my data is as follows :-
data2 = [
{
"key": "How many Employees are there in company",
"values": [
{
"label" : "0-10" ,
"value" : 29
} ,
{
"label" : "10-20" ,
"value" : 0
} ,
{
"label" : "20-30" ,
"value" : 49
} ,
{
"label" : "30-40" ,
"value" : 30
} ,
{
"label" : "40-50" ,
"value" : 8
} ,
{
"label" : "50-60" ,
"value" : 98
} ,
{
"label" : "60-70" ,
"value" : 13
} ,
{
"label" : "70-80" ,
"value" : 5
}
]
}
]
And i want the result of "value" in percentage on the top of each bar instead of its value itself. For 98(the highest one) it should be 100% , for value 49, it should be 50% (calculated on the base of highest value) and so on. Is it possible to achieve this ?

ElasticSearch returns Zero Hits with MongoDB-River plugin

I am trying make "elasticsearch-river-mongodb" plug-in with my local MongoDB instance.
With help from http://satishgandham.com/2012/09/a-complete-guide-to-integrating-mongodb-with-elastic-search/#comment-2871
I am able to get rid of 404 exception. However, all my queries to ElasticSearch index return NO hits.
Following are the steps I have followed in sequence.
Installed MongoDB 2.4.3
Enabled ReplicaSet = rs0
Enabled oplogSize = 100
Restarted MongoDB Server
Configured the rsConf variable on MongoShell
Initiated the ReplicaSet
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2013-05-21T17:42:41Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 865,
"optime" : {
"t" : 1369157994,
"i" : 1
},
"optimeDate" : ISODate("2013-05-21T17:39:54Z"),
"self" : true
}
],
"ok" : 1
}
Installed ES
Installed attachment plugin (ES_HOME/bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/1.4.0)
Installed River plugin (ES_HOME/bin/plugin -install richardwilly98/elasticsearch-river-mongodb/1.4.0)
Restarted ES
Curl ES to index documents from MongoDB with following command successfully.
purl -XPUT 'http://`127.0.0.1`:9200/_river/mongodb/_meta' -d '{
"type": "mongodb",
"mongodb": {
"db": "players",
"collection": "scores"
},
"index": {
"name": "scoresindex",
"type": "score"
}
}'
Added new documents to MongoDB
On MongDB db.oplog.rs.find() collection returns new updates
rs0:PRIMARY> db.oplog.rs.find()
{ "ts" : { "t" : 1369152540, "i" : 1 }, "h" : NumberLong(0), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } }
{ "ts" : { "t" : 1369152706, "i" : 1 }, "h" : NumberLong("7734203254950592529"), "v" : 2, "op" : "i", "ns" : "players.scores", "o" : { "_id" : ObjectId("519b9cc2871b3116f0b8006e"), "name" : "rohit", "score" : 20 } }
{ "ts" : { "t" : 1369157720, "i" : 1 }, "h" : NumberLong("2546253279621321716"), "v" : 2, "op" : "i", "ns" : "test.scores", "o" : { "_id" : ObjectId("519bb058b6fd31855ec8b0af"), "name" : "karthik", "score" : 20 } }
{ "ts" : { "t" : 1369157994, "i" : 1 }, "h" : NumberLong("-3356531630527802451"), "v" : 2, "op" : "i", "ns" : "test.scores", "o" : { "_id" : ObjectId("519bb16ab6fd31855ec8b0b0"), "name" : "dinesh", "score" : 20 } }
The problem i am facing is : Searching on ElasticSearch using following command
curl -XGET 'http://`127.0.0.1`:9200/scoresindex/_search?q=name:dinesh'
for newly added document to scores collection on MongoDB does not return any results.
I have tried posting document directly to ES and it DOES return the results but not from the documents in MongoDB.
Is there something i am missing? Appreciate your help in advance.
Thank You
According to the project 1 MongoDB 2.4.3 is only supported in version 1.6.6 and above of the river.
Which version of Elasticsearch are you running?
Please take a look a the wiki page "install guide" section.
Thanks,
Richard.

How to create map from JSON response in Ruby on Rails 3?

I need to create a map/array for auto complete from a JSON response and I am looking for the best, most efficient way to do it in Ruby and Rails 3. A portion of the response is below and the working code I have is before it. What is the one line of code I need to create locations for me?
# Need help making this more efficient
response_fields = JSON.parse(response.body)
predictions = response_fields['predictions']
predictions.each do |prediction|
locations << prediction['description']
end
Sample response from API:
{
"predictions" : [
{
"description" : "Napa, CA, United States",
"id" : "cf268f9fb9a1b46aed72d59ab85ed40f982763c6",
"matched_substrings" : [
{
"length" : 4,
"offset" : 0
}
],
"reference" : "CjQvAAAAqZWNGzqtJf3awNuQNQdnZpl4dBVVXFPrPdz29r1jo1GMWYFuz3KRlK9HgdgszOThEhDeYz_vYgcOPJTaYehF11bUGhR8yH9zqMGV9kenZIo9OTBrSwftgg",
"terms" : [
{
"offset" : 0,
"value" : "Napa"
},
{
"offset" : 6,
"value" : "CA"
},
{
"offset" : 10,
"value" : "United States"
}
],
"types" : [ "locality", "political", "geocode" ]
},
You can shorten your code like this:
locations = JSON.parse(response.body)['predictions'].map { |p| p['description'] }

Resources