Skip to content

Instantly share code, notes, and snippets.

@AvocadoVenom
AvocadoVenom / data.service.ts
Created January 27, 2019 09:49
Angular service to provide random data.
import { Injectable } from '@angular/core';
export interface Item {
name: string;
value: number;
abs: number;
}
@Injectable({
providedIn: 'root'
@AvocadoVenom
AvocadoVenom / pie-chart.component.ts
Last active February 3, 2019 16:32
d3 pie chart angular component
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Item, DataService } from '../data.service';
import * as d3 from 'd3';
@Component({
selector: 'app-pie-chart',
template: `<div id="pie"><svg></svg></div>`,
encapsulation: ViewEncapsulation.None
})
export class PieChartComponent implements OnInit {
@AvocadoVenom
AvocadoVenom / bar-chart.component.ts
Last active February 3, 2019 16:38
A d3 bar chart angular component
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import * as d3 from 'd3';
import { Item, DataService } from '../data.service';
@Component({
selector: 'app-bar-chart',
template: `<div id="bar"><svg></svg></div>`,
encapsulation: ViewEncapsulation.None
})
export class BarChartComponent implements OnInit {
@AvocadoVenom
AvocadoVenom / bar-chart-responsive.component.ts
Last active February 3, 2019 16:41
d3 responsive charts
ngOnInit() {
// ...
window.addEventListener('resize', this.resize.bind(this));
}
private resize() {
this.setSVGDimensions();
this.setAxisScales();
this.repaint();
}
@AvocadoVenom
AvocadoVenom / bar-chart-tooltip.component.ts
Last active July 11, 2019 11:50
Adding a tooltip to d3 angular component
private tooltip: any; private total: number;
constructor(private service: DataService) {
// ...
this.total = this.dataSource.reduce((sum, it) => sum += it.abs, 0);
}
ngOnInit() {
// ...
this.tooltip = d3.select('#pie') // or d3.select('#bar')
@AvocadoVenom
AvocadoVenom / bar-chart-labels.component.ts
Last active February 3, 2019 16:53
Adding labels to d3 bar chart
private labels: any; // Labels
private draw() {
// ...
this.drawLabels();
}
private repaint() {
// ...
this.drawLabels();
@AvocadoVenom
AvocadoVenom / donut-chart-labels.component.ts
Last active February 3, 2019 16:58
Adding labels in the donut hole d3 angular component
// Labels
private labelValue: any; private labelName: any;
private labelPercent: any; private labelTotal: any;
private initSvg() {
// ...
this.labelTotal = this.mainContainer.append('text')
.attr('text-anchor', 'middle').style('font-size', '6em')
.attr('dy', '.5em').text(this.total);
this.labelName = this.mainContainer.append('text')
@AvocadoVenom
AvocadoVenom / pie-chart-labels.component.ts
Last active February 3, 2019 17:02
Adding labels in the pie chart d3 angular component
private arcLabel: any;
private texts: any;
private draw() {
// ...
this.drawLabels();
}
private setArcs() {
// ...
@AvocadoVenom
AvocadoVenom / summarize-list.ts
Last active February 8, 2019 14:55
Transform a list of items into a smaller one (containing X elements). Keep the X-1 first items and concatenate the remaining into one single item.
/**
* Keep the @param nb first values of the list @param values.
* Apply the @param reduceFn function to "concatenate" the remaining values.
* @param values List of values/objects.
* @param nb Final number of values in the list.
* @param orderingFn Function to use to order the list.
* @param reduceFn Reduction function to group exceeding items.
*/
export function summarizeList<T>(values: T[], nb: number = 10, orderingFn: (a: T, b: T) => number, reduceFn: (list: T[]) => T) {
// From nb+ values, we trigger the grouping
/**
* Unflatten an array of objects.
*
* @param arr An array of objects.
* @param keyFn Key property accessor function.
* @param parentFn Parent property accessor function.
* @example unflatten<FlatTreeNode>(x, i => i.id, i => i.parentId);
*/
private unflatten<T>(arr: T[], keyFn: (item: T) => number, parentFn: (item: T) => number): T[] {
const tree = [],