how to modify path's order according to the x axis when using d3.js












1















I want to draw a stacked area graph which can show the tendency and the value.



For example:
data:



year,orange,apple
1976,30,20
1980,20,40
1988,50,30


related code:



stack = d3.stack()
.order(d3.stackOrderAscending)
// .order(order)
.keys(keys);

series = stack(data);

area = d3
.area()
.x(function(d) {
return xScale(new Date(d.data.year, 0, 1));
})
.y0(function(d) {
return yScale(d[0]);
})
.y1(function(d) {
return yScale(d[1]);
});

$ele
.selectAll('path')
.data(series)
.enter()
.append('path')
.attr('class', 'area')
.attr('transform', 'translate(' + template.padding + ',0)')
.attr('d', area)
.attr('fill', function(d, i) {
return get_colors(i);
});


It shows like this:



The current result
enter image description here
But what I want to do is to put the max value on the top (according to the x axis's value)of the graph
enter image description here
And therefore the order of the categories will change with the x axis.



I tried to define my own order function,but it only return an array which seems to define only a order, which means the order can not change.



I hope you can understand what I said.Thanks!!!










share|improve this question

























  • you have to write the stacking yourself, start by using the series as created by stack(). Result: i.imgur.com/TjKwy9Z.png

    – rioV8
    Nov 22 '18 at 14:28











  • Yeah! I succeeded it as you said! Thanks!

    – 朱融晨
    Nov 23 '18 at 3:27
















1















I want to draw a stacked area graph which can show the tendency and the value.



For example:
data:



year,orange,apple
1976,30,20
1980,20,40
1988,50,30


related code:



stack = d3.stack()
.order(d3.stackOrderAscending)
// .order(order)
.keys(keys);

series = stack(data);

area = d3
.area()
.x(function(d) {
return xScale(new Date(d.data.year, 0, 1));
})
.y0(function(d) {
return yScale(d[0]);
})
.y1(function(d) {
return yScale(d[1]);
});

$ele
.selectAll('path')
.data(series)
.enter()
.append('path')
.attr('class', 'area')
.attr('transform', 'translate(' + template.padding + ',0)')
.attr('d', area)
.attr('fill', function(d, i) {
return get_colors(i);
});


It shows like this:



The current result
enter image description here
But what I want to do is to put the max value on the top (according to the x axis's value)of the graph
enter image description here
And therefore the order of the categories will change with the x axis.



I tried to define my own order function,but it only return an array which seems to define only a order, which means the order can not change.



I hope you can understand what I said.Thanks!!!










share|improve this question

























  • you have to write the stacking yourself, start by using the series as created by stack(). Result: i.imgur.com/TjKwy9Z.png

    – rioV8
    Nov 22 '18 at 14:28











  • Yeah! I succeeded it as you said! Thanks!

    – 朱融晨
    Nov 23 '18 at 3:27














1












1








1








I want to draw a stacked area graph which can show the tendency and the value.



For example:
data:



year,orange,apple
1976,30,20
1980,20,40
1988,50,30


related code:



stack = d3.stack()
.order(d3.stackOrderAscending)
// .order(order)
.keys(keys);

series = stack(data);

area = d3
.area()
.x(function(d) {
return xScale(new Date(d.data.year, 0, 1));
})
.y0(function(d) {
return yScale(d[0]);
})
.y1(function(d) {
return yScale(d[1]);
});

$ele
.selectAll('path')
.data(series)
.enter()
.append('path')
.attr('class', 'area')
.attr('transform', 'translate(' + template.padding + ',0)')
.attr('d', area)
.attr('fill', function(d, i) {
return get_colors(i);
});


It shows like this:



The current result
enter image description here
But what I want to do is to put the max value on the top (according to the x axis's value)of the graph
enter image description here
And therefore the order of the categories will change with the x axis.



I tried to define my own order function,but it only return an array which seems to define only a order, which means the order can not change.



I hope you can understand what I said.Thanks!!!










share|improve this question
















I want to draw a stacked area graph which can show the tendency and the value.



For example:
data:



year,orange,apple
1976,30,20
1980,20,40
1988,50,30


related code:



stack = d3.stack()
.order(d3.stackOrderAscending)
// .order(order)
.keys(keys);

series = stack(data);

area = d3
.area()
.x(function(d) {
return xScale(new Date(d.data.year, 0, 1));
})
.y0(function(d) {
return yScale(d[0]);
})
.y1(function(d) {
return yScale(d[1]);
});

$ele
.selectAll('path')
.data(series)
.enter()
.append('path')
.attr('class', 'area')
.attr('transform', 'translate(' + template.padding + ',0)')
.attr('d', area)
.attr('fill', function(d, i) {
return get_colors(i);
});


It shows like this:



The current result
enter image description here
But what I want to do is to put the max value on the top (according to the x axis's value)of the graph
enter image description here
And therefore the order of the categories will change with the x axis.



I tried to define my own order function,but it only return an array which seems to define only a order, which means the order can not change.



I hope you can understand what I said.Thanks!!!







d3.js stacked-chart






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 12:21









rioV8

4,5742311




4,5742311










asked Nov 22 '18 at 12:17









朱融晨朱融晨

82




82













  • you have to write the stacking yourself, start by using the series as created by stack(). Result: i.imgur.com/TjKwy9Z.png

    – rioV8
    Nov 22 '18 at 14:28











  • Yeah! I succeeded it as you said! Thanks!

    – 朱融晨
    Nov 23 '18 at 3:27



















  • you have to write the stacking yourself, start by using the series as created by stack(). Result: i.imgur.com/TjKwy9Z.png

    – rioV8
    Nov 22 '18 at 14:28











  • Yeah! I succeeded it as you said! Thanks!

    – 朱融晨
    Nov 23 '18 at 3:27

















you have to write the stacking yourself, start by using the series as created by stack(). Result: i.imgur.com/TjKwy9Z.png

– rioV8
Nov 22 '18 at 14:28





you have to write the stacking yourself, start by using the series as created by stack(). Result: i.imgur.com/TjKwy9Z.png

– rioV8
Nov 22 '18 at 14:28













Yeah! I succeeded it as you said! Thanks!

– 朱融晨
Nov 23 '18 at 3:27





Yeah! I succeeded it as you said! Thanks!

– 朱融晨
Nov 23 '18 at 3:27












0






active

oldest

votes











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430854%2fhow-to-modify-paths-order-according-to-the-x-axis-when-using-d3-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430854%2fhow-to-modify-paths-order-according-to-the-x-axis-when-using-d3-js%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]