-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable trim and wrap feature to axis label #1905
Open
kedij777
wants to merge
14
commits into
swimlane:master
Choose a base branch
from
kedij777:X/Y_Axis_Label
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ef5d117
added trim and wrap feature to axis label
kedij777 a2c9d89
added tooltip for trimmed label
kedij777 818e912
fixed bug for not trim and not wrap
kedij777 7deb091
fixed bug in wrap
kedij777 7c659b1
Update projects/swimlane/ngx-charts/src/lib/common/axes/x-axis.compon…
kedij777 0c3cae4
Update projects/swimlane/ngx-charts/src/lib/common/axes/y-axis.compon…
kedij777 f45b5ea
Update projects/swimlane/ngx-charts/src/lib/common/axes/x-axis.compon…
kedij777 a978037
Update projects/swimlane/ngx-charts/src/lib/common/axes/x-axis.compon…
kedij777 b17bbc1
Update projects/swimlane/ngx-charts/src/lib/common/axes/x-axis.compon…
kedij777 359aa63
Update projects/swimlane/ngx-charts/src/lib/common/axes/x-axis.compon…
kedij777 4f07555
Update projects/swimlane/ngx-charts/src/lib/common/axes/y-axis.compon…
kedij777 615000d
Update projects/swimlane/ngx-charts/src/lib/common/axes/y-axis.compon…
kedij777 dca799e
Update projects/swimlane/ngx-charts/src/lib/common/axes/x-axis.compon…
kedij777 981f9f7
Update projects/swimlane/ngx-charts/src/lib/common/axes/x-axis.compon…
kedij777 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import { | |
import { XAxisTicksComponent } from './x-axis-ticks.component'; | ||
import { Orientation } from '../types/orientation.enum'; | ||
import { ViewDimensions } from '../types/view-dimension.interface'; | ||
import { select } from 'd3-selection'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Group d3 import with other external imports (eg @angular/core) |
||
|
||
@Component({ | ||
selector: 'g[ngx-charts-x-axis]', | ||
|
@@ -35,14 +36,19 @@ import { ViewDimensions } from '../types/view-dimension.interface'; | |
[wrapTicks]="wrapTicks" | ||
(dimensionsChanged)="emitTicksHeight($event)" | ||
/> | ||
<title>{{ labelText }}</title> | ||
<svg:g | ||
ngx-charts-axis-label | ||
class="x-axis-label" | ||
*ngIf="showLabel" | ||
[label]="labelText" | ||
[label]="labelTextTemp" | ||
[offset]="labelOffset" | ||
[orient]="orientation.Bottom" | ||
[height]="dims.height" | ||
[width]="dims.width" | ||
[trimLabel]="trimLabel" | ||
[maxLabelLength]="maxLabelLength" | ||
[wrapLabel]="wrapLabel" | ||
></svg:g> | ||
</svg:g> | ||
`, | ||
|
@@ -63,6 +69,9 @@ export class XAxisComponent implements OnChanges { | |
@Input() xOrient: Orientation = Orientation.Bottom; | ||
@Input() xAxisOffset: number = 0; | ||
@Input() wrapTicks = false; | ||
@Input() maxLabelLength: number; | ||
@Input() trimLabel: boolean; | ||
@Input() wrapLabel: boolean; | ||
|
||
@Output() dimensionsChanged = new EventEmitter(); | ||
|
||
|
@@ -76,6 +85,7 @@ export class XAxisComponent implements OnChanges { | |
tickStroke: string = '#ccc'; | ||
strokeWidth: string = 'none'; | ||
padding: number = 5; | ||
labelTextTemp: string; | ||
|
||
readonly orientation = Orientation; | ||
|
||
|
@@ -91,6 +101,92 @@ export class XAxisComponent implements OnChanges { | |
if (typeof this.xAxisTickCount !== 'undefined') { | ||
this.tickArguments = [this.xAxisTickCount]; | ||
} | ||
|
||
// labelLength = number of characters | ||
let labelLength = this.labelText.length; | ||
this.labelTextTemp = this.labelText; | ||
this.maxLabelLength = Number(this.maxLabelLength); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this.maxLabelLength already a number? |
||
let labelElement = select('.xAxisLabel'); | ||
let textElement = labelElement.select('text'); | ||
let xVal; | ||
if (!textElement.empty()) { | ||
xVal = textElement.attr('x'); | ||
} | ||
|
||
if (this.showLabel && this.trimLabel && this.maxLabelLength > 0) { | ||
if (this.maxLabelLength < labelLength) { | ||
// trim | ||
let tspanElements = textElement.selectAll('tspan'); | ||
if (tspanElements) { | ||
tspanElements.remove(); | ||
} | ||
labelLength = this.maxLabelLength; | ||
this.labelTextTemp = this.labelText.slice(0, labelLength) + '...'; | ||
textElement.text(this.labelTextTemp); | ||
} else { | ||
this.labelTextTemp = this.labelText; | ||
textElement.text(this.labelTextTemp); | ||
} | ||
} else if (this.showLabel && this.wrapLabel && this.maxLabelLength > 0) { | ||
// wrap with specified maxLabelLength | ||
|
||
if (this.maxLabelLength < labelLength && this.maxLabelLength > 0) { | ||
textElement.text(''); | ||
|
||
const firstLine = this.labelTextTemp.slice(0, this.maxLabelLength); | ||
textElement.append('tspan') | ||
.text(firstLine) | ||
.attr('dy', '1em'); | ||
let start = this.maxLabelLength; | ||
while (start + this.maxLabelLength <= labelLength) { | ||
let line = this.labelTextTemp.slice(start, start + this.maxLabelLength); | ||
textElement.append('tspan') | ||
.text(line) | ||
.attr('x', xVal) | ||
.attr('dy', '1.2em'); | ||
start += this.maxLabelLength; | ||
} | ||
if (start < labelLength) { | ||
let lastLine = this.labelTextTemp.slice(start, labelLength); | ||
textElement.append('tspan') | ||
.text(lastLine) | ||
.attr('x', xVal) | ||
.attr('dy', '1.2em'); | ||
} | ||
} else { | ||
this.labelTextTemp = this.labelText; | ||
textElement.text(this.labelTextTemp); | ||
} | ||
} else if (this.maxLabelLength == 0 && this.wrapLabel) { | ||
// auto-wrap without specified maxLabelLength | ||
if (labelLength > this.dims.width / 11) { | ||
let wrappedLines = this.wrapText(this.labelTextTemp, this.dims.width / 11); | ||
let firstLine = wrappedLines[0]; | ||
textElement.text(''); | ||
textElement.append('tspan') | ||
.text(firstLine) | ||
.attr('dy', '1em'); | ||
let xVal = textElement.attr('x'); | ||
for (let i = 1; i < wrappedLines.length; i++) { | ||
let line = wrappedLines[i]; | ||
textElement.append('tspan') | ||
.text(line) | ||
.attr('x', xVal) | ||
.attr('dy', '1.2em'); | ||
} | ||
} else { | ||
this.labelTextTemp = this.labelText; | ||
textElement.text(this.labelTextTemp); | ||
} | ||
} else if (!this.trimLabel && !this.wrapLabel) { | ||
let tspanElements = textElement.selectAll('tspan'); | ||
if (tspanElements) { | ||
tspanElements.remove(); | ||
} | ||
this.labelTextTemp = this.labelText; | ||
textElement.text(this.labelTextTemp); | ||
} | ||
|
||
} | ||
|
||
emitTicksHeight({ height }): void { | ||
|
@@ -102,4 +198,28 @@ export class XAxisComponent implements OnChanges { | |
}, 0); | ||
} | ||
} | ||
|
||
wrapText(text, maxLineWidth) { | ||
const words = text.split(' '); | ||
let lines = []; | ||
let currentLine = ''; | ||
|
||
for (const word of words) { | ||
const testLine = currentLine ? `${currentLine} ${word}` : word; | ||
const testLineLength = testLine.length; | ||
|
||
if (testLineLength <= maxLineWidth) { | ||
currentLine = testLine; | ||
} else { | ||
lines.push(currentLine); | ||
currentLine = word; | ||
} | ||
} | ||
|
||
if (currentLine) { | ||
lines.push(currentLine); | ||
} | ||
|
||
return lines; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are these inputs for?