Introduction
Today we will learn how to create a drag-able legend and maintain that position in the chart area, so that once we drag and drop the legend, even if we reload the chart again, it will take the legend position from the session storage, where we have set the values when dropping the legend. Cool, right?
Please see this article in my blog
Background
For the past few weeks I have been working in Charts, so a few days ago I got a requirement to make a drag-able legend and maintain the position. As you all know, everyone expects the best from their products, so I began working to satisfy my client by giving them all the feasible options in the chart. Once I was done with that it was good.
Load the required files
To start, we must load some files, you can determine the files below.
- <script src="Scripts/jquery-1.11.1.min.js"></script>
- <script src="Scripts/highcharts.js"></script>
- <script src="Scripts/highcharts-more.js"></script>
- <script src="Scripts/draggable-legend.js"></script>
Now we need some UI elements to load the chart and show the drag and drop position. We will show the position values while the user drags and drops the legend so that it looks more active.
- <div id="container" >"min-width: 310px; height: 400px; margin: 0 auto"></div>
- Legend X: <input id="txtLegendX" type="number" /> <br />
- Legend X: <input id="txtLegendY" type="number" />
The next step we need is to set the options for the chart. Please find the following settings.
- var options = {
- title: {
- text: 'Worked hours in a week'
- },
- xAxis: {
- categories: ['Developing', 'Maitenance', 'Marketing', 'Business', 'Admin']
- },
- series: [{
- type: 'column',
- name: 'Monday',
- data: [3, 2, 1, 3, 4]
- }, {
- type: 'column',
- name: 'Tuesday',
- data: [2, 3, 5, 7, 6]
- }, {
- type: 'column',
- name: 'Wednesday',
- data: [4, 3, 3, 9, 0]
- }, {
- type: 'column',
- name: 'Thursday',
- data: [4, 3, 3, 9, 0]
- }, {
- type: 'column',
- name: 'Friday',
- data: [4, 3, 3, 9, 0]
- }, {
- type: 'spline',
- name: 'Thursday',
- data: [3, 2.67, 3, 6.33, 3.33],
- marker: {
- lineWidth: 2,
- lineColor: Highcharts.getOptions().colors[3],
- fillColor: 'white'
- }
- }],
- legend: {
- enabled: true,
- layout: 'vertical',
- backgroundColor: 'white',
- align: 'left',
- verticalAlign: 'top',
- y: 50,
- x: 50,
- borderWidth: 1,
- borderRadius: 0,
- title: {
- text: '::Drag Me'
- },
- floating: true,
- draggable: true,
- zIndex: 20
- }
- };
- floating: true,
- draggable: true,

So our application is working fine, isn't it?
Changes in draggable-legend.js
To continue with the process, we must make some changes in the draggable-legend.js. You can see the changes below. If you download the source code attached, you can see the changes by default.
- addEvent(chart.container, 'mousemove', function (e) {
- });
- $("#txtLegendX").val(options.x);
- $("#txtLegendY").val(options.y);
- sessionStorage.setItem("legendx", options.x);
- sessionStorage.setItem("legendy", options.y);

Now if you drag and drop the legend somewhere else, you can see that the values specified in the text boxes have changed.

No, we will check whether the values are updated to the session storage in the browser console.

Cool, it is updated, we are happy.
Now we need to retain this position even if the user reloads the chart, right? Normally what happens is, the chart will be loaded with the initial settings we have set in the option.
Maintain the legend position
To maintain the legend position, we must apply the legend position to the chart, we can take the values from the session storage where we already set the values.
- if (sessionStorage.getItem("legendx") != null && typeof sessionStorage.getItem("legendx") != 'undefined' && sessionStorage.getItem("legendy") != null && typeof sessionStorage.getItem("legendy") != 'undefined') {
- options.legend.x = parseInt(sessionStorage.getItem("legendx"));
- options.legend.y = parseInt(sessionStorage.getItem("legendy"));
- $("#txtLegendX").val(sessionStorage.getItem("legendx"));
- $("#txtLegendY").val(sessionStorage.getItem("legendy"));
- }
- $('#container').highcharts(options);
Complete code
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Dragable Legend - SibeeshPassion</title>
- <script src="Scripts/jquery-1.11.1.min.js"></script>
- <script src="Scripts/highcharts.js"></script>
- <script src="Scripts/highcharts-more.js"></script>
- <script src="Scripts/draggable-legend.js"></script>
- <script>
- $(function () {
- var options = {
- title: {
- text: 'Worked hours in a week'
- },
- xAxis: {
- categories: ['Developing', 'Maitenance', 'Marketing', 'Business', 'Admin']
- },
- series: [{
- type: 'column',
- name: 'Monday',
- data: [3, 2, 1, 3, 4]
- }, {
- type: 'column',
- name: 'Tuesday',
- data: [2, 3, 5, 7, 6]
- }, {
- type: 'column',
- name: 'Wednesday',
- data: [4, 3, 3, 9, 0]
- }, {
- type: 'column',
- name: 'Thursday',
- data: [4, 3, 3, 9, 0]
- }, {
- type: 'column',
- name: 'Friday',
- data: [4, 3, 3, 9, 0]
- }, {
- type: 'spline',
- name: 'Thursday',
- data: [3, 2.67, 3, 6.33, 3.33],
- marker: {
- lineWidth: 2,
- lineColor: Highcharts.getOptions().colors[3],
- fillColor: 'white'
- }
- }],
- legend: {
- enabled: true,
- layout: 'vertical',
- backgroundColor: 'white',
- align: 'left',
- verticalAlign: 'top',
- y: 50,
- x: 50,
- borderWidth: 1,
- borderRadius: 0,
- title: {
- text: '::Drag Me'
- },
- floating: true,
- draggable: true,
- zIndex: 20
- }
- };
- if (sessionStorage.getItem("legendx") != null && typeof sessionStorage.getItem("legendx") != 'undefined' && sessionStorage.getItem("legendy") != null && typeof sessionStorage.getItem("legendy") != 'undefined') {
- options.legend.x = parseInt(sessionStorage.getItem("legendx"));
- options.legend.y = parseInt(sessionStorage.getItem("legendy"));
- $("#txtLegendX").val(sessionStorage.getItem("legendx"));
- $("#txtLegendY").val(sessionStorage.getItem("legendy"));
- }
- $('#container').highcharts(options);
- });
- </script>
- </head>
- <body>
- Dragable Legend - SibeeshPassion
- <br />
- <br />
- <div id="container" s>"min-width: 310px; height: 400px; margin: 0 auto"></div>
- Legend X: <input id="txtLegendX" type="number" /> <br />
- Legend X: <input id="txtLegendY" type="number" />
- </body>
- </html>
Conclusion
I hope you liked this article. Please provide your valuable suggestions. It matters a lot. Please download the source code to determine more.
Points of interest
Legend, Chart, Drag legend, Drop legend, Drag and drop legend in chart.

Abhishek JaiswalPosted Aug 13, 2015, 1:37 PM
Interesting, thanks for sharing! :)
Sibeesh VenuPosted Aug 13, 2015, 3:33 AM
Nilesh Jadav Thank you :)
Sibeesh VenuPosted Aug 13, 2015, 3:33 AM
Shridhar Sharma Thank you :)
Sibeesh VenuPosted Aug 13, 2015, 3:33 AM
Debasis Saha Thank you :)
Sibeesh VenuPosted Aug 13, 2015, 3:33 AM
Chervine Bhiwoo Thank you :)
Sibeesh VenuPosted Aug 13, 2015, 3:33 AM
Ankit Bansal Thank you :)
Nilesh JadavPosted Aug 13, 2015, 3:04 AM
100th article sir :) keep it up
Shridhar SharmaPosted Aug 12, 2015, 7:11 PM
nice article.
Debasis SahaPosted Aug 12, 2015, 3:16 PM
nice one..
Chervine BhiwooPosted Aug 12, 2015, 10:55 AM
Good job!
Ankit BansalPosted Aug 12, 2015, 8:53 AM
good one..
Sibeesh VenuPosted Aug 12, 2015, 8:12 AM
Jaipal Reddy Thanks dude
Sibeesh VenuPosted Aug 12, 2015, 8:12 AM
Gopi Chand Thanks buddy
Sibeesh VenuPosted Aug 12, 2015, 8:12 AM
Vaikesh K P Thank you
Jaipal ReddyPosted Aug 12, 2015, 8:02 AM
good one sir
Gopi ChandPosted Aug 12, 2015, 7:39 AM
Great work done by you...:)
Vaikesh K PPosted Aug 12, 2015, 6:59 AM
Good article
Sibeesh VenuPosted Aug 12, 2015, 6:01 AM
Rajeesh Menoth Thank you so much
Rajeesh MenothPosted Aug 12, 2015, 5:55 AM
NIce one
Sibeesh VenuPosted Aug 12, 2015, 5:40 AM
Karthikeyan K Thank you
Karthikeyan KPosted Aug 12, 2015, 5:31 AM
Good one sir..Thanks for sharing
Sibeesh VenuPosted May 14, 2015, 8:57 AM
Santhakumar Munuswamy Thank you so much
Santhakumar MunuswamyPosted May 13, 2015, 4:24 PM
keep it up...
Santhakumar MunuswamyPosted May 13, 2015, 4:23 PM
Thanks for nice article
NitinPosted May 13, 2015, 12:09 PM
nice
Sibeesh VenuPosted May 13, 2015, 8:21 AM
Gowtham Rajamanickam thank you
Gowtham RajamanickamPosted May 13, 2015, 8:16 AM
good one
Sibeesh VenuPosted May 13, 2015, 3:54 AM
Gopi Chand thank you
Gopi ChandPosted May 13, 2015, 3:24 AM
Good presentation along with description.