I have an array which has time stored in Unix timestamp and number of matches played on that day,
[
{
"time": 1332547200000,
"count": 2
},
{
"time": 1332633600000,
"count": 1
},
{
"time": 1332720000000,
"count": 4
},
{
"time": 1332806400000,
"count": 4
},
...
],
I am trying to convert this into a string of elements with date format "20070101", value
for example, I should get the output in the form, "20070101, 46 \n" +20072102, 26 \n" + etc....
:)
function pad2(number) {
return (number < 10 ? '0' : '') + number
}
var ar = [{ "time": 1332547200000, "count": 2 }, { "time": 1332633600000, "count": 1 }, { "time": 1332720000000, "count": 4 }];
var output = ""
for (i=0; i<ar.length; i++) {
var d = new Date(parseInt(ar[i].time));
var year = ""+d.getFullYear();
var month = pad2(d.getMonth());
var date = pad2(d.getDate());
var output = output+year+date+month+", "+ar[i].count+"\n";
}
document.write(output);
var d = new Date(milliseconds);
var formatted = d.getFullYear()+d.getMonth()+d.getDate();
Unfortunately there doesn't seem to be any date formatting/zero padding functions in raw javascript. I'd recommend using jQuery or another library. This is the raw solution.
var data = [
{ "time": 1332547200000, "count": 2 },
{ "time": 1332633600000, "count": 1 },
{ "time": 1332720000000, "count": 4 }
];
function pad(str, n) {
if (str.length < n) {
return pad("0" + str, n);
} else {
return str
}
}
for (var i = 0; i < data.length; i++) {
var datum = data[i];
var date = new Date(datum.time);
var str = date.getFullYear()
+ pad((date.getMonth() + 1).toString(), 2)
+ pad(date.getDate().toString(), 2)
+ ", " + datum.count + " \\n";
window.alert(str);
}
That is a JSON string and you can use $.parseJSON in jQuery:
myJSON = $.parseJSON('[{ "time": 1332547200000, "count": 2 }, { "time": 1332633600000, "count": 1 }]');
var newJSON = new Array();
var i = 0;
$.each(myJSON, function(count, line){
var newTime = new Date(parseInt(line.time));
newTime = newTime.getFullYear()+''+('0' + (newTime.getMonth()+1)).slice(-2)+''+('0' + newTime.getDate()).slice(-2);
$('#json').append(newTime+', '+line.count+'\n');
});
DEMO JSFIDDLE
Here is Your FIDDLE
FIDDLE
AND Your Code:
var a=[
{ "time": 1332547200000, "count": 2 }, { "time": 1332633600000, "count": 1 }, { "time": 1332720000000, "count": 4 }, { "time": 1332806400000, "count": 4 }, { "time": 1332892800000, "count": 5 }, { "time": 1332979200000, "count": 1 }, { "time": 1333065600000, "count": 1 }, { "time": 1333152000000, "count": 1 }, { "time": 1333324800000, "count": 2 }, { "time": 1333411200000, "count": 2 }, { "time": 1333497600000, "count": 3 }, { "time": 1333584000000, "count": 4 }, { "time": 1333670400000, "count": 3 }, { "time": 1333756800000, "count": 2 }, { "time": 1333843200000, "count": 2 }, { "time": 1333929600000, "count": 1 }, { "time": 1334102400000, "count": 9 }, { "time": 1334188800000, "count": 8 }, { "time": 1334275200000, "count": 6 }, { "time": 1334361600000, "count": 3 }, { "time": 1334448000000, "count": 2 }, { "time": 1334534400000, "count": 3 }, { "time": 1334620800000, "count": 3 }, { "time": 1334707200000, "count": 5 }, { "time": 1334793600000, "count": 4 }, { "time": 1334880000000, "count": 1 }, { "time": 1334966400000, "count": 5 }, { "time": 1335139200000, "count": 6 }, { "time": 1335225600000, "count": 3 }, { "time": 1335312000000, "count": 8 }, { "time": 1335398400000, "count": 8 }, { "time": 1335484800000, "count": 9 }, { "time": 1335571200000, "count": 4 }, { "time": 1338508800000, "count": 69 }, { "time": 1338595200000, "count": 30 }, { "time": 1338681600000, "count": 17 }, { "time": 1338768000000, "count": 70 }, { "time": 1338854400000, "count": 109 }, { "time": 1338940800000, "count": 80 }, { "time": 1339027200000, "count": 141 }, { "time": 1339113600000, "count": 94 }, { "time": 1339200000000, "count": 34 }, { "time": 1339286400000, "count": 26 }, { "time": 1339372800000, "count": 91 }, { "time": 1339459200000, "count": 166 }, { "time": 1339545600000, "count": 89 }, { "time": 1339632000000, "count": 121 }, { "time": 1339718400000, "count": 74 }, { "time": 1339804800000, "count": 48 }, { "time": 1339891200000, "count": 13 }, { "time": 1339977600000, "count": 58 }, { "time": 1340064000000, "count": 123 }, { "time": 1340150400000, "count": 110 }]
function getformateddate(date)
{
var date= new Date(date);
return date.getFullYear().toString()+date.getDate()+date.getMonth();
}
var finalString='';
for (var i=0;i<a.length;i++)
{
finalString+=getformateddate(a[i].time)+' '+a[i].count+" \\"+"n"
}
document.write(finalString)
Related
I made a line change via axios.path, but after changes in db.json I constantly need to reload the page. How can I avoid this and make the data update automatically?
const submitEdits = (item) => {
axios.patch(`http://localhost:3004/item/${item.id}`, { text: editingText })
setIdItem(null);
setEditingText('')
}
My db.json
{
"item": [
{
"text": "123123123",
"id": 0,
"data": {
"year": 2012,
"day": 25,
"month": 1
}
},
{
"text": "Поступил в институт",
"id": 1,
"data": {
"year": 2007,
"day": 12,
"month": 4
}
},
{
"id": 2,
"text": "123",
"data": {
"year": 2022,
"day": 16,
"month": 5
}
}
]
}
I'm using amcharts4 for my data visualization, using nodejs, mongodb, my problem is how can I filter my charts between dates, using from this date to this date. I'm having a hard time here, nobody is giving me any ideas on how to do this. Here is a sample demo using amcharts 3: https://www.amcharts.com/demos-v3/multiple-data-sets-v3/, any idea is appreciated, I need to finish this for my thesis please somebody give me any idea
initChart();
function initChart(){
// Themes begin
am4core.useTheme(am4themes_dark);
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"date": "2012-09-08",
"value": 43
}, {
"date": "2012-09-09",
"value": 40
}, {
"date": "2012-09-10",
"value": 39
}, {
"date": "2012-09-11",
"value": 34
}, {
"date": "2012-09-12",
"value": 29
}, {
"date": "2012-09-13",
"value": 34
}, {
"date": "2012-09-14",
"value": 37
}, {
"date": "2012-09-15",
"value": 42
}, {
"date": "2012-09-16",
"value": 49
}, {
"date": "2012-09-17",
"value": 46
}, {
"date": "2012-09-18",
"value": 47
}, {
"date": "2012-09-19",
"value": 55
}, {
"date": "2012-09-20",
"value": 59
}, {
"date": "2012-09-21",
"value": 58
}, {
"date": "2012-09-22",
"value": 57
}, {
"date": "2012-09-23",
"value": 61
}, {
"date": "2012-09-24",
"value": 59
}, {
"date": "2012-09-25",
"value": 67
}, {
"date": "2012-09-26",
"value": 65
}, {
"date": "2012-09-27",
"value": 61
}, {
"date": "2012-09-28",
"value": 66
}, {
"date": "2012-09-29",
"value": 69
}, {
"date": "2012-09-30",
"value": 71
}, {
"date": "2012-10-01",
"value": 67
}, {
"date": "2012-10-02",
"value": 63
}, {
"date": "2012-10-03",
"value": 46
}, {
"date": "2012-10-04",
"value": 32
}, {
"date": "2012-10-05",
"value": 21
}, {
"date": "2012-10-06",
"value": 18
}, {
"date": "2012-10-07",
"value": 21
}, {
"date": "2012-10-08",
"value": 28
}, {
"date": "2012-10-09",
"value": 27
}, {
"date": "2012-10-10",
"value": 36
}, {
"date": "2012-10-11",
"value": 33
}, {
"date": "2012-10-12",
"value": 31
}, {
"date": "2012-10-13",
"value": 30
}, {
"date": "2012-10-14",
"value": 34
}, {
"date": "2012-10-15",
"value": 38
}, {
"date": "2012-10-16",
"value": 37
}, {
"date": "2012-10-17",
"value": 44
}, {
"date": "2012-10-18",
"value": 49
}, {
"date": "2012-10-19",
"value": 53
}, {
"date": "2012-10-20",
"value": 57
}, {
"date": "2012-10-21",
"value": 60
}, {
"date": "2012-10-22",
"value": 61
}, {
"date": "2012-10-23",
"value": 69
}, {
"date": "2012-10-24",
"value": 67
}, {
"date": "2012-10-25",
"value": 72
}, {
"date": "2012-10-26",
"value": 77
}, {
"date": "2012-10-27",
"value": 75
}, {
"date": "2012-10-28",
"value": 70
}, {
"date": "2012-10-29",
"value": 72
}, {
"date": "2012-10-30",
"value": 70
}];
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.minGridDistance = 50;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value";
series.dataFields.dateX = "date";
series.strokeWidth = 3;
series.fillOpacity = 0.5;
// Add vertical scrollbar
chart.scrollbarY = new am4core.Scrollbar();
chart.scrollbarY.marginLeft = 0;
// Add cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "zoomY";
chart.cursor.lineX.disabled = true;
// Create a horizontal scrollbar with previe and place it underneath the date axis
chart.scrollbarX = new am4charts.XYChartScrollbar();
chart.scrollbarX.series.push(series);
chart.scrollbarX.parent = chart.bottomAxesContainer;
chart.events.on("ready", function () {
dateAxis.zoom({start:0.10, end:1});
});
}
body { background-color: #30303d; color: #fff; }
#chartdiv {
width: 100%;
height: 300px;
}
<!-- Resources -->
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/dark.js"></script>
<div id="chartdiv"></div>
You can filter by start and end date:
const startDate = new Date("2012-10-25");
const endDate = new Date("2012-10-30");
const data = [{
"date": "2012-10-21",
"value": 60
}, {
"date": "2012-10-22",
"value": 61
}, {
"date": "2012-10-23",
"value": 69
}, {
"date": "2012-10-24",
"value": 67
}, {
"date": "2012-10-25",
"value": 72
}, {
"date": "2012-10-26",
"value": 77
}, {
"date": "2012-10-27",
"value": 75
}, {
"date": "2012-10-28",
"value": 70
}, {
"date": "2012-10-29",
"value": 72
}, {
"date": "2012-10-30",
"value": 70
}]
const filteredData = data.filter(el => new Date(el.date).getTime() >= startDate.getTime() && new Date(el.date) <= endDate.getTime());
console.log(filteredData);
I want to filter the items array objects which match the "model" key in the models array and store them in an array. I did succeed in my attempt but I am not very satisfied with my effort. Are there any better ways of doing it?
Any suggestions on how to do it using underscore.js and lodash? Or using the native javascript map and filter functions?
The JSON object
{
"items": [
{
"model": "ooc0d",
"code": "2x4qr",
"price": 33
},
{
"model": "ruie9",
"code": "2f6gi",
"price": 22
},
{
"model": "aqu0d",
"code": "2f6gi",
"price": 21
},
{
"model": "ddebd",
"code": "2f6gi",
"price": 25
},
{
"model": "ddebd",
"code": "2f6gi",
"price": 29
}
],
"models": [
{
"model": "ruie9",
"year": 1998
},
{
"model": "ooc0d",
"year": 1991
},
{
"model": "aqu0d",
"year": 1994
},
{
"model": "ddebd",
"year": 1995
},
{
"model": "odq76",
"year": 1999
}
]
}
My Solution
const { models, items } = jsonData;
const newarray = [];
for(let i = 0; i < models.length; i++) {
for(let j = 0; j < items.length; j++) {
if(items[j].model===models[i].model) {
let obj = {
...items[j],
year: models[i].year
}
newarray.push(obj);
}
}
}
I would take a slightly different approach. I guess you might like it.
const models = [
{
"model": "ruie9",
"year": 1998
},
{
"model": "not-found",
"year": 1991
},
{
"model": "aqu0d",
"year": 1994
},
{
"model": "ddebd",
"year": 1995
},
{
"model": "odq76",
"year": 1999
}
];
const items = [
{
"model": "ooc0d",
"code": "2x4qr",
"price": 33
},
{
"model": "ruie9",
"code": "2f6gi",
"price": 22
},
{
"model": "aqu0d",
"code": "2f6gi",
"price": 21
},
{
"model": "ddebd",
"code": "2f6gi",
"price": 25
},
{
"model": "ddebd",
"code": "2f6gi",
"price": 29
}
];
const transformed = models.reduce((res, val) => {
res[val.model] = val;
return res;
}, {}); // Transform models into a dictionary.
const filtered = items.filter(i => i.model in transformed);
console.log(filtered);
You could do this:
I thought you wanted to add the year from models array too.
If so, look at this implementation. This is more efficient O(n) than O(n*n) solution that you attempted earlier. For large arrays O(n*n) is not preferred.
let items = [{
"model": "ooc0d",
"code": "2x4qr",
"price": 33
},
{
"model": "ruie9",
"code": "2f6gi",
"price": 22
},
{
"model": "aqu0d",
"code": "2f6gi",
"price": 21
},
{
"model": "ddebd",
"code": "2f6gi",
"price": 25
},
{
"model": "ddebd",
"code": "2f6gi",
"price": 29
}
];
let models = [
{
"model": "ruie9",
"year": 1998
},
{
"model": "ooc0d",
"year": 1991
},
{
"model": "aqu0d",
"year": 1994
}
];
let objModels = models.reduce(function(r,v) {
r[v.model] = v;
return r;
}, {});
let objItems = items.reduce(function(r,v) {
r[v.model] = v;
return r;
}, {});
let ans = [];
for(let key in objItems) {
if(key in objModels) {
let o = objItems[key];
o.year = objModels[key].year;
ans.push(o);
}
}
console.log(ans);
You can rewrite
let obj = {
...items[j],
year: models[i].year
}
as
let obj = Object.assign({}, items[j], { year: models[i].year });
And you can also use Array.prototype.forEach instead of a for loop, like so
models.forEach((m) => {
items.forEach((i) => {
if (m.id === i.id) {
let obj = Object.assign({}, i, { year: m.year });
newArray.push(obj);
}
})
})
I tried to keep it as similar to your solution as possible.
Try this snippet:
const jsonData = {
"items": [{
"model": "ooc0d",
"code": "2x4qr",
"price": 33
},
{
"model": "ruie9",
"code": "2f6gi",
"price": 22
},
{
"model": "aqu0d",
"code": "2f6gi",
"price": 21
},
{
"model": "ddebd",
"code": "2f6gi",
"price": 25
},
{
"model": "ddebd",
"code": "2f6gi",
"price": 29
}
],
"models": [{
"model": "ruie9",
"year": 1998
},
{
"model": "ooc0d",
"year": 1991
},
{
"model": "aqu0d",
"year": 1994
},
{
"model": "ddebd",
"year": 1995
},
{
"model": "odq76",
"year": 1999
}
]
};
var newArray = jsonData.models.reduce(
(acc, modelData) => {
let filteredItems = jsonData.items.filter(item => item.model === modelData.model);
if (filteredItems.length) {
acc.push(...filteredItems);
}
return acc;
}, [])
console.log(newArray);
I have the following array:
"devices": [
{
"_id": "HP Printer",
"count": 1
},
{
"_id": "55UA8990",
"count": 1
},
{
"_id": "Mac OS X 10.5",
"count": 1
},
{
"_id": "Nokia",
"count": 4
},
{
"_id": "Ubuntu Linux",
"count": 3
},
{
"_id": "LG handset",
"count": 1
},
{
"_id": "Samsung Home Audio/Video equipment",
"count": 1
},
{
"_id": "Generic Linux",
"count": 1
},
{
"_id": "Sony handset",
"count": 1
},
{
"_id": "Mac OS X 10.8",
"count": 12
},
{
"_id": "Linux Handheld",
"count": 1
},
{
"_id": "Symbian OS",
"count": 15
},
{
"_id": "RIM BlackBerry",
"count": 28
},
{
"_id": "Apple iPod",
"count": 1
},
{
"_id": "LG BL40",
"count": 1
},
{
"_id": "Slingbox",
"count": 67
},
{
"_id": "Windows XP",
"count": 60
},
{
"_id": "BlackBerry",
"count": 51
},
{
"_id": "Chrome OS",
"count": 7
},
{
"_id": "Eye-Fi Wireless Memory Card",
"count": 6
},
{
"_id": "Mac OS X 10.9",
"count": 62
},
{
"_id": "Mac OS X 10.10",
"count": 215
},
{
"_id": "Windows Vista",
"count": 4
},
{
"_id": "Windows Mobile OS",
"count": 213
},
{
"_id": "Windows 7/Vista",
"count": 394
},
{
"_id": "Mac OS X 10.11",
"count": 303
},
{
"_id": "Samsung TV",
"count": 1
},
{
"_id": "PlayStation Portable",
"count": 1
},
{
"_id": "Mac OS X",
"count": 755
},
{
"_id": "Windows 7",
"count": 612
},
{
"_id": "Apple iPad",
"count": 2597
},
{
"_id": "Ellipsis 8",
"count": 193
},
{
"_id": "Mac OS X 10.12",
"count": 550
},
{
"_id": "Nexus",
"count": 4657
},
{
"_id": "Windows 10",
"count": 772
},
{
"_id": "Windows 8",
"count": 1191
},
{
"_id": "Xbox 360",
"count": 39
},
{
"_id": "Mac OS X 10.6",
"count": 5
},
{
"_id": "Apple iPhone",
"count": 41039
},
{
"_id": "iOS",
"count": 25725
},
{
"_id": "Debian-based Linux",
"count": 9
},
{
"_id": null,
"count": 5291
},
{
"_id": "Mac OS X 10.7",
"count": 16
},
{
"_id": "Belkin Wireless Router",
"count": 1
},
{
"_id": "Windows",
"count": 1002
},
{
"_id": "Android",
"count": 51314
}
]
I'm trying to group all the values where the _id corresponds to the same os and add the count from each element, so I could get something like:
[
{
"label": "Windows",
"value": "9999"
},
{
"label": "Android",
"value": "8888"
},
{
"label": "iOS",
"value": "7777"
},
{
"label": "Macos",
"value": "10000"
},
{
"label": "Other",
"value": "5000"
}
]
I'm at a complete lost here, I would really appreciate some help and guidance.
Used Array.prototype.reduce and String.prototype.match for your scenario. Hope this helps.
var input = {"devices":[{"_id":"HP Printer","count":1},{"_id":"55UA8990","count":1},{"_id":"Mac OS X 10.5","count":1},{"_id":"Nokia","count":4},{"_id":"Ubuntu Linux","count":3},{"_id":"LG handset","count":1},{"_id":"Samsung Home Audio/Video equipment","count":1},{"_id":"Generic Linux","count":1},{"_id":"Sony handset","count":1},{"_id":"Mac OS X 10.8","count":12},{"_id":"Linux Handheld","count":1},{"_id":"Symbian OS","count":15},{"_id":"RIM BlackBerry","count":28},{"_id":"Apple iPod","count":1},{"_id":"LG BL40","count":1},{"_id":"Slingbox","count":67},{"_id":"Windows XP","count":60},{"_id":"BlackBerry","count":51},{"_id":"Chrome OS","count":7},{"_id":"Eye-Fi Wireless Memory Card","count":6},{"_id":"Mac OS X 10.9","count":62},{"_id":"Mac OS X 10.10","count":215},{"_id":"Windows Vista","count":4},{"_id":"Windows Mobile OS","count":213},{"_id":"Windows 7/Vista","count":394},{"_id":"Mac OS X 10.11","count":303},{"_id":"Samsung TV","count":1},{"_id":"PlayStation Portable","count":1},{"_id":"Mac OS X","count":755},{"_id":"Windows 7","count":612},{"_id":"Apple iPad","count":2597},{"_id":"Ellipsis 8","count":193},{"_id":"Mac OS X 10.12","count":550},{"_id":"Nexus","count":4657},{"_id":"Windows 10","count":772},{"_id":"Windows 8","count":1191},{"_id":"Xbox 360","count":39},{"_id":"Mac OS X 10.6","count":5},{"_id":"Apple iPhone","count":41039},{"_id":"iOS","count":25725},{"_id":"Debian-based Linux","count":9},{"_id":null,"count":5291},{"_id":"Mac OS X 10.7","count":16},{"_id":"Belkin Wireless Router","count":1},{"_id":"Windows","count":1002},{"_id":"Android","count":51314}]};
var output = input.devices.reduce(function(a, b) {
var match = b._id?b._id.match(/(Mac|Windows|Android|iOS)/):null;
match = match ? match[0] : 'Others';
a[match] = (a[match] || 0) + b.count;
return a;
}, {});
output = Object.keys(output).map(function(k){
return {
label: k,
value: output[k]
};
});
console.log(output);
Create an array of regex values each matching specific OS like .*windows.*.
Iterate your data array trying to match each entry's _id with the regex.
If it's a match add count value to value in output array. If it's not a match then add to "Other".
How you do it is up to you, it's just a general idea.
You could do something like:
var results = [];
devices.forEach(function(d) {
if(results.length === 0){ //shortcut for the first element
results.push({label:d._id, value: d.count});
} else {
var found = false;
for(var j =0; j < results.length; j++){
var i = results[j];
if(i.label === d._id){
found = true;
i.value += d.count;
results[j] = i;
}
});
if(!found) {
results.push({label:d._id, value: d.count});
}
}
});
return results;
This will iterate over the devices array and find the element in the array that matches and add the count to the value for that item in the array of results. If the item is not found, or it is the first item, a new object is pushed to the results array.
You could use an array with the grouping strings and use a hash table for counting.
var data = { devices: [{ _id: "HP Printer", count: 1 }, { _id: "55UA8990", count: 1 }, { _id: "Mac OS X 10.5", count: 1 }, { _id: "Nokia", count: 4 }, { _id: "Ubuntu Linux", count: 3 }, { _id: "LG handset", count: 1 }, { _id: "Samsung Home Audio/Video equipment", count: 1 }, { _id: "Generic Linux", count: 1 }, { _id: "Sony handset", count: 1 }, { _id: "Mac OS X 10.8", count: 12 }, { _id: "Linux Handheld", count: 1 }, { _id: "Symbian OS", count: 15 }, { _id: "RIM BlackBerry", count: 28 }, { _id: "Apple iPod", count: 1 }, { _id: "LG BL40", count: 1 }, { _id: "Slingbox", count: 67 }, { _id: "Windows XP", count: 60 }, { _id: "BlackBerry", count: 51 }, { _id: "Chrome OS", count: 7 }, { _id: "Eye-Fi Wireless Memory Card", count: 6 }, { _id: "Mac OS X 10.9", count: 62 }, { _id: "Mac OS X 10.10", count: 215 }, { _id: "Windows Vista", count: 4 }, { _id: "Windows Mobile OS", count: 213 }, { _id: "Windows 7/Vista", count: 394 }, { _id: "Mac OS X 10.11", count: 303 }, { _id: "Samsung TV", count: 1 }, { _id: "PlayStation Portable", count: 1 }, { _id: "Mac OS X", count: 755 }, { _id: "Windows 7", count: 612 }, { _id: "Apple iPad", count: 2597 }, { _id: "Ellipsis 8", count: 193 }, { _id: "Mac OS X 10.12", count: 550 }, { _id: "Nexus", count: 4657 }, { _id: "Windows 10", count: 772 }, { _id: "Windows 8", count: 1191 }, { _id: "Xbox 360", count: 39 }, { _id: "Mac OS X 10.6", count: 5 }, { _id: "Apple iPhone", count: 41039 }, { _id: "iOS", count: 25725 }, { _id: "Debian-based Linux", count: 9 }, { _id: null, count: 5291 }, { _id: "Mac OS X 10.7", count: 16 }, { _id: "Belkin Wireless Router", count: 1 }, { _id: "Windows", count: 1002 }, { _id: "Android", count: 51314 }] },
os = ["Windows", "Android", "iOS", "Mac OS", "Other"],
result = data.devices.reduce(function (hash) {
return function (r, a) {
var key = 'Other';
os.some(function (o) {
if (typeof a._id === 'string' && a._id.toLowerCase().indexOf(o.toLowerCase()) !== -1) {
key = o;
return true;
}
});
if (!hash[key]) {
hash[key] = { label: key, value: 0 };
r.push(hash[key]);
}
hash[key].value += a.count;
return r;
};
}(Object.create(null)), []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have a set of data including possible multiple values for a day.
I would like to plot each single record on the chart to see their distribution.
I tried using serial charts but the problems are 1) many records are missing from the chart (not sure why), and 2) the cursor only hovers the first entry (see the very first Y value only first entry can be hovered.)
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"graphs": [{
"bullet": "round",
"valueField": "value",
"lineAlpha": 0
}],
"chartCursor": {
"zoomable": false,
},
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
},
"dataProvider": [{
"date": "2012-07-27",
"value": 13
}, {
"date": "2012-07-27",
"value": 11
}, {
"date": "2012-07-27",
"value": 15
}, {
"date": "2012-07-27",
"value": 16
}, {
"date": "2012-07-27",
"value": 18
}, {
"date": "2012-08-27",
"value": 13
}, {
"date": "2012-08-02",
"value": 22
}, {
"date": "2012-08-03",
"value": 23
}, {
"date": "2012-08-04",
"value": 20
}, {
"date": "2012-08-05",
"value": 17
}, {
"date": "2012-08-06",
"value": 16
}, {
"date": "2012-08-07",
"value": 18
}, {
"date": "2012-08-08",
"value": 21
}, {
"date": "2012-08-09",
"value": 26
}, {
"date": "2012-08-10",
"value": 24
}, {
"date": "2012-08-11",
"value": 29
}, {
"date": "2012-08-12",
"value": 32
}, {
"date": "2012-08-13",
"value": 18
}, {
"date": "2012-08-14",
"value": 24
}, {
"date": "2012-08-15",
"value": 22
}, {
"date": "2012-08-16",
"value": 18
}, {
"date": "2012-08-17",
"value": 19
}, {
"date": "2012-08-18",
"value": 14
}, {
"date": "2012-08-19",
"value": 15
}, {
"date": "2012-08-20",
"value": 12
}]
}
);
Is there any workaround?
I also tried XY Chart, but it doesn't have the date support.
Thanks
There can be only one data item per unique date in amCharts.
You should combine data points into one, like:
{"date":"2012-08-27","value1":10,"value2":12,"value3":14}
and create a separate graph for each value field.
Here is the solution.Suddenly, someone will need
AmCharts.makeChart("chartdiv", {
categoryAxesSettings: {
minPeriod: "hh",//(at least that is not grouped)
groupToPeriods : ["DD", "WW","MM"]//(Data will be grouped by day,week and month)
}
});
more