Solved

Moving Image Graph

Posted on 2013-10-31
15
699 Views
Last Modified: 2013-11-04
I'm trying to take the below image and make it so that depending on the data fed to it by the database it can rotate around to the correct %.

I have no idea how to do this, with CSS or many images?  Would prefer if the moving if the bar were animated, but again no idea where to start.
rotating-image.jpg
0
Comment
Question by:N R
  • 8
  • 6
15 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
I've been playing around with this for a bit. I was sure a CSS3 expert would come up with some nice and easy pure CSS solution but since that hasn't happened (yet) I want to share my javascript solution with you. It uses 360 rotated divs, I used small parts of the image you posted, and I put some extra work in it to make it kind of like a plugin. You can test it straight away on my website: http://schutt.nl/ee/Q_28282364/ or download the attached files and test them locally or on your own website. I'm sure there are tons of improvements that could be made but I wanted to share this with you if only to let you know your question has not gone unnoticed...
index.html
rotimg0.png
rotimg1.png
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
Robert,

There is no neat CSS solution because it is a processing indicator and the processing would require scripting no matter what.  

If it was just a hover effect it would be a simple transform(rotate... effect with a transition to handle the timing, but CSS is event driven and does not have the capacity to do any of looping or conditional processing other than the ability to change some properties in response to changes in the state of elements.

That is a nice piece of code you have written.


Cd&
0
 
LVL 11

Author Comment

by:N R
Comment Utility
Robert, that works great!

I'm looking through the code trying to understand exactly how it works. So if I want to have 2 graphs on the page and use 2 different colors in each I would need to replicate your above code in index.html then change the css (this is how I've currently done it) or is there an easier way without having to duplicate all the code?
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
Cd&
Thanks, coming from you that's meaningful, the conclusion about CSS and the praise!

Gallitin
the colors are determined by the 2 images I took from your example image. If you can describe what color you want to change exactly (like blue to pink for a female version maybe? or get rid of the images and use any color?), then I could try to extend the parameters for that. I also started on a canvas version but that was nowhere near as nice as just using your already nicely shaded image.
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
Just to show a possible extended functionality with a minor change here:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Rotating divs</title>
    <style type="text/css">
      .rotdiv { position: absolute; left: 200px; top:  50px; width: 160px; height: 160px; background-color: white; border: 1px solid silver; }
      .rotsub { position: absolute; left:  20px; top:  30px; width: 120px; height: 120px; background-position: right center; background-repeat: no-repeat; }
      .rotim0 { background-image: url('rotimg0.png'); }
      .rotim1 { background-image: url('rotimg1.png'); }
      .rotim2 { background-image: url('rotimg2.png'); }
      .rotprc { position: absolute; left:  60px; top: 100px; width:  40px; height:  20px; text-align: center; font-size: 15pt; color: gray; }
      .inprng { position: absolute; left:  20px; top:   0px; width: 120px; }
    </style>
    <script type="text/javascript">

      var ri1, ri2, ri3;

      var rotatingImage = function(prm) {

        var that = this, a, d;

        // init main <div>
        that.rotdiv = document.createElement('DIV');
        that.rotdiv.className = 'rotdiv';
        that.rotdiv.style.top = prm.y + 'px';
        that.rotdiv.style.left = prm.x + 'px';
        document.body.appendChild(that.rotdiv);

        that.currentValue = 0;
        that.showLabel = prm.showLabel;
        that.showSlider = prm.showSlider;
        that.setImageNumber = prm.setImageNumber || 1;

        that.tmr = null;
        that.prcspan = null;

        // init child <div>s
        for (a=0; a<360; a++) {
          d = document.createElement('DIV');
          d.className = 'rotsub rotim0';
          d.style.transform = d.style.MozTransform = d.style.webkitTransform = 'rotate(' + a + 'deg)';
          that.rotdiv.appendChild(d);
        }

        // init slider if necessary
        if (that.showSlider) {
          that.inprng = document.createElement('INPUT');
          that.inprng.className = 'inprng';
          that.inprng.type = 'range';
          that.inprng.min = 0;
          that.inprng.max = 100;
          that.inprng.value = 0;
          that.inprng.onchange = function() {
            that.draw(this.value);
          };
          that.rotdiv.appendChild(that.inprng);
        }

        // init percentage span if necessary
        if (that.showLabel) {
          that.prcspan = document.createElement('SPAN');
          that.prcspan.className = 'rotprc';
          that.rotdiv.appendChild(that.prcspan);
        }

        // animate towards the target value
        that.incValue = function() {
          that.currentValue++;
          if (that.currentValue >= that.targetValue ) {
            clearInterval(that.tmr);
            that.tmr = null;
          }
          that.draw();
        };

        that.draw = function(prc) {
          var a;
          if (prc) {
            that.targetValue = prc;
            that.currentValue = prc;
          }
          for (a=0; a<360; a++) {
            that.rotdiv.childNodes[a].className = 'rotsub rotim' + (a / 3.6 >= that.currentValue ? 0 : that.setImageNumber);
          }
          if (that.showLabel) {
            that.prcspan.textContent = that.currentValue + '%';
          }
          if (that.showSlider) {
            that.inprng.value = that.currentValue;
          }
        };

        that.anim = function(prc, ms) {
          that.targetValue = prc;
          that.tmr = setInterval(that.incValue, ms);
        };

        // show value direct or animated
        if (prm.animDelay) {
          that.anim(prm.value, prm.animDelay);
        } else {
          that.draw(prm.value);
        }
      }

      document.addEventListener("DOMContentLoaded", init, false);
      function init() {
        ri1 = new rotatingImage( { x: 100, y: 100, value: 25, showLabel: false, showSlider: true, animDelay: 50 } );
        ri2 = new rotatingImage( { x: 300, y: 100, value: 50, showLabel: true, showSlider: false, animDelay: 50 } );
        ri3 = new rotatingImage( { x: 500, y: 100, value: 75, showLabel: true, showSlider: true, setImageNumber: 2 } );
      }

    </script>
  </head>
  <body>
  </body>
</html>

Open in new window

Where I added "setImageNumber: 2" at the end of the code, make sure the class exists (.rotim2) and the new image as well ('rotimg2.png'), then you can vary the colors as needed. If you need more freedom, let me know, I may have some more options.
0
 
LVL 11

Author Comment

by:N R
Comment Utility
Ah makes sense, that works perfectly.  Thanks for taking the time!
0
 
LVL 11

Author Comment

by:N R
Comment Utility
Had another question on your code robert, created a new question for it:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_28283333.html

Thanks!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
That new question is incomplete but I have reproduced your problem and will answer you here. I think it would be better to delete that new question?
0
 
LVL 11

Author Comment

by:N R
Comment Utility
Ok will do, thanks.
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
The problem is caused by the anim function calling the increment function once even when value = 0. This can be solved in a number of ways, easiest probably (around line 100 in my last posted code):
        // show value direct or animated
        if (prm.animDelay && prm.value > 0) {

Open in new window

0
 
LVL 11

Author Comment

by:N R
Comment Utility
Got it thanks again!
0
 
LVL 11

Author Comment

by:N R
Comment Utility
It seems the graph always is inserted into the page after the last <div> even if I have it within a div on the page.  Is there a way to modify so it will populate within the <div> I put it?
0
 
LVL 11

Author Comment

by:N R
Comment Utility
I'm not sure but I think this line may have something to do with it?

document.body.appendChild(that.rotdiv);

I've tried different syntax, but haven't got it done.  I want it to reside within the "leftportion" div, but can't quite get it there.
0
 
LVL 11

Author Comment

by:N R
Comment Utility
Well took a bit but fixed by replacing that line with:
 document.getElementById("leftportion").appendChild(that.rotdiv1);

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
ah, well good you found a solution. If you wanted to generalise that, you could add a parameter something like
ri1 = new rotatingImage( { x: 100, y: 100, /* ..., */ parentId: "leftportion" } );

Open in new window

then use something along the lines of (untested):
that.parentElement = prm.parentId ? document.getElementById(prm.parentId) : document.body;
that.parentElement.appendChild(that.rotdiv);

Open in new window

instead of
document.body.appendChild(that.rotdiv);

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now