2015-12-03 111 views
3

我有以下代碼:如何顯示在Plottable.js多個情節傳奇

var indata = [{ 
 
    'sample': "Foo", 
 
    "pies_pct": [{ 
 
    "score": 6.7530200000000002, 
 
    "celltype": "Bcells" 
 
    }, { 
 
    "score": 11.432763461538459, 
 
    "celltype": "DendriticCells" 
 
    }] 
 

 
}, { 
 
    'sample': "Bar", 
 
    "pies_pct": [{ 
 
    "score": 26.8530200000000002, 
 
    "celltype": "Bcells" 
 
    }, { 
 
    "score": 31.432763461538459, 
 
    "celltype": "DendriticCells" 
 
    }] 
 

 
}, ]; 
 

 

 

 
processData(indata); 
 

 
function processData(data) { 
 

 
    pies = data.map(function(data) { 
 
    return { 
 
     title: data.sample, 
 
     dataset: data.pies_pct 
 
    }; 
 

 

 
    }); 
 

 
    buildPlots(); 
 
} 
 

 
function buildPlots() { 
 
    var $pieContainer = $('#sample-pies'); 
 

 
    pies.forEach(function(pie, index) { 
 
    var elementId = "sample-pie-" + index; 
 

 
    $(document.createElementNS('http://www.w3.org/2000/svg', 'svg')) 
 
     .css({ 
 
     width: '200px', 
 
     height: '200px', 
 
     display: 'inline-block' 
 
     }) 
 
     .attr('id', elementId) 
 
     .appendTo($pieContainer); 
 

 

 
    plotSamplePie(pie.title, pie.dataset, '#' + elementId); 
 
    }); 
 

 

 
} 
 

 

 

 

 
function plotSamplePie(title, purity_data, targetElement) { 
 
    var scale = new Plottable.Scales.Linear(); 
 
    var tableau20 = ['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', 
 
    '#9467BD', '#8C564B', '#CFECF9', '#7F7F7F', '#BCBD22', '#17BECF' 
 
    ]; 
 
    var colorScale = new Plottable.Scales.Color(); 
 
    var legend = new Plottable.Components.Legend(colorScale); 
 
    colorScale.range(tableau20); 
 

 

 

 

 
    var titleLabel = new Plottable.Components.TitleLabel(title); 
 
    var plot = new Plottable.Plots.Pie() 
 
    .addDataset(new Plottable.Dataset(purity_data)) 
 
    .attr("fill", function(d) { 
 
     return d.score; 
 
    }, colorScale) 
 
    .sectorValue(function(d) { 
 
     return d.score; 
 
    }, scale) 
 
    .labelsEnabled(true); 
 

 

 
    new Plottable.Components.Table([ 
 
    [titleLabel], 
 
    [plot] 
 
    ]).renderTo(targetElement); 
 

 
} 
 

 

 
function drawPieLegend(targetElement) { 
 
    new Plottable.Components.Legend(colorScale) 
 
    .renderTo(targetElement); 
 
} 
 

 

 
drawPieLegend('#pies-legend')
<html> 
 

 
<head> 
 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.15.0/plottable.css" rel="stylesheet" /> 
 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.1/basic/jquery.qtip.css" rel="stylesheet" /> 
 

 

 
</head> 
 

 
<body> 
 

 

 
    My Plot 
 

 
    <!-- Show histograms --> 
 
    <div id="sample-pies"></div> 
 
    
 
    Legend here: 
 
    <div id="pies-legend"></div> 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.15.0/plottable.js"></script> 
 

 

 

 

 

 
</body> 
 

 
</html>

我想你做的是顯示一個傳說下兩個餅圖'#pies-legend'作爲函數drawPieLegend()調用的目標元素。但爲什麼它不顯示?

回答

3

colorScale被內部plotSamplePie()定義的,所以它不會在drawPieLegend()範圍出現。在兩個函數之外聲明colorScale將使兩個函數都可以訪問它(儘管這有點麻煩,因爲它現在是一個全局變量)。在處理函數中創建colorScale可能會更清晰,然後將它傳遞給分別製作餅圖和圖例的函數。下面是在修復它快速的嘗試:

var indata = [{ 
 
    'sample': "Foo", 
 
    "pies_pct": [{ 
 
    "score": 6.7530200000000002, 
 
    "celltype": "Bcells" 
 
    }, { 
 
    "score": 11.432763461538459, 
 
    "celltype": "DendriticCells" 
 
    }] 
 

 
}, { 
 
    'sample': "Bar", 
 
    "pies_pct": [{ 
 
    "score": 26.8530200000000002, 
 
    "celltype": "Bcells" 
 
    }, { 
 
    "score": 31.432763461538459, 
 
    "celltype": "DendriticCells" 
 
    }] 
 

 
}, ]; 
 

 
       
 
var colorScale = new Plottable.Scales.Color(); 
 
var tableau20 = ['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', 
 
    '#9467BD', '#8C564B', '#CFECF9', '#7F7F7F', '#BCBD22', '#17BECF' 
 
]; 
 
colorScale.range(tableau20); 
 

 
processData(indata); 
 

 
function processData(data) { 
 

 
    pies = data.map(function(data) { 
 
    return { 
 
     title: data.sample, 
 
     dataset: data.pies_pct 
 
    }; 
 

 

 
    }); 
 

 
    buildPlots(); 
 
} 
 

 
function buildPlots() { 
 
    var $pieContainer = $('#sample-pies'); 
 

 
    pies.forEach(function(pie, index) { 
 
    var elementId = "sample-pie-" + index; 
 

 
    $(document.createElementNS('http://www.w3.org/2000/svg', 'svg')) 
 
     .css({ 
 
     width: '200px', 
 
     height: '200px', 
 
     display: 'inline-block' 
 
     }) 
 
     .attr('id', elementId) 
 
     .appendTo($pieContainer); 
 

 

 
    plotSamplePie(pie.title, pie.dataset, '#' + elementId); 
 
    }); 
 

 

 
} 
 

 

 

 

 
function plotSamplePie(title, purity_data, targetElement) { 
 
    var scale = new Plottable.Scales.Linear(); 
 
    var titleLabel = new Plottable.Components.TitleLabel(title); 
 
    var plot = new Plottable.Plots.Pie() 
 
    .addDataset(new Plottable.Dataset(purity_data)) 
 
    .attr("fill", function(d) { 
 
     return d.celltype; 
 
    }, colorScale) 
 
    .sectorValue(function(d) { 
 
     return d.score; 
 
    }, scale) 
 
    .labelsEnabled(true); 
 

 

 
    new Plottable.Components.Table([ 
 
    [titleLabel], 
 
    [plot] 
 
    ]).renderTo(targetElement); 
 

 
} 
 

 

 
function drawPieLegend(targetElement) { 
 
    new Plottable.Components.Legend(colorScale) 
 
    .maxEntriesPerRow(Infinity) 
 
    .renderTo(targetElement); 
 
} 
 

 

 
drawPieLegend('#pies-legend');
<html> 
 

 
<head> 
 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.15.0/plottable.css" rel="stylesheet" /> 
 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.1/basic/jquery.qtip.css" rel="stylesheet" /> 
 

 

 
</head> 
 

 
<body> 
 

 

 
    My Plot 
 

 
    <!-- Show histograms --> 
 
    <div id="sample-pies"></div> 
 
    
 
    Legend here: 
 
    <svg id="pies-legend" height="50" width="400"></svg> 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.15.0/plottable.js"></script> 
 

 

 

 

 

 
</body> 
 

 
</html>

還要注意從

.attr("fill", function(d) { 
    return d.score; 
}, colorScale) 

.attr("fill", function(d) { 
    return d.celltype; 
}, colorScale) 

的變化導致的顏色由celltype驅動而不是score

+0

謝謝。但是我們希望這個傳說只有**兩種顏色:'Bcells'和'DendriticCells'。此外,傳說的價值應該是「Bcells」和「DendriticCells」,而不是6.75等。我們如何修改你的代碼? – neversaint

+2

我修改了上面的註釋來驅動基於celltype的填充顏色。 – jtlan

2

您需要將圖例添加到新的Plottable.Components.Table()。

new Plottable.Components.Table([ 
    [titleLabel], 
    [plot], 
    [legend] 
    ]).renderTo(targetElement); 

通過下面的代碼片段。

var indata = [{ 
 
    'sample': "Foo", 
 
    "pies_pct": [{ 
 
    "score": 6.7530200000000002, 
 
    "celltype": "Bcells" 
 
    }, { 
 
    "score": 11.432763461538459, 
 
    "celltype": "DendriticCells" 
 
    }] 
 

 
}, { 
 
    'sample': "Bar", 
 
    "pies_pct": [{ 
 
    "score": 26.8530200000000002, 
 
    "celltype": "Bcells" 
 
    }, { 
 
    "score": 31.432763461538459, 
 
    "celltype": "BCells" 
 
    }] 
 

 
}, ]; 
 

 

 

 
processData(indata); 
 

 
function processData(data) { 
 

 
    pies = data.map(function(data) { 
 
    return { 
 
     title: data.sample, 
 
     dataset: data.pies_pct 
 
    }; 
 

 

 
    }); 
 

 
    buildPlots(); 
 
} 
 

 
function buildPlots() { 
 
    var $pieContainer = $('#sample-pies'); 
 

 
    pies.forEach(function(pie, index) { 
 
    var elementId = "sample-pie-" + index; 
 

 
    $(document.createElementNS('http://www.w3.org/2000/svg', 'svg')) 
 
     .css({ 
 
     width: '200px', 
 
     height: '200px', 
 
     display: 'inline-block' 
 
     }) 
 
     .attr('id', elementId) 
 
     .appendTo($pieContainer); 
 

 

 
    plotSamplePie(pie.title, pie.dataset, '#' + elementId); 
 
    }); 
 

 

 
} 
 

 

 

 

 
function plotSamplePie(title, purity_data, targetElement) { 
 
    var scale = new Plottable.Scales.Linear(); 
 
    var tableau20 = ['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', 
 
    '#9467BD', '#8C564B', '#CFECF9', '#7F7F7F', '#BCBD22', '#17BECF' 
 
    ]; 
 
    var colorScale = new Plottable.Scales.Color(); 
 
    var legend = new Plottable.Components.Legend(colorScale); 
 
    colorScale.range(tableau20); 
 

 

 

 

 
    var titleLabel = new Plottable.Components.TitleLabel(title); 
 
    var plot = new Plottable.Plots.Pie() 
 
    .addDataset(new Plottable.Dataset(purity_data)) 
 
    .attr("fill", function(d) { 
 
     return d.score; 
 
    }, colorScale) 
 
    .sectorValue(function(d) { 
 
     return d.score; 
 
    }, scale) 
 
    .labelsEnabled(true); 
 

 

 
    new Plottable.Components.Table([ 
 
    [titleLabel], 
 
    [plot], 
 
    [legend] 
 
    ]).renderTo(targetElement); 
 

 
} 
 

 

 
function drawPieLegend(targetElement) { 
 
    new Plottable.Components.Legend(colorScale) 
 
    .renderTo(targetElement); 
 
} 
 

 

 
drawPieLegend('#pies-legend')
<html> 
 

 
<head> 
 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.15.0/plottable.css" rel="stylesheet" /> 
 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.1/basic/jquery.qtip.css" rel="stylesheet" /> 
 

 

 
</head> 
 

 
<body> 
 

 

 
    My Plot 
 

 
    <!-- Show histograms --> 
 
    <div id="sample-pies"></div> 
 
    
 
    Legend here: 
 
    <div id="pies-legend"></div> 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.15.0/plottable.js"></script> 
 

 

 

 

 

 
</body> 
 

 
</html>

+0

謝謝。但那不是我想要的。我們想爲兩個餅圖顯示**一個**圖例。傳說內容應該是*樹突狀細胞*和* B細胞*不是它的價值。 – pdubois

+0

嘗試使用legend.renderTo(「svg#legend_example」);但這需要工作,如jsfiddle http://jsfiddle.net/3fpw33oL/ –

+0

謝謝。不完全在那裏。通過這樣做,您可以將多個圖表圖例附加到一個圖表。 – pdubois