简体   繁体   English

Javascript + Highcharts:用series.data代替我自己的

[英]Javascript+Highcharts: Substituting series.data for my own

I've been doing this script for a volunteering university-site job. 我一直在为志愿者的大学工作做这个脚本。

Please ignore all parameters apart from wattages and schools . 除了功率学校以外,请忽略所有参数。 Those are SQL result sets that have been converted to arrays using json_encode() . 这些是已使用json_encode()转换为数组的SQL结果集。 Result sets have Strings as values, I believe, so both wattages and schools should be arrays of strings. 结果集有一个字符串作为值,我相信,这样既wattagesschools应该是字符串数组。

What I want to do is input my own data for the pie graph, in this case mySeries , which I build/fill up at the start and put as data later. 我想要做的是为饼图输入我自己的data ,在本例中是mySeries ,我一开始就将其建立/填充,然后作为data放置。

function createPieChartGradient(data,title,xlabel,ylabel,type,step,div_id,wattages,schools){

    var float_watt = new Array();
    for(var index = 0; index < wattages.length; index++)
    {
        float_watt[index] = parseFloat(wattages[index]).toFixed(2);
    }

    var mySeries = [];  //Hopefully this creates a [String, number] array that can be used as Data.
    for (var i = 0; i < float_watt.length; i++) {
        mySeries.push([schools[i], float_watt[i]]);
    }

    var options = {
        chart: {
            renderTo: 'graph',
            zoomType: 'x',
            defaultSeriesType: type
        },
        title: {
                text: 'Consumption Percentage of IHU Schools, Last 7 days'
            },
        tooltip: {
            //pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        xAxis: {
            categories: [],
            tickPixelInterval: 150,
           // maxZoom: 20 * 1000,
            title: {
            style: {
                fontSize: '14px'
            },
                text: xlabel
            },
            labels: {
                rotation: -45,
                step: step,
                align: 'right'//,
            //  step: temp
            }
        },
        yAxis: {
            title: {
            style: {
                fontSize: '14px'
            },
                text: ylabel
            },
            labels: {
                align: 'right',
                formatter: function() {
                    return parseFloat(this.value).toFixed(2);
                }
            },
            min: 0
        },
        legend: {
             layout: 'vertical',
             align: 'right',
             verticalAlign: 'center',
             floating: true,
             shadow: true
          },

        series: [{
                    type: 'pie',
                    name: 'Consumption Percentage',
                    data: mySeries       //Problematic line.
                }]                                                                                      //Faculty with the smallest wattage -> Green.
    };  //end of var options{}                                                                          //Next: -> Yellow.
                                                                                                        //Last -> Red.
    //Draw the chart.
    var chart = new Highcharts.Chart(options);                                                          //TODO: Change colours.
    document.write("FINISHED");
}

The thing is, the above won't work. 问题是,上述方法不起作用。 Since I'm not using an environment (writing in notepad++ and testing on my apache web server, via the results) I have manually aliminated the problematic line to be data: mySeries . 由于我没有使用环境(通过结果在notepad ++中编写和在apache Web服务器上进行测试,因此我将结果中的问题手动删除data: mySeries

Any idea why that is? 知道为什么吗? Aren't they the same type of array, [String, number] ? 它们不是同一类型的数组[String, number]吗?

Additionally, are there any environments that will help me debug javascript programs? 此外,是否有任何环境可以帮助我调试JavaScript程序? I'm really at my wit's end with this situation and I'd very much prefer to have an IDE tell me what's wrong, or at least point me at the right direction. 在这种情况下,我真的不知所措,我非常希望让IDE告诉我出了什么问题,或者至少将我指出正确的方向。

You see where you are calling "toFixed"? 您看到哪里叫“ toFixed”了吗? Let's run a small experiment: 让我们进行一个小实验:

var wattages = [2.0, 3.0];

 var float_watt = new Array();
 for(var index = 0; index < wattages.length; index++)
 {
    float_watt[index] = parseFloat(wattages[index]).toFixed(2);
 }

 console.log(JSON.stringify(float_watt));

The output is not what you expect: 输出不是您期望的:

["2.00", "3.00"]

See how it got converted to a string? 看看如何将其转换为字符串? If you delete the "toFixed" and let your formatter do its job, things will go just fine. 如果删除“ toFixed”并让格式化程序执行其工作,一切将会顺利进行。

EDIT 编辑

Here's how you fix your formatter: 这是修复格式程序的方法:

    plotOptions: {
        pie: {
            dataLabels: {
                formatter: function() {
                    return parseFloat(this.y).toFixed(2);
                }
            }
        }
    },

The yLabels formatter is doing nothing for the pie. yLabels格式化程序对饼没有任何作用。

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

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