Display top n values based on count and not on alphabets dc.js
I have tried using the code below to display the top 3 values based on the count of the val in the json provided below.
yearRingChart.width(300).height(300).dimension(yearDim).group(
spendPertime).innerRadius(50).controlsUseVisibility(true)
.data(function(d)
return d.order(
function(d)
return d.val;
).top(3);
).ordering(function(d)
return -d.val;
);
But this is getting sorted based on alphabetical order and not based on val.
I need to display the top 3 names with highest val in the json provided.
The code snippet is provided below -
var yearRingChart = dc.pieChart("#chart-ring-year"),
spendHistChart = dc.barChart("#chart-hist-spend"),
spenderRowChart = dc.rowChart("#chart-row-spenders");
var table = dc.dataTable('#table');
// use static or load via d3.csv("spendData.csv", function(error, spendData) /* do stuff */);
var spendData = [
Name : 'A',
val : '100',
time : 9,
place : 'Kolkata'
,
Name : 'B',
val : '40',
time : 10,
place : 'Angalore'
,
Name : 'C',
val : '5',
time : 11,
place : 'Raipur'
,
Name : 'A',
val : '70',
time : 12,
place : 'Chennai'
,
Name : 'B',
val : '20',
time : 10,
place : 'Mumbai'
];
// normalize/parse data
spendData.forEach(function(d)
d.val = d.val.match(/d+/)[0];
);
// set crossfilter
var ndx = crossfilter(spendData), yearDim =
ndx.dimension(function(
d)
return d.place;
), spendDim = ndx.dimension(function(d)
return Math.floor(d.val / 10);
), nameDim = ndx.dimension(function(d)
return d.Name;
), spendPertime = yearDim.group().reduceSum(function(d)
return +d.val;
)
, spendPerSev = yearDim.group().reduceSum(function(d)
return +d.val;
), spendPerName = nameDim.group().reduceSum(function(d)
return +d.val;
), spendHist = spendDim.group().reduceCount();
yearRingChart.width(300).height(300).dimension(yearDim).group(
spendPertime).innerRadius(50).controlsUseVisibility(true)
.data(function(d)
return d.order(
function(d)
return d.val;
).top(3);
).ordering(function(d)
return -d.ue;
);
I am a newbie in dc.js. Any help will be appreciated.
Thankyou
dc.js crossfilter
add a comment |
I have tried using the code below to display the top 3 values based on the count of the val in the json provided below.
yearRingChart.width(300).height(300).dimension(yearDim).group(
spendPertime).innerRadius(50).controlsUseVisibility(true)
.data(function(d)
return d.order(
function(d)
return d.val;
).top(3);
).ordering(function(d)
return -d.val;
);
But this is getting sorted based on alphabetical order and not based on val.
I need to display the top 3 names with highest val in the json provided.
The code snippet is provided below -
var yearRingChart = dc.pieChart("#chart-ring-year"),
spendHistChart = dc.barChart("#chart-hist-spend"),
spenderRowChart = dc.rowChart("#chart-row-spenders");
var table = dc.dataTable('#table');
// use static or load via d3.csv("spendData.csv", function(error, spendData) /* do stuff */);
var spendData = [
Name : 'A',
val : '100',
time : 9,
place : 'Kolkata'
,
Name : 'B',
val : '40',
time : 10,
place : 'Angalore'
,
Name : 'C',
val : '5',
time : 11,
place : 'Raipur'
,
Name : 'A',
val : '70',
time : 12,
place : 'Chennai'
,
Name : 'B',
val : '20',
time : 10,
place : 'Mumbai'
];
// normalize/parse data
spendData.forEach(function(d)
d.val = d.val.match(/d+/)[0];
);
// set crossfilter
var ndx = crossfilter(spendData), yearDim =
ndx.dimension(function(
d)
return d.place;
), spendDim = ndx.dimension(function(d)
return Math.floor(d.val / 10);
), nameDim = ndx.dimension(function(d)
return d.Name;
), spendPertime = yearDim.group().reduceSum(function(d)
return +d.val;
)
, spendPerSev = yearDim.group().reduceSum(function(d)
return +d.val;
), spendPerName = nameDim.group().reduceSum(function(d)
return +d.val;
), spendHist = spendDim.group().reduceCount();
yearRingChart.width(300).height(300).dimension(yearDim).group(
spendPertime).innerRadius(50).controlsUseVisibility(true)
.data(function(d)
return d.order(
function(d)
return d.val;
).top(3);
).ordering(function(d)
return -d.ue;
);
I am a newbie in dc.js. Any help will be appreciated.
Thankyou
dc.js crossfilter
add a comment |
I have tried using the code below to display the top 3 values based on the count of the val in the json provided below.
yearRingChart.width(300).height(300).dimension(yearDim).group(
spendPertime).innerRadius(50).controlsUseVisibility(true)
.data(function(d)
return d.order(
function(d)
return d.val;
).top(3);
).ordering(function(d)
return -d.val;
);
But this is getting sorted based on alphabetical order and not based on val.
I need to display the top 3 names with highest val in the json provided.
The code snippet is provided below -
var yearRingChart = dc.pieChart("#chart-ring-year"),
spendHistChart = dc.barChart("#chart-hist-spend"),
spenderRowChart = dc.rowChart("#chart-row-spenders");
var table = dc.dataTable('#table');
// use static or load via d3.csv("spendData.csv", function(error, spendData) /* do stuff */);
var spendData = [
Name : 'A',
val : '100',
time : 9,
place : 'Kolkata'
,
Name : 'B',
val : '40',
time : 10,
place : 'Angalore'
,
Name : 'C',
val : '5',
time : 11,
place : 'Raipur'
,
Name : 'A',
val : '70',
time : 12,
place : 'Chennai'
,
Name : 'B',
val : '20',
time : 10,
place : 'Mumbai'
];
// normalize/parse data
spendData.forEach(function(d)
d.val = d.val.match(/d+/)[0];
);
// set crossfilter
var ndx = crossfilter(spendData), yearDim =
ndx.dimension(function(
d)
return d.place;
), spendDim = ndx.dimension(function(d)
return Math.floor(d.val / 10);
), nameDim = ndx.dimension(function(d)
return d.Name;
), spendPertime = yearDim.group().reduceSum(function(d)
return +d.val;
)
, spendPerSev = yearDim.group().reduceSum(function(d)
return +d.val;
), spendPerName = nameDim.group().reduceSum(function(d)
return +d.val;
), spendHist = spendDim.group().reduceCount();
yearRingChart.width(300).height(300).dimension(yearDim).group(
spendPertime).innerRadius(50).controlsUseVisibility(true)
.data(function(d)
return d.order(
function(d)
return d.val;
).top(3);
).ordering(function(d)
return -d.ue;
);
I am a newbie in dc.js. Any help will be appreciated.
Thankyou
dc.js crossfilter
I have tried using the code below to display the top 3 values based on the count of the val in the json provided below.
yearRingChart.width(300).height(300).dimension(yearDim).group(
spendPertime).innerRadius(50).controlsUseVisibility(true)
.data(function(d)
return d.order(
function(d)
return d.val;
).top(3);
).ordering(function(d)
return -d.val;
);
But this is getting sorted based on alphabetical order and not based on val.
I need to display the top 3 names with highest val in the json provided.
The code snippet is provided below -
var yearRingChart = dc.pieChart("#chart-ring-year"),
spendHistChart = dc.barChart("#chart-hist-spend"),
spenderRowChart = dc.rowChart("#chart-row-spenders");
var table = dc.dataTable('#table');
// use static or load via d3.csv("spendData.csv", function(error, spendData) /* do stuff */);
var spendData = [
Name : 'A',
val : '100',
time : 9,
place : 'Kolkata'
,
Name : 'B',
val : '40',
time : 10,
place : 'Angalore'
,
Name : 'C',
val : '5',
time : 11,
place : 'Raipur'
,
Name : 'A',
val : '70',
time : 12,
place : 'Chennai'
,
Name : 'B',
val : '20',
time : 10,
place : 'Mumbai'
];
// normalize/parse data
spendData.forEach(function(d)
d.val = d.val.match(/d+/)[0];
);
// set crossfilter
var ndx = crossfilter(spendData), yearDim =
ndx.dimension(function(
d)
return d.place;
), spendDim = ndx.dimension(function(d)
return Math.floor(d.val / 10);
), nameDim = ndx.dimension(function(d)
return d.Name;
), spendPertime = yearDim.group().reduceSum(function(d)
return +d.val;
)
, spendPerSev = yearDim.group().reduceSum(function(d)
return +d.val;
), spendPerName = nameDim.group().reduceSum(function(d)
return +d.val;
), spendHist = spendDim.group().reduceCount();
yearRingChart.width(300).height(300).dimension(yearDim).group(
spendPertime).innerRadius(50).controlsUseVisibility(true)
.data(function(d)
return d.order(
function(d)
return d.val;
).top(3);
).ordering(function(d)
return -d.ue;
);
I am a newbie in dc.js. Any help will be appreciated.
Thankyou
dc.js crossfilter
dc.js crossfilter
edited Nov 16 '18 at 7:55
Spandana
asked Nov 16 '18 at 7:30
SpandanaSpandana
66110
66110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I suspect that the big problem you are running into is that .ordering()
- which is the right way to do this - takes group data, not raw data. Once you have aggregated your data using a group, you are going to have an array of key,value
pairs, not your original array of data.
So I think when you attempt to read d.val
or d.ue
, you're just getting undefined
, which results in no sorting. You could verify this by putting a breakpoint or console.log
in your ordering
callbacks. That's how I usually debug these kinds of things, when I have a running example.
group.order
isn't going to have much effect, so I would suggest removing that.
I'd also suggest using .cap() instead of that complicated data-order-top thing you have, which looks complicated / delicate. Capping is built-in functionality for pie charts and row charts.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333280%2fdisplay-top-n-values-based-on-count-and-not-on-alphabets-dc-js%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I suspect that the big problem you are running into is that .ordering()
- which is the right way to do this - takes group data, not raw data. Once you have aggregated your data using a group, you are going to have an array of key,value
pairs, not your original array of data.
So I think when you attempt to read d.val
or d.ue
, you're just getting undefined
, which results in no sorting. You could verify this by putting a breakpoint or console.log
in your ordering
callbacks. That's how I usually debug these kinds of things, when I have a running example.
group.order
isn't going to have much effect, so I would suggest removing that.
I'd also suggest using .cap() instead of that complicated data-order-top thing you have, which looks complicated / delicate. Capping is built-in functionality for pie charts and row charts.
add a comment |
I suspect that the big problem you are running into is that .ordering()
- which is the right way to do this - takes group data, not raw data. Once you have aggregated your data using a group, you are going to have an array of key,value
pairs, not your original array of data.
So I think when you attempt to read d.val
or d.ue
, you're just getting undefined
, which results in no sorting. You could verify this by putting a breakpoint or console.log
in your ordering
callbacks. That's how I usually debug these kinds of things, when I have a running example.
group.order
isn't going to have much effect, so I would suggest removing that.
I'd also suggest using .cap() instead of that complicated data-order-top thing you have, which looks complicated / delicate. Capping is built-in functionality for pie charts and row charts.
add a comment |
I suspect that the big problem you are running into is that .ordering()
- which is the right way to do this - takes group data, not raw data. Once you have aggregated your data using a group, you are going to have an array of key,value
pairs, not your original array of data.
So I think when you attempt to read d.val
or d.ue
, you're just getting undefined
, which results in no sorting. You could verify this by putting a breakpoint or console.log
in your ordering
callbacks. That's how I usually debug these kinds of things, when I have a running example.
group.order
isn't going to have much effect, so I would suggest removing that.
I'd also suggest using .cap() instead of that complicated data-order-top thing you have, which looks complicated / delicate. Capping is built-in functionality for pie charts and row charts.
I suspect that the big problem you are running into is that .ordering()
- which is the right way to do this - takes group data, not raw data. Once you have aggregated your data using a group, you are going to have an array of key,value
pairs, not your original array of data.
So I think when you attempt to read d.val
or d.ue
, you're just getting undefined
, which results in no sorting. You could verify this by putting a breakpoint or console.log
in your ordering
callbacks. That's how I usually debug these kinds of things, when I have a running example.
group.order
isn't going to have much effect, so I would suggest removing that.
I'd also suggest using .cap() instead of that complicated data-order-top thing you have, which looks complicated / delicate. Capping is built-in functionality for pie charts and row charts.
answered Nov 16 '18 at 20:28
GordonGordon
13.5k32362
13.5k32362
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333280%2fdisplay-top-n-values-based-on-count-and-not-on-alphabets-dc-js%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown