0
A
回答
4
好吧,經過大量的Flot和下載源代碼後,我終於想出了一個很好的起點。
jsFiddle演示是here。
代碼的膽量使用鉤其中提請標籤:
function drawSeriesHook(plot, canvascontext, series) {
var ctx = canvascontext,
plotOffset = plot.offset(),
labelText = 'TEST', // customise this text, maybe to series.label
points = series.datapoints.points,
ps = series.datapoints.pointsize,
xaxis = series.xaxis,
yaxis = series.yaxis,
textWidth, textHeight, textX, textY;
// only draw label for top yellow series
if (series.label === 'baz') {
ctx.save();
ctx.translate(plotOffset.left, plotOffset.top);
ctx.lineWidth = series.bars.lineWidth;
ctx.fillStyle = '#000'; // customise the colour here
for (var i = 0; i < points.length; i += ps) {
if (points[i] == null) continue;
textWidth = ctx.measureText(labelText).width; // measure how wide the label will be
textHeight = parseInt(ctx.font); // extract the font size from the context.font string
textX = xaxis.p2c(points[i] + series.bars.barWidth/2) - textWidth/2;
textY = yaxis.p2c(points[i + 1]) - textHeight/2;
ctx.fillText(labelText, textX, textY); // draw the label
}
ctx.restore();
}
}
見,你可以自定義標籤的意見。
要刪除Y軸刻度,這只是一個簡單的選項設置。另外,您可以計算每個條形碼堆棧的最大y值,然後添加大約100,以設置允許標籤佔用空間的最大Y值。所有這些代碼然後變成:
// determine the max y value from the given data and add a bit to allow for the text
var maxYValue = 0;
var sums = [];
$.each(data,function(i,e) {
$.each(this.data, function(i,e) {
if (!sums[i]) {
sums[i]=0;
}
sums[i] += this[1]; // y-value
});
});
$.each(sums, function() {
maxYValue = Math.max(maxYValue, this);
});
maxYValue += 100; // to allow for the text
var plot = $.plot($("#placeholder"), data, {
series: {
stack: 1,
bars: {
show: true,
barWidth: 0.6,
},
yaxis: {
min: 0,
tickLength: 0
}
},
yaxis: {
max: maxYValue, // set a manual maximum to allow for labels
ticks: 0 // this line removes the y ticks
},
hooks: {
drawSeries: [drawSeriesHook]
}
});
這應該讓你開始。我敢肯定,你可以從這裏拿走它。
相關問題
- 1. Flot對數和動態y軸刻度
- 2. 刪除軸刻度
- 3. 浮動圖表:如何沿y軸刪除/隱藏刻度值?
- 4. nvd3:從離散條形圖中刪除y軸刻度標籤
- 5. Python - 刪除軸刻度標籤,保留刻度和軸標籤
- 6. R和ggplot-刪除y軸上的「0」刻度標記
- 7. 如何從我的Y軸刻度在d3js圖中刪除小數點
- 8. Y軸刻度不齊
- 9. Python Plotly熱圖子圖 - 刪除內部Y軸號和刻度
- 10. matplotlib中的twiny()改變y軸刻度
- 11. 在python中更改y軸的刻度
- 12. 如何刪除matplotlib中的一個刻度軸標籤
- 13. 如何更改時間軸圖表上的Y軸刻度
- 14. 如何刪除D3軸刻度中的逗號並刪除最後一個刻度?
- 15. geom_line與不同的y軸刻度
- 16. jqPlot,Y軸刻度的字符名稱
- 17. 如何減少d3條形圖中的Y軸刻度值?
- 18. 如何減少dimple.js中Y軸刻度的數量?
- 19. 如何在y軸上將刻度線長度設置爲0或從高軸圖表中的y軸中刪除標線?
- 20. plot3d - 如何更改框的顏色和刪除軸刻度
- 21. C3js:如何在y軸上隱藏刻度? Y軸標籤被切斷
- 22. 禁用Y軸刻度和標籤
- 23. ggplot對數刻度y軸直線
- 24. R barplot Y軸刻度太短
- 25. Highcharts |製作多個y軸刻度
- 26. achartengine固定範圍y軸刻度
- 27. 重複y軸刻度標籤
- 28. Flot圖表 - 刪除點之間的刻度標籤
- 29. 如何在YUI圖表中定製y軸刻度?
- 30. 如何在chart.js中爲y軸分配對數刻度?
嗨格雷格感謝文本的酒吧,但在演示後刪除y軸第一個酒吧去最頂部..所以我的文字不可見那裏...有沒有解決方案呢? – 2012-02-21 13:32:55
@Coder_sLaY看到我的編輯,我甚至爲你工作。 jsFiddle鏈接也被更新了。 – GregL 2012-02-21 13:59:21
愛你格雷格。你是最棒的:) – 2012-02-21 14:00:15