vertical bar chart with validation

kentas

I changed my code abit, because i want some validation inside my code. before the the bars had the "%" on it and the name typed in under the bar, now if i type in 2 participants, it creates 4 bars instead of 2. At the place where the names should be is an "undefined" and where the % should be is NaN%. Does someone know where the mistake is?

<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>
Lance

Try this:

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

Google chart vertical axis strange label

분류에서Dev

Flot Bar Chart Data Conversion

분류에서Dev

Subset ordinal domain for a bar chart

분류에서Dev

Flot Chart's Bar Width

분류에서Dev

Custom bar chart text with Highcharts

분류에서Dev

White space on the left of vertical nav bar

분류에서Dev

How to make vertical bar | work in grep?

분류에서Dev

Text (Absolute) Will Not Pass Through Vertical Scroll Bar

분류에서Dev

How to create Vertical progress bar in Bootstrap with

분류에서Dev

How to create a bar chart/histogram with bar per discrete value?

분류에서Dev

Dimple.js - Add data labels to each bar of the bar chart

분류에서Dev

Stacked Bar Chart Labels - D3

분류에서Dev

Horizontal bar chart y axis alignment

분류에서Dev

Stacked Bar Chart D3js

분류에서Dev

show row values vertical in crystal report graph(Cross Tab Chart)

분류에서Dev

how to move vertical scroll bar on clicking an element to display the content correctly?

분류에서Dev

Add word with pipe/vertical bar to MS Word or Office Dictionary

분류에서Dev

What do the vertical lines represent in Seaborn bar charts?

분류에서Dev

Label stacked bar chart with variable other than plotted Y

분류에서Dev

stacked bar and line chart in same graph with any android library?

분류에서Dev

How to show a column with the value Zero in Bar chart in Highcharts?

분류에서Dev

How to change section color of a stacked bar chart in Google Charts API?

분류에서Dev

How can I draw an additional horizontal grid line with a specific value in Google Bar chart (Not average value)

분류에서Dev

Python - How to make a bar chart from csv file when the data is not numeric

분류에서Dev

Google Bar Chart data.push는 함수가 아닙니다.

분류에서Dev

Altair Bar Chart-라벨 배치 및 포맷

분류에서Dev

R ggplot How to plot a bar chart with different colours inside depicting 3 differents columns from the dataframe?

분류에서Dev

Google Charts remove white border around vAxis textPosition 'in' text on Bar Chart

분류에서Dev

vAxis : {slantedText : true}는 Bar Chart [Google Charts]에서 가능합니까?

Related 관련 기사

  1. 1

    Google chart vertical axis strange label

  2. 2

    Flot Bar Chart Data Conversion

  3. 3

    Subset ordinal domain for a bar chart

  4. 4

    Flot Chart's Bar Width

  5. 5

    Custom bar chart text with Highcharts

  6. 6

    White space on the left of vertical nav bar

  7. 7

    How to make vertical bar | work in grep?

  8. 8

    Text (Absolute) Will Not Pass Through Vertical Scroll Bar

  9. 9

    How to create Vertical progress bar in Bootstrap with

  10. 10

    How to create a bar chart/histogram with bar per discrete value?

  11. 11

    Dimple.js - Add data labels to each bar of the bar chart

  12. 12

    Stacked Bar Chart Labels - D3

  13. 13

    Horizontal bar chart y axis alignment

  14. 14

    Stacked Bar Chart D3js

  15. 15

    show row values vertical in crystal report graph(Cross Tab Chart)

  16. 16

    how to move vertical scroll bar on clicking an element to display the content correctly?

  17. 17

    Add word with pipe/vertical bar to MS Word or Office Dictionary

  18. 18

    What do the vertical lines represent in Seaborn bar charts?

  19. 19

    Label stacked bar chart with variable other than plotted Y

  20. 20

    stacked bar and line chart in same graph with any android library?

  21. 21

    How to show a column with the value Zero in Bar chart in Highcharts?

  22. 22

    How to change section color of a stacked bar chart in Google Charts API?

  23. 23

    How can I draw an additional horizontal grid line with a specific value in Google Bar chart (Not average value)

  24. 24

    Python - How to make a bar chart from csv file when the data is not numeric

  25. 25

    Google Bar Chart data.push는 함수가 아닙니다.

  26. 26

    Altair Bar Chart-라벨 배치 및 포맷

  27. 27

    R ggplot How to plot a bar chart with different colours inside depicting 3 differents columns from the dataframe?

  28. 28

    Google Charts remove white border around vAxis textPosition 'in' text on Bar Chart

  29. 29

    vAxis : {slantedText : true}는 Bar Chart [Google Charts]에서 가능합니까?

뜨겁다태그

보관