简体   繁体   English

在下拉列表 function 中未显示正确数据集的问题

[英]Problem not showing proper datasets in dropdown list function

please help me with my issue regarding the code, when I clicked the dropdown list, it should show the datas per datasets but when i click other elements.请帮助我解决有关代码的问题,当我单击下拉列表时,它应该显示每个数据集的数据,但是当我单击其他元素时。 It shows wrong value, the value should be 200 not 2.5.它显示错误的值,该值应该是 200 而不是 2.5。 Thank you.谢谢你。


    dataObjects.forEach(o => {
                const opt = document.createElement('option');
                opt.value = o.name;
                opt.appendChild(document.createTextNode(o.name));
                document.getElementById('operator').appendChild(opt);
                console.log(opt)
            });
        function refreshChart(name) {
            firstChart.data.labels = [name];
            if (name == 'All') {
                firstChart.data.labels = dataObjects.map(o => o.name),
                    firstChart.data.datasets[0].data = dataObjects.map(o => o.rate_per_liters);
            } else {
                firstChart.data.labels = [name];
                    firstChart.data.datasets[0].data = dataObjects.find(o => o.name == name).rate_per_liters;
                    // firstChart.data.datasets[0].data = dataObjects.get(o => o.name == name).push(rate_per_liters);
            }
            console.log(name)
            firstChart.update();
            firstChart.render();
        }

here is my chart code这是我的图表代码


    dataObjects = [
            { name: '10', rate_per_liters: '200'},
            { name: '20', rate_per_liters: '200'},
            { name: '30', rate_per_liters: '200'},
            { name: '40', rate_per_liters: '200'},
            { name: '50', rate_per_liters: '200'},
            { name: '60', rate_per_liters: '200'}
        ];
        
        // Data1 setup
        var ctx = document.getElementById('firstChart');
        
        const firstChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: dataObjects.map(o => o.name),
                datasets: [{
                    fill: false,
                    label: 'System Requirements per L/s',
                    data: dataObjects.map(o => o.rate_per_liters),
                    backgroundColor: 'orange',
                    borderColor: 'orange',
                    borderWidth: 1,
                    yAxisID: 'kPa',
                    xAxisID: 'Lits',
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        id: "kPa",
                        ticks: {
                            beginAtZero: true,  
                            stepSize: 50
                        },
                        scaleLabel: {
                            display: true,
                            labelString: 'kPa'
                        }
                    }],
                    xAxes: [{
                        id: "Lits",
                        scaleLabel: {
                            display: true,
                            labelString: 'Liter per seconds'
                        }

                    }]
                        },
                    title: {
                        display: false,
                        text: "SAMPLE!"
                        },
                        legend: {
                        display: false,
                        position: 'bottom',
                        labels: {
                            fontColor: "#17202A",
                        },
                    }
                }
        });

And also i will attach a screenshot of my issue.我还将附上我的问题的屏幕截图。 Here > SCREENSHOT 1 SCREENSHOT 2 WITH THE ISSUE Thanks!此处 >屏幕截图 1带有问题的屏幕截图 2谢谢!

I already fixed this issue.我已经解决了这个问题。 I just had errors in the loops.我只是在循环中有错误。

I changed the whole code and throw this codes for HTML我更改了整个代码并将此代码用于 HTML

<div class="selectBox" onchange="eventTracker()"><label>SYSTEM REQUIREMENTS</label>
        <select id="idSample">
            <option value="0, 0, 0, 0, 0, 0"></option>
            <option value="200, 0, 0, 0, 0, 0">10</option>
            <option value="0, 200, 0, 0, 0, 0">20</option>
            <option value="0, 0, 200, 0, 0, 0">30</option>
            <option value="0, 0, 0, 200, 0, 0">40</option>
            <option value="0, 0, 0, 0, 200, 0">50</option>
            <option value="0, 0, 0, 0, 0, 200">60</option>
        </select> L/s @ A MINIMUM OF 200 kPa <div class="palette purle">
        </div>

and this is for the function这是针对 function

function eventTracker() {
    firstChart.data.datasets[0].data = idSample.value.split(',');
    firstChart.update();
    firstChart.render();
}

this is for my Chart这是给我的图表

var ctx = document.getElementById('firstChart');
const firstChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['10', '20', '30', '40', '50', '60'],
        datasets: [{
            fill: false,
            label: 'System Requirements per L/s',
            data: ['0', '0', '0', '0', '0', '0'],
            backgroundColor: 'orange',
            borderColor: 'orange',
            borderWidth: 1,
            yAxisID: 'kPa',
            xAxisID: 'Lits',
        }]
    },
    options: {
        scales: {
            yAxes: [{
                id: "kPa",
                ticks: {
                    beginAtZero: true,
                    stepSize: 50
                },
                scaleLabel: {
                    display: true,
                    labelString: 'Pressure (kPa)'
                }
            }],
            xAxes: [{
                id: "Lits",
                scaleLabel: {
                    display: true,
                    labelString: 'Flow Rate (L/s)'
                }

            }]
        },
        title: {
            display: false,
            text: "SAMPLE!"
        },
        legend: {
            display: false,
            position: 'bottom',
            labels: {
                fontColor: "#17202A",
            },
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM