D3 Angular - line break in axis label










1















I have a stackblitz here - https://stackblitz.com/edit/lable-line-break-p1f9hb?file=src/app/bar-chart.ts



The x-axis have long labels that I need to split after the second word, so I have a title and sub title



When I'm creating the x-axis I'm calling a function that splits the string up and adds them as separate tspans



this.chart.append("g")
.call(this.x_axis)
.classed('x-axis', true)
.attr("transform", "translate(0," + this.height + ")")

.selectAll(".tick text")
.each(this.insertLinebreak)


private insertLinebreak (d)

let labels = d3.select(".x-axis .tick text");
let words = d;
labels.text('');

let index = words.indexOf(' ', words.indexOf( ' ' ) + 1)
let title = words.substr(0, index)
let subtitle = words.substr(index + 1)

let tspantitle = labels.append('tspan').text(title)
let tspansubtitle = labels.append('tspan').text(subtitle)

tspantitle
.attr('x', 0)
.attr('dy', '15')
.attr('class', 'x-axis-title');
tspansubtitle
.attr('x', 0)
.attr('dy', '16')
.attr('class', 'x-axis-subtitle');

;


My probelm is its adding them in the wrong order and missing one of the titles



What I am doing wrong, is there a better way to do this.










share|improve this question
























  • Can you give the stackblitz URL that would allow us to fork it?

    – ConnorsFan
    Nov 12 '18 at 19:50











  • I have updated the link

    – ttmt
    Nov 12 '18 at 20:13















1















I have a stackblitz here - https://stackblitz.com/edit/lable-line-break-p1f9hb?file=src/app/bar-chart.ts



The x-axis have long labels that I need to split after the second word, so I have a title and sub title



When I'm creating the x-axis I'm calling a function that splits the string up and adds them as separate tspans



this.chart.append("g")
.call(this.x_axis)
.classed('x-axis', true)
.attr("transform", "translate(0," + this.height + ")")

.selectAll(".tick text")
.each(this.insertLinebreak)


private insertLinebreak (d)

let labels = d3.select(".x-axis .tick text");
let words = d;
labels.text('');

let index = words.indexOf(' ', words.indexOf( ' ' ) + 1)
let title = words.substr(0, index)
let subtitle = words.substr(index + 1)

let tspantitle = labels.append('tspan').text(title)
let tspansubtitle = labels.append('tspan').text(subtitle)

tspantitle
.attr('x', 0)
.attr('dy', '15')
.attr('class', 'x-axis-title');
tspansubtitle
.attr('x', 0)
.attr('dy', '16')
.attr('class', 'x-axis-subtitle');

;


My probelm is its adding them in the wrong order and missing one of the titles



What I am doing wrong, is there a better way to do this.










share|improve this question
























  • Can you give the stackblitz URL that would allow us to fork it?

    – ConnorsFan
    Nov 12 '18 at 19:50











  • I have updated the link

    – ttmt
    Nov 12 '18 at 20:13













1












1








1








I have a stackblitz here - https://stackblitz.com/edit/lable-line-break-p1f9hb?file=src/app/bar-chart.ts



The x-axis have long labels that I need to split after the second word, so I have a title and sub title



When I'm creating the x-axis I'm calling a function that splits the string up and adds them as separate tspans



this.chart.append("g")
.call(this.x_axis)
.classed('x-axis', true)
.attr("transform", "translate(0," + this.height + ")")

.selectAll(".tick text")
.each(this.insertLinebreak)


private insertLinebreak (d)

let labels = d3.select(".x-axis .tick text");
let words = d;
labels.text('');

let index = words.indexOf(' ', words.indexOf( ' ' ) + 1)
let title = words.substr(0, index)
let subtitle = words.substr(index + 1)

let tspantitle = labels.append('tspan').text(title)
let tspansubtitle = labels.append('tspan').text(subtitle)

tspantitle
.attr('x', 0)
.attr('dy', '15')
.attr('class', 'x-axis-title');
tspansubtitle
.attr('x', 0)
.attr('dy', '16')
.attr('class', 'x-axis-subtitle');

;


My probelm is its adding them in the wrong order and missing one of the titles



What I am doing wrong, is there a better way to do this.










share|improve this question
















I have a stackblitz here - https://stackblitz.com/edit/lable-line-break-p1f9hb?file=src/app/bar-chart.ts



The x-axis have long labels that I need to split after the second word, so I have a title and sub title



When I'm creating the x-axis I'm calling a function that splits the string up and adds them as separate tspans



this.chart.append("g")
.call(this.x_axis)
.classed('x-axis', true)
.attr("transform", "translate(0," + this.height + ")")

.selectAll(".tick text")
.each(this.insertLinebreak)


private insertLinebreak (d)

let labels = d3.select(".x-axis .tick text");
let words = d;
labels.text('');

let index = words.indexOf(' ', words.indexOf( ' ' ) + 1)
let title = words.substr(0, index)
let subtitle = words.substr(index + 1)

let tspantitle = labels.append('tspan').text(title)
let tspansubtitle = labels.append('tspan').text(subtitle)

tspantitle
.attr('x', 0)
.attr('dy', '15')
.attr('class', 'x-axis-title');
tspansubtitle
.attr('x', 0)
.attr('dy', '16')
.attr('class', 'x-axis-subtitle');

;


My probelm is its adding them in the wrong order and missing one of the titles



What I am doing wrong, is there a better way to do this.







angular d3.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 20:13







ttmt

















asked Nov 12 '18 at 17:09









ttmtttmt

2,0771152103




2,0771152103












  • Can you give the stackblitz URL that would allow us to fork it?

    – ConnorsFan
    Nov 12 '18 at 19:50











  • I have updated the link

    – ttmt
    Nov 12 '18 at 20:13

















  • Can you give the stackblitz URL that would allow us to fork it?

    – ConnorsFan
    Nov 12 '18 at 19:50











  • I have updated the link

    – ttmt
    Nov 12 '18 at 20:13
















Can you give the stackblitz URL that would allow us to fork it?

– ConnorsFan
Nov 12 '18 at 19:50





Can you give the stackblitz URL that would allow us to fork it?

– ConnorsFan
Nov 12 '18 at 19:50













I have updated the link

– ttmt
Nov 12 '18 at 20:13





I have updated the link

– ttmt
Nov 12 '18 at 20:13












1 Answer
1






active

oldest

votes


















1














The first line in insertLinebreak is the source of the problem, because it retrieves all the label elements every time you want to process one of them (watch the console in this stackblitz):



private insertLinebreak(d) 
let labels = d3.select(".x-axis .tick text"); // <-- This line causes the problem
...




In order to select only the label that you are processing, use d3.select(this):



private insertLinebreak(d) 
let labels = d3.select(this);
...



See this stackblitz for a demo.






share|improve this answer

























  • This is how I have it in my actual app but I get an error - error TS2345: Argument of type 'this' is not assignable to parameter of type 'ArrayLike<BaseType>'. I know this is a different question but I can't replicate it to show the code. The code for my actual app is exactly the same as the stackblitz

    – ttmt
    Nov 12 '18 at 21:08












  • I can only help with the code that is provided in the question and in the stackblitz. As you can see, the forked stackblitz works with the suggested correction.

    – ConnorsFan
    Nov 12 '18 at 21:13











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%2f53266965%2fd3-angular-line-break-in-axis-label%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









1














The first line in insertLinebreak is the source of the problem, because it retrieves all the label elements every time you want to process one of them (watch the console in this stackblitz):



private insertLinebreak(d) 
let labels = d3.select(".x-axis .tick text"); // <-- This line causes the problem
...




In order to select only the label that you are processing, use d3.select(this):



private insertLinebreak(d) 
let labels = d3.select(this);
...



See this stackblitz for a demo.






share|improve this answer

























  • This is how I have it in my actual app but I get an error - error TS2345: Argument of type 'this' is not assignable to parameter of type 'ArrayLike<BaseType>'. I know this is a different question but I can't replicate it to show the code. The code for my actual app is exactly the same as the stackblitz

    – ttmt
    Nov 12 '18 at 21:08












  • I can only help with the code that is provided in the question and in the stackblitz. As you can see, the forked stackblitz works with the suggested correction.

    – ConnorsFan
    Nov 12 '18 at 21:13
















1














The first line in insertLinebreak is the source of the problem, because it retrieves all the label elements every time you want to process one of them (watch the console in this stackblitz):



private insertLinebreak(d) 
let labels = d3.select(".x-axis .tick text"); // <-- This line causes the problem
...




In order to select only the label that you are processing, use d3.select(this):



private insertLinebreak(d) 
let labels = d3.select(this);
...



See this stackblitz for a demo.






share|improve this answer

























  • This is how I have it in my actual app but I get an error - error TS2345: Argument of type 'this' is not assignable to parameter of type 'ArrayLike<BaseType>'. I know this is a different question but I can't replicate it to show the code. The code for my actual app is exactly the same as the stackblitz

    – ttmt
    Nov 12 '18 at 21:08












  • I can only help with the code that is provided in the question and in the stackblitz. As you can see, the forked stackblitz works with the suggested correction.

    – ConnorsFan
    Nov 12 '18 at 21:13














1












1








1







The first line in insertLinebreak is the source of the problem, because it retrieves all the label elements every time you want to process one of them (watch the console in this stackblitz):



private insertLinebreak(d) 
let labels = d3.select(".x-axis .tick text"); // <-- This line causes the problem
...




In order to select only the label that you are processing, use d3.select(this):



private insertLinebreak(d) 
let labels = d3.select(this);
...



See this stackblitz for a demo.






share|improve this answer















The first line in insertLinebreak is the source of the problem, because it retrieves all the label elements every time you want to process one of them (watch the console in this stackblitz):



private insertLinebreak(d) 
let labels = d3.select(".x-axis .tick text"); // <-- This line causes the problem
...




In order to select only the label that you are processing, use d3.select(this):



private insertLinebreak(d) 
let labels = d3.select(this);
...



See this stackblitz for a demo.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 16:54

























answered Nov 12 '18 at 20:40









ConnorsFanConnorsFan

31.3k43162




31.3k43162












  • This is how I have it in my actual app but I get an error - error TS2345: Argument of type 'this' is not assignable to parameter of type 'ArrayLike<BaseType>'. I know this is a different question but I can't replicate it to show the code. The code for my actual app is exactly the same as the stackblitz

    – ttmt
    Nov 12 '18 at 21:08












  • I can only help with the code that is provided in the question and in the stackblitz. As you can see, the forked stackblitz works with the suggested correction.

    – ConnorsFan
    Nov 12 '18 at 21:13


















  • This is how I have it in my actual app but I get an error - error TS2345: Argument of type 'this' is not assignable to parameter of type 'ArrayLike<BaseType>'. I know this is a different question but I can't replicate it to show the code. The code for my actual app is exactly the same as the stackblitz

    – ttmt
    Nov 12 '18 at 21:08












  • I can only help with the code that is provided in the question and in the stackblitz. As you can see, the forked stackblitz works with the suggested correction.

    – ConnorsFan
    Nov 12 '18 at 21:13

















This is how I have it in my actual app but I get an error - error TS2345: Argument of type 'this' is not assignable to parameter of type 'ArrayLike<BaseType>'. I know this is a different question but I can't replicate it to show the code. The code for my actual app is exactly the same as the stackblitz

– ttmt
Nov 12 '18 at 21:08






This is how I have it in my actual app but I get an error - error TS2345: Argument of type 'this' is not assignable to parameter of type 'ArrayLike<BaseType>'. I know this is a different question but I can't replicate it to show the code. The code for my actual app is exactly the same as the stackblitz

– ttmt
Nov 12 '18 at 21:08














I can only help with the code that is provided in the question and in the stackblitz. As you can see, the forked stackblitz works with the suggested correction.

– ConnorsFan
Nov 12 '18 at 21:13






I can only help with the code that is provided in the question and in the stackblitz. As you can see, the forked stackblitz works with the suggested correction.

– ConnorsFan
Nov 12 '18 at 21:13




















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%2f53266965%2fd3-angular-line-break-in-axis-label%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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages