1

How i can convert this js function on angular 2?

This is a function of pie chart:

$(function() {

var data = [{
    label: "Series 0",
    data: 1
}, {
    label: "Series 1",
    data: 3
}, {
    label: "Series 2",
    data: 9
}, {
    label: "Series 3",
    data: 20
}];

var plotObj = $.plot($("#flot-pie-chart"), data, {
    series: {
        pie: {
            show: true
        }
    },
    grid: {
        hoverable: true
    },
    tooltip: true,
    tooltipOpts: {
        content: "%p.0%, %s", // show percentages, rounding to 2 decimal places
        shifts: {
            x: 20,
            y: 0
        },
        defaultTheme: false
    }
});

And how to show in html? With class or something else? Maybe ngShow? This code to solve some pie chart.

2
  • are you using angular-cli for your project ? Commented Jul 25, 2016 at 11:28
  • @pd farhad i dont know what it this Commented Jul 25, 2016 at 12:04

1 Answer 1

2

You can create a angular2 directive like this:

declare var $: any;

@Directive({
  selector: 'pie-flot',
  host: {
    '[style.display]': '"block"',
    '[style.width]': '"300px"',
    '[style.height]': '"300px"'
  }
})
export class PieFlotDirective {
  @Input() data: any;
  plotObj: any;

  constructor(private elRef: ElementRef) {}

  ngAfterViewInit() {
    this.plotObj = $.plot($(this.elRef.nativeElement), this.data, {
      series: {
        pie: {
          show: true
        } 
      },
      grid: {
        hoverable: true
      },
      tooltip: true,  
      tooltipOpts: {
        content: "%p.0%, %s",
        shifts: {
          x: 20,
          y: 0
        },
        defaultTheme: false
      }
    });
  }

  ngOnDestroy() {
    this.plotObj.destroy();
  }
}

And then use it:

<pie-flot [data]="data"></pie-flot>

The corresponding plunkr is located here

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for quickly answer! But now i have this error ReferenceError: $ is not defined
and how i can use *ngFor with this chart? i
declare var $: any;
thanks a lot, and last question, how i cat change legend position? for example in righr turn on chart?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.