Kazi
Kazi

Reputation: 31

How to insert values to an array using input fields?

I have a chart which shows two columns of data. When the page loads, I need to show 0 for both bars in the chart. Then I want to give the control to the user the change the values using two text boxes. Possibly using 'keyup', so the user does not have to hit a submit button.

var barvalues = [0,0];

I am planning on using HTML/CSS and JS, JQUERY. I can use any charts out there as long as it's not HTML5. Users are coming from IE 8, 9, 10 and 11. The problem I have is binding the data from the input field 'on-the-fly' to the barvalues array. How can i do it? How can I dynamically change/modify the values of the array so the chart get's updated whenever I hit the tab from the input box? Thanks!!

Upvotes: 3

Views: 915

Answers (2)

c2willi
c2willi

Reputation: 106

Put this together using jQuery and the JS charting package HighCharts. Just 2 input boxes bound to a respective bar in the chart.
You can change the values by pressing arrow up/down or just typing in a value and the chart will be updated 'on-the-fly'

http://jsfiddle.net/46jad9xk

HTML

<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
Changes bar 1 <input type='text' name='series-1' value=10>
Changes bar 2 <input type='text' name='series-2' value=10>

JS:

$(function () {

$('input').keyup(function(event) {

    var v = parseInt($(this).val(),10);

    if(event.which == 38){//arrow up            
        v++
    }
    else if(event.which == 40){//arrow down
        if(v>0)v--
    }

    var chart = $('#container').highcharts(),
        series = chart.get($(this).attr('name'));
    $(this).val(v)        
    series.setData([v]);
})

var chart1 = $('#container').highcharts({
    chart: {
        type: 'column'
    },
    xAxis: {
        categories: [
            'Bars' 
        ]
    },
    yAxis: {
        min: 0,
        title: {
            text: 'User setting'
        }
    },
    series: [{
        id: 'series-1',
        name: 'Series 1',
        data: [10]

    }, {
        id: 'series-2',
        name: 'Series 2',
        data: [10]

    }]
});  
});

Upvotes: 0

dm4web
dm4web

Reputation: 4652

http://jsfiddle.net/r3ou2b9q/5/

HTML:

<div id="bar_graph_wrapper">
    <div id="bar1" class="bars"></div>
    <div id="bar2" class="bars"></div>
</div>

<input type="text" value="0" class="bar_val" id="bar1_val" />
<input type="text" value="0" class="bar_val" id="bar2_val" />

CSS:

#bar_graph_wrapper {
    height:300px;
    position:relative;
    width:100px
}
.bars {
    width:50%;
    height:0;
    float:left;
    position:absolute;
    bottom:0;
}
#bar1 {
    background-color:red
}
#bar2 {
    background-color:blue;
    right:0
}

JQ:

function setValue(b1, b2) {

    var maxValue = 100;

    if (b1 > maxValue) maxValue = b1;
    if (b2 > maxValue) maxValue = b2;

    var perc1 = b1 * 100 / maxValue;
    var perc2 = b2 * 100 / maxValue;

    $('#bar1').animate({
        height: perc1 + "%"
    }, 500)
    $('#bar2').animate({
        height: perc2 + "%"
    }, 500)

}

$('.bar_val').keyup(function () {
    var val1 = ~~$('#bar1_val').val();
    var val2 = ~~$('#bar2_val').val();

    setValue(val1, val2)
});

Upvotes: 1

Related Questions