質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

257閲覧

jQuery animatedでグラフ作成をLOOPさせると、変数がみんな同じ値になってしまう

TEKA-e1

総合スコア5

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2020/05/30 02:45

編集2020/05/30 02:52

jQuery のAnimationを使ったJavascript JQMeterJQMeterを、phpでLoopさせて複数のグラフを表示させたいと思っています。
動作デモ

棒グラフを0からカウントアップさせるのですが、そこに表示される数字の単位は%となっており、ここに、グラフに毎に変数を与え、数字と共に表示させたいと思っています。以下の様に、単位(tani)という変数を与え、%を変数に置き換え、グラフ単体では問題なく表示されました。

Javascript

1$({ Counter: 0 }).animate({ Counter: $(elem).children('.outer-therm').children('.inner-therm').children().text() }, { 2 duration : counterSpeed, 3 easing : 'swing', 4 step : function() { 5 $(elem).children('.outer-therm').children('.inner-therm').children().text(Math.ceil(this.Counter) + '%'); 6 } 7 });

を↓

Javascript

1tani = options.tani; 2と設定した上で 3$({ Counter: 0 }).animate({ Counter: $(elem).children('.outer-therm').children('.inner-therm').children().text() }, { 4 duration : counterSpeed, 5 easing : 'swing', 6 step : function() { 7 $(elem).children('.outer-therm').children('.inner-therm').children().text(Math.ceil(this.Counter) + tani);

しかし、これをLOOPでグラフを複数作成しようとすると、単位(tani)がすべてのグラフで、一番最後に与えらえた値になってしまいます。
この、単位(tani)以外の変数は、それぞれのグラフに与えた値で処理されています。

javascriptでループの中に関数があると変数が最後の値になるよ?
の案件だと思うのですが、これに合わせて設定をしてみたのですが、うまくいきません。

アドバイスをいただきたく、よろしくお願いします。

Javascript

1/* 2 3Title: jQMeter: a jQuery Progress Meter Plugin 4Author: Gerardo Larios 5Version: 0.1 6Website: http://www.gerardolarios.com/jqmeter 7License: Dual licensed under the MIT and GPL licenses. 8 9*/ 10 11(function($) { 12 13 //Extend the jQuery prototype 14 $.fn.extend({ 15 jQMeter: function(options) { 16 if (options && typeof(options) == 'object') { 17 options = $.extend( {}, $.jQMeter.defaults, options ); 18 } 19 this.each(function() { 20 new $.jQMeter(this, options); 21 }); 22 return; 23 } 24 }); 25 $.jQMeter = function(elem, options) { 26 //Define plugin options 27 goal = parseInt((options.goal).replace(/\D/g,'')); 28 raised = parseInt((options.raised).replace(/\D/g,'')); 29 min = parseInt((options.min).replace(/\D/g,'')); 30 width = options.width; 31 height = options.height; 32 bgColor = options.bgColor; 33 tani = options.tani;//これを追加 34 meterOrientation = options.meterOrientation; 35 animationSpeed = options.animationSpeed; 36 counterSpeed = options.counterSpeed; 37 displayTotal = options.displayTotal; 38 total = ((raised - min) / (goal - min)) * 100; 39 40 /* 41 * Since the thermometer width/height is set based off of 42 * the total, we force the total to 100% if the goal has 43 * been exceeded. 44 */ 45 if(total >= 100) { 46 total = 100; 47 } 48 49 //Create the thermometer layout based on orientation option 50 if(meterOrientation == 'vertical') { 51 52 $(elem).html('<div class="therm outer-therm vertical"><div class="therm inner-therm vertical"><span style="display:none;">' + total + '</span></div></div>'); 53 $(elem).children('.outer-therm').attr('style','width:' + width + ';height:' + height + ';background-color:' + bgColor); 54 $(elem).children('.outer-therm').children('.inner-therm').attr('style','background-color:' + barColor + ';height:0;width:100%'); 55 $(elem).children('.outer-therm').children('.inner-therm').animate({height : total + '%'},animationSpeed); 56 57 } else { 58 $(elem).html('<div class="therm outer-therm"><div class="therm inner-therm"><span style="display:none;">' + total + '</span></div></div>'); 59 $(elem).children('.outer-therm').attr('style','width:' + width + ';height:' + height + ';background-color:' + bgColor); 60 $(elem).children('.outer-therm').children('.inner-therm').attr('style','background-color:' + barColor1 + ';height:' + height + ';width:0'); 61 $(elem).children('.outer-therm').children('.inner-therm').animate({width : total + '%'},animationSpeed); 62 63 } 64 65 //If the user wants the total percentage to be displayed in the thermometer 66 if(displayTotal) { 67 68 //Accomodate the padding of the thermometer to include the total percentage text 69 var formatted_height = parseInt(height); 70 var padding = (formatted_height/2) - 13 + 'px 10px'; 71 72 if(meterOrientation != 'horizontal'){ 73 padding = '10px 0'; 74 } 75 76 $(elem).children('.outer-therm').children('.inner-therm').children().show(); 77 $(elem).children('.outer-therm').children('.inner-therm').children().css('padding', padding); 78 79 //Animate the percentage total. Borrowed from: http://stackoverflow.com/questions/23006516/jquery-animated-number-counter-from-zero-to-value 80 $({ Counter: 0 }).animate({ Counter: raised }, { 81 duration : counterSpeed, 82 easing : 'swing', 83 step : function() { 84 $(elem).children('.outer-therm').children('.inner-therm').children().text(Math.ceil(this.Counter) + tani);//ここを変更 85 } 86 }); 87 } 88 89 //Add CSS 90 $(elem).append('<style>.therm{height:30px;border-radius:5px;}.outer-therm{margin:20px 0;}.inner-therm span {color: #fff;display: inline-block;float: right;font-family: Trebuchet MS;font-size: 20px;font-weight: bold;}.vertical.inner-therm span{width:100%;text-align:center;}.vertical.outer-therm{position:relative;}.vertical.inner-therm{position:absolute;bottom:0;}</style>'); 91 92 }; 93 94 //Set plugin defaults 95 $.jQMeter.defaults = { 96 97 width : '100%', 98 height : '50px', 99 bgColor : '#444', 100 barColor : '#bfd255', 101 meterOrientation : 'horizontal', 102 animationSpeed : 2000, 103 counterSpeed : 2000, 104 displayTotal : true 105 106 }; 107 108})(jQuery);

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

こんにちは、
下のコードの箇所を

Javascript

1//Animate the percentage total. Borrowed from: http://stackoverflow.com/questions/23006516/jquery-animated-number-counter-from-zero-to-value 2$({ Counter: 0 }).animate({ Counter: raised }, { 3 duration : counterSpeed, 4 easing : 'swing', 5 step : function() { 6 $(elem).children('.outer-therm').children('.inner-therm').children().text(Math.ceil(this.Counter) + tani);//ここを変更 7 } 8});

こちらに変更してみたらどうでしょう?

Javascript

1//Animate the percentage total. Borrowed from: http://stackoverflow.com/questions/23006516/jquery-animated-number-counter-from-zero-to-value 2$({ Counter: 0, tani: tani }).animate({ Counter: raised }, { 3 duration : counterSpeed, 4 easing : 'swing', 5 step : function() { 6 $(elem).children('.outer-therm').children('.inner-therm').children().text(Math.ceil(this.Counter) + this.tani);//ここを変更 7 } 8});

$({ Counter: 0, tani: tani })と代入してやることで、this.taniからそれぞれオプションで設定したtaniを取得できると思います。

投稿2020/05/30 05:11

fake_shibe

総合スコア806

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TEKA-e1

2020/05/31 01:13

fake_shibe様、ありがとうございました。Loopの部分の単位がそれぞれ表示されるようになりました。 ただ、1つだけ同じグラフでループをさせずに表示しているものがあるのですが、同じJSを使うと、グラフに表示が、数字も単位も合わせてNanとなってしまいます。 ループされる場合とされない場合の条件分けをどのようにすれば良いでしょうか? よろしくお願いします。
fake_shibe

2020/05/31 01:49

NaNになる場合には、単位を表示しないということだと解釈しました。 以下のように変更してみたらどうでしょう? $({ Counter: 0, tani: tani }).animate( { Counter: raised }, { duration: counterSpeed, easing: "swing", step: function () { if (is_NaN(Math.ceil(this.Counter) + this.tani)) { $(elem).children(".outer-therm").children(".inner-therm").children().text(Math.ceil(this.Counter)); } else { $(elem) .children(".outer-therm") .children(".inner-therm") .children() .text(Math.ceil(this.Counter) + this.tani); } }, } ); function is_NaN(x) { return x !== x; }
TEKA-e1

2020/06/01 02:08

ありがとうございました!しっかり表示できるようになりました。 本当にありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問