Issues filtering dc.seriesChart - d3.js

I am new to posting a question on stackoverflow, so any guidance is much appreciated!
I am using crossfilter.js and dc.js to plot charts (fairly new to both). One of the requirements is for a seriesChart (scatterplot). Note: I am using the latest beta release, since the scatterplot is a requirement and the latest stable version does not appear support seriesChart & scatterplot. This particular chart is posing me a problem when I filter/zoom. I see the following error in the console when doing so:
Uncaught TypeError: dimension.filterFunction is not a function...
PriceVsTime = dc.seriesChart("#PriceVsTime");
//$('#PriceVsTime').parent('td').addClass('tdOrders1');
PriceVsTimeDimension = crossfilterData.dimension(function (d) { if (d.chart_price > 0) return d.start_datetime; });
PriceVsTimeGroup = PriceVsTimeDimension.group().reduce(
function reduceAdd(p, v) {
++p.count;
p.order_type = v.order_type;
p.execution_type = v.execution_type == 'REPLACED' ? (v.change_qty > 0 ? 'UPSIZE' : 'DOWNSIZE') : v.execution_type;
p.chart_price = v.chart_price;
return p;
},
function reduceRemove(p, v) {
--p.count;
return p;
},
function reduceInitial() {
return { count: 0 };
}
);
var symbolScale = d3.scale.ordinal().range(d3.svg.symbolTypes);
console.log(d3.svg.symbolTypes);
var symbolAccessor = function (d) {
switch (d.value.execution_type) {
case 'NEW':
return d3.svg.symbolTypes[1]; // diamond
case 'CANCELED':
return d3.svg.symbolTypes[0]; // cross
case 'UPSIZE':
return d3.svg.symbolTypes[4]; // triangle-up
case 'DOWNSIZE':
return d3.svg.symbolTypes[5]; // triangle-down
default:
return d3.svg.symbolTypes[3];
}
};
var subChart = function (c) {
return dc.scatterPlot(c)
.existenceAccessor(function (d) { if (d.value.count > 0) { return d.value.execution_type; } })
.symbol(symbolAccessor)
.symbolSize(8)
.highlightedSize(12)
;
};
var PriceVsTimeSeries = function (d) { if (d.value.count > 0) { return d.value.execution_type; } };
var PriceVsTimeKey = function (d) { if (d.value.count > 0) { return d.key; } };
var PriceVsTimeValue = function (d) { if (d.value.count > 0) { return d.value.chart_price; } };
var yPriceVsTime = roundAxis(d3.extent(PriceVsTimeGroup.all(), function (d) { if (d.value.chart_price > 0) { return d.value.chart_price; } }), 10);
function roundAxis(item, interval) {
return [item[0] - item[0] % interval - interval, item[1] - item[1] % interval + interval];
}
PriceVsTime
.chart(subChart)
.width(2 * width1)
.height(height3)
.dimension(PriceVsTimeDimension)
.group(PriceVsTimeGroup)
.seriesAccessor(PriceVsTimeSeries)
.keyAccessor(PriceVsTimeKey)
.valueAccessor(PriceVsTimeValue)
.x(d3.time.scale().domain(DateTimeDomain))
.y(d3.scale.linear().domain(yPriceVsTime))
.yAxisLabel('Price')
.xAxisLabel('Time')
.margins(margin1)
;
PriceVsTime.yAxis().tickFormat(d3.format('s'));
PriceVsTime.brushOn(true).mouseZoomable(true);
PriceVsTime.legend(dc.legend().x(1075).y(0).itemHeight(13).gap(5).horizontal(false).itemWidth(100));

Related

Discord Bot on Heroku not online

Hello everyone I have looked up this issue but can't find an answer to my specific problem.
So basically the bot is not turning on, it is offline. I don't know where to put the token or how to put the token. Please let me know of the problem or if you need more code/details. Thank you.
Code URL: https://github.com/Verggz/Electrolite
main.bot.js
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const discord_js_1 = __importDefault(require("discord.js"));
const builders_1 = require("#discordjs/builders");
const SlashCommand_model_1 = require("./model/SlashCommand.model");
const HelpCommand_command_1 = require("./commands/HelpCommand.command");
const fs_extra_1 = __importDefault(require("fs-extra"));
const BINFlipCommand_command_1 = require("./commands/flip/BINFlipCommand.command");
var client = new discord_js_1.default.Client({ "intents": [discord_js_1.default.Intents.FLAGS.GUILDS, discord_js_1.default.Intents.FLAGS.GUILD_MEMBERS, discord_js_1.default.Intents.FLAGS.GUILD_MESSAGES] });
client.on('ready', () => __awaiter(void 0, void 0, void 0, function* () {
var helpcommandbuilder = new builders_1.SlashCommandBuilder()
.setName("help")
.setDescription("Get the list of commands that Project: Scyll has.");
var binflipcommandbuilder = new builders_1.SlashCommandBuilder()
.setName("binflip")
.setDescription("Finds a BIN snipe on the auction house based on the amount of profit you can make.")
.addIntegerOption(option => option.setName("profit")
.setDescription("the amount of profit you would like to make.").setRequired(true));
SlashCommand_model_1.SlashCommand.CreateSlashCommands([helpcommandbuilder, binflipcommandbuilder]);
}));
client.on('interactionCreate', function (interaction) {
return __awaiter(this, void 0, void 0, function* () {
if (!interaction.isCommand())
return;
new HelpCommand_command_1.HelpCommand(interaction);
new BINFlipCommand_command_1.BINFlipCommand(interaction);
});
});
client.login(fs_extra_1.default.readJSONSync("./config.json").SERVER_BOT_KEY);
Another file:
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SlashCommand = void 0;
const discord_js_1 = require("discord.js");
const axios = __importStar(require("axios"));
const rest_1 = require("#discordjs/rest");
const v9_1 = require("discord-api-types/v9");
const fs_extra_1 = __importDefault(require("fs-extra"));
var token = fs_extra_1.default.readJSONSync("./config.json").SERVER_BOT_KEY;
class SlashCommand {
constructor(interaction) {
this.http = axios.default;
this.interaction = interaction;
this.command = interaction.commandName;
this.purple = "#BA55D3";
this.backtick = "`";
this.gold = "#d4af37";
this.red = "#C70039";
}
CreateEmbed() {
return new discord_js_1.MessageEmbed()
.setAuthor("Project: Scyll", this.interaction.client.user.avatarURL()).setFooter("Project:Scyll 0.1.0").setTimestamp();
}
static CreateSlashCommands(commands) {
return __awaiter(this, void 0, void 0, function* () {
var clientid = (yield fs_extra_1.default.readJSON("./config.json")).SERVER_CLIENT_ID;
yield SlashCommand.REST.put(v9_1.Routes.applicationCommands(clientid), { "body": commands });
});
}
}
exports.SlashCommand = SlashCommand;
SlashCommand.REST = new rest_1.REST({ "version": "9" }).setToken(token);
Look for a config.json file and put you token there

Need to make custom reducer to deduct two values

"Date(UTC)","Market","Type","Price","Amount","Total","Fee","Fee Coin"
12:18:07","ETCBTC","BUY","0.002064","1.05","0.00216720","0.00105","ETC"
"2018-05-26 06:01:12","ETCBTC","SELL","0.00207","5.86","0.01213020","0.00001213","BTC"
"2018-05-25 22:47:14","ETCBTC","BUY","0.002","1.32","0.00264000","0.00132","ETC"
This is part of my dataset. The problem is that in my data set "Total" that I need to use is just numbers - to make them work I have to connect them with "Type" (BUY/SELL).
BUY should work like "-" and SELL like "+"; the difference between them is that what I need to display.
I am just learning. So I didn't try a lot.
function show_profit(ndx) {
var typeDim = ndx.dimension(dc.pluck("Type"));
var profit = typeDim.group().reduce(
function (p, v) {
p.count++;
p.total += v.Total;
return p;
},
function (p, v) {
p.count--;
p.total -= v.Total;
return p;
},
function () {
return { count:0, total: 0};
}
);
dc.barChart("#profit")
.width(500)
.height(300)
.dimension(typeDim)
.group(profit)
.valueAccessor(function (d) {
if (d.value.count == 0) {
return 0;
} else {
return d.value.total;
}
})
.transitionDuration(500)
.x(d3.scale.ordinal())
.xUnits(dc.units.ordinal)
.elasticY(true)
.xAxisLabel("Type")
.yAxisLabel("Amount")
.yAxis().ticks(20);
}
I just made graph with volume per BUY and SELL.
My target is to find the difference between BUY and SELL and display it in a line graph.
I'm not sure if I understand your question completely, but if you simply want to apply SELL as positive and BUY as negative, you should be able to multiply the values by 1 or -1 in your reduction functions:
function mult(type) {
switch(type) {
case 'SELL': return 1;
case 'BUY': return -1;
default: throw new Error('unknown Type ' + type);
}
var profit = typeDim.group().reduce(
function (p, v) {
p.count++;
p.total += mult(v.Type) * v.Total;
return p;
},
function (p, v) {
p.count--;
p.total -= mult(v.Type) * v.Total;
return p;
},
function () {
return { count:0, total: 0};
}
);

Display multiple bar on barChart from a custom reducer

I have a group with custom reducer calculating various total and average values. The goal is to show them all on the same barChart. But I can only get the first bar to show. Here is the JSFiddler
https://jsfiddle.net/71k0guxe/15/
Is it possible to show all the value on the barChart?
Thanks in advance!
Data
ID,SurveySent,ResponseReceived
1,Yes,No
2,No,No
3,Yes,Yes
4,No,No
5,Yes,Yes
6,No,No
7,Yes,No
8,No,No
9,Yes,No
10,No,No
Code
var chart = dc.barChart("#test");
//d3.csv("morley.csv", function(error, experiments) {
var experiments = d3.csvParse(d3.select('pre#data').text());
var ndx = crossfilter(experiments),
dimStat = ndx.dimension(function(d) {return "Statistics";}),
groupStat = dimStat.group().reduce(reduceAdd, reduceRemove, reduceInitial);
function reduceAdd(p, v) {
++p.count;
if (v.SurveySent === "Yes") p.sent++;
if (v.ResponseReceived === "Yes") p.received++;
return p;
}
function reduceRemove(p, v) {
--p.count;
if (v.SurveySent === "Yes") p.sent--;
if (v.ResponseReceived === "Yes") p.received--;
return p;
}
function reduceInitial() {
return {count: 0, sent: 0, received: 0};
}
chart
.width(400)
.height(400)
.xUnits(dc.units.ordinal)
.label(function(d) { return d.data.value })
.elasticY(true)
.x(d3.scaleOrdinal().domain(["Total", "Sent", "Received"]))
.brushOn(false)
.yAxisLabel("This is the Y Axis!")
.dimension(dimStat)
.group(groupStat)
.valueAccessor(function (d) {
//Is it possible to return count sent and received all from here?
return d.value.count;
})
.on('renderlet', function(chart) {
chart.selectAll('rect').on("click", function(d) {
console.log("click!", d);
});
});
chart.render();
Just got some idea from the FAQ section of dc.js/wiki/FAQ
Fake Groups
"dc.js uses a very limited part of the crossfilter API - in fact, it really only uses dimension.filter() and group.all()."
I don't care about filtering, so i just need to mark up my own group.all. Basically transpose it from one row to multiple row. Works my purpose.
/* solution */
var groupStatTranposed = group_transpose(groupStat);
function group_transpose(source_group, f) {
return {
all:function () {
return [
{key: "Total", value: source_group.all()[0].value.count},
{key: "Sent", value: source_group.all()[0].value.sent},
{key: "Received", value: source_group.all()[0].value.received}
];
}
};
}
//use groupStatTranposed in the chart.
/** solution */

Update multiple tags rowchart in dc.js

I am looking for how to create a rowchart in dc.js to show and filter items with multiple tags. I've summed up a few answers given on stack overflow, and now have a working code.
var data = [
{id:1, tags: [1,2,3]},
{id:2, tags: [3]},
{id:3, tags: [1]},
{id:4, tags: [2,3]},
{id:5, tags: [3]},
{id:6, tags: [1,2,3]},
{id:7, tags: [1,2]}];
var content=crossfilter(data);
var idDimension = content.dimension(function (d) { return d.id; });
var grid = dc.dataTable("#idgrid");
grid
.dimension(idDimension)
.group(function(d){ return "ITEMS" })
.columns([
function(d){return d.id+" : "; },
function(d){return d.tags;},
])
function reduceAdd(p, v) {
v.tags.forEach (function(val, idx) {
p[val] = (p[val] || 0) + 1; //increment counts
});
return p;
}
function reduceRemove(p, v) {
v.tags.forEach (function(val, idx) {
p[val] = (p[val] || 0) - 1; //decrement counts
});
return p;
}
function reduceInitial() {
return {};
}
var tags = content.dimension(function (d) { return d.tags });
var groupall = tags.groupAll();
var tagsGroup = groupall.reduce(reduceAdd, reduceRemove, reduceInitial).value();
tagsGroup.all = function() {
var newObject = [];
for (var key in this) {
if (this.hasOwnProperty(key) && key != "") {
newObject.push({
key: key,
value: this[key]
});
}
}
return newObject;
}
var tagsChart = dc.rowChart("#idtags")
tagsChart
.width(400)
.height(200)
.renderLabel(true)
.labelOffsetY(10)
.gap(2)
.group(tagsGroup)
.dimension(tags)
.elasticX(true)
.transitionDuration(1000)
.colors(d3.scale.category10())
.label(function (d) { return d.key })
.filterHandler (function (dimension, filters) {
var fm = filters.map(Number)
dimension.filter(null);
if (fm.length === 0)
dimension.filter(null);
else
dimension.filterFunction(function (d) {
for (var i=0; i < fm.length; i++) {
if (d.indexOf(fm[i]) <0) return false;
}
return true;
});
return filters;
}
)
.xAxis().ticks(5);
It can be seen on http://jsfiddle.net/ewm76uru/24/
Nevertheless, the rowchart is not updated when I filter by one tag. For example, on jsfiddle, if you select tag '1', it filters items 1,3,6 and 7. Fine. But the rowchart is not updated... I Should have tag '3' count lowered to 2 for example.
Is there a way to have the rowchart tags counts updated each time I filter by tags ?
Thanks.
After a long struggle, I think I have finally gathered a working solution.
As said on crossfilter documentation : "a grouping intersects the crossfilter's current filters, except for the associated dimension's filter"
So, the tags dimension is not filtered when tag selection is modified, and there is no flag or function to force this reset. Nevertheless, there is a workaround (given here : https://github.com/square/crossfilter/issues/146).
The idea is to duplicate the 'tags' dimension, and to use it as the filtered dimension :
var tags = content.dimension(function (d) { return d.tags });
// duplicate the dimension
var tags2 = content.dimension(function (d) { return d.tags });
var groupall = tags.groupAll();
...
tagsChart
.group(tagsGroup)
.dimension(tags2) // and use this duplicated dimension
as it can been seen here :
http://jsfiddle.net/ewm76uru/30/
I hope this will help.

dc.js - pieChart ignore unwanted values

I have a piechart I'm displaying using the following dimension -
var types = facts.dimension(function (d) {
if (d.types === 2)
return "Type 2";
else if (d.types === 3)
return "Type 3";
else
return "Other";
});
I would like to not return, ignore, all other types, so the pie chart would just display Type 2 and Type 3. I cannot seem to get this working though I'm sure it's simple. Can I do something within that dimension or do I need to filter before?
Thanks for any help.
Have you tried creating a new type and then creating a dimension on top of that?
facts.forEach(function(d) {
if (d.types == 2) {
d._type = "Type 2";
} else if (d.types == 3) {
d._type = "Type 3";
} else {
d._type = "Other";
}
});
var types = facts.dimension(dc.pluck('_type'))
You need to (1) Filter your data then (2) remove it from the bin. Run your data through ensure_group_bins(myGroup) you'll get the chart you're after
function remove_empty_bins(source_group) {
return {
all:function () {
return source_group.all().filter(function(d) {
return d.key != "unwanted" ;
});
}
};}
function ensure_group_bins(source_group, bins) {
return {
all:function () {
var result = source_group.all().slice(0), // copy original results (we mustn't modify them)
found = {};
result.forEach(function(d) {
found[d.key] = true;
});
bins.forEach(function(d) {
if(!found[d])
result.push({key: d, value: 0});
});
return result;
}
};};
dc.js Github

Resources