D3 Angular - line break in axis label
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.
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
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%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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
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%2f53266965%2fd3-angular-line-break-in-axis-label%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
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