Question

I have a page with a pie chart (canvas 2d) which uses js function to create. With the function, I can then in html input some values to draw the pie chart. It simply looks like this in codes

  <script id="chart">
     piechart("piechart1", ["cyan", "yellow", "green"], [ 75, 75, 75]);
  </script>

Which means I can input fixed colors/angles for my chart. What I'm trying to do is trying to make a few text input area so I can input numbers and either the script can automatically change to the number being input or using a submit button.

This is all I have at the moment can someone give me a hand with this?

<canvas id="piechart1"></canvas>

  <script id="chart">
     piechart("piechart1", ["cyan", "yellow", "green"], [ 75, 75, 75]);
  </script>

      <form action="" method="post">

        <label>Angle 1</label>
        <label>Angle 2</label>
        <label>Angle 3</label>
        <br>
        <input name="" id="angle1" value="" type="number">
        <input name="" id="angle2" value="" type="number">
        <input name="" id="angle3" value="" type="number">

        <input type="submit" id="submitBtn" value="submit">
      </form>

So let's say if I input 100 into the first text area and click submit then my

piechart("..........[ 75, 75, 75]); would change to

piechart("..........[ 100, 75, 75]); or

piechart("..........[ 100, 0, 0]);since I didn't input anything into the 2nd and 3rd text area

Thanks in advance.

Was it helpful?

Solution

This isn't really passing values. It's just calling a function when you press a button:

$(document).ready(function() {
    $('form').on('submit', function(e) {
        e.preventDefault(); // we don't want to submit the form

        $('#piechart').each(function() { // only do this if the element is found
            // clear the canvas
            this.getContext('2d').clearRect(0, 0, this.width, this.height);
        });

        var angles = [
            $('#angle1').val() || 0,
            $('#angle2').val() || 0,
            $('#angle3').val() || 0
        ];

        piechart("piechart1", ["cyan", "yellow", "green"], angles);
    });
});

val() gets the value of the input element. We do || 0 to set it to 0 just in case the browser doesn't understand type="number" and sends us an empty string.

OTHER TIPS

Try this, Change code as required

<canvas id="piechart1"></canvas>
<script type="text/javascript" >
$(function() {
$("#submitBtn").click(function() 
    {   
    var input1 = $("#angle1").val();
    if($("#angle1").val()=='') input1="";

    var input2 = $("#angle2").val();
    if($("#angle2").val()=='') input2="";

    var input3 = $("#angle3").val();
    if($("#angle3").val()=='') input3="";

    piechart("piechart1", ["cyan", "yellow", "green"], [input1, input2, input3]);

    });
});
</script>

      <form action="" method="post">

        <label>Angle 1</label>
        <label>Angle 2</label>
        <label>Angle 3</label>
        <br>
        <input name="" id="angle1" value="" type="number">
        <input name="" id="angle2" value="" type="number">
        <input name="" id="angle3" value="" type="number">

        <input type="submit" id="submitBtn" value="submit">
      </form>

I would do it this way:

$('form').on('submit', function (e) {
    e.preventDefault();
    var angles = $('input[id^="angle"]').map(function () {
        return (this.value !== "") ? this.value : 0;
    }).get();
    console.log(angles);
    piechart("piechart1", ["cyan", "yellow", "green"], angles);
});

Tryout in Fiddle here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top