<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Drop legend &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/drop-legend/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 12 Aug 2015 06:59:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Drop legend &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Drag and Drop the Legend and Maintain the Position in Chart</title>
		<link>https://www.sibeeshpassion.com/drag-and-drop-the-legend-and-maintain-the-position-in-chart/</link>
					<comments>https://www.sibeeshpassion.com/drag-and-drop-the-legend-and-maintain-the-position-in-chart/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 29 Apr 2015 20:40:08 +0000</pubDate>
				<category><![CDATA[HighChart]]></category>
		<category><![CDATA[Products]]></category>
		<category><![CDATA[chart]]></category>
		<category><![CDATA[Drag and drop legend in chart.]]></category>
		<category><![CDATA[Drag legend]]></category>
		<category><![CDATA[Draggable Legend]]></category>
		<category><![CDATA[Drop legend]]></category>
		<category><![CDATA[HighChart Legend]]></category>
		<category><![CDATA[HTML5 Chart]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Legend]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=1961</guid>

					<description><![CDATA[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? 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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>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?</p>
<p><strong>Background</strong></p>
<p>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.</p>
<p><strong>Download Source Code</strong></p>
<p><a href="http://sibeeshpassion.com/download/DragAndDropLegend.rar" target="_blank">Drag and Drop the Legend and Maintain the Position</a></p>
<p><strong>Load the required files</strong></p>
<p>To start, we must load some files, you can determine the files below.<br />
[js]<br />
   &lt;script src=&quot;Scripts/jquery-1.11.1.min.js&quot;&gt;&lt;/script&gt;<br />
   &lt;script src=&quot;Scripts/highcharts.js&quot;&gt;&lt;/script&gt;<br />
   &lt;script src=&quot;Scripts/highcharts-more.js&quot;&gt;&lt;/script&gt;<br />
   &lt;script src=&quot;Scripts/draggable-legend.js&quot;&gt;&lt;/script&gt;<br />
[/js]</p>
<p><strong>Creating UI elements</strong></p>
<p>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.<br />
[html]<br />
    &lt;div id=&quot;container&quot; style=&quot;min-width: 310px; height: 400px; margin: 0 auto&quot;&gt;&lt;/div&gt;<br />
    Legend X: &lt;input id=&quot;txtLegendX&quot; type=&quot;number&quot; /&gt; &lt;br /&gt;<br />
    Legend X: &lt;input id=&quot;txtLegendY&quot; type=&quot;number&quot; /&gt;<br />
[/html]</p>
<p><strong>Creating options of chart</strong></p>
<p>The next step we need is to set the options of the chart. Please find the following settings.<br />
[js]<br />
var options = {<br />
                title: {<br />
                    text: &#8216;Worked hours in a week&#8217;<br />
                },<br />
                xAxis: {<br />
                    categories: [&#8216;Developing&#8217;, &#8216;Maitenance&#8217;, &#8216;Marketing&#8217;, &#8216;Business&#8217;, &#8216;Admin&#8217;]<br />
                },<br />
                series: [{<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Monday&#8217;,<br />
                    data: [3, 2, 1, 3, 4]<br />
                }, {<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Tuesday&#8217;,<br />
                    data: [2, 3, 5, 7, 6]<br />
                }, {<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Wednesday&#8217;,<br />
                    data: [4, 3, 3, 9, 0]<br />
                }, {<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Thursday&#8217;,<br />
                    data: [4, 3, 3, 9, 0]<br />
                }, {<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Friday&#8217;,<br />
                    data: [4, 3, 3, 9, 0]<br />
                }, {<br />
                    type: &#8216;spline&#8217;,<br />
                    name: &#8216;Thursday&#8217;,<br />
                    data: [3, 2.67, 3, 6.33, 3.33],<br />
                    marker: {<br />
                        lineWidth: 2,<br />
                        lineColor: Highcharts.getOptions().colors[3],<br />
                        fillColor: &#8216;white&#8217;<br />
                    }<br />
                }],<br />
                legend: {<br />
                    enabled: true,<br />
                    layout: &#8216;vertical&#8217;,<br />
                    backgroundColor: &#8216;white&#8217;,<br />
                    align: &#8216;left&#8217;,<br />
                    verticalAlign: &#8216;top&#8217;,<br />
                    y: 50,<br />
                    x: 50,<br />
                    borderWidth: 1,<br />
                    borderRadius: 0,<br />
                    title: {<br />
                        text: &#8216;::Drag Me&#8217;<br />
                    },<br />
                    floating: true,<br />
                    draggable: true,<br />
                    zIndex: 20<br />
                }<br />
            };<br />
[/js]</p>
<p>In the preceding code block we have set the series and other settings including the legend. Please note that we have set the draggable and floating property to true. You can customize all of these properties as you need to.<br />
[js]<br />
floating: true,<br />
draggable: true,<br />
[/js]</p>
<p>Now if you run your application, you will get output as follows.</p>
<div id="attachment_9161" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart1-e1439362346617.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-9161" src="http://sibeeshpassion.com/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart1-e1439362346617.png" alt="Drag and Drop the Legend and Maintain the Position in Chart" width="650" height="148" class="size-full wp-image-9161" srcset="/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart1-e1439362346617.png 650w, /wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart1-e1439362346617-300x68.png 300w, /wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart1-e1439362346617-400x91.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-9161" class="wp-caption-text">Drag and Drop the Legend and Maintain the Position in Chart</p></div>
<p>So our application is working fine, isn&#8217;t it?</p>
<p><strong>Changes in draggable-legend.js</strong></p>
<p>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.<br />
[js]<br />
addEvent(chart.container, &#8216;mousemove&#8217;, function (e) {  </p>
<p>});<br />
[/js]</p>
<p>I will include some code for our purposes in the preceding function in draggable-legend.js. At the end of the specified function I am including the following code block.<br />
[js]<br />
$(&quot;#txtLegendX&quot;).val(options.x);<br />
$(&quot;#txtLegendY&quot;).val(options.y);<br />
sessionStorage.setItem(&quot;legendx&quot;, options.x);<br />
sessionStorage.setItem(&quot;legendy&quot;, options.y);<br />
[/js]</p>
<p>The purpose of that code block is to set the value to the session storage and the UI element. Now we will run our application and see the output.</p>
<div id="attachment_9171" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart2-e1439362411227.png"><img decoding="async" aria-describedby="caption-attachment-9171" src="http://sibeeshpassion.com/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart2-e1439362411227.png" alt="Drag and Drop the Legend and Maintain the Position in Chart" width="650" height="194" class="size-full wp-image-9171" srcset="/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart2-e1439362411227.png 650w, /wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart2-e1439362411227-300x90.png 300w, /wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart2-e1439362411227-400x119.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-9171" class="wp-caption-text">Drag and Drop the Legend and Maintain the Position in Chart</p></div>
<p>Now if you drag and drop the legend somewhere else, you can see that the values specified in the text boxes has changed.</p>
<div id="attachment_9181" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart3.png"><img decoding="async" aria-describedby="caption-attachment-9181" src="http://sibeeshpassion.com/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart3-1024x280.png" alt="Drag and Drop the Legend and Maintain the Position in Chart" width="634" height="173" class="size-large wp-image-9181" srcset="/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart3.png 1024w, /wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart3-300x82.png 300w, /wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart3-768x210.png 768w, /wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart3-400x109.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-9181" class="wp-caption-text">Drag and Drop the Legend and Maintain the Position in Chart</p></div>
<p>No we will check whether the values are updated to the session storage in the browser console.</p>
<div id="attachment_9191" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart4-e1439362495549.png"><img decoding="async" aria-describedby="caption-attachment-9191" src="http://sibeeshpassion.com/wp-content/uploads/2015/04/drag-and-drop-the-legend-and-maintain-the-position-in-chart4-1024x313.png" alt="Drag and Drop the Legend and Maintain the Position in Chart" width="634" height="194" class="size-large wp-image-9191" /></a><p id="caption-attachment-9191" class="wp-caption-text">Drag and Drop the Legend and Maintain the Position in Chart</p></div>
<p>Cool, it is updated, we are happy.</p>
<p>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. </p>
<p><strong>Maintain the legend position</strong></p>
<p>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.<br />
[js]<br />
if (sessionStorage.getItem(&quot;legendx&quot;) != null &amp;&amp; typeof sessionStorage.getItem(&quot;legendx&quot;) != &#8216;undefined&#8217; &amp;&amp; sessionStorage.getItem(&quot;legendy&quot;) != null &amp;&amp; typeof sessionStorage.getItem(&quot;legendy&quot;) != &#8216;undefined&#8217;) {<br />
                options.legend.x = parseInt(sessionStorage.getItem(&quot;legendx&quot;));<br />
                options.legend.y = parseInt(sessionStorage.getItem(&quot;legendy&quot;));<br />
                $(&quot;#txtLegendX&quot;).val(sessionStorage.getItem(&quot;legendx&quot;));<br />
                $(&quot;#txtLegendY&quot;).val(sessionStorage.getItem(&quot;legendy&quot;));<br />
            }<br />
            $(&#8216;#container&#8217;).highcharts(options);<br />
[/js]</p>
<p>We are checking the session storage values and applying values to the options and we provide that options to the chart. Sounds good? So our complete code for this implementation is as follows.</p>
<p><strong>Complete code</strong><br />
[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Dragable Legend &#8211; sibeeshpassion&lt;/title&gt;<br />
    &lt;script src=&quot;Scripts/jquery-1.11.1.min.js&quot;&gt;&lt;/script&gt;<br />
    &lt;script src=&quot;Scripts/highcharts.js&quot;&gt;&lt;/script&gt;<br />
    &lt;script src=&quot;Scripts/highcharts-more.js&quot;&gt;&lt;/script&gt;<br />
    &lt;script src=&quot;Scripts/draggable-legend.js&quot;&gt;&lt;/script&gt;<br />
    &lt;script&gt;<br />
        $(function () {<br />
            var options = {<br />
                title: {<br />
                    text: &#8216;Worked hours in a week&#8217;<br />
                },<br />
                xAxis: {<br />
                    categories: [&#8216;Developing&#8217;, &#8216;Maitenance&#8217;, &#8216;Marketing&#8217;, &#8216;Business&#8217;, &#8216;Admin&#8217;]<br />
                },<br />
                series: [{<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Monday&#8217;,<br />
                    data: [3, 2, 1, 3, 4]<br />
                }, {<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Tuesday&#8217;,<br />
                    data: [2, 3, 5, 7, 6]<br />
                }, {<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Wednesday&#8217;,<br />
                    data: [4, 3, 3, 9, 0]<br />
                }, {<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Thursday&#8217;,<br />
                    data: [4, 3, 3, 9, 0]<br />
                }, {<br />
                    type: &#8216;column&#8217;,<br />
                    name: &#8216;Friday&#8217;,<br />
                    data: [4, 3, 3, 9, 0]<br />
                }, {<br />
                    type: &#8216;spline&#8217;,<br />
                    name: &#8216;Thursday&#8217;,<br />
                    data: [3, 2.67, 3, 6.33, 3.33],<br />
                    marker: {<br />
                        lineWidth: 2,<br />
                        lineColor: Highcharts.getOptions().colors[3],<br />
                        fillColor: &#8216;white&#8217;<br />
                    }<br />
                }],<br />
                legend: {<br />
                    enabled: true,<br />
                    layout: &#8216;vertical&#8217;,<br />
                    backgroundColor: &#8216;white&#8217;,<br />
                    align: &#8216;left&#8217;,<br />
                    verticalAlign: &#8216;top&#8217;,<br />
                    y: 50,<br />
                    x: 50,<br />
                    borderWidth: 1,<br />
                    borderRadius: 0,<br />
                    title: {<br />
                        text: &#8216;::Drag Me&#8217;<br />
                    },<br />
                    floating: true,<br />
                    draggable: true,<br />
                    zIndex: 20<br />
                }<br />
            };<br />
            if (sessionStorage.getItem(&quot;legendx&quot;) != null &amp;&amp; typeof sessionStorage.getItem(&quot;legendx&quot;) != &#8216;undefined&#8217; &amp;&amp; sessionStorage.getItem(&quot;legendy&quot;) != null &amp;&amp; typeof sessionStorage.getItem(&quot;legendy&quot;) != &#8216;undefined&#8217;) {<br />
                options.legend.x = parseInt(sessionStorage.getItem(&quot;legendx&quot;));<br />
                options.legend.y = parseInt(sessionStorage.getItem(&quot;legendy&quot;));<br />
                $(&quot;#txtLegendX&quot;).val(sessionStorage.getItem(&quot;legendx&quot;));<br />
                $(&quot;#txtLegendY&quot;).val(sessionStorage.getItem(&quot;legendy&quot;));<br />
            }<br />
            $(&#8216;#container&#8217;).highcharts(options);  </p>
<p>        });<br />
    &lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    Dragable Legend &#8211; sibeeshpassion<br />
    &lt;br /&gt;<br />
    &lt;br /&gt;<br />
    &lt;div id=&quot;container&quot; style=&quot;min-width: 310px; height: 400px; margin: 0 auto&quot;&gt;&lt;/div&gt;<br />
    Legend X: &lt;input id=&quot;txtLegendX&quot; type=&quot;number&quot; /&gt; &lt;br /&gt;<br />
    Legend X: &lt;input id=&quot;txtLegendY&quot; type=&quot;number&quot; /&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p><strong>Conclusion</strong></p>
<p>I hope you liked this article. Please provide your valuable suggestions. It matters a lot. Please download the source code to determine more.</p>
<p><strong>Points of interest</strong></p>
<p>Legend, Chart, Drag legend, Drop legend, Drag and drop legend in chart.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/drag-and-drop-the-legend-and-maintain-the-position-in-chart/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
