Update charts to work with generic data processing.
This commit is contained in:
Родитель
f2d1bacaa5
Коммит
7d735490d2
|
@ -11,15 +11,7 @@ function IntentInterestCharts() {
|
|||
}
|
||||
|
||||
IntentInterestCharts.prototype = {
|
||||
_graphPie: function(chartType, domainList, title) {
|
||||
let chartData = [];
|
||||
for (let domain in domainList) {
|
||||
let obj = {
|
||||
"label": domain,
|
||||
"value": domainList[domain]
|
||||
};
|
||||
chartData.push(obj);
|
||||
}
|
||||
_graphPie: function(chartType, chartData, title) {
|
||||
let div = d3.select("#" + chartType)
|
||||
.append("div")
|
||||
.attr("height", 100)
|
||||
|
@ -45,25 +37,12 @@ IntentInterestCharts.prototype = {
|
|||
graph: function(data, clearChart) {
|
||||
d3.select('#intentCharts').selectAll("*").remove();
|
||||
d3.select('#interestCharts').selectAll("*").remove();
|
||||
|
||||
for (let intentData of data[this._currentType][this._currentNamespace]["sortedIntents"]) {
|
||||
let maxWeightDate = intentData["maxWeightDate"];
|
||||
let domainList = intentData["dates"][maxWeightDate]["domainList"];
|
||||
let maxIntentDate = d3.time.format('%x')(new Date(intentData["dates"][maxWeightDate]["x"]))
|
||||
let title = intentData["category"] + " (" + maxIntentDate + ")";
|
||||
this._graphPie("intentCharts", domainList, title);
|
||||
this._graphPie("intentCharts", intentData["chartJSON"], intentData["title"]);
|
||||
}
|
||||
for (let interestData of data[this._currentType][this._currentNamespace]["sortedInterests"]) {
|
||||
let domainList = {};
|
||||
for (let dateInfo in interestData["dates"]) {
|
||||
for (let domain in interestData["dates"][dateInfo]["domainList"]) {
|
||||
if (!domainList[domain]) {
|
||||
domainList[domain] = 0;
|
||||
}
|
||||
domainList[domain] += interestData["dates"][dateInfo]["domainList"][domain];
|
||||
}
|
||||
}
|
||||
let title = interestData["category"];
|
||||
this._graphPie("interestCharts", domainList, title);
|
||||
this._graphPie("interestCharts", interestData["chartJSON"], interestData["title"]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -46,34 +46,15 @@ WeightIntensityChart.prototype = {
|
|||
graph: function(data, clearChart) {
|
||||
// _pointToInterestsMap is used to make up for a bug in nvd3 where multiple points can't
|
||||
// appear in the same location.
|
||||
this._pointToInterestsMap = {};
|
||||
this._values = [];
|
||||
d3.select('#weightIntensityChart svg').selectAll("*").remove();
|
||||
|
||||
this._pointToInterestsMap = data[this._currentType][this._currentNamespace]["pointToInterestsMap"];
|
||||
this._xMin = data[this._currentType][this._currentNamespace]["xMin"];
|
||||
this._yMin = data[this._currentType][this._currentNamespace]["yMin"];
|
||||
this._xMax = data[this._currentType][this._currentNamespace]["xMax"];
|
||||
this._yMax = data[this._currentType][this._currentNamespace]["yMax"];
|
||||
|
||||
for (let interest in data[this._currentType][this._currentNamespace]["interests"]) {
|
||||
let x = data[this._currentType][this._currentNamespace]["interests"][interest]["x"];
|
||||
let y = data[this._currentType][this._currentNamespace]["interests"][interest]["y"];
|
||||
let hash = x.toString() + y.toString();
|
||||
|
||||
if (!this._pointToInterestsMap[hash]) {
|
||||
this._pointToInterestsMap[hash] = [];
|
||||
let point = data[this._currentType][this._currentNamespace]["interests"][interest];
|
||||
this._values.push(point);
|
||||
}
|
||||
this._pointToInterestsMap[hash].push(interest);
|
||||
}
|
||||
|
||||
let chartJSON = [{
|
||||
key: "test",
|
||||
values: this._values
|
||||
}];
|
||||
d3.select('#weightIntensityChart svg').selectAll("*").remove();
|
||||
d3.select('#weightIntensityChart svg')
|
||||
.datum(chartJSON)
|
||||
.datum(data[this._currentType][this._currentNamespace]["chartJSON"])
|
||||
.transition().duration(500)
|
||||
.call(this._chart);
|
||||
|
||||
|
|
|
@ -202,114 +202,91 @@ TimelineDataProcessor.prototype = {
|
|||
});
|
||||
Services.obs.notifyObservers(null, "chart-update",
|
||||
JSON.stringify({"type": "timeline", "data": storage.chartData.timelineData}));
|
||||
return storage.chartData.timelineData;
|
||||
return bucketData;
|
||||
},
|
||||
}
|
||||
|
||||
WeightIntensityDataProcessor.prototype = {
|
||||
_setXYMaxMin: function(storedData) {
|
||||
let categories = Object.keys(storedData["interests"]);
|
||||
let xVals = categories.map((category) => {
|
||||
return storedData["interests"][category]["x"];
|
||||
});
|
||||
storedData["xMax"] = Math.max.apply(null, xVals);
|
||||
storedData["xMin"] = Math.min.apply(null, xVals);
|
||||
|
||||
let yVals = categories.map((category) => {
|
||||
return storedData["interests"][category]["y"];
|
||||
});
|
||||
storedData["yMax"] = Math.max.apply(null, yVals);
|
||||
storedData["yMin"] = Math.min.apply(null, yVals);
|
||||
},
|
||||
|
||||
consume: function(bucketData) {
|
||||
DataProcessorHelper.initChartInStorage("weightIntensityData");
|
||||
DataProcessorHelper.iterateOverTypeNamespace(bucketData, storage.chartData.weightIntensityData, (bucketData, storedData) => {
|
||||
if (!storedData["interests"]) {
|
||||
storedData["interests"] = {};
|
||||
// pointToInterestsMap is used to make up for a bug in nvd3 where multiple points can't
|
||||
// appear in the same location.
|
||||
let pointToInterestsMap = {};
|
||||
let values = [];
|
||||
|
||||
storedData["xMin"] = bucketData["xMin"];
|
||||
storedData["yMin"] = bucketData["yMin"];
|
||||
storedData["xMax"] = bucketData["xMax"];
|
||||
storedData["yMax"] = bucketData["yMax"];
|
||||
|
||||
for (let interest in bucketData["interests"]) {
|
||||
let x = bucketData["interests"][interest]["x"];
|
||||
let y = bucketData["interests"][interest]["y"];
|
||||
let hash = x.toString() + y.toString();
|
||||
|
||||
if (!pointToInterestsMap[hash]) {
|
||||
pointToInterestsMap[hash] = [];
|
||||
values.push({"x": x, "y": y});
|
||||
}
|
||||
pointToInterestsMap[hash].push(interest);
|
||||
}
|
||||
|
||||
// Sort interests by maxWeight and dayCount.
|
||||
let sortedByWeights = [];
|
||||
let sortedByDayCount = [];
|
||||
DataProcessorHelper.interestsToArray(bucketData, sortedByWeights);
|
||||
DataProcessorHelper.interestsToArray(bucketData, sortedByDayCount);
|
||||
sortedByWeights.sort(DataProcessorHelper.propertyComparator("maxWeight"));
|
||||
sortedByDayCount.sort(DataProcessorHelper.propertyComparator("dayCount"));
|
||||
|
||||
// Rank interests.
|
||||
let rankMaxWeight = 1;
|
||||
let rankDayCount = 1;
|
||||
for (let i = 0; i < sortedByWeights.length; i++) {
|
||||
if (i > 0 && (sortedByWeights[i - 1]["maxWeight"] != sortedByWeights[i]["maxWeight"])) {
|
||||
rankMaxWeight++;
|
||||
}
|
||||
if (i > 0 && (sortedByDayCount[i - 1]["dayCount"] != sortedByDayCount[i]["dayCount"])) {
|
||||
rankDayCount++;
|
||||
}
|
||||
|
||||
if (!storedData["interests"][sortedByWeights[i]["category"]]) {
|
||||
storedData["interests"][sortedByWeights[i]["category"]] = {};
|
||||
}
|
||||
if (!storedData["interests"][sortedByDayCount[i]["category"]]) {
|
||||
storedData["interests"][sortedByDayCount[i]["category"]] = {};
|
||||
}
|
||||
storedData["interests"][sortedByDayCount[i]["category"]]["x"] = rankDayCount;
|
||||
storedData["interests"][sortedByWeights[i]["category"]]["y"] = rankMaxWeight;
|
||||
|
||||
storedData["interests"][sortedByWeights[i]["category"]]["maxWeightDate"] = sortedByWeights[i]["maxWeightDate"];
|
||||
storedData["interests"][sortedByWeights[i]["category"]]["dates"] = sortedByWeights[i]["dates"];
|
||||
}
|
||||
this._setXYMaxMin(storedData);
|
||||
storedData["chartJSON"] = [{
|
||||
key: "key",
|
||||
values: values
|
||||
}];
|
||||
storedData["pointToInterestsMap"] = pointToInterestsMap;
|
||||
});
|
||||
|
||||
Services.obs.notifyObservers(null, "chart-update",
|
||||
JSON.stringify({"type": "weightIntensity", "data": storage.chartData.weightIntensityData}));
|
||||
return storage.chartData.weightIntensityData;
|
||||
return bucketData;
|
||||
},
|
||||
}
|
||||
|
||||
IntentInterestDataProcessor.prototype = {
|
||||
_cartesianDistance: function(x1, y1, x2, y2) {
|
||||
return Math.sqrt(Math.pow(y1 - y2, 2) + Math.pow((x1 - x2), 2));
|
||||
_createChartData: function(domainList, storedData, dataType, title) {
|
||||
let chartData = [];
|
||||
for (let domain in domainList) {
|
||||
let obj = {
|
||||
"label": domain,
|
||||
"value": domainList[domain]
|
||||
};
|
||||
chartData.push(obj);
|
||||
}
|
||||
storedData[dataType].push({
|
||||
"chartJSON": chartData,
|
||||
"title": title
|
||||
});
|
||||
},
|
||||
|
||||
consume: function(bucketData) {
|
||||
DataProcessorHelper.initChartInStorage("intentInterestData");
|
||||
DataProcessorHelper.iterateOverTypeNamespace(bucketData, storage.chartData.intentInterestData, (bucketData, storedData) => {
|
||||
if (!storedData["interests"]) {
|
||||
storedData["interests"] = {};
|
||||
storedData["sortedInterests"] = [];
|
||||
storedData["sortedIntents"] = [];
|
||||
|
||||
for (let intentData of bucketData["sortedIntents"].splice(0, 10)) {
|
||||
let maxWeightDate = intentData["maxWeightDate"];
|
||||
let domainList = intentData["dates"][maxWeightDate]["domainList"];
|
||||
let maxIntentDate = (new Date(intentData["dates"][maxWeightDate]["x"])).toLocaleDateString();
|
||||
let title = intentData["category"] + " (" + maxIntentDate + ")";
|
||||
let chartJSON = this._createChartData(domainList, storedData, "sortedIntents", title);
|
||||
}
|
||||
|
||||
let intentX = bucketData["xMin"];
|
||||
let intentY = bucketData["yMax"];
|
||||
let interestX = bucketData["xMax"];
|
||||
let interestY = bucketData["yMax"];
|
||||
|
||||
let sortedInterests = [];
|
||||
let sortedIntents = [];
|
||||
for (let category in bucketData["interests"]) {
|
||||
if (!storedData["interests"][category]) {
|
||||
storedData["interests"][category] = {};
|
||||
for (let interestData of bucketData["sortedInterests"].splice(0, 10)) {
|
||||
let domainList = {};
|
||||
for (let dateInfo in interestData["dates"]) {
|
||||
for (let domain in interestData["dates"][dateInfo]["domainList"]) {
|
||||
if (!domainList[domain]) {
|
||||
domainList[domain] = 0;
|
||||
}
|
||||
domainList[domain] += interestData["dates"][dateInfo]["domainList"][domain];
|
||||
}
|
||||
}
|
||||
let categoryX = bucketData["interests"][category]["x"];
|
||||
let categoryY = bucketData["interests"][category]["y"];
|
||||
|
||||
storedData["interests"][category]["intentDist"] =
|
||||
this._cartesianDistance(intentX, intentY, categoryX, categoryY);
|
||||
storedData["interests"][category]["interestDist"] =
|
||||
this._cartesianDistance(interestX, interestY, categoryX, categoryY);
|
||||
storedData["interests"][category]["dates"] = bucketData["interests"][category]["dates"];
|
||||
storedData["interests"][category]["maxWeightDate"] = bucketData["interests"][category]["maxWeightDate"];
|
||||
let title = interestData["category"];
|
||||
let chartJSON = this._createChartData(domainList, storedData, "sortedInterests", title);
|
||||
}
|
||||
DataProcessorHelper.interestsToArray(storedData["interests"], sortedInterests);
|
||||
DataProcessorHelper.interestsToArray(storedData["interests"], sortedIntents);
|
||||
sortedInterests.sort(DataProcessorHelper.propertyComparator("interestDist"));
|
||||
sortedIntents.sort(DataProcessorHelper.propertyComparator("intentDist"));
|
||||
|
||||
delete storedData["interests"];
|
||||
storedData["sortedIntents"] = sortedIntents.slice(0, 10);
|
||||
storedData["sortedInterests"] = sortedInterests.slice(0, 10);
|
||||
});
|
||||
Services.obs.notifyObservers(null, "chart-update",
|
||||
JSON.stringify({"type": "intentInterest", "data": storage.chartData.intentInterestData}));
|
||||
|
|
Загрузка…
Ссылка в новой задаче