Skip to content
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

Heat map show data label #1939

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ interface Cell {
(deactivate)="deactivate.emit(c.cell)"
[gradient]="gradient"
[animations]="animations"
[showDataLabel]="showDataLabel"
ngx-tooltip
[tooltipDisabled]="tooltipDisabled"
[tooltipPlacement]="placementTypes.Top"
[tooltipType]="styleTypes.tooltip"
[tooltipTitle]="tooltipTemplate ? undefined : tooltipText(c)"
[tooltipTemplate]="tooltipTemplate"
[tooltipContext]="{ series: c.series, name: c.label, value: c.data }"
></svg:g>
></svg:g>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
Expand All @@ -64,6 +65,7 @@ export class HeatCellSeriesComponent implements OnChanges, OnInit {
@Input() tooltipText: any;
@Input() tooltipTemplate: TemplateRef<any>;
@Input() animations: boolean = true;
@Input() showDataLabel: boolean = false;

@Output() select: EventEmitter<DataItem> = new EventEmitter();
@Output() activate: EventEmitter<DataItem> = new EventEmitter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import { id } from '../utils/id';
class="cell"
(click)="onClick()"
/>
<svg:g *ngIf="showDataLabel" [attr.transform]="calculateTranslation()">
<svg:text> {{this.data}} </svg:text>
</svg:g>
</svg:g>
`,
changeDetection: ChangeDetectionStrategy.OnPush
Expand All @@ -48,6 +51,7 @@ export class HeatMapCellComponent implements OnChanges {
@Input() data: number;
@Input() gradient: boolean = false;
@Input() animations: boolean = true;
@Input() showDataLabel: boolean = false;

@Output() select: EventEmitter<number> = new EventEmitter();
@Output() activate: EventEmitter<number> = new EventEmitter();
Expand Down Expand Up @@ -93,6 +97,54 @@ export class HeatMapCellComponent implements OnChanges {
}
];
}
calculateTranslation(): string {
var valueWidth = this.calculateWidth(this.data);
if (valueWidth >= (this.width + 4)) {
var formatedValue = this.shortenNum(this.data);
valueWidth = this.calculateWidth(formatedValue);
let textElement = select(this.element).select('.cell').select('text');
textElement.text(formatedValue);
}
// default value height is 25 px
var valueHeight = 25;
const translateX = (this.width - valueWidth) / 2;
const translateY = (this.height - valueHeight) / 2 + 20;
return `translate(${translateX}, ${translateY})`;
}
shortenNum(value: number): string {
const abbreviations = [
{ value: 1e9, symbol: 'B' },
{ value: 1e6, symbol: 'M' },
{ value: 1e3, symbol: 'K' },
];

for (const abbreviation of abbreviations) {
if (Math.abs(value) >= abbreviation.value) {
const formattedValue = value / abbreviation.value;
return `${Math.floor(formattedValue)}${abbreviation.symbol}`;
}
}

return value.toString();
}
calculateWidth(value): number {
const digitWidth = 12.6;
const kbmWidth = 19;

const stringValue = value.toLocaleString();

let totalWidth = 0;

for (const char of stringValue) {
if (/[0-9]/.test(char)) {
totalWidth += digitWidth;
} else {
totalWidth += kbmWidth;
}
totalWidth -= 3;
}
return totalWidth;
}

loadAnimation(): void {
const node = select(this.element).select('.cell');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ interface RectItem {
[tooltipDisabled]="tooltipDisabled"
[tooltipTemplate]="tooltipTemplate"
[tooltipText]="tooltipText"
[showDataLabel]="showDataLabel"
(select)="onClick($event)"
(activate)="onActivate($event, undefined)"
(deactivate)="onDeactivate($event, undefined)"
Expand Down Expand Up @@ -125,6 +126,7 @@ export class HeatMapComponent extends BaseChartComponent {
@Input() max: number;
@Input() activeEntries: any[] = [];
@Input() wrapTicks = false;
@Input() showDataLabel: boolean = false;

@Output() activate: EventEmitter<any> = new EventEmitter();
@Output() deactivate: EventEmitter<any> = new EventEmitter();
Expand Down
1 change: 1 addition & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@
[maxXAxisTickLength]="maxXAxisTickLength"
[maxYAxisTickLength]="maxYAxisTickLength"
[wrapTicks]="wrapTicks"
[showDataLabel]="showDataLabel"
(select)="select($event)"
(activate)="activate($event)"
(deactivate)="deactivate($event)"
Expand Down
3 changes: 2 additions & 1 deletion src/app/chartTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,8 @@ const chartGroups = [
'maxYAxisTickLength',
'min',
'max',
'wrapTicks'
'wrapTicks',
'showDataLabel'
],
defaults: {
yAxisLabel: 'Census Date',
Expand Down