1

1 つの x 軸項目ごとに 2 つのバーがある棒グラフを取得しました。2 番目の棒の値は、各 x 軸項目の最初の棒の値よりも常に小さくなります。2 番目のバー ラベルに、最初のバーの値に対するパーセンテージを表示する必要があります。

例:

600|  550 
500|   H
400|   H 350(63,63%)
300|   H  H
200|   H  H
100|___H__H______________________________ 
         1      2      3      4      5    

ラベル フォーマッタ関数 (すべての計算を実行し、値を 2 番目のバーの値に戻す必要があります):

$.jqplot.LabelFormatter = function(format, val){ return val; };

pointLabels は次のように定義されます。

pointLabels: { show:true, formatString: "%#.3f",  formatter: $.jqplot.LabelFormatter}
4

1 に答える 1

1

問題の回避策は次のとおりです: JsFiddle リンク

これが問題を解決するコードです。このコードを変更するか、重要な部分をつかんでコードに入れてください。

$(document).ready(function () {
    var s1 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
    var s2 = [5, 15, 15, 30, 45, 60, 35, 49, 75, 95];
    var ticks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var seriesData = [], seriesIndex = 0;

    function storeSeriesData(){
        seriesData[seriesIndex] = this.data;
        seriesIndex = seriesIndex + 1;
    }

    $.jqplot.LabelFormatter = function(format, val){ 
        var result, pointVal = -1;
        if(seriesIndex > 1){
            for(var i = 0; i < seriesData[seriesIndex - 1].length; i++){
                var tempData = seriesData[seriesIndex - 1][i];
                if(tempData[1] == val){
                    break;
                }
            }
            pointVal = seriesData[seriesIndex - 2][i];
            result = val + "("+ parseFloat(val/pointVal[1] * 100).toFixed(1) + "%)";
        }
        else{
            result = val;
        }
        return result; 
    };

    $.jqplot.preDrawSeriesHooks.push(storeSeriesData);

    var plot1 = $.jqplot('chart1', [s1, s2], {
        seriesDefaults: {
            renderer: $.jqplot.BarRenderer,
            rendererOptions: {
                barPadding: 15,
                barWidth: 25
            },
            pointLabels: { 
                show:true, 
                formatString: "%#.1f",  
                formatter: $.jqplot.LabelFormatter
            }
        },
        grid: {
            drawBorder: true,
            background: '#ffffff'  
        },
        axesDefaults: {
            tickRenderer: $.jqplot.CanvasAxisTickRenderer,
            tickOptions: {
                markSize: 4
            }
        },
        axes: {         
            xaxis: {
                pad: -1.05,
                renderer: $.jqplot.CategoryAxisRenderer,
                ticks: ticks,
                tickOptions: {
                    showGridline: true
                }
            },
            yaxis: {
                pad: 1.05,
                tickOptions: {
                    formatString: '%d',
                    showGridline: true
                }
            }
        }
    });
});
于 2013-10-28T15:42:18.833 に答える