{"id":19081,"date":"2020-02-12T09:58:30","date_gmt":"2020-02-12T08:58:30","guid":{"rendered":"http:\/\/www.highcharts.com\/blog\/?p=19081"},"modified":"2026-01-12T11:39:07","modified_gmt":"2026-01-12T11:39:07","slug":"drag-and-drop-charts-with-angular-and-highcharts","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/integration\/drag-and-drop-charts-with-angular-and-highcharts\/","title":{"rendered":"Drag and drop charts with Angular and Highcharts"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Whether you want to create a dashboard or some other application with floating charts, Angular makes it easy to move charts around in a drag-and-drop fashion.<\/p>\n<p>In this quick tutorial, you\u2019ll learn how to create a project that looks like the image below (check also the live project on <a href=\"https:\/\/stackblitz.com\/edit\/angular-3sqtns\" target=\"_blank\" rel=\"noopener noreferrer\">stackblitz<\/a>):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-19103\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/02\/12095643\/ezgif.com-resize.gif\" alt=\"Drag and drop chart with Highcharts and Angular\" width=\"942\" height=\"500\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Let\u2019s get started \ud83d\ude42<\/p>\n<p>For this project, we\u2019re using the following technologies:<\/p>\n<ul>\n<li>Angular CDK angular\/cdk 8.2.3.<\/li>\n<li>Highcharts V8.0.0.<\/li>\n<li><a href=\"https:\/\/github.com\/highcharts\/highcharts-angular\" target=\"_blank\" rel=\"noopener noreferrer\">Highcharts-Angular<\/a>.<\/li>\n<\/ul>\n<p>The project is set up using an Angular application environment and workspace with the following settings:<\/p>\n<ul>\n<li>node 6.10.2+<\/li>\n<li>npm 4.6.1+<\/li>\n<li>angular\/cli 6.0.0+<\/li>\n<\/ul>\n<p>If you are not familiar with this type of environment, the official Angular documentation describes how to set up the <a href=\"https:\/\/angular.io\/guide\/setup-local\" target=\"_blank\" rel=\"noopener noreferrer\">local environment and workspace<\/a>.<\/p>\n<p>Let\u2019s move on and create the project!<\/p>\n<p>The project is divided into this standard files architecture:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-19084\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/02\/11094653\/image1.jpg\" alt=\"\" width=\"245\" height=\"196\" \/><\/p>\n<p>Next up is the installation of highcharts-angular and the Highcharts library:<\/p>\n<pre># install highcharts-angular and highcharts\r\nnpm install highcharts-angular highcharts<\/pre>\n<p>Highcharts allow us to access the highcharts library, and <b>highchats-angular<\/b> is the official angular wrapper. To use those packages, you have to import them. So, in the <code>app.module.ts<\/code> file, I import the module <code>HighchartsChartModule<\/code> from the highcharts-angular package.<\/p>\n<pre>import { HighchartsChartModule } from 'highcharts-angular';\r\n @NgModule({\r\n  imports: [\r\n    ...\r\n    HighchartsChartModule<\/pre>\n<p>And, in the <code>app.component.ts<\/code> file, I import Highcharts like this:<\/p>\n<pre>import * as Highcharts from 'highcharts';\r\n<\/pre>\n<p>We also have to install The <b>@angular\/cdk\/drag-drop<\/b> package from the Angular Component Development Kit (CDK). This package is central in this project since it includes the functionality to easily create drag &amp; drop interfaces with full flexibility using an API.<\/p>\n<p>Here is the command line to install the latest version of CDK:<\/p>\n<pre>npm install @angular\/cdk\r\n<\/pre>\n<p>Once the package is installed, we can add and integrate its main modules to the angular app.<br \/>\nHere is how we add both modules <b>Highcharts-angular<\/b> and <code>DragDropModule<\/code> in <code>app.module.ts<\/code><\/p>\n<pre>import { NgModule } from '@angular\/core';\r\nimport { BrowserModule } from '@angular\/platform-browser';\r\nimport { FormsModule } from '@angular\/forms';\r\n\r\nimport { AppComponent } from '.\/app.component';\r\nimport { HelloComponent } from '.\/hello.component';\r\nimport { HighchartsChartModule } from 'highcharts-angular';\r\nimport { DragDropModule } from '@angular\/cdk\/drag-drop';\r\n@NgModule({\r\n  imports:      [ BrowserModule, FormsModule,HighchartsChartModule ,DragDropModule],\r\n  declarations: [ AppComponent, HelloComponent ],\r\n  bootstrap:    [ AppComponent ]\r\n})\r\nexport class AppModule { }<\/pre>\n<p>Now, let\u2019s create some charts for the test. Let\u2019s use a line chart and a bar chart in <code>app.component<\/code> for this demo.<\/p>\n<p>We\u2019ll use a highcharts instance to customize the charts by accessing the API:<\/p>\n<pre>import { Component } from \"@angular\/core\";\r\nimport * as Highcharts from \"highcharts\";\r\n@Component({\r\n  selector: \"my-app\",\r\n  templateUrl: \".\/app.component.html\",\r\n  styleUrls: [\".\/app.component.css\"]\r\n})\r\nexport class AppComponent {\r\n  Highcharts = Highcharts;\r\n  linechart = {\r\n    series: [\r\n      {\r\n        data: [1, 2, 3]\r\n      }\r\n    ],\r\n    chart: {\r\n      type: \"line\"\r\n    },\r\n    title: {\r\n      text: \"linechart\"\r\n    }\r\n  };\r\n  barchart = {\r\n    series: [\r\n      {\r\n        data: [1, 2, 3]\r\n      }\r\n    ],\r\n    chart: {\r\n      type: \"bar\"\r\n    },\r\n    title: {\r\n      text: \"barchart\"\r\n    }    \r\n  };\r\n}<\/pre>\n<p>Finally, add an HTML snippet into <code>app.component.html<\/code>:<\/p>\n<pre>&lt;div class=\"example-box\" cdkDrag&gt;\r\n&lt;highcharts-chart \r\n  [Highcharts]=\"Highcharts\"\r\n  [options]=\"linechart\"\r\n  style=\"width: 100%; height: 400px; display: block;\" &amp;gt&lt;\/highcharts-chart&gt;\r\n&lt;\/div&amp;gt\r\n&lt;div class=\"example-box\" cdkDrag&gt;\r\n&lt;highcharts-chart \r\n  [Highcharts]=\"Highcharts\"\r\n  [options]=\"barchart\"\r\n  style=\"width: 100%; height: 400px; display: block;\" &gt;&lt;\/highcharts-chart&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>I hope you find this tutorial useful. Let me know if you have any questions or comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use Highcharts-Angular to create an application with floating charts.<\/p>\n","protected":false},"author":249,"featured_media":20551,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"meta_title":"","meta_description":"","hc_selected_options":[],"footnotes":""},"categories":[1105],"tags":[822,1094,1031],"coauthors":[779],"class_list":["post-19081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-integration","tag-angular","tag-highcharts-core","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/19081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/comments?post=19081"}],"version-history":[{"count":1,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/19081\/revisions"}],"predecessor-version":[{"id":29227,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/19081\/revisions\/29227"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/20551"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=19081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=19081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=19081"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=19081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}