簡體   English   中英

每個x軸項目兩個條形圖,第二個條形圖必須在jqplot圖表中顯示類似值(百分比)的百分比

[英]Two bars per one x-axis item and second bar must show the percentage like value(percentage) in jqplot chart

我得到了一個條形圖,每個x軸項目有兩個條形圖。 在每個x軸項目中,第二個柱線值始終小於第一個柱線值。 我需要在第二個條形標簽中顯示它占第一個條形值的百分比。

例:

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

標簽格式化程序功能(它應該執行所有計算並將值返回第二個小節值):

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

pointLabels的定義如下:

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

這是您問題的解決方法: 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
                }
            }
        }
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM