検証付きの垂直棒グラフ

ケンタス

コード内で検証が必要なため、コードを少し変更しました。バーに「%」が表示され、バーの下に名前が入力される前は、2人の参加者を入力すると、2ではなく4つのバーが作成されます。名前のある場所は「未定義」であり、ここで、%はNaN%です。誰かが間違いがどこにあるか知っていますか?

<head>
    <title>examAnalysis</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <style type="text/css">
    div {
        float: left; margin-right: 10px;
    }
    div p { 
        text-align: center;
    }
    </style>
    <script type="text/javascript">
var participant = [];
var maxPoints = 200;
var barWidth = 50;

function gatherData() {
    var name;
    var points;
    while (name = window.prompt('Please enter the name of the participant:')) {
        name = name.replace(/\s+/g, '')
    if ((/[^A-Z\s\-\']/gi.test(name)) || (name == '')) {
        alert ('You must enter a valid name! ');
    } else {
        participant.push({name: name});

        while(points = window.prompt('Please enter the achieved points of the participant:')) {
        points = parseInt(points, 10); 
        if ((/[^0-9\s\-\']/gi.test(points)) || (points == '')) {
            alert ('You must enter a valid number! ');
        } else {
            participant.push({points: points});
            break;
          }
        }  
    }
}
    createChart();
};

function createChart ()
    {
        var i = 0;
        var len = participant.length;
        var bar = null;

        var container = document.getElementById('chart');
        container.innerHTML='';

        while (i < len)
        {

            bar = document.createElement('div');
            bar.style.width = barWidth + 'px';
            bar.style.backgroundColor = getRandomColor();
            bar.style.float = 'left';
            bar.style.marginRight = '10px';

            bar.style.height = ((participant[i].points / maxPoints) * 200) + 'px';
            bar.style.marginTop = 200 - parseInt(bar.style.height) + 'px';
            percent = ((participant[i].points / maxPoints) * 100) + '%';    
            bar.innerHTML = ['<p style="margin-top: ' + (parseInt(bar.style.height) - 17) + 'px">', percent, '<br />', participant[i].name, '</p>'].join('');

            container.appendChild(bar);

            i = i + 1;
        }
    };

function getRandomColor () {
    return ['rgb(', Math.floor(Math.random() * 255), ', ', Math.floor(Math.random() * 255), ', ', Math.floor(Math.random() * 255), ')'].join('');
 };
    </script>
</head>

<body>
    <button onclick="gatherData()">Add Participant</button>
    <h4>Chart</h4>
    <div id="chart"></div>
</body>
ランス

これを試して:

function gatherData()
{
    var name = window.prompt('Please enter the name of the participant:');
    name = name.replace(/\s+/g, '');
    if ((/[^A-Z\s\-\']/gi.test(name)) || (name == '') || (name == 'undefined'))
    {
        alert ('You must enter a valid name! ');
        return;
    }

    var points = window.prompt('Please enter the achieved points of the participant:');
    points = parseInt(points, 10);
    if ((/[^0-9\s\-\']/gi.test(points)) || (points == ''))
    {
        alert ('You must enter a valid number! ');
        return;
    }

    participant.push({name: name, points: points});

    createChart();
};

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

python / matplotlibの垂直ラベル付きの棒グラフ

分類Dev

棒グラフの垂直線

分類Dev

棒グラフの垂直線

分類Dev

ユーザー入力と検証を含む垂直棒グラフ

分類Dev

ggplot2のエラーバー付きの棒グラフ

分類Dev

Scilabのエラーバー付きの棒グラフ

分類Dev

グループ付きのPythonプロット棒グラフ

分類Dev

エラーバー付きのPythonmatplotlib3D棒グラフ

分類Dev

凡例付きの棒グラフRの列

分類Dev

各棒に対応する凡例付きのChartJS棒グラフ

分類Dev

リアルタイムの色付き棒グラフ

分類Dev

タイトル付きの縦棒グラフ

分類Dev

python-ボケ-条件付きの色付きの積み上げ棒グラフ

分類Dev

SSRS棒グラフの日付形式

分類Dev

棒グラフに条件付き書式を追加するときのTypeError / Undefined

分類Dev

引出線付きのggplot2積み上げ棒グラフラベル

分類Dev

log10transを使用したエラーバー付きの棒グラフ

分類Dev

関数からの棒グラフ-関数内の棒の順序を条件付きで変更します

分類Dev

検証付きのHTMLフォーム

分類Dev

検証付きのJavaScriptフォーム

分類Dev

Rのベクトルを使用した条件付き棒グラフの着色

分類Dev

ギャザー付きシャイニーの反応性棒グラフ

分類Dev

HighChart:ドロップダウン付きの積み上げ縦棒グラフ

分類Dev

vega-lite棒グラフスタックされた色付きの値

分類Dev

既存の垂直棒グラフを水平にする

分類Dev

各棒の垂直線を含む棒グラフ

分類Dev

複数の垂直軸を持つ剣道棒グラフ

分類Dev

棒グラフに2本の垂直線を追加します

分類Dev

垂直棒グラフの描画方法を逆にします

Related 関連記事

  1. 1

    python / matplotlibの垂直ラベル付きの棒グラフ

  2. 2

    棒グラフの垂直線

  3. 3

    棒グラフの垂直線

  4. 4

    ユーザー入力と検証を含む垂直棒グラフ

  5. 5

    ggplot2のエラーバー付きの棒グラフ

  6. 6

    Scilabのエラーバー付きの棒グラフ

  7. 7

    グループ付きのPythonプロット棒グラフ

  8. 8

    エラーバー付きのPythonmatplotlib3D棒グラフ

  9. 9

    凡例付きの棒グラフRの列

  10. 10

    各棒に対応する凡例付きのChartJS棒グラフ

  11. 11

    リアルタイムの色付き棒グラフ

  12. 12

    タイトル付きの縦棒グラフ

  13. 13

    python-ボケ-条件付きの色付きの積み上げ棒グラフ

  14. 14

    SSRS棒グラフの日付形式

  15. 15

    棒グラフに条件付き書式を追加するときのTypeError / Undefined

  16. 16

    引出線付きのggplot2積み上げ棒グラフラベル

  17. 17

    log10transを使用したエラーバー付きの棒グラフ

  18. 18

    関数からの棒グラフ-関数内の棒の順序を条件付きで変更します

  19. 19

    検証付きのHTMLフォーム

  20. 20

    検証付きのJavaScriptフォーム

  21. 21

    Rのベクトルを使用した条件付き棒グラフの着色

  22. 22

    ギャザー付きシャイニーの反応性棒グラフ

  23. 23

    HighChart:ドロップダウン付きの積み上げ縦棒グラフ

  24. 24

    vega-lite棒グラフスタックされた色付きの値

  25. 25

    既存の垂直棒グラフを水平にする

  26. 26

    各棒の垂直線を含む棒グラフ

  27. 27

    複数の垂直軸を持つ剣道棒グラフ

  28. 28

    棒グラフに2本の垂直線を追加します

  29. 29

    垂直棒グラフの描画方法を逆にします

ホットタグ

アーカイブ